Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for offset_cc #170

Merged
merged 5 commits into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/sfizz/Defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ namespace Default
constexpr float delay { 0.0 };
constexpr float delayRandom { 0.0 };
constexpr Range<float> delayRange { 0.0, 100.0 };
constexpr uint32_t offset { 0 };
constexpr uint32_t offsetRandom { 0 };
constexpr Range<uint32_t> offsetRange { 0, std::numeric_limits<uint32_t>::max() };
constexpr int64_t offset { 0 };
constexpr int64_t offsetRandom { 0 };
constexpr Range<int64_t> offsetRange { 0, std::numeric_limits<uint32_t>::max() };
constexpr Range<int64_t> offsetCCRange = offsetRange;
constexpr Range<uint32_t> sampleEndRange { 0, std::numeric_limits<uint32_t>::max() };
constexpr Range<uint32_t> sampleCountRange { 0, std::numeric_limits<uint32_t>::max() };
constexpr SfzLoopMode loopMode { SfzLoopMode::no_loop };
Expand Down
15 changes: 12 additions & 3 deletions src/sfizz/Region.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ bool sfz::Region::parseOpcode(const Opcode& opcode)
case hash("offset_random"):
setValueFromOpcode(opcode, offsetRandom, Default::offsetRange);
break;
case hash("offset_cc&"):
if (opcode.parameters.back() > config::numCCs)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I note there exist multiple occurrences of ccNumber > config::numCCs in code.
There is also for (int cc = 0; cc < config::numCCs; cc++).
One of these has to be wrong. Perhaps instead the checks should use >=.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right it should be >= in all these checks.

return false;
if (auto value = readOpcode(opcode.value, Default::offsetCCRange))
offsetCC[opcode.parameters.back()] = *value;
break;
case hash("end"):
setValueFromOpcode(opcode, sampleEnd, Default::sampleEndRange);
break;
Expand Down Expand Up @@ -1134,10 +1140,13 @@ float sfz::Region::getPhase() const noexcept
return phase;
}

uint32_t sfz::Region::getOffset(Oversampling factor) const noexcept
uint64_t sfz::Region::getOffset(Oversampling factor) const noexcept
{
std::uniform_int_distribution<uint32_t> offsetDistribution { 0, offsetRandom };
return (offset + offsetDistribution(Random::randomGenerator)) * static_cast<uint32_t>(factor);
std::uniform_int_distribution<int64_t> offsetDistribution { 0, offsetRandom };
uint64_t finalOffset = offset + offsetDistribution(Random::randomGenerator);
for (const auto& mod: offsetCC)
finalOffset += static_cast<uint64_t>(mod.data * midiState.getCCValue(mod.cc));
return Default::offsetCCRange.clamp(offset + offsetDistribution(Random::randomGenerator)) * static_cast<uint64_t>(factor);
}

float sfz::Region::getDelay() const noexcept
Expand Down
7 changes: 4 additions & 3 deletions src/sfizz/Region.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ struct Region {
*
* @return uint32_t
*/
uint32_t getOffset(Oversampling factor = Oversampling::x1) const noexcept;
uint64_t getOffset(Oversampling factor = Oversampling::x1) const noexcept;
/**
* @brief Get the region delay in seconds
*
Expand Down Expand Up @@ -230,8 +230,9 @@ struct Region {
std::string sample {}; // Sample
float delay { Default::delay }; // delay
float delayRandom { Default::delayRandom }; // delay_random
uint32_t offset { Default::offset }; // offset
uint32_t offsetRandom { Default::offsetRandom }; // offset_random
int64_t offset { Default::offset }; // offset
int64_t offsetRandom { Default::offsetRandom }; // offset_random
CCMap<int64_t> offsetCC { Default::offset };
uint32_t sampleEnd { Default::sampleEndRange.getEnd() }; // end
absl::optional<uint32_t> sampleCount {}; // count
absl::optional<SfzLoopMode> loopMode {}; // loopmode
Expand Down
8 changes: 7 additions & 1 deletion src/sfizz/Synth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,13 @@ bool sfz::Synth::loadSfzFile(const fs::path& file)
region->hasStereoSample = true;

// TODO: adjust with LFO targets
const auto maxOffset = region->offset + region->offsetRandom;
const auto maxOffset = [region]() {
uint64_t sumOffsetCC = region->offset + region->offsetRandom;
for (const auto& offsets: region->offsetCC)
sumOffsetCC += offsets.data;
return Default::offsetCCRange.clamp(sumOffsetCC);
}();

if (!resources.filePool.preloadFile(region->sample, maxOffset))
removeCurrentRegion();
}
Expand Down
13 changes: 13 additions & 0 deletions tests/RegionT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ TEST_CASE("[Region] Parsing opcodes")
region.parseOpcode({ "offset", "-1" });
REQUIRE(region.offset == 0);
}
SECTION("offset_cc")
{
REQUIRE(region.offsetCC.empty());
region.parseOpcode({ "offset_cc1", "1" });
REQUIRE(region.offsetCC.contains(1));
REQUIRE(region.offsetCC[1] == 1);
region.parseOpcode({ "offset_cc2", "15420" });
REQUIRE(region.offsetCC.contains(2));
REQUIRE(region.offsetCC[2] == 15420);
region.parseOpcode({ "offset_cc2", "-1" });
REQUIRE(region.offsetCC[2] == 0);
}


SECTION("offset_random")
{
Expand Down