Package javax.sound.sampled

Examples of javax.sound.sampled.SourceDataLine


     */
    @Test
    public void StreamingPlayerTest() throws MalformedURLException, UnsupportedAudioFileException, IOException, LineUnavailableException, InterruptedException {

        DataLine.Info info = new DataLine.Info(SourceDataLine.class, TargetFormat);
        SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
        line.open();

        StreamPlayer player = StreamPlayer.create(line, "Music-Thread");

        URL url = new URL("http://www.twelvepm.de/vorbis/Agogo.ogg");
        AudioFileFormat fmt = AudioSystem.getAudioFileFormat(url);
View Full Code Here


     * @return A new SourceDataLine or null if none is available
     */
    public static SourceDataLine getLine() {
        if (activeMixer != null) {
            try {
                SourceDataLine line = (SourceDataLine) activeMixer.getLine(TargetLineInfo);
                line.open();
                return line;
            } catch (LineUnavailableException ex) {
                LOG.log(Level.SEVERE, null, ex);
            }
        }
View Full Code Here

     * @param arg
     */
    public static void main(String[] arg) {
        // get a line and construct a new player
        AudioFormat requestedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, Rate, 16, Channels, 4, Rate, false);
        SourceDataLine line;
        try {
            line = AudioSystem.getSourceDataLine(requestedFormat);
            line.open();
        } catch (LineUnavailableException ex) {
            LOG.log(Level.SEVERE, null, ex);
            return;
        }
        line.start();
        OggPlayer player = new OggPlayer(line);

        InputStream in;
        try {
            in = new FileInputStream("Agogo.ogg");
        } catch (FileNotFoundException ex) {
            LOG.log(Level.SEVERE, null, ex);
            return;
        }
        player.play(in);

        line.stop();
        line.close();
    }
View Full Code Here

        // get buffer
        byte[] data = new byte[4096];

        // get line
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, targetFormat);
        SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);

        if (line != null) {
            // open it
            line.open();

            // start
            line.start();
            int nBytesRead = 0, nBytesWritten = 0;
            while (nBytesRead != -1) {
                nBytesRead = dataIn.read(data, 0, data.length);
                if (nBytesRead != -1) {
                    nBytesWritten = line.write(data, 0, nBytesRead);
                }
            }

            // stop line
            line.drain();
            line.stop();
            line.close();

            // stop input stream
            dataIn.close();
        }
    }
View Full Code Here

  private void perform() throws LineUnavailableException {
    // JavaSound setup.
    int sampleSize = 2;
    AudioFormat audioFormat = new AudioFormat(sampleRate, 8 * sampleSize, outChans, true, true);
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
    SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(info);
    sourceDataLine.open(audioFormat);
    sourceDataLine.start();

    // Buffer setup for exchanging samples between libpd and JavaSound.
    // Question: Is this the best possible solution?  It seems to involve too
    // much copying.
    int frames = PdBase.blockSize() * ticks;
    short[] dummy = new short[0];
    short[] samples = new short[frames * outChans];
    byte[] rawSamples = new byte[samples.length * sampleSize];
    ByteBuffer buf = ByteBuffer.wrap(rawSamples);
    ShortBuffer shortBuf = buf.asShortBuffer();

    while (!terminated) {  // Note: sourceDataLine.write seems to clear the interrupted flag, and so Thread.interrupted() doesn't work here.
      PdBase.process(ticks, dummy, samples);
      shortBuf.rewind();
      shortBuf.put(samples);
      sourceDataLine.write(rawSamples, 0, rawSamples.length);
    }

    // Shutdown.
    sourceDataLine.drain();
    sourceDataLine.stop();
    sourceDataLine.close();
  }
