Search in sources :

Example 56 with AudioInputStream

use of javax.sound.sampled.AudioInputStream in project jdk8u_jdk by JetBrains.

the class AuFileReader method getAudioInputStream.

/**
     * Obtains an audio stream from the File provided.  The File must
     * point to valid audio file data.
     * @param file the File for which the <code>AudioInputStream</code> should be
     * constructed
     * @return an <code>AudioInputStream</code> object based on the audio file data pointed
     * to by the File
     * @throws UnsupportedAudioFileException if the File does not point to valid audio
     * file data recognized by the system
     * @throws IOException if an I/O exception occurs
     */
public AudioInputStream getAudioInputStream(File file) throws UnsupportedAudioFileException, IOException {
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    AudioFileFormat fileFormat = null;
    // throws IOException
    fis = new FileInputStream(file);
    AudioInputStream result = null;
    // part of fix for 4325421
    try {
        bis = new BufferedInputStream(fis, bisBufferSize);
        result = getAudioInputStream((InputStream) bis);
    } finally {
        if (result == null) {
            fis.close();
        }
    }
    return result;
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) BufferedInputStream(java.io.BufferedInputStream) DataInputStream(java.io.DataInputStream) BufferedInputStream(java.io.BufferedInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream) AudioFileFormat(javax.sound.sampled.AudioFileFormat)

Example 57 with AudioInputStream

use of javax.sound.sampled.AudioInputStream in project jdk8u_jdk by JetBrains.

the class SoftSynthesizer method open.

public void open(SourceDataLine line, Map<String, Object> info) throws MidiUnavailableException {
    if (isOpen()) {
        synchronized (control_mutex) {
            implicitOpen = false;
        }
        return;
    }
    synchronized (control_mutex) {
        try {
            if (line != null) {
                // can throw IllegalArgumentException
                setFormat(line.getFormat());
            }
            AudioInputStream ais = openStream(getFormat(), info);
            weakstream = new WeakAudioStream(ais);
            ais = weakstream.getAudioInputStream();
            if (line == null) {
                if (testline != null) {
                    line = testline;
                } else {
                    // can throw LineUnavailableException,
                    // IllegalArgumentException, SecurityException
                    line = AudioSystem.getSourceDataLine(getFormat());
                }
            }
            double latency = this.latency;
            if (!line.isOpen()) {
                int bufferSize = getFormat().getFrameSize() * (int) (getFormat().getFrameRate() * (latency / 1000000f));
                // can throw LineUnavailableException,
                // IllegalArgumentException, SecurityException
                line.open(getFormat(), bufferSize);
                // Remember that we opened that line
                // so we can close again in SoftSynthesizer.close()
                sourceDataLine = line;
            }
            if (!line.isActive())
                line.start();
            int controlbuffersize = 512;
            try {
                controlbuffersize = ais.available();
            } catch (IOException e) {
            }
            // Tell mixer not fill read buffers fully.
            // This lowers latency, and tells DataPusher
            // to read in smaller amounts.
            //mainmixer.readfully = false;
            //pusher = new DataPusher(line, ais);
            int buffersize = line.getBufferSize();
            buffersize -= buffersize % controlbuffersize;
            if (buffersize < 3 * controlbuffersize)
                buffersize = 3 * controlbuffersize;
            if (jitter_correction) {
                ais = new SoftJitterCorrector(ais, buffersize, controlbuffersize);
                if (weakstream != null)
                    weakstream.jitter_stream = ais;
            }
            pusher = new SoftAudioPusher(line, ais, controlbuffersize);
            pusher_stream = ais;
            pusher.start();
            if (weakstream != null) {
                weakstream.pusher = pusher;
                weakstream.sourceDataLine = sourceDataLine;
            }
        } catch (final LineUnavailableException | SecurityException | IllegalArgumentException e) {
            if (isOpen()) {
                close();
            }
            // am: need MidiUnavailableException(Throwable) ctor!
            MidiUnavailableException ex = new MidiUnavailableException("Can not open line");
            ex.initCause(e);
            throw ex;
        }
    }
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) MidiUnavailableException(javax.sound.midi.MidiUnavailableException) LineUnavailableException(javax.sound.sampled.LineUnavailableException) IOException(java.io.IOException)

Example 58 with AudioInputStream

use of javax.sound.sampled.AudioInputStream in project jdk8u_jdk by JetBrains.

the class SoftSynthesizer method close.

