Search code examples
c++audiogame-engineopenal

OpenAL playing audio from certain timestamp


I am writing a dialogue system for my game engine in C++. In order to group dialogue together I am having different dialogue sections placed within one file, and one buffer. Therefore how do I tell OpenAL to play the buffer from a specific time (or sample it doesn't really matter to me) into the buffer. Thanks for any help in advance!


Solution

  • void PlayFromSpecifiedTime(ALfloat seconds) const
    {
        alSourcef(source, AL_SEC_OFFSET, seconds);
        alSourcePlay(source);
    }
    

    Or, if you want to play from a certain sample from the buffer:

    void PlayFromSpecifiedSample(ALint sample) const
    {
        alSourcei(source, AL_SAMPLE_OFFSET, sample);
        alSourcePlay(source);
    }
    

    You can also add a check at the beginning to see if you're not trying to skip to a certain time (or sample) beyond the total amount from the buffer. If it does, you simply return; out of it. This assumes you know what you're doing.