Merge pull request #170 from paulfd/offset-cc

Add support for `offset_cc`
This commit is contained in:
JP Cimalando 2020-04-16 14:06:05 +02:00 committed by GitHub
commit 4f573a6a23
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 10 deletions

View file

@ -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 };

View file

@ -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)
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;
@ -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

View file

@ -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
*
@ -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

View file

@ -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();
}

View file

@ -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")
{