public void close() {
    if (!isOpen())
        return;
    SoftAudioPusher pusher_to_be_closed = null;
    AudioInputStream pusher_stream_to_be_closed = null;
    synchronized (control_mutex) {
        if (pusher != null) {
            pusher_to_be_closed = pusher;
            pusher_stream_to_be_closed = pusher_stream;
            pusher = null;
            pusher_stream = null;
        }
    }
    if (pusher_to_be_closed != null) {
        // Pusher must not be closed synchronized against control_mutex,
        // this may result in synchronized conflict between pusher
        // and current thread.
        pusher_to_be_closed.stop();
        try {
            pusher_stream_to_be_closed.close();
        } catch (IOException e) {
        //e.printStackTrace();
        }
    }
    synchronized (control_mutex) {
        if (mainmixer != null)
            mainmixer.close();
        open = false;
        implicitOpen = false;
        mainmixer = null;
        voices = null;
        channels = null;
        if (external_channels != null)
            for (int i = 0; i < external_channels.length; i++) external_channels[i].setChannel(null);
        if (sourceDataLine != null) {
            sourceDataLine.close();
            sourceDataLine = null;
        }
        inslist.clear();
        loadedlist.clear();
        tunings.clear();
        while (recvslist.size() != 0) recvslist.get(recvslist.size() - 1).close();
    }
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) IOException(java.io.IOException)

Example 59 with AudioInputStream

use of javax.sound.sampled.AudioInputStream in project jdk8u_jdk by JetBrains.

the class SoftMixingMixer method open.

public void open(SourceDataLine line) throws LineUnavailableException {
    if (isOpen()) {
        implicitOpen = false;
        return;
    }
    synchronized (control_mutex) {
        try {
            if (line != null)
                format = line.getFormat();
            AudioInputStream ais = openStream(getFormat());
            if (line == null) {
                synchronized (SoftMixingMixerProvider.mutex) {
                    SoftMixingMixerProvider.lockthread = Thread.currentThread();
                }
                try {
                    Mixer defaultmixer = AudioSystem.getMixer(null);
                    if (defaultmixer != null) {
                        // Search for suitable line
                        DataLine.Info idealinfo = null;
                        AudioFormat idealformat = null;
                        Line.Info[] lineinfos = defaultmixer.getSourceLineInfo();
                        idealFound: for (int i = 0; i < lineinfos.length; i++) {
                            if (lineinfos[i].getLineClass() == SourceDataLine.class) {
                                DataLine.Info info = (DataLine.Info) lineinfos[i];
                                AudioFormat[] formats = info.getFormats();
                                for (int j = 0; j < formats.length; j++) {
                                    AudioFormat format = formats[j];
                                    if (format.getChannels() == 2 || format.getChannels() == AudioSystem.NOT_SPECIFIED)
                                        if (format.getEncoding().equals(Encoding.PCM_SIGNED) || format.getEncoding().equals(Encoding.PCM_UNSIGNED))
                                            if (format.getSampleRate() == AudioSystem.NOT_SPECIFIED || format.getSampleRate() == 48000.0)
                                                if (format.getSampleSizeInBits() == AudioSystem.NOT_SPECIFIED || format.getSampleSizeInBits() == 16) {
                                                    idealinfo = info;
                                                    int ideal_channels = format.getChannels();
                                                    boolean ideal_signed = format.getEncoding().equals(Encoding.PCM_SIGNED);
                                                    float ideal_rate = format.getSampleRate();
                                                    boolean ideal_endian = format.isBigEndian();
                                                    int ideal_bits = format.getSampleSizeInBits();
                                                    if (ideal_bits == AudioSystem.NOT_SPECIFIED)
                                                        ideal_bits = 16;
                                                    if (ideal_channels == AudioSystem.NOT_SPECIFIED)
                                                        ideal_channels = 2;
                                                    if (ideal_rate == AudioSystem.NOT_SPECIFIED)
                                                        ideal_rate = 48000;
                                                    idealformat = new AudioFormat(ideal_rate, ideal_bits, ideal_channels, ideal_signed, ideal_endian);
                                                    break idealFound;
                                                }
                                }
                            }
                        }
                        if (idealformat != null) {
                            format = idealformat;
                            line = (SourceDataLine) defaultmixer.getLine(idealinfo);
                        }
                    }
                    if (line == null)
                        line = AudioSystem.getSourceDataLine(format);
                } finally {
                    synchronized (SoftMixingMixerProvider.mutex) {
                        SoftMixingMixerProvider.lockthread = null;
                    }
                }
                if (line == null)
                    throw new IllegalArgumentException("No line matching " + info.toString() + " is supported.");
            }
            double latency = this.latency;
            if (!line.isOpen()) {
                int bufferSize = getFormat().getFrameSize() * (int) (getFormat().getFrameRate() * (latency / 1000000f));
                line.open(getFormat(), bufferSize);
                // Remember that we opened that line
                // so we can close again in SoftSynthesizer.close()
                sourceDataLine = line;
            }
            if (!line.isActive())
                line.start();
            int controlbuffersize = 512;
            try {
                controlbuffersize = ais.available();
            } catch (IOException e) {
            }
            // Tell mixer not fill read buffers fully.
            // This lowers latency, and tells DataPusher
            // to read in smaller amounts.
            // mainmixer.readfully = false;
            // pusher = new DataPusher(line, ais);
            int buffersize = line.getBufferSize();
            buffersize -= buffersize % controlbuffersize;
            if (buffersize < 3 * controlbuffersize)
                buffersize = 3 * controlbuffersize;
            if (jitter_correction) {
                ais = new SoftJitterCorrector(ais, buffersize, controlbuffersize);
            }
            pusher = new SoftAudioPusher(line, ais, controlbuffersize);
            pusher_stream = ais;
            pusher.start();
        } catch (LineUnavailableException e) {
            if (isOpen())
                close();
            throw new LineUnavailableException(e.toString());
        }
    }
}
Also used : Mixer(javax.sound.sampled.Mixer) DataLine(javax.sound.sampled.DataLine) SourceDataLine(javax.sound.sampled.SourceDataLine) LineUnavailableException(javax.sound.sampled.LineUnavailableException) IOException(java.io.IOException) AudioInputStream(javax.sound.sampled.AudioInputStream) SourceDataLine(javax.sound.sampled.SourceDataLine) AudioFormat(javax.sound.sampled.AudioFormat)

