From 15d360663104fc5d22076b064fba157264848272 Mon Sep 17 00:00:00 2001 From: Iliyas Jorio Date: Sun, 25 Sep 2022 19:06:52 +0200 Subject: [PATCH] Support snd resources with non-0 sustain loop start frame --- src/SoundMixer/SoundManager.cpp | 17 +++++++++++++++-- src/SoundMixer/cmixer.cpp | 3 ++- src/SoundMixer/cmixer.h | 1 + 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/SoundMixer/SoundManager.cpp b/src/SoundMixer/SoundManager.cpp index e02a9fe..76f238b 100644 --- a/src/SoundMixer/SoundManager.cpp +++ b/src/SoundMixer/SoundManager.cpp @@ -175,8 +175,21 @@ static void InstallSoundInChannel(SndChannelPtr chan, const Ptr sampledSoundHead { impl.source.SetLoop(true); - if (info.loopStart != 0) - TODO2("Warning: looping on a portion of the snd isn't supported yet"); + // Set sustain loop start frame + if ((int) info.loopStart >= impl.source.length) + { + TODO2("Warning: Illegal sustain loop start frame"); + } + else + { + impl.source.sustainOffset = info.loopStart; + } + + // Check sustain loop end frame + if ((int) info.loopEnd != impl.source.length) + { + TODO2("Warning: Unsupported sustain loop end frame"); + } } //--------------------------------- diff --git a/src/SoundMixer/cmixer.cpp b/src/SoundMixer/cmixer.cpp index 55346ad..32403ca 100644 --- a/src/SoundMixer/cmixer.cpp +++ b/src/SoundMixer/cmixer.cpp @@ -247,6 +247,7 @@ void Source::Init(int theSampleRate, int theLength) { this->samplerate = theSampleRate; this->length = theLength; + this->sustainOffset = 0; SetGain(1); SetPan(0); SetPitch(1); @@ -592,7 +593,7 @@ void WavStream::FillBuffer(int16_t* dst, int fillLength) // Loop back and continue filling buffer if we didn't fill the buffer if (fillLength > 0) { - idx = 0; + idx = sustainOffset; } } } diff --git a/src/SoundMixer/cmixer.h b/src/SoundMixer/cmixer.h index e68c4ff..6e88467 100644 --- a/src/SoundMixer/cmixer.h +++ b/src/SoundMixer/cmixer.h @@ -46,6 +46,7 @@ namespace cmixer int16_t pcmbuf[BUFFER_SIZE]; // Internal buffer with raw stereo PCM int samplerate; // Stream's native samplerate int length; // Stream's length in frames + int sustainOffset; // Offset of the sustain loop in frames int end; // End index for the current play-through int state; // Current state (playing|paused|stopped) int64_t position; // Current playhead position (fixed point)