diff --git a/src/sfizz/Config.h b/src/sfizz/Config.h index 2a597c67..451a5bcc 100644 --- a/src/sfizz/Config.h +++ b/src/sfizz/Config.h @@ -139,6 +139,17 @@ namespace config { */ static constexpr float overflowVoiceMultiplier { 1.5f }; static_assert(overflowVoiceMultiplier >= 1.0f, "This needs to add voices"); + + /** + * @brief Calculate the effective voice number for the polyphony setting, + * accounting for the overflow factor. + */ + inline constexpr int calculateActualVoices(int polyphony) + { + return + (int(polyphony * config::overflowVoiceMultiplier) < int(config::maxVoices)) ? + int(polyphony * config::overflowVoiceMultiplier) : int(config::maxVoices); + } } // namespace config } // namespace sfz diff --git a/src/sfizz/PolyphonyGroup.cpp b/src/sfizz/PolyphonyGroup.cpp index 18b7a34f..dcb608f8 100644 --- a/src/sfizz/PolyphonyGroup.cpp +++ b/src/sfizz/PolyphonyGroup.cpp @@ -1,9 +1,13 @@ #include "PolyphonyGroup.h" +sfz::PolyphonyGroup::PolyphonyGroup() +{ + voices.reserve(config::maxVoices); +} + void sfz::PolyphonyGroup::setPolyphonyLimit(unsigned limit) noexcept { polyphonyLimit = limit; - voices.reserve(limit); } void sfz::PolyphonyGroup::registerVoice(Voice* voice) noexcept diff --git a/src/sfizz/PolyphonyGroup.h b/src/sfizz/PolyphonyGroup.h index 6a87d05f..015c3a75 100644 --- a/src/sfizz/PolyphonyGroup.h +++ b/src/sfizz/PolyphonyGroup.h @@ -16,6 +16,8 @@ namespace sfz { class PolyphonyGroup { public: + PolyphonyGroup(); + /** * @brief Set the polyphony limit for this polyphony group. * diff --git a/src/sfizz/RegionSet.cpp b/src/sfizz/RegionSet.cpp index bfceb4e2..dfd85872 100644 --- a/src/sfizz/RegionSet.cpp +++ b/src/sfizz/RegionSet.cpp @@ -1,9 +1,16 @@ #include "RegionSet.h" +sfz::RegionSet::RegionSet(RegionSet* parentSet, OpcodeScope level) + : parent(parentSet), level(level) +{ + voices.reserve(config::maxVoices); + if (parentSet != nullptr) + parentSet->addSubset(this); +} + void sfz::RegionSet::setPolyphonyLimit(unsigned limit) noexcept { polyphonyLimit = limit; - voices.reserve(limit); } void sfz::RegionSet::addRegion(Region* region) noexcept diff --git a/src/sfizz/RegionSet.h b/src/sfizz/RegionSet.h index 4077fbe7..66b11d2f 100644 --- a/src/sfizz/RegionSet.h +++ b/src/sfizz/RegionSet.h @@ -18,12 +18,7 @@ namespace sfz class RegionSet { public: RegionSet() = delete; - RegionSet(RegionSet* parentSet, OpcodeScope level) - : parent(parentSet), level(level) - { - if (parentSet != nullptr) - parentSet->addSubset(this); - } + RegionSet(RegionSet* parentSet, OpcodeScope level); /** * @brief Set the polyphony limit for the set * diff --git a/src/sfizz/Voice.cpp b/src/sfizz/Voice.cpp index cdad7af7..084d4a81 100644 --- a/src/sfizz/Voice.cpp +++ b/src/sfizz/Voice.cpp @@ -1645,7 +1645,7 @@ void Voice::Impl::pitchEnvelope(absl::Span pitchSpan) noexcept if (!bends) return; - const auto events = resources_.midiState.getPitchEvents(); + const EventVector& events = resources_.midiState.getPitchEvents(); const auto bendLambda = [this](float bend) { return centsFactor(region_->getBendInCents(bend)); }; diff --git a/src/sfizz/VoiceManager.cpp b/src/sfizz/VoiceManager.cpp index b4d93e13..490927b2 100644 --- a/src/sfizz/VoiceManager.cpp +++ b/src/sfizz/VoiceManager.cpp @@ -159,15 +159,14 @@ Voice* VoiceManager::findFreeVoice() noexcept void VoiceManager::requireNumVoices(int numVoices, Resources& resources) { - numActualVoices_ = - static_cast(config::overflowVoiceMultiplier * numVoices); numRequiredVoices_ = numVoices; + const int numEffectiveVoices = getNumEffectiveVoices(); clear(); - list_.reserve(numActualVoices_); - activeVoices_.reserve(numActualVoices_); + list_.reserve(numEffectiveVoices); + activeVoices_.reserve(numEffectiveVoices); - for (int i = 0; i < numActualVoices_; ++i) { + for (int i = 0; i < numEffectiveVoices; ++i) { list_.emplace_back(i, resources); Voice& lastVoice = list_.back(); lastVoice.setStateListener(this); diff --git a/src/sfizz/VoiceManager.h b/src/sfizz/VoiceManager.h index 627fd600..e924ae96 100644 --- a/src/sfizz/VoiceManager.h +++ b/src/sfizz/VoiceManager.h @@ -133,7 +133,7 @@ struct VoiceManager final : public Voice::StateListener private: int numRequiredVoices_ { config::numVoices }; - int numActualVoices_ { static_cast(config::numVoices * config::overflowVoiceMultiplier) }; + int getNumEffectiveVoices() const noexcept { return config::calculateActualVoices(numRequiredVoices_); } std::vector list_; std::vector activeVoices_; // These are the `group=` groups where you can off voices