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;
}
}
}
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());
}
}
}
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();
}
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");
}
}
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)");
}
}
Aggregations