From c2716c3beab0440395bdd9fa6dcb4d9d8bd231aa Mon Sep 17 00:00:00 2001 From: Paul Ferrand Date: Sat, 18 Jul 2020 18:20:56 +0200 Subject: [PATCH] getNumActiveVoices takes a boolean flag to recompute the number of active voices All tests updated as this was the previous default behavior. --- src/sfizz/Synth.cpp | 13 ++++++++-- src/sfizz/Synth.h | 2 +- tests/FilesT.cpp | 16 ++++++------- tests/PolyphonyT.cpp | 26 ++++++++++---------- tests/SynthT.cpp | 56 ++++++++++++++++++++++---------------------- 5 files changed, 61 insertions(+), 52 deletions(-) diff --git a/src/sfizz/Synth.cpp b/src/sfizz/Synth.cpp index caf410b1..9a62067a 100644 --- a/src/sfizz/Synth.cpp +++ b/src/sfizz/Synth.cpp @@ -625,9 +625,18 @@ sfz::Voice* sfz::Synth::findFreeVoice() noexcept return {}; } -int sfz::Synth::getNumActiveVoices() const noexcept +int sfz::Synth::getNumActiveVoices(bool recompute) const noexcept { - return activeVoices; + if (!recompute) + return activeVoices; + + int active { 0 }; + for (auto& voice: voices) { + if (!voice->isFree()) + active++; + } + + return active; } void sfz::Synth::garbageCollect() noexcept diff --git a/src/sfizz/Synth.h b/src/sfizz/Synth.h index a0f6f09a..d7f481af 100644 --- a/src/sfizz/Synth.h +++ b/src/sfizz/Synth.h @@ -396,7 +396,7 @@ public: * * @return int */ - int getNumActiveVoices() const noexcept; + int getNumActiveVoices(bool recompute = false) const noexcept; /** * @brief Get the total number of voices in the synth (the polyphony) * diff --git a/tests/FilesT.cpp b/tests/FilesT.cpp index 8d85b23a..73ea4ab0 100644 --- a/tests/FilesT.cpp +++ b/tests/FilesT.cpp @@ -474,7 +474,7 @@ TEST_CASE("[Files] Off by with different delays") synth.loadSfzFile(fs::current_path() / "tests/TestFiles/off_by.sfz"); REQUIRE( synth.getNumRegions() == 4 ); synth.noteOn(0, 63, 63); - REQUIRE( synth.getNumActiveVoices() == 1 ); + REQUIRE( synth.getNumActiveVoices(true) == 1 ); auto group1Voice = synth.getVoiceView(0); REQUIRE( group1Voice->getRegion()->group == 1ul ); REQUIRE( group1Voice->getRegion()->offBy == 2ul ); @@ -490,7 +490,7 @@ TEST_CASE("[Files] Off by with the same delays") synth.loadSfzFile(fs::current_path() / "tests/TestFiles/off_by.sfz"); REQUIRE( synth.getNumRegions() == 4 ); synth.noteOn(0, 63, 63); - REQUIRE( synth.getNumActiveVoices() == 1 ); + REQUIRE( synth.getNumActiveVoices(true) == 1 ); auto group1Voice = synth.getVoiceView(0); REQUIRE( group1Voice->getRegion()->group == 1ul ); REQUIRE( group1Voice->getRegion()->offBy == 2ul ); @@ -505,14 +505,14 @@ TEST_CASE("[Files] Off by with the same notes at the same time") synth.loadSfzFile(fs::current_path() / "tests/TestFiles/off_by.sfz"); REQUIRE( synth.getNumRegions() == 4 ); synth.noteOn(0, 65, 63); - REQUIRE( synth.getNumActiveVoices() == 2 ); + REQUIRE( synth.getNumActiveVoices(true) == 2 ); synth.noteOn(0, 65, 63); - REQUIRE( synth.getNumActiveVoices() == 4 ); + REQUIRE( synth.getNumActiveVoices(true) == 4 ); AudioBuffer buffer { 2, 256 }; synth.renderBlock(buffer); synth.noteOn(0, 65, 63); synth.renderBlock(buffer); - REQUIRE( synth.getNumActiveVoices() == 2 ); + REQUIRE( synth.getNumActiveVoices(true) == 2 ); } TEST_CASE("[Files] Off modes") @@ -522,7 +522,7 @@ TEST_CASE("[Files] Off modes") synth.loadSfzFile(fs::current_path() / "tests/TestFiles/off_mode.sfz"); REQUIRE( synth.getNumRegions() == 3 ); synth.noteOn(0, 64, 63); - REQUIRE( synth.getNumActiveVoices() == 2 ); + REQUIRE( synth.getNumActiveVoices(true) == 2 ); const auto* fastVoice = synth.getVoiceView(0)->getRegion()->offMode == SfzOffMode::fast ? synth.getVoiceView(0) : @@ -532,10 +532,10 @@ TEST_CASE("[Files] Off modes") synth.getVoiceView(1) : synth.getVoiceView(0) ; synth.noteOn(100, 63, 63); - REQUIRE( synth.getNumActiveVoices() == 3 ); + REQUIRE( synth.getNumActiveVoices(true) == 3 ); AudioBuffer buffer { 2, 256 }; synth.renderBlock(buffer); - REQUIRE( synth.getNumActiveVoices() == 2 ); + REQUIRE( synth.getNumActiveVoices(true) == 2 ); REQUIRE( fastVoice->isFree() ); REQUIRE( !normalVoice->isFree() ); } diff --git a/tests/PolyphonyT.cpp b/tests/PolyphonyT.cpp index 23716cab..aeac0ebe 100644 --- a/tests/PolyphonyT.cpp +++ b/tests/PolyphonyT.cpp @@ -78,7 +78,7 @@ TEST_CASE("[Polyphony] group polyphony limits") synth.noteOn(0, 65, 64); synth.noteOn(0, 65, 64); synth.noteOn(0, 65, 64); - REQUIRE(synth.getNumActiveVoices() == 2); // group polyphony should block the last note + REQUIRE(synth.getNumActiveVoices(true) == 2); // group polyphony should block the last note } TEST_CASE("[Polyphony] Hierarchy polyphony limits") @@ -91,7 +91,7 @@ TEST_CASE("[Polyphony] Hierarchy polyphony limits") synth.noteOn(0, 65, 64); synth.noteOn(0, 65, 64); synth.noteOn(0, 65, 64); - REQUIRE(synth.getNumActiveVoices() == 2); + REQUIRE(synth.getNumActiveVoices(true) == 2); } TEST_CASE("[Polyphony] Hierarchy polyphony limits (group)") @@ -104,7 +104,7 @@ TEST_CASE("[Polyphony] Hierarchy polyphony limits (group)") synth.noteOn(0, 65, 64); synth.noteOn(0, 65, 64); synth.noteOn(0, 65, 64); - REQUIRE(synth.getNumActiveVoices() == 2); + REQUIRE(synth.getNumActiveVoices(true) == 2); } TEST_CASE("[Polyphony] Hierarchy polyphony limits (master)") @@ -118,7 +118,7 @@ TEST_CASE("[Polyphony] Hierarchy polyphony limits (master)") synth.noteOn(0, 65, 64); synth.noteOn(0, 65, 64); synth.noteOn(0, 65, 64); - REQUIRE(synth.getNumActiveVoices() == 2); + REQUIRE(synth.getNumActiveVoices(true) == 2); } TEST_CASE("[Polyphony] Hierarchy polyphony limits (limit in another master)") @@ -137,7 +137,7 @@ TEST_CASE("[Polyphony] Hierarchy polyphony limits (limit in another master)") synth.noteOn(0, 66, 64); synth.noteOn(0, 66, 64); synth.noteOn(0, 66, 64); - REQUIRE(synth.getNumActiveVoices() == 5); + REQUIRE(synth.getNumActiveVoices(true) == 5); } TEST_CASE("[Polyphony] Hierarchy polyphony limits (global)") @@ -151,7 +151,7 @@ TEST_CASE("[Polyphony] Hierarchy polyphony limits (global)") synth.noteOn(0, 65, 64); synth.noteOn(0, 65, 64); synth.noteOn(0, 65, 64); - REQUIRE(synth.getNumActiveVoices() == 2); + REQUIRE(synth.getNumActiveVoices(true) == 2); } TEST_CASE("[Polyphony] Polyphony in master") @@ -171,21 +171,21 @@ TEST_CASE("[Polyphony] Polyphony in master") synth.noteOn(0, 65, 64); synth.noteOn(0, 65, 64); synth.noteOn(0, 65, 64); - REQUIRE(synth.getNumActiveVoices() == 2); // group polyphony should block the last note + REQUIRE(synth.getNumActiveVoices(true) == 2); // group polyphony should block the last note synth.allSoundOff(); synth.renderBlock(buffer); - REQUIRE(synth.getNumActiveVoices() == 0); + REQUIRE(synth.getNumActiveVoices(true) == 0); synth.noteOn(0, 63, 64); synth.noteOn(0, 63, 64); synth.noteOn(0, 63, 64); - REQUIRE(synth.getNumActiveVoices() == 2); // group polyphony should block the last note + REQUIRE(synth.getNumActiveVoices(true) == 2); // group polyphony should block the last note synth.allSoundOff(); synth.renderBlock(buffer); - REQUIRE(synth.getNumActiveVoices() == 0); + REQUIRE(synth.getNumActiveVoices(true) == 0); synth.noteOn(0, 61, 64); synth.noteOn(0, 61, 64); synth.noteOn(0, 61, 64); - REQUIRE(synth.getNumActiveVoices() == 3); + REQUIRE(synth.getNumActiveVoices(true) == 3); } @@ -198,7 +198,7 @@ TEST_CASE("[Polyphony] Self-masking") synth.noteOn(0, 64, 63); synth.noteOn(0, 64, 62); synth.noteOn(0, 64, 64); - REQUIRE(synth.getNumActiveVoices() == 3); // One of these is releasing + REQUIRE(synth.getNumActiveVoices(true) == 3); // One of these is releasing REQUIRE(synth.getVoiceView(0)->getTriggerValue() == 63_norm); REQUIRE(!synth.getVoiceView(0)->releasedOrFree()); REQUIRE(synth.getVoiceView(1)->getTriggerValue() == 62_norm); @@ -216,7 +216,7 @@ TEST_CASE("[Polyphony] Not self-masking") synth.noteOn(0, 66, 63); synth.noteOn(0, 66, 62); synth.noteOn(0, 66, 64); - REQUIRE(synth.getNumActiveVoices() == 3); // One of these is releasing + REQUIRE(synth.getNumActiveVoices(true) == 3); // One of these is releasing REQUIRE(synth.getVoiceView(0)->getTriggerValue() == 63_norm); REQUIRE(synth.getVoiceView(0)->releasedOrFree()); // The first encountered voice is the masking candidate REQUIRE(synth.getVoiceView(1)->getTriggerValue() == 62_norm); diff --git a/tests/SynthT.cpp b/tests/SynthT.cpp index bd6b847b..44a3faaa 100644 --- a/tests/SynthT.cpp +++ b/tests/SynthT.cpp @@ -23,11 +23,11 @@ TEST_CASE("[Synth] Play and check active voices") synth.noteOn(0, 36, 24); synth.noteOn(0, 36, 89); - REQUIRE(synth.getNumActiveVoices() == 2); + REQUIRE(synth.getNumActiveVoices(true) == 2); // Render for a while for (int i = 0; i < 200; ++i) synth.renderBlock(buffer); - REQUIRE(synth.getNumActiveVoices() == 0); + REQUIRE(synth.getNumActiveVoices(true) == 0); } TEST_CASE("[Synth] All sound off") @@ -36,9 +36,9 @@ TEST_CASE("[Synth] All sound off") synth.loadSfzFile(fs::current_path() / "tests/TestFiles/groups_avl.sfz"); synth.noteOn(0, 36, 24); synth.noteOn(0, 36, 89); - REQUIRE(synth.getNumActiveVoices() == 2); + REQUIRE(synth.getNumActiveVoices(true) == 2); synth.allSoundOff(); - REQUIRE(synth.getNumActiveVoices() == 0); + REQUIRE(synth.getNumActiveVoices(true) == 0); } TEST_CASE("[Synth] Change the number of voice while playing") @@ -51,9 +51,9 @@ TEST_CASE("[Synth] Change the number of voice while playing") synth.noteOn(0, 36, 24); synth.noteOn(0, 36, 89); synth.renderBlock(buffer); - REQUIRE(synth.getNumActiveVoices() == 2); + REQUIRE(synth.getNumActiveVoices(true) == 2); synth.setNumVoices(8); - REQUIRE(synth.getNumActiveVoices() == 0); + REQUIRE(synth.getNumActiveVoices(true) == 0); REQUIRE(synth.getNumVoices() == 8); } @@ -131,15 +131,15 @@ TEST_CASE("[Synth] All notes offs/all sounds off") )"); synth.noteOn(0, 60, 63); synth.noteOn(0, 62, 63); - REQUIRE( synth.getNumActiveVoices() == 2 ); + REQUIRE( synth.getNumActiveVoices(true) == 2 ); synth.cc(0, 120, 63); - REQUIRE( synth.getNumActiveVoices() == 0 ); + REQUIRE( synth.getNumActiveVoices(true) == 0 ); synth.noteOn(0, 62, 63); synth.noteOn(0, 60, 63); - REQUIRE( synth.getNumActiveVoices() == 2 ); + REQUIRE( synth.getNumActiveVoices(true) == 2 ); synth.cc(0, 123, 63); - REQUIRE( synth.getNumActiveVoices() == 0 ); + REQUIRE( synth.getNumActiveVoices(true) == 0 ); } TEST_CASE("[Synth] Reset all controllers") @@ -491,14 +491,14 @@ TEST_CASE("[Synth] sample quality") // default sample quality synth.noteOn(0, 60, 100); - REQUIRE(synth.getNumActiveVoices() == 1); + REQUIRE(synth.getNumActiveVoices(true) == 1); REQUIRE(synth.getVoiceView(0)->getCurrentSampleQuality() == sfz::Default::sampleQuality); synth.allSoundOff(); // default sample quality, freewheeling synth.enableFreeWheeling(); synth.noteOn(0, 60, 100); - REQUIRE(synth.getNumActiveVoices() == 1); + REQUIRE(synth.getNumActiveVoices(true) == 1); REQUIRE(synth.getVoiceView(0)->getCurrentSampleQuality() == sfz::Default::sampleQualityInFreewheelingMode); synth.allSoundOff(); synth.disableFreeWheeling(); @@ -506,7 +506,7 @@ TEST_CASE("[Synth] sample quality") // user-defined sample quality synth.setSampleQuality(sfz::Synth::ProcessLive, 3); synth.noteOn(0, 60, 100); - REQUIRE(synth.getNumActiveVoices() == 1); + REQUIRE(synth.getNumActiveVoices(true) == 1); REQUIRE(synth.getVoiceView(0)->getCurrentSampleQuality() == 3); synth.allSoundOff(); @@ -514,21 +514,21 @@ TEST_CASE("[Synth] sample quality") synth.enableFreeWheeling(); synth.setSampleQuality(sfz::Synth::ProcessFreewheeling, 8); synth.noteOn(0, 60, 100); - REQUIRE(synth.getNumActiveVoices() == 1); + REQUIRE(synth.getNumActiveVoices(true) == 1); REQUIRE(synth.getVoiceView(0)->getCurrentSampleQuality() == 8); synth.allSoundOff(); synth.disableFreeWheeling(); // region sample quality synth.noteOn(0, 61, 100); - REQUIRE(synth.getNumActiveVoices() == 1); + REQUIRE(synth.getNumActiveVoices(true) == 1); REQUIRE(synth.getVoiceView(0)->getCurrentSampleQuality() == 5); synth.allSoundOff(); // region sample quality, freewheeling synth.enableFreeWheeling(); synth.noteOn(0, 61, 100); - REQUIRE(synth.getNumActiveVoices() == 1); + REQUIRE(synth.getNumActiveVoices(true) == 1); REQUIRE(synth.getVoiceView(0)->getCurrentSampleQuality() == 5); synth.allSoundOff(); synth.disableFreeWheeling(); @@ -551,7 +551,7 @@ TEST_CASE("[Synth] Sister voices") REQUIRE( synth.getVoiceView(0)->getNextSisterVoice() == synth.getVoiceView(0) ); REQUIRE( synth.getVoiceView(0)->getPreviousSisterVoice() == synth.getVoiceView(0) ); synth.noteOn(0, 62, 85); - REQUIRE( synth.getNumActiveVoices() == 3 ); + REQUIRE( synth.getNumActiveVoices(true) == 3 ); REQUIRE( sfz::SisterVoiceRing::countSisterVoices(synth.getVoiceView(1)) == 2 ); REQUIRE( synth.getVoiceView(1)->getNextSisterVoice() == synth.getVoiceView(2) ); REQUIRE( synth.getVoiceView(1)->getPreviousSisterVoice() == synth.getVoiceView(2) ); @@ -559,7 +559,7 @@ TEST_CASE("[Synth] Sister voices") REQUIRE( synth.getVoiceView(2)->getNextSisterVoice() == synth.getVoiceView(1) ); REQUIRE( synth.getVoiceView(2)->getPreviousSisterVoice() == synth.getVoiceView(1) ); synth.noteOn(0, 63, 85); - REQUIRE( synth.getNumActiveVoices() == 6 ); + REQUIRE( synth.getNumActiveVoices(true) == 6 ); REQUIRE( sfz::SisterVoiceRing::countSisterVoices(synth.getVoiceView(3)) == 3 ); REQUIRE( synth.getVoiceView(3)->getNextSisterVoice() == synth.getVoiceView(4) ); REQUIRE( synth.getVoiceView(3)->getPreviousSisterVoice() == synth.getVoiceView(5) ); @@ -599,14 +599,14 @@ TEST_CASE("[Synth] Sisters and off-by") group=2 key=63 sample=*saw )"); synth.noteOn(0, 62, 85); - REQUIRE( synth.getNumActiveVoices() == 2 ); + REQUIRE( synth.getNumActiveVoices(true) == 2 ); REQUIRE( sfz::SisterVoiceRing::countSisterVoices(synth.getVoiceView(0)) == 2 ); synth.renderBlock(buffer); - REQUIRE( synth.getNumActiveVoices() == 2 ); + REQUIRE( synth.getNumActiveVoices(true) == 2 ); synth.noteOn(0, 63, 85); - REQUIRE( synth.getNumActiveVoices() == 3 ); + REQUIRE( synth.getNumActiveVoices(true) == 3 ); synth.renderBlock(buffer); - REQUIRE( synth.getNumActiveVoices() == 2 ); + REQUIRE( synth.getNumActiveVoices(true) == 2 ); REQUIRE( sfz::SisterVoiceRing::countSisterVoices(synth.getVoiceView(0)) == 1 ); } @@ -619,7 +619,7 @@ TEST_CASE("[Synth] Release key") synth.noteOn(0, 62, 85); synth.cc(0, 64, 127); synth.noteOff(0, 62, 85); - REQUIRE( synth.getNumActiveVoices() == 1 ); + REQUIRE( synth.getNumActiveVoices(true) == 1 ); } TEST_CASE("[Synth] Release") @@ -631,9 +631,9 @@ TEST_CASE("[Synth] Release") synth.noteOn(0, 62, 85); synth.cc(0, 64, 127); synth.noteOff(0, 62, 85); - REQUIRE( synth.getNumActiveVoices() == 0 ); + REQUIRE( synth.getNumActiveVoices(true) == 0 ); synth.cc(0, 64, 0); - REQUIRE( synth.getNumActiveVoices() == 1 ); + REQUIRE( synth.getNumActiveVoices(true) == 1 ); } TEST_CASE("[Synth] Release key (Different sustain CC)") @@ -646,7 +646,7 @@ TEST_CASE("[Synth] Release key (Different sustain CC)") synth.noteOn(0, 62, 85); synth.cc(0, 54, 127); synth.noteOff(0, 62, 85); - REQUIRE( synth.getNumActiveVoices() == 1 ); + REQUIRE( synth.getNumActiveVoices(true) == 1 ); } TEST_CASE("[Synth] Release (Different sustain CC)") @@ -659,9 +659,9 @@ TEST_CASE("[Synth] Release (Different sustain CC)") synth.noteOn(0, 62, 85); synth.cc(0, 54, 127); synth.noteOff(0, 62, 85); - REQUIRE( synth.getNumActiveVoices() == 0 ); + REQUIRE( synth.getNumActiveVoices(true) == 0 ); synth.cc(0, 54, 0); - REQUIRE( synth.getNumActiveVoices() == 1 ); + REQUIRE( synth.getNumActiveVoices(true) == 1 ); } TEST_CASE("[Synth] Sustain threshold default")