tags:

views:

507

answers:

3

I play back sound with Java's SourceDataLine but whenever I try to adjust the volume (gain), there is a 0.2-0.5 second delay between the action and the response in the speaker. The audio data is written in 4k-16k chunks (16bit mono, 22kHz ~ 44k/s).

How can I make this volume adjustment more real-time?

Does the write(byte[], int, int) lock out the gain adjustment of the FloatControl?

Do I need to revert back to a DSP way of concurrent adjustment of the sound buffer data volume or submit smaller chunks?

JDK7, Decent Windows PC

+2  A: 

Sound system takes the data from SourceDataLine in chunks of a few (hundred...) kilobytes and buffers them. If you change params for it, it won't take effect until system playback buffer is empty and new data is read from SDL.

What you need to do is to hack somewhere around changing system playback volume (which has immediate effect) rather than modyfing data you provide to it.

Marcin
Messing with the system's master volume doesn't seem to be a good way as I guess I would need JNI/JNA to do it. And I personally hate those applications which do it that way. Plus, I have to play back music and other sound simultaneously with different volume settings.
kd304
Unfortunately I cannot help you more than that. That's the only thing I know about JavaSound that it is broken in many unexpected ways.. :(
Marcin
Thanks, unfortunately, this topic is also beyond my experties too - this is why I asked. Maybe I should have a second look on the game sound generation.
kd304
A: 

If you can somehow affect/create the Line.Info class your Mixer contains for your Line, you can change its minimum and maximum buffers. Then, you can set the buffers to the size of chunks you're feeding it, and then your delay will reduce to none (supposedly)

Aviad Ben Dov
A: 

Have you tried using a custom (lower) buffer size when calling SourceDataLine.open()?

SourceDataLine.open( AudioFormat format, int bufferSize )

That should decrease latency. The master playback volume solution is still probably going to be faster though.

TomA