Search in sources :

Example 11 with LineUnavailableException

use of javax.sound.sampled.LineUnavailableException 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 12 with LineUnavailableException

use of javax.sound.sampled.LineUnavailableException 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 13 with LineUnavailableException

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

the class DataLine_ArrayIndexOutOfBounds method testTDL.

static void testTDL(Mixer mixer, Scenario scenario) {
    log("  Testing TDL (scenario: " + scenario + ")...");
    Line.Info linfo = new Line.Info(TargetDataLine.class);
    TargetDataLine line = null;
    try {
        line = (TargetDataLine) 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("    read...");
    try {
        line.read(buffer, offset, len);
        log("    ERROR: didn't get ArrayIndexOutOfBoundsException");
        failed++;
    } catch (ArrayIndexOutOfBoundsException ex) {
        log("    OK: got ArrayIndexOutOfBoundsException: " + ex);
    }
    lineStopper.force();
}
Also used : TargetDataLine(javax.sound.sampled.TargetDataLine) Line(javax.sound.sampled.Line) DataLine(javax.sound.sampled.DataLine) SourceDataLine(javax.sound.sampled.SourceDataLine) LineUnavailableException(javax.sound.sampled.LineUnavailableException) TargetDataLine(javax.sound.sampled.TargetDataLine)

Example 14 with LineUnavailableException

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

the class DataLine_ArrayIndexOutOfBounds method main.

public static void main(String[] args) throws Exception {
    Mixer.Info[] infos = AudioSystem.getMixerInfo();
    log("" + infos.length + " mixers detected");
    for (int i = 0; i < infos.length; i++) {
        Mixer mixer = AudioSystem.getMixer(infos[i]);
        log("Mixer " + (i + 1) + ": " + infos[i]);
        try {
            mixer.open();
            for (Scenario scenario : scenarios) {
                testSDL(mixer, scenario);
                testTDL(mixer, scenario);
            }
            mixer.close();
        } catch (LineUnavailableException ex) {
            log("LineUnavailableException: " + ex);
        }
    }
    if (failed == 0) {
        log("PASSED (" + total + " tests)");
    } else {
        log("FAILED (" + failed + " of " + total + " tests)");
        throw new Exception("Test FAILED");
    }
}
Also used : Mixer(javax.sound.sampled.Mixer) LineUnavailableException(javax.sound.sampled.LineUnavailableException) LineUnavailableException(javax.sound.sampled.LineUnavailableException)

Example 15 with LineUnavailableException

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

the class ClipSetPos method main.

public static void main(String[] args) {
    boolean testPassed = true;
    Clip clip = null;
    try {
        clip = (Clip) AudioSystem.getLine(new DataLine.Info(Clip.class, audioFormat));
        clip.open(audioFormat, dataBuffer, 0, dataBuffer.length);
    } catch (LineUnavailableException ex) {
        log(ex);
        log("Cannot test (this is not failure)");
        return;
    } catch (IllegalArgumentException ex) {
        log(ex);
        log("Cannot test (this is not failure)");
        return;
    }
    log("clip: " + clip.getClass().getName());
    int len = clip.getFrameLength();
    for (int pos = 0; pos < len; pos += (len / 100)) {
        clip.setFramePosition(pos);
        int curPos = clip.getFramePosition();
        if (Math.abs(pos - curPos) > MAX_FRAME_DELTA) {
            log("Tried to set pos to " + pos + ", but got back " + curPos);
            testPassed = false;
        } else {
            log("Sucessfully set pos to " + pos);
        }
    }
    clip.close();
    if (testPassed) {
        log("Test PASSED.");
    } else {
        log("Test FAILED.");
        throw new RuntimeException("Test FAILED (see log)");
    }
}
Also used : Clip(javax.sound.sampled.Clip) DataLine(javax.sound.sampled.DataLine) LineUnavailableException(javax.sound.sampled.LineUnavailableException)

Aggregations

LineUnavailableException (javax.sound.sampled.LineUnavailableException)16 DataLine (javax.sound.sampled.DataLine)11 SourceDataLine (javax.sound.sampled.SourceDataLine)7 IOException (java.io.IOException)6 AudioInputStream (javax.sound.sampled.AudioInputStream)6 AudioFormat (javax.sound.sampled.AudioFormat)5 Clip (javax.sound.sampled.Clip)3 Mixer (javax.sound.sampled.Mixer)3 TargetDataLine (javax.sound.sampled.TargetDataLine)3 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)3 FileNotFoundException (java.io.FileNotFoundException)2 URL (java.net.URL)2 Line (javax.sound.sampled.Line)2 Countdown (com.bixly.pastevid.screencap.components.capturebox.Countdown)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 FileInputStream (java.io.FileInputStream)1 MissingResourceException (java.util.MissingResourceException)1 MidiUnavailableException (javax.sound.midi.MidiUnavailableException)1 LineEvent (javax.sound.sampled.LineEvent)1