I'm making a game with LWJGL 3, and for the first time I'm learning about the OpenAL library. I'm going to have to include long audio files, and I have decided that OGG will be used for the audio, since music would otherwise take up a lot of space.
What I don't know, though, is anything about how to get the audio data out of the OGG and put them into the OpenAL buffers, which need a ShortBuffer
of signed short PCM samples.
I've already been trying to look for answers, but everything I find seems to be outdated or irrelevant to my case.
So to summarize: I have OGG files and I need to be able to load them to OpenAL buffers. Also the ability to stream from the files is obviously a must (in this case, read the files one chunk at a time into a double buffer), since music is involved.
Example:
ShortBuffer samples = readOggSamplesToBuffer(someInputStream);
int bufferId = AL10.alGenBuffers();
// OpenAL wants the samples as signed short
AL10.alBufferData(bufferId, AL10.AL_FORMAT_MONO16, samples, 44100);
Where the function readOggSamplesToBuffer
would be something like this:
private ShortBuffer readOggSamplesToBuffer(InputStream in) {
// This theoretical class is what I need – something that
// can read the data from the OGG file.
OggReader reader = new OggReader(in);
// Very simplified example
ShortBuffer buffer = BufferUtils.createShortBuffer(reader.getSampleCount());
buffer.put(reader.samplesAsShortArray());
return buffer;
}
The STBVorbis
class can read Ogg Vorbis files. For streaming you probably want to use
STBVorbis.stb_vorbis_open_filename
STBVorbis.stb_vorbis_get_info
STBVorbis.stb_vorbis_get_samples_short_interleaved