View Full Code Here

            e1.printStackTrace();
            return;
        }
        AudioFormat format = audioInputStream.getFormat();
        SourceDataLine auline = null;
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
        try {
            auline = (SourceDataLine) AudioSystem.getLine(info);
            auline.open(format);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
            return;
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
            fCont = (FloatControl) auline.getControl(FloatControl.Type.VOLUME);
        if (auline.isControlSupported(FloatControl.Type.PAN)) {
            FloatControl pan = (FloatControl) auline
                    .getControl(FloatControl.Type.PAN);

            if (curPosition == Position.RIGHT)
                pan.setValue(1.0f);
            else if (curPosition == Position.LEFT)
                pan.setValue(-1.0f);
        }
        auline.start();
        int nBytesRead = 0;
        byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
        try {
            while (nBytesRead != -1) {
                nBytesRead = audioInputStream.read(abData, 0, abData.length);
                if (nBytesRead >= 0)
                    auline.write(abData, 0, nBytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return;
        } finally {
            auline.drain();
            mute();
            auline.close();
        }
      }
    }
View Full Code Here

                public void run() {
                    float seconds = 0.3f;
                    try {
                        AudioFormat af = new AudioFormat(SAMPLE_RATE, 8, 1, true, true);
                        DataLine.Info info = new DataLine.Info(SourceDataLine.class, af);
                        SourceDataLine source = (SourceDataLine) AudioSystem.getLine(info);
                        source.open(af);
                        source.start();
                        byte[] buf = new byte[(int) (SAMPLE_RATE * seconds)];
                        for (int i = 0; i < buf.length; i++) {
                            buf[i] = (byte) (Math.sin(RAD * frequency / SAMPLE_RATE * i) * 64.0 / Math.sqrt(i));
                        }
                        source.write(buf, 0, buf.length);
                        source.drain();
                        source.stop();
                        source.close();
                    } catch (Exception e) {
                        System.out.println(e);
                    }
                }
            });
View Full Code Here

            e1.printStackTrace();
            return;
        }

        AudioFormat format = audioInputStream.getFormat();
        SourceDataLine auline = null;
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

        try {
            auline = (SourceDataLine) AudioSystem.getLine(info);
            auline.open(format);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
            return;
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        if (auline.isControlSupported(FloatControl.Type.PAN)) {
            FloatControl pan = (FloatControl) auline
                    .getControl(FloatControl.Type.PAN);
            if (curPosition == Position.RIGHT)
                pan.setValue(1.0f);
            else if (curPosition == Position.LEFT)
                pan.setValue(-1.0f);
        }

        auline.start();
        int nBytesRead = 0;
        byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];

        try {
            while (nBytesRead != -1) {
                nBytesRead = audioInputStream.read(abData, 0, abData.length);
                if (nBytesRead >= 0)
                    auline.write(abData, 0, nBytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return;
        } finally {
            auline.drain();
            auline.close();
        }

    }
View Full Code Here

    }

    static void testSDL(Mixer mixer, Scenario scenario) {
        log("  Testing SDL (scenario: " + scenario + ")...");
        Line.Info linfo = new Line.Info(SourceDataLine.class);
        SourceDataLine line = null;
        try {
            line = (SourceDataLine)mixer.getLine(linfo);
            log("    got line: " + line);
            log("    open...");
            line.open();
        } catch (IllegalArgumentException ex) {
            log("    unsupported (IllegalArgumentException)");
            return;
        } catch (LineUnavailableException ex) {
            log("    unavailable: " + ex);
            return;
        }

        total++;

        log("    start...");
        line.start();

        AsyncLineStopper lineStopper = new AsyncLineStopper(line, STOPPER_DELAY);
        int offset = scenario.getBufferOffset(line);
        int len = scenario.getBufferLength(line);
        // ensure len represents integral number of frames
        len -= len % line.getFormat().getFrameSize();

        log("    write...");
        lineStopper.schedule();
        try {
            line.write(buffer, offset, len);
            log("    ERROR: didn't get ArrayIndexOutOfBoundsException");
            failed++;
        } catch (ArrayIndexOutOfBoundsException  ex) {
            log("    OK: got ArrayIndexOutOfBoundsException: " + ex);
        }
View Full Code Here

            if (!(AudioSystem.isLineSupported(info)) ) {
                if (DEBUG || Printer.err)Printer.err("Line not supported: "+loadedAudioFormat);
                // fail silently
                return false;
            }
            SourceDataLine source = (SourceDataLine) AudioSystem.getLine(info);
            datapusher = new DataPusher(source, loadedAudioFormat, loadedAudio, loadedAudioByteLength);
        } catch (Exception e) {
            if (DEBUG || Printer.err)e.printStackTrace();
            // fail silently
            return false;
View Full Code Here

TOP

Related Classes of javax.sound.sampled.SourceDataLine

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.