Example 60 with AudioInputStream

use of javax.sound.sampled.AudioInputStream in project jdk8u_jdk by JetBrains.

the class SoftMixingSourceDataLine method open.

public void open(AudioFormat format, int bufferSize) throws LineUnavailableException {
    LineEvent event = null;
    if (bufferSize < format.getFrameSize() * 32)
        bufferSize = format.getFrameSize() * 32;
    synchronized (control_mutex) {
        if (!isOpen()) {
            if (!mixer.isOpen()) {
                mixer.open();
                mixer.implicitOpen = true;
            }
            event = new LineEvent(this, LineEvent.Type.OPEN, 0);
            this.bufferSize = bufferSize - bufferSize % format.getFrameSize();
            this.format = format;
            this.framesize = format.getFrameSize();
            this.outputformat = mixer.getFormat();
            out_nrofchannels = outputformat.getChannels();
            in_nrofchannels = format.getChannels();
            open = true;
            mixer.getMainMixer().openLine(this);
            cycling_buffer = new byte[framesize * bufferSize];
            cycling_read_pos = 0;
            cycling_write_pos = 0;
            cycling_avail = 0;
            cycling_framepos = 0;
            InputStream cycling_inputstream = new InputStream() {

                public int read() throws IOException {
                    byte[] b = new byte[1];
                    int ret = read(b);
                    if (ret < 0)
                        return ret;
                    return b[0] & 0xFF;
                }

                public int available() throws IOException {
                    synchronized (cycling_buffer) {
                        return cycling_avail;
                    }
                }

                public int read(byte[] b, int off, int len) throws IOException {
                    synchronized (cycling_buffer) {
                        if (len > cycling_avail)
                            len = cycling_avail;
                        int pos = cycling_read_pos;
                        byte[] buff = cycling_buffer;
                        int buff_len = buff.length;
                        for (int i = 0; i < len; i++) {
                            b[off++] = buff[pos];
                            pos++;
                            if (pos == buff_len)
                                pos = 0;
                        }
                        cycling_read_pos = pos;
                        cycling_avail -= len;
                        cycling_framepos += len / framesize;
                    }
                    return len;
                }
            };
            afis = AudioFloatInputStream.getInputStream(new AudioInputStream(cycling_inputstream, format, AudioSystem.NOT_SPECIFIED));
            afis = new NonBlockingFloatInputStream(afis);
            if (Math.abs(format.getSampleRate() - outputformat.getSampleRate()) > 0.000001)
                afis = new AudioFloatInputStreamResampler(afis, outputformat);
        } else {
            if (!format.matches(getFormat())) {
                throw new IllegalStateException("Line is already open with format " + getFormat() + " and bufferSize " + getBufferSize());
            }
        }
    }
    if (event != null)
        sendEvent(event);
}
Also used : LineEvent(javax.sound.sampled.LineEvent) AudioInputStream(javax.sound.sampled.AudioInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) InputStream(java.io.InputStream)

Aggregations

AudioInputStream (javax.sound.sampled.AudioInputStream)92 IOException (java.io.IOException)44 AudioFormat (javax.sound.sampled.AudioFormat)41 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)27 InputStream (java.io.InputStream)24 ByteArrayInputStream (java.io.ByteArrayInputStream)19 LineUnavailableException (javax.sound.sampled.LineUnavailableException)19 File (java.io.File)18 SourceDataLine (javax.sound.sampled.SourceDataLine)14 Clip (javax.sound.sampled.Clip)13 AudioFileFormat (javax.sound.sampled.AudioFileFormat)12 DataLine (javax.sound.sampled.DataLine)12 FileInputStream (java.io.FileInputStream)11 BufferedInputStream (java.io.BufferedInputStream)10 URL (java.net.URL)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 DataInputStream (java.io.DataInputStream)5 SequenceInputStream (java.io.SequenceInputStream)5 LineEvent (javax.sound.sampled.LineEvent)5 AudioSynthesizer (com.sun.media.sound.AudioSynthesizer)4