Skip to content

Commit

Permalink
Merge pull request #619 from jpcima/voice-manager-fix
Browse files Browse the repository at this point in the history
Fix voice groups destroyed after polyphony changed
  • Loading branch information
jpcima authored Feb 2, 2021
2 parents 00f2e7e + 53fc2a0 commit 0a63827
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/sfizz/PolyphonyGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ void sfz::PolyphonyGroup::removeVoice(const Voice* voice) noexcept
swapAndPopFirst(voices, [voice](const Voice* v) { return v == voice; });
}

void sfz::PolyphonyGroup::removeAllVoices() noexcept
{
voices.clear();
}

unsigned sfz::PolyphonyGroup::numPlayingVoices() const noexcept
{
return absl::c_count_if(voices, [](const Voice* v) {
Expand Down
4 changes: 4 additions & 0 deletions src/sfizz/PolyphonyGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class PolyphonyGroup {
* @param voice
*/
void removeVoice(const Voice* voice) noexcept;
/**
* @brief Remove all the voices from this polyphony group.
*/
void removeAllVoices() noexcept;
/**
* @brief Get the polyphony limit for this group
*
Expand Down
27 changes: 17 additions & 10 deletions src/sfizz/VoiceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,22 @@ namespace sfz {

void VoiceManager::onVoiceStateChanging(NumericId<Voice> id, Voice::State state)
{
(void)id;
if (state == Voice::State::idle) {
auto voice = getVoiceById(id);
RegionSet::removeVoiceFromHierarchy(voice->getRegion(), voice);
Voice* voice = getVoiceById(id);
const Region* region = voice->getRegion();
const uint32_t group = region->group;
RegionSet::removeVoiceFromHierarchy(region, voice);
swapAndPopFirst(activeVoices_, [voice](const Voice* v) { return v == voice; });
polyphonyGroups_[voice->getRegion()->group].removeVoice(voice);
ASSERT(group < polyphonyGroups_.size());
polyphonyGroups_[group].removeVoice(voice);
} else if (state == Voice::State::playing) {
auto voice = getVoiceById(id);
Voice* voice = getVoiceById(id);
const Region* region = voice->getRegion();
const uint32_t group = region->group;
activeVoices_.push_back(voice);
RegionSet::registerVoiceInHierarchy(voice->getRegion(), voice);
polyphonyGroups_[voice->getRegion()->group].registerVoice(voice);
RegionSet::registerVoiceInHierarchy(region, voice);
ASSERT(group < polyphonyGroups_.size());
polyphonyGroups_[group].registerVoice(voice);
}
}

Expand Down Expand Up @@ -81,8 +86,9 @@ bool VoiceManager::playingAttackVoice(const Region* releaseRegion) noexcept

void VoiceManager::ensureNumPolyphonyGroups(unsigned groupIdx) noexcept
{
while (polyphonyGroups_.size() <= groupIdx)
polyphonyGroups_.emplace_back();
size_t neededSize = static_cast<size_t>(groupIdx) + 1;
if (polyphonyGroups_.size() < neededSize)
polyphonyGroups_.resize(neededSize);
}

void VoiceManager::setGroupPolyphony(unsigned groupIdx, unsigned polyphony) noexcept
Expand All @@ -99,7 +105,8 @@ const PolyphonyGroup* VoiceManager::getPolyphonyGroupView(int idx) const noexcep

void VoiceManager::clear()
{
reset();
for (PolyphonyGroup& pg : polyphonyGroups_)
pg.removeAllVoices();
list_.clear();
activeVoices_.clear();
}
Expand Down

0 comments on commit 0a63827

Please sign in to comment.