Skip to content

Commit

Permalink
Merge pull request #467 from paulfd/multiple-globals-polyphony
Browse files Browse the repository at this point in the history
Don't assume a single <global> for region sets
  • Loading branch information
paulfd authored Oct 4, 2020
2 parents 0086760 + 0b502b4 commit 991873c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/sfizz/Opcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ enum OpcodeCategory {
*/
enum OpcodeScope {
//! unknown scope or other
kOpcodeScopeGeneric,
kOpcodeScopeGeneric = 0,
//! global scope
kOpcodeScopeGlobal,
//! control scope
Expand Down
16 changes: 16 additions & 0 deletions src/sfizz/RegionSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "Region.h"
#include "Voice.h"
#include "Opcode.h"
#include "SwapAndPop.h"
#include <vector>

Expand All @@ -16,6 +17,13 @@ namespace sfz

class RegionSet {
public:
RegionSet() = delete;
RegionSet(RegionSet* parentSet, OpcodeScope level)
: parent(parentSet), level(level)
{
if (parentSet != nullptr)
parentSet->addSubset(this);
}
/**
* @brief Set the polyphony limit for the set
*
Expand Down Expand Up @@ -73,6 +81,13 @@ class RegionSet {
* @return RegionSet*
*/
RegionSet* getParent() const noexcept { return parent; }

/**
* @brief Get the set level
*
* @return OpcodeScope
*/
OpcodeScope getLevel() const noexcept { return level; }
/**
* @brief Set the parent set
*
Expand Down Expand Up @@ -109,6 +124,7 @@ class RegionSet {
const std::vector<RegionSet*>& getSubsets() const noexcept { return subsets; }
private:
RegionSet* parent { nullptr };
OpcodeScope level { kOpcodeScopeGeneric };
std::vector<Region*> regions;
std::vector<RegionSet*> subsets;
std::vector<Voice*> voices;
Expand Down
38 changes: 15 additions & 23 deletions src/sfizz/Synth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,19 @@ void sfz::Synth::onVoiceStateChanged(NumericId<Voice> id, Voice::State state)

void sfz::Synth::onParseFullBlock(const std::string& header, const std::vector<Opcode>& members)
{
const auto newRegionSet = [&](RegionSet* parentSet) {
ASSERT(parentSet != nullptr);
sets.emplace_back(new RegionSet);
auto newSet = sets.back().get();
parentSet->addSubset(newSet);
newSet->setParent(parentSet);
currentSet = newSet;
const auto newRegionSet = [&](OpcodeScope level) {
auto parent = currentSet;
while (parent && parent->getLevel() >= level)
parent = parent->getParent();

sets.emplace_back(new RegionSet(parent, level));
currentSet = sets.back().get();
};

switch (hash(header)) {
case hash("global"):
globalOpcodes = members;
currentSet = sets.front().get();
lastHeader = OpcodeScope::kOpcodeScopeGlobal;
newRegionSet(OpcodeScope::kOpcodeScopeGlobal);
groupOpcodes.clear();
masterOpcodes.clear();
handleGlobalOpcodes(members);
Expand All @@ -101,19 +100,14 @@ void sfz::Synth::onParseFullBlock(const std::string& header, const std::vector<O
break;
case hash("master"):
masterOpcodes = members;
newRegionSet(sets.front().get());
newRegionSet(OpcodeScope::kOpcodeScopeMaster);
groupOpcodes.clear();
lastHeader = OpcodeScope::kOpcodeScopeMaster;
handleMasterOpcodes(members);
numMasters++;
break;
case hash("group"):
groupOpcodes = members;
if (lastHeader == OpcodeScope::kOpcodeScopeGroup)
newRegionSet(currentSet->getParent());
else
newRegionSet(currentSet);
lastHeader = OpcodeScope::kOpcodeScopeGroup;
newRegionSet(OpcodeScope::kOpcodeScopeGroup);
handleGroupOpcodes(members, masterOpcodes);
numGroups++;
break;
Expand Down Expand Up @@ -145,8 +139,6 @@ void sfz::Synth::onParseWarning(const SourceRange& range, const std::string& mes

void sfz::Synth::buildRegion(const std::vector<Opcode>& regionOpcodes)
{
ASSERT(currentSet != nullptr);

int regionNumber = static_cast<int>(regions.size());
auto lastRegion = absl::make_unique<Region>(regionNumber, resources.midiState, defaultPath);

Expand Down Expand Up @@ -184,8 +176,10 @@ void sfz::Synth::buildRegion(const std::vector<Opcode>& regionOpcodes)
if (lastRegion->group != Default::group && lastRegion->polyphony != config::maxVoices)
setGroupPolyphony(lastRegion->group, lastRegion->polyphony);

lastRegion->parent = currentSet;
currentSet->addRegion(lastRegion.get());
if (currentSet != nullptr) {
lastRegion->parent = currentSet;
currentSet->addRegion(lastRegion.get());
}

// Adapt the size of the delayed releases to avoid allocating later on
lastRegion->delayedReleases.reserve(lastRegion->keyRange.length());
Expand All @@ -204,10 +198,8 @@ void sfz::Synth::clear()
for (auto& list : ccActivationLists)
list.clear();

lastHeader = OpcodeScope::kOpcodeScopeGlobal;
currentSet = nullptr;
sets.clear();
sets.emplace_back(new RegionSet);
currentSet = sets.front().get();
regions.clear();
effectBuses.clear();
effectBuses.emplace_back(new EffectBus);
Expand Down
3 changes: 1 addition & 2 deletions src/sfizz/Synth.h
Original file line number Diff line number Diff line change
Expand Up @@ -817,8 +817,7 @@ class Synth final : public Voice::StateListener, public Parser::Listener {
std::vector<VoicePtr> voices;

// These are more general "groups" than sfz and encapsulates the full hierarchy
RegionSet* currentSet;
OpcodeScope lastHeader { OpcodeScope::kOpcodeScopeGlobal };
RegionSet* currentSet { nullptr };
std::vector<RegionSetPtr> sets;

// These are the `group=` groups where you can off voices
Expand Down
12 changes: 6 additions & 6 deletions tests/PolyphonyT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ TEST_CASE("[Polyphony] Polyphony in hierarchy")
<region> key=64 sample=*sine
)");
REQUIRE( synth.getRegionView(0)->polyphony == 2 );
REQUIRE( synth.getRegionSetView(1)->getPolyphonyLimit() == 2 );
REQUIRE( synth.getRegionSetView(0)->getPolyphonyLimit() == 2 );
REQUIRE( synth.getRegionView(1)->polyphony == 2 );
REQUIRE( synth.getRegionSetView(2)->getPolyphonyLimit() == 3 );
REQUIRE( synth.getRegionSetView(2)->getRegions()[0]->polyphony == 3 );
REQUIRE( synth.getRegionSetView(3)->getPolyphonyLimit() == 4 );
REQUIRE( synth.getRegionSetView(3)->getRegions()[0]->polyphony == 5 );
REQUIRE( synth.getRegionSetView(3)->getRegions()[1]->polyphony == 4 );
REQUIRE( synth.getRegionSetView(1)->getPolyphonyLimit() == 3 );
REQUIRE( synth.getRegionSetView(1)->getRegions()[0]->polyphony == 3 );
REQUIRE( synth.getRegionSetView(2)->getPolyphonyLimit() == 4 );
REQUIRE( synth.getRegionSetView(2)->getRegions()[0]->polyphony == 5 );
REQUIRE( synth.getRegionSetView(2)->getRegions()[1]->polyphony == 4 );
}

TEST_CASE("[Polyphony] Polyphony groups")
Expand Down

0 comments on commit 991873c

Please sign in to comment.