Skip to main content

Audio source latency

No replies
hellbard
Offline
Joined: 2009-04-21

Hi,
I have problems with audio latency on TargetDataLine. I am using java 1.6 on Windows XP. It seems that java reads audio from system in 200ms chunks.

This is my audio capture code:

audioFormat = new AudioFormat( sampleRate, sampleSizeInBits,
channels, signed, bigEndian);
logger.debug("Try to get audio source line with format: " + audioFormat.toString());
try {
// create new audio data line info
DataLine.Info lineInfo = new DataLine.Info(TargetDataLine.class, new AudioFormat[] {audioFormat}, 160, 320);
// get and open input audio line
inLine = (TargetDataLine) AudioSystem.getLine(lineInfo);
inLine.open(audioFormat);
} catch (LineUnavailableException ex) {
ex.printStackTrace();
logger.error("Cannot get audio source line due to error. Stack trace:", ex);
}
inLine.start();
// initialize read buffer
int bufferSize = 160; //(int)audioFormat.getSampleRate() * audioFormat.getFrameSize();
byte buffer[] = new byte[bufferSize];
while (capturing) {
int count = inLine.read(buffer, 0, buffer.length);
if (count > 0) {
audioReceiver.processMediaData(ByteBuffer.wrap(buffer, 0, count), bufferSize);
}
}

As you can see above, I am trying to use 160 bytes buffer. But when I check the data available in targetDataLine`s internal buffer I saw that the length of incoming data is 1600b.

Is there any way to force java to read audio data from system in smaller chunks (perhaps 160b)?

Thanks for any advice.

Milan