Since I've given up on JavaSound...

…I’m trying to convert my work to OpenAL. So far I’ve implemented an OpenAL version of Clip, which was fairly simple and works great (almost no latency anymore!).
Now I’m trying to find a way to implement streaming, for realtime audio generation. For example my arcade emulator needs this, but also my audio synthesis library I’m working on.
I basically need an equivalent to this (code coming from my arcade emulator in an update() method which is called every frame):


sourceDataLine.write(soundChip.getData(), 0, WRITE_BUF_SIZE * 2);

Very easy to do in JavaSound, but since JavaSound behaves very differently in every other version, it proved to be unusable. I’ve done enough tinkering with the sound code and now it doesn’t seem to work again on java5 so I’m finally scrapping JavaSound in favour of OpenAL.

I’ve looked on the internet, but found almost nothing about this, just an overcomplicated technique was discussed somewhere but without any code samples.

Has anybody already done this already and even have a code sample?

Yes I know, I’m a lazy bum ;D

OggPlayer uses OpenAL to stream ogg files. You can look at its source and the OpenAL documentation on openal.org to figure how to do it.

Basicly you must set up a queue of 2 or more buffers. OpenAL lets you know when a buffer is finished playing. You then unqueue the buffer, fill it with your data and add it at the end of the queue.

Fantastic, thanks Tom! :smiley:

Do you know if there are any constraints in the size of the buffers? No power of 2 or anything? I’m asking because I’m thinking about 2 buffers, exactly the size needed for one frame (so usually 1/60th second of 44.1khz sound), so that I can queue a new buffer every frame.

There is no restrictions on the size of the buffers as far as I can tell.

You need to use more than 2 buffers if you intend to queue a new buffer every frame. It is unlikely that you can manage to synchronize the buffer swaps exactly with the display. You also need to guard yourself against pauses caused by the GC or other background processes.