Search code examples
audiooffsetopenal

How does one jump to a particular offset more than once?


Imagine this function:

void SoundManager::playSource(ALuint sourceID, float offset)
{
    alSourceStop(sourceID);

    ALint iTotal = 0;
    ALint iCurrent = 0;
    ALint uiBuffer = 0;
    alGetSourcei(sourceID, AL_BUFFER, &uiBuffer);
    alGetBufferi(uiBuffer, AL_SIZE, &iTotal);
    iCurrent = iTotal * offset;

    alSourcei(sourceID, AL_BYTE_OFFSET, iCurrent);
    alSourcePlay(sourceID);
}

The idea is calling playSource(x, 0.5f) would jump to (roughly) halfway through the buffer, etc.

It works fine the first time I call it, but if I call it again on the same source (whether that source is playing or not) it begins playing as though I'd called it with offset 0.

Any ideas why?


Solution

  • Solved!

    Even though the API claims that setting the offsets works on sources in any state, the problem was I should have been calling alSourceRewind instead of alSourceStop at the start.

    It seems setting offsets only works on sources in the AL_INITIAL state.