I am using the library that comes with Lwjgl3, STB. As such, I’m using the STBVorbis classes to load an ogg audio buffer into my game.
Here is the buffer creation code (It is not my own):
private ShortBuffer readVorbis(String resource, int bufferSize, STBVorbisInfo info) throws Exception {
try (MemoryStack stack = MemoryStack.stackPush()) {
vorbis = IOUtil.ioResourceToByteBuffer(resource, bufferSize);
IntBuffer error = stack.mallocInt(1);
long decoder = stb_vorbis_open_memory(vorbis, error, null);
if (decoder == NULL) {
throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0));
}
stb_vorbis_get_info(decoder, info);
int channels = info.channels();
int lengthSamples = stb_vorbis_stream_length_in_samples(decoder);
pcm = MemoryUtil.memAllocShort(lengthSamples);
pcm.limit(stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm) * channels);
stb_vorbis_close(decoder);
return pcm;
}
}