c++openalfmod

How to get length (duration) of a source with single buffer in OpenAL?


I'm migrating from FMOD to OpenAL and I can't find an analogue of the FSOUND_Sample_GetLength() function, which returns the length of the sample in samples (it doesn't take frequency in consideration)... I get OpenAL buffer name from alutLoadMemoryFromFileImage(), so I can't get the waveform data this way.

Please help!


Solution

  • You need to piece it together yourself using alGetBufferi():

    ALint sizeInBytes;
    ALint channels;
    ALint bits;
    
    alGetBufferi(bufferID, AL_SIZE, &sizeInBytes);
    alGetBufferi(bufferID, AL_CHANNELS, &channels);
    alGetBufferi(bufferID, AL_BITS, &bits);
    
    lengthInSamples = sizeInBytes * 8 / (channels * bits);
    

    And for duration in seconds:

    ALint frequency;
    
    alGetBufferi(bufferID, AL_FREQUENCY, &frequency);
    
    durationInSeconds = (float)lengthInSamples / (float)frequency;