The next feature that we will implement is the ability to endlessly make a sound loop. Like the ability to pause a sound, this is also quite trivial to implement. We begin by repeating everything that we did for pausing, but for looping instead:
public: bool GetLoop() const { return loop; } void SetLoop(bool value) { loop = value } private: bool loop;
Inside the WriteSoundData()
method, in the part where we used to detect if the sound had already reached the end, we first check if the loop variable is set to true
, and if that is the case, we set the position back to the beginning instead of stopping the sound:
void MyChannel::WriteSoundData(PCM16* data, int count) { if(sound == 0 || paused) return; for (int i = 0; i < count; i += 2) { if(position >= sound->count) { if(loop) { position = 0; } else { Stop(); return; } } PCM16 value = sound->data[position]; data[i] = value; data[i+1] = value; ...