LWJGL3 OpenAL seems to cut off the end of sounds

Hello.

I’ve noticed that the last second (there abouts) of any audio clip gets cut-off when I play it in my game. To play the sound I am using: alSourcePlay(id);

The “quick-fix” is obviously to add some sound-less noise to the end of the clip. But this is just a workaround. Any ideas?

What library are you using to decode the audio, and are you streaming it or storing it in one buffer?

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;
    }
}

If you’re just loading the audio in one call you can use:

IntBuffer channels = stack.mallocInt(1), sampleRate = stack.mallocInt(1);
ShortBuffer pcm = STBVorbis.stb_vorbis_decode_memory(vorbis, channels, sampleRate);