Slick util OpenAL get song length

Hi, I am trying to get the length of a song with OpenAL.
The song streamed rather than fully loaded. I am using .ogg files. A java version of the below code just returns 0.

ALfloat GetBufferLength(ALuint buffer)
    ALint size, bits, channels, freq;

    alGetBufferi(buffer, AL_SIZE, &size);
    alGetBufferi(buffer, AL_BITS, &bits);
    alGetBufferi(buffer, AL_CHANNELS, &channels);
    alGetBufferi(buffer, AL_FREQUENCY, &freq);
    if(alGetError() != AL_NO_ERROR)
        return -1.0f;

    return (ALfloat)((ALuint)size/channels/(bits/8)) / (ALfloat)freq;

I think it’s because the song is streaming? Is there any way to fix this?

Since you’re streaming the file in chunks, there is no easy means of determining its full length.

Your options:
a) Hardcode the song length in your Java code as a constant, or include it in a “properties” file alongside your OGG audio.

b) Stream through the file as quickly as you can without playing it to determine its total length. (Will probably not be possible with SlickUtil.)

c) Read up on the OGG spec and parse the headers yourself. Find the end time stamp on the last Ogg page and subtract the starting time stamp from the first Ogg page.

Option A is definitely the easiest.

Thanks davedes :slight_smile:
A combination of a + b sounds good for me, streaming at normal speed and saving the length to a file when the song finishes.

alGetBufferi(buffer, AL_BITS, &bits);

AL_BITS most likely returns the number of bits in each audio sample (8 or 16)