Skip to content

Commit

Permalink
Merge pull request #653 from paulfd/delay-oncc
Browse files Browse the repository at this point in the history
Add delay_cc and delay_oncc
  • Loading branch information
paulfd authored Feb 23, 2021
2 parents 7782ded + d112cfa commit a4d52e6
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/sfizz/Defaults.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ constexpr auto uint32_t_max = std::numeric_limits<uint32_t>::max();

extern const OpcodeSpec<float> delay { 0.0f, Range<float>(0.0f, 100.0f), 0 };
extern const OpcodeSpec<float> delayRandom { 0.0f, Range<float>(0.0f, 100.0f), 0 };
extern const OpcodeSpec<float> delayMod { 0.0f, Range<float>(0.0f, 100.0f), 0 };
extern const OpcodeSpec<int64_t> offset { 0, Range<int64_t>(0, uint32_t_max), 0 };
extern const OpcodeSpec<int64_t> offsetMod { 0, Range<int64_t>(0, uint32_t_max), 0 };
extern const OpcodeSpec<int64_t> offsetRandom { 0, Range<int64_t>(0, uint32_t_max), 0 };
Expand Down
1 change: 1 addition & 0 deletions src/sfizz/Defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ namespace Default
{
extern const OpcodeSpec<float> delay;
extern const OpcodeSpec<float> delayRandom;
extern const OpcodeSpec<float> delayMod;
extern const OpcodeSpec<int64_t> offset;
extern const OpcodeSpec<int64_t> offsetMod;
extern const OpcodeSpec<int64_t> offsetRandom;
Expand Down
12 changes: 11 additions & 1 deletion src/sfizz/Region.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ bool sfz::Region::parseOpcode(const Opcode& rawOpcode)
case hash("delay"):
delay = opcode.read(Default::delay);
break;
case hash("delay_oncc&"): // also delay_cc&
if (opcode.parameters.back() > config::numCCs)
return false;

delayCC[opcode.parameters.back()] = opcode.read(Default::delayMod);
break;
case hash("delay_random"):
delayRandom = opcode.read(Default::delayRandom);
break;
Expand Down Expand Up @@ -1639,7 +1645,11 @@ uint64_t sfz::Region::getOffset(Oversampling factor) const noexcept
float sfz::Region::getDelay() const noexcept
{
fast_real_distribution<float> delayDistribution { 0, delayRandom };
return delay + delayDistribution(Random::randomGenerator);
float finalDelay { delay };
finalDelay += delayDistribution(Random::randomGenerator);
for (const auto& mod: delayCC)
finalDelay += mod.data * midiState.getCCValue(mod.cc);
return Default::delay.bounds.clamp(finalDelay);
}

uint32_t sfz::Region::trueSampleEnd(Oversampling factor) const noexcept
Expand Down
1 change: 1 addition & 0 deletions src/sfizz/Region.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ struct Region {
absl::optional<int> sampleQuality {};
float delay { Default::delay }; // delay
float delayRandom { Default::delayRandom }; // delay_random
CCMap<float> delayCC { Default::delayMod };
int64_t offset { Default::offset }; // offset
int64_t offsetRandom { Default::offsetRandom }; // offset_random
CCMap<int64_t> offsetCC { Default::offsetMod };
Expand Down
5 changes: 5 additions & 0 deletions src/sfizz/SynthMessaging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ void sfz::Synth::dispatchMessage(Client& client, int delay, const char* path, co
client.receive<'f'>(delay, path, region.delayRandom);
} break;

MATCH("/region&/delay_cc&", "") {
GET_REGION_OR_BREAK(indices[0])
client.receive<'f'>(delay, path, region.delayCC.getWithDefault(indices[1]));
} break;

MATCH("/region&/offset", "") {
GET_REGION_OR_BREAK(indices[0])
client.receive<'h'>(delay, path, region.offset);
Expand Down
24 changes: 24 additions & 0 deletions tests/RegionValuesT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,30 @@ TEST_CASE("[Values] Delay")
};
REQUIRE(messageList == expected);
}

SECTION("CC")
{
synth.loadSfzString(fs::current_path() / "tests/TestFiles/value_tests.sfz", R"(
<region> sample=kick.wav
<region> sample=kick.wav delay_cc12=1.5
<region> sample=kick.wav delay_cc12=-1.5
<region> sample=kick.wav delay_cc14=3 delay_cc12=2 delay_cc12=-12
)");
synth.dispatchMessage(client, 0, "/region0/delay_cc12", "", nullptr);
synth.dispatchMessage(client, 0, "/region1/delay_cc12", "", nullptr);
synth.dispatchMessage(client, 0, "/region2/delay_cc12", "", nullptr);
synth.dispatchMessage(client, 0, "/region3/delay_cc14", "", nullptr);
// TODO: activate for the new region parser ; ignore the second value
// synth.dispatchMessage(client, 0, "/region3/delay_cc12", "", nullptr);
std::vector<std::string> expected {
"/region0/delay_cc12,f : { 0 }",
"/region1/delay_cc12,f : { 1.5 }",
"/region2/delay_cc12,f : { 0 }",
"/region3/delay_cc14,f : { 3 }",
// "/region3/delay_cc12,f : { 2 }",
};
REQUIRE(messageList == expected);
}
}

TEST_CASE("[Values] Sample and direction")
Expand Down

0 comments on commit a4d52e6

Please sign in to comment.