use of javax.sound.sampled.LineEvent in project jdk8u_jdk by JetBrains.
the class AbstractDataLine method setStarted.
/**
* This method sets the started state and generates
* events if it changes.
*/
final void setStarted(boolean started) {
if (Printer.trace)
Printer.trace("> AbstractDataLine: setStarted(" + started + ")");
boolean sendEvents = false;
long position = getLongFramePosition();
synchronized (this) {
if (this.started != started) {
this.started = started;
sendEvents = true;
}
}
if (sendEvents) {
if (started) {
sendEvents(new LineEvent(this, LineEvent.Type.START, position));
} else {
sendEvents(new LineEvent(this, LineEvent.Type.STOP, position));
}
}
if (Printer.trace)
Printer.trace("< AbstractDataLine: setStarted completed");
}
use of javax.sound.sampled.LineEvent 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);
}
use of javax.sound.sampled.LineEvent in project jdk8u_jdk by JetBrains.
the class SoftMixingMixer method close.
public void close() {
if (!isOpen())
return;
sendEvent(new LineEvent(this, LineEvent.Type.CLOSE, AudioSystem.NOT_SPECIFIED));
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;
if (sourceDataLine != null) {
sourceDataLine.drain();
sourceDataLine.close();
sourceDataLine = null;
}
}
}
Aggregations