Skip to content

Commit

Permalink
Fixes an issue that caused stuck notes.
Browse files Browse the repository at this point in the history
Fixes an issue that caused a crash when using a loop.
  • Loading branch information
KKQ-KKQ authored and paulfd committed Mar 20, 2024
1 parent 4cafa37 commit 4055582
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/sfizz/ADSREnvelope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void ADSREnvelope::getBlockInternal(absl::Span<Float> output) noexcept
if (releaseDelay > 0) {
// prevent computing the segment further than release point
size = std::min<size_t>(size, releaseDelay);
} else if (releaseDelay == 0 && delay < 0) {
} else if (releaseDelay == 0 && delay <= 0) {
// release takes effect this frame
currentState = State::Release;
releaseDelay = -1;
Expand Down
2 changes: 1 addition & 1 deletion src/sfizz/Voice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,7 @@ void Voice::Impl::fillWithData(AudioSpan<float> buffer) noexcept
unsigned i = 0;
while (i < numSamples) {
int wrappedIndex = (*indices)[i] - loop.size * blockRestarts;
if (wrappedIndex > loop.end) {
while (wrappedIndex > loop.end) {
wrappedIndex -= loop.size;
blockRestarts += 1;
loop_.restarts += 1;
Expand Down
43 changes: 43 additions & 0 deletions tests/SynthT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2160,3 +2160,46 @@ TEST_CASE("[Synth] Reuse offed voices in the last case scenario for new notes")
REQUIRE( notes == std::vector<int> { i - 1, i } );
}
}

TEST_CASE("[Synth] Note on and off at the maximum delay")
{
sfz::Synth synth;
synth.setSampleRate(12800);
synth.setSamplesPerBlock(128);
sfz::AudioBuffer<float> buffer { 2, static_cast<unsigned>(synth.getSamplesPerBlock()) };
synth.loadSfzString(fs::current_path() / "tests/TestFiles/noteonoff.sfz", R"(
<region> loop_start=4 loop_end=124 ampeg_release=0.5 sample=looped_flute.wav
)");
synth.setNumVoices(128);
synth.noteOn(127, 64, 64);
CHECK(synth.getNumActiveVoices() == 1);
synth.noteOff(127, 64, 0);
CHECK(synth.getNumActiveVoices() == 1);
// Render for a while
for (int i = 0; i < 100; ++i)
synth.renderBlock(buffer);
CHECK(synth.getNumActiveVoices() == 0);
}

TEST_CASE("[Synth] Note on and off with delay")
{
sfz::Synth synth;
synth.setSampleRate(12800);
synth.setSamplesPerBlock(128);
sfz::AudioBuffer<float> buffer { 2, static_cast<unsigned>(synth.getSamplesPerBlock()) };
synth.loadSfzString(fs::current_path() / "tests/TestFiles/noteonoff.sfz", R"(
<region> loop_start=4 loop_end=124 ampeg_release=0.5 sample=looped_flute.wav
)");
synth.setNumVoices(128);

int i;
for (i = 0; i < 127; ++i) {
synth.noteOn(i, i, 64);
synth.noteOff(i+1, i, 0);
}
CHECK(synth.getNumActiveVoices() == 127);
// Render for a while
for (int i = 0; i < 100; ++i)
synth.renderBlock(buffer);
CHECK(synth.getNumActiveVoices() == 0);
}

0 comments on commit 4055582

Please sign in to comment.