Package javax.sound.sampled

Examples of javax.sound.sampled.SourceDataLine


            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


            }
            Collections.sort(scale);

            System.out.println("Scale: " + scale);

            SourceDataLine line = AudioSystem.getSourceDataLine(af);
            line.open(af);
            line.start();

            Double[] scaleFrequencies = scale.toArray(new Double[scale.size()]);

            // first play the whole scale
            WaveMelodyGenerator.playScale(line, scaleFrequencies);
            // then generate a random melody in the scale
            WaveMelodyGenerator.playMelody(line, scaleFrequencies);

            line.drain();
            line.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
View Full Code Here

    @Override public void run() {
        /* code running in separate thread */

        playingUrl = null;
        AudioInputStream audioInputStream = null;
        SourceDataLine audioOutputLine = null;
        AudioFormat audioFormat = null;
        byte[] abData = new byte[(int)chunk];

        for (;;) {
            try {
                switch (state) {
                    case INITIALIZING:
                        // we're ready to take interrupts
                        state = State.NOTPLAYING;
                        break;
                    case NOTPLAYING:
                    case PAUSED:
                        sleep(200);
                        break;
                    case PLAYING:
                        command.possiblyInterrupt();
                        for(;;) {
                            int nBytesRead = 0;
                            nBytesRead = audioInputStream.read(abData, 0, abData.length);
                            position += nBytesRead / bytesPerSecond;
                            command.possiblyInterrupt();
                            if (nBytesRead < 0) { break; }
                            audioOutputLine.write(abData, 0, nBytesRead); // => int nBytesWritten
                            command.possiblyInterrupt();
                        }
                        // end of audio, clean up
                        audioOutputLine.drain();
                        audioOutputLine.close();
                        audioOutputLine = null;
                        Utils.close(audioInputStream);
                        audioInputStream = null;
                        playingUrl = null;
                        state = State.NOTPLAYING;
                        command.possiblyInterrupt();
                        break;
                }
            } catch (InterruptedException e) {
                interrupted(); // just in case we get an interrupt
                State stateChange = state;
                state = State.INTERRUPTED;
                try {
                    switch (command.command()) {
                        case PLAY:
                            double offset = command.offset();
                            speed = command.speed();
                            if (playingUrl != command.url() ||
                                    stateChange != State.PAUSED ||
                                    offset != 0.0)
                            {
                                if (audioInputStream != null) {
                                    Utils.close(audioInputStream);
                                    audioInputStream = null;
                                }
                                playingUrl = command.url();
                                audioInputStream = AudioSystem.getAudioInputStream(playingUrl);
                                audioFormat = audioInputStream.getFormat();
                                long nBytesRead = 0;
                                position = 0.0;
                                offset -= leadIn;
                                double calibratedOffset = offset * calibration;
                                bytesPerSecond = audioFormat.getFrameRate() /* frames per second */
                                * audioFormat.getFrameSize() /* bytes per frame */;
                                if (speed * bytesPerSecond > 256000.0) {
                                    speed = 256000 / bytesPerSecond;
                                }
                                if (calibratedOffset > 0.0) {
                                    long bytesToSkip = (long)(
                                            calibratedOffset /* seconds (double) */ * bytesPerSecond);
                                    /* skip doesn't seem to want to skip big chunks, so
                                     * reduce it to smaller ones
                                     */
                                    // audioInputStream.skip(bytesToSkip);
                                    while (bytesToSkip > chunk) {
                                        nBytesRead = audioInputStream.skip(chunk);
                                        if (nBytesRead <= 0)
                                            throw new IOException(tr("This is after the end of the recording"));
                                        bytesToSkip -= nBytesRead;
                                    }
                                    while (bytesToSkip > 0) {
                                        long skippedBytes = audioInputStream.skip(bytesToSkip);
                                        bytesToSkip -= skippedBytes;
                                        if (skippedBytes == 0) {
                                            // Avoid inifinite loop
                                            Main.warn("Unable to skip bytes from audio input stream");
                                            bytesToSkip = 0;
                                        }
                                    }
                                    position = offset;
                                }
                                if (audioOutputLine != null) {
                                    audioOutputLine.close();
                                }
                                audioFormat = new AudioFormat(audioFormat.getEncoding(),
                                        audioFormat.getSampleRate() * (float) (speed * calibration),
                                        audioFormat.getSampleSizeInBits(),
                                        audioFormat.getChannels(),
                                        audioFormat.getFrameSize(),
                                        audioFormat.getFrameRate() * (float) (speed * calibration),
                                        audioFormat.isBigEndian());
                                DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
                                audioOutputLine = (SourceDataLine) AudioSystem.getLine(info);
                                audioOutputLine.open(audioFormat);
                                audioOutputLine.start();
                            }
                            stateChange = State.PLAYING;
                            break;
                        case PAUSE:
                            stateChange = State.PAUSED;
View Full Code Here

        // sound system setup
        SoundSystem.setup();

        // get a line and create the jukebox
        if (SoundSystem.hasActiveMixer()) {
            SourceDataLine line = SoundSystem.getLine();
            // sometimes you have a mixer, but don't get a line, disable the mixer in these cases
            if (line == null) {
                SoundSystem.clearMixer();
                return;
            }
View Full Code Here

            ByteArrayInputStream sourceBis = new ByteArrayInputStream(bytes);

            int bufferSize = format.getFrameSize() * Math.round(format.getSampleRate() / 10);
            byte[] buffer = new byte[bufferSize];
            DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
            SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
            line.open(format, buffer.length);
            line.start();

            int numBytesRead = 0;
            while (numBytesRead != -1) {
                numBytesRead = sourceBis.read(buffer, 0, buffer.length);
                if (numBytesRead != -1) {
                    line.write(buffer, 0, numBytesRead);
                }
            }
            line.drain();
            line.close();

        } catch (UnsupportedAudioFileException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
View Full Code Here

        // use a short, 100ms (1/10th sec) buffer for filters that
        // change in real-time
        int bufferSize = playbackFormat.getFrameSize() * Math.round(playbackFormat.getSampleRate() / 10);

        // create, open, and start the line
        SourceDataLine line;
        DataLine.Info lineInfo = new DataLine.Info(SourceDataLine.class, playbackFormat);
        try {
            line = (SourceDataLine) AudioSystem.getLine(lineInfo);
            line.open(playbackFormat, bufferSize);
        } catch (LineUnavailableException ex) {
            // the line is unavailable - signal to end this thread
            Thread.currentThread().interrupt();
            return;
        }

        line.start();

        // create the buffer
        byte[] buffer = new byte[bufferSize];

        // set this thread's locals
View Full Code Here

    /**
     * Signals that a PooledThread has stopped. Drains and closes the Thread's
     * Line.
     */
    protected void threadStopped() {
        SourceDataLine line = (SourceDataLine) localLine.get();
        if (line != null) {
            line.drain();
            line.close();
        }
    }
View Full Code Here

            this.source = source;
        }

        public void run() {
            // get line and buffer from ThreadLocals
            SourceDataLine line = (SourceDataLine) localLine.get();
            byte[] buffer = (byte[]) localBuffer.get();
            if (line == null || buffer == null) {
                // the line is unavailable
                return;
            }

            // copy data to the line
            try {
                int numBytesRead = 0;
                while (numBytesRead != -1) {
                    // if paused, wait until unpaused
                    synchronized (pausedLock) {
                        if (paused) {
                            try {
                                pausedLock.wait();
                            } catch (InterruptedException ex) {
                                return;
                            }
                        }
                    }
                    // copy data
                    numBytesRead = source.read(buffer, 0, buffer.length);
                    if (numBytesRead != -1) {
                        line.write(buffer, 0, numBytesRead);
                    }
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
View Full Code Here

            return;
        }

        try {
            // Create a SourceDataLine for play back (throws LineUnavailableException). 
            SourceDataLine dataLine = (SourceDataLine) AudioSystem.getLine(info);
            // System.out.println("SourceDataLine class=" + dataLine.getClass());

            // The line acquires system resources (throws LineAvailableException).
            dataLine.open(audioFormat);

            // Adjust the volume on the output line.
            if (dataLine.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
                FloatControl volume = (FloatControl) dataLine.getControl(FloatControl.Type.MASTER_GAIN);
                volume.setValue(100.0F);
            }

            // Allows the line to move data in and out to a port.
            dataLine.start();

            // Create a buffer for moving data from the audio stream to the line.  
            int bufferSize = (int) audioFormat.getSampleRate() * audioFormat.getFrameSize();
            byte[] buffer = new byte[bufferSize];

            // Move the data until done or there is an error.
            try {
                int bytesRead = 0;
                while (bytesRead >= 0) {
                    bytesRead = audioInputStream.read(buffer, 0, buffer.length);
                    if (bytesRead >= 0) {
                        // System.out.println("Play.playAudioStream bytes read=" + bytesRead +
                        //    ", frame size=" + audioFormat.getFrameSize() + ", frames read=" + bytesRead / audioFormat.getFrameSize());
                        // Odd sized sounds throw an exception if we don't write the same amount.
                        dataLine.write(buffer, 0, bytesRead);
                    }
                } // while
            }
            catch (IOException e) {
                e.printStackTrace();
            }

            System.out.println("Play.playAudioStream draining line.");
            // Continues data line I/O until its buffer is drained.
            dataLine.drain();

            System.out.println("Play.playAudioStream closing line.");
            // Closes the data line, freeing any resources such as the audio device.
            dataLine.close();
        }
        catch (LineUnavailableException e) {
            e.printStackTrace();
        }
    } // playAudioStream
View Full Code Here

    // other wise just play the audio directly

    else
    {
      IStream stream = mContainer.getStream(event.getStreamIndex());
      SourceDataLine line = getJavaSoundLine(stream);
      if (line != null)
        playAudio(stream, line, samples);
    }
  }
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.