From 5139c648f0dab00c3b9c600bbe5091e460436efa Mon Sep 17 00:00:00 2001 From: Paul Ferrand Date: Tue, 25 Aug 2020 19:31:47 +0200 Subject: [PATCH] Engine polyphony is checked in findFreeVoice() --- src/sfizz/Synth.cpp | 29 ++++++++++++----------------- src/sfizz/Synth.h | 5 ++++- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/sfizz/Synth.cpp b/src/sfizz/Synth.cpp index e6da53ad..09fa9736 100644 --- a/src/sfizz/Synth.cpp +++ b/src/sfizz/Synth.cpp @@ -659,7 +659,18 @@ sfz::Voice* sfz::Synth::findFreeVoice() noexcept if (freeVoice != voices.end()) return freeVoice->get(); - return {}; + // Engine polyphony reached + Voice* stolenVoice = stealer.steal(absl::MakeSpan(voiceViewArray)); + if (stolenVoice == nullptr) + return {}; + + auto tempSpan = resources.bufferPool.getStereoBuffer(samplesPerBlock); + SisterVoiceRing::applyToRing(stolenVoice, [&] (Voice* v) { + renderVoiceToOutputs(*v, *tempSpan); + v->reset(); + }); + + return stolenVoice; } int sfz::Synth::getNumActiveVoices(bool recompute) const noexcept @@ -997,27 +1008,11 @@ void sfz::Synth::noteOnDispatch(int delay, int noteNumber, float velocity) noexc } Voice* selectedVoice = findFreeVoice(); - - // Engine polyphony reached, we're stealing something - if (selectedVoice == nullptr) { - selectedVoice = stealer.steal(absl::MakeSpan(voiceViewArray)); - } - // For some reason we did not find a voice to use. // This is a degraded case but we'll just drop the note on. if (selectedVoice == nullptr) continue; - // Kill voice if necessary, pre-rendering it into the output buffers - if (!selectedVoice->isFree()) { - auto tempSpan = resources.bufferPool.getStereoBuffer(samplesPerBlock); - SisterVoiceRing::applyToRing(selectedVoice, [&] (Voice* v) { - renderVoiceToOutputs(*v, *tempSpan); - v->reset(); - }); - } - - // Voice should be free now ASSERT(selectedVoice->isFree()); selectedVoice->startVoice(region, delay, noteNumber, velocity, Voice::TriggerType::NoteOn); ring.addVoiceToRing(selectedVoice); diff --git a/src/sfizz/Synth.h b/src/sfizz/Synth.h index 3fb85f7f..7dd3b0ad 100644 --- a/src/sfizz/Synth.h +++ b/src/sfizz/Synth.h @@ -765,18 +765,21 @@ private: using RegionSetPtr = std::unique_ptr; std::vector regions; std::vector voices; + // These are more general "groups" than sfz and encapsulates the full hierarchy RegionSet* currentSet; OpcodeScope lastHeader { OpcodeScope::kOpcodeScopeGlobal }; std::vector sets; + // These are the `group=` groups where you can off voices std::vector polyphonyGroups; + // Views to speed up iteration over the regions and voices when events // occur in the audio callback VoiceViewVector regionPolyphonyArray; + VoiceViewVector voiceViewArray; VoiceStealing stealer; - VoiceViewVector voiceViewArray; std::array noteActivationLists; std::array ccActivationLists;