Move LFO modulation keys into the description

This commit is contained in:
Jean Pierre Cimalando 2021-03-10 14:18:09 +01:00
parent 97c9411f8d
commit dfd777fd3e
8 changed files with 26 additions and 33 deletions

View file

@ -116,8 +116,7 @@ int main(int argc, char* argv[])
std::vector<std::unique_ptr<sfz::LFO>> lfos(numLfos);
for (size_t l = 0; l < numLfos; ++l) {
const NumericId<sfz::LFO> id { static_cast<int>(l) };
sfz::LFO* lfo = new sfz::LFO(id, bufferPool);
sfz::LFO* lfo = new sfz::LFO(bufferPool);
lfos[l].reset(lfo);
lfo->setSampleRate(sampleRate);
lfo->configure(&desc[l]);

View file

@ -21,9 +21,8 @@
namespace sfz {
struct LFO::Impl {
explicit Impl(NumericId<LFO> id, BufferPool& bufferPool, BeatClock* beatClock, ModMatrix* modMatrix)
: id_(id),
bufferPool_(bufferPool),
explicit Impl(BufferPool& bufferPool, BeatClock* beatClock, ModMatrix* modMatrix)
: bufferPool_(bufferPool),
beatClock_(beatClock),
modMatrix_(modMatrix),
sampleRate_(config::defaultSampleRate),
@ -31,7 +30,6 @@ struct LFO::Impl {
{
}
NumericId<LFO> id_;
BufferPool& bufferPool_;
BeatClock* beatClock_ = nullptr;
ModMatrix* modMatrix_ = nullptr;
@ -48,8 +46,8 @@ struct LFO::Impl {
std::array<int, config::maxLFOSubs> sampleHoldState_ {{}};
};
LFO::LFO(NumericId<LFO> id, BufferPool& bufferPool, BeatClock* beatClock, ModMatrix* modMatrix)
: impl_(new Impl(id, bufferPool, beatClock, modMatrix))
LFO::LFO(BufferPool& bufferPool, BeatClock* beatClock, ModMatrix* modMatrix)
: impl_(new Impl(bufferPool, beatClock, modMatrix))
{
}
@ -57,11 +55,6 @@ LFO::~LFO()
{
}
NumericId<LFO> LFO::getId() const noexcept
{
return impl_->id_;
}
void LFO::setSampleRate(double sampleRate)
{
impl_->sampleRate_ = sampleRate;
@ -221,7 +214,7 @@ void LFO::processSteps(absl::Span<float> out, const float* phaseIn)
}
}
void LFO::process(absl::Span<float> out, NumericId<Region> regionId)
void LFO::process(absl::Span<float> out)
{
Impl& impl = *impl_;
const LFODescription& desc = *impl.desc_;
@ -252,13 +245,13 @@ void LFO::process(absl::Span<float> out, NumericId<Region> regionId)
absl::Span<float> phases = *phasesTemp;
if (desc.seq) {
generatePhase(0, phases, regionId);
generatePhase(0, phases);
processSteps(out, phases.data());
++subno;
}
for (; subno < countSubs; ++subno) {
generatePhase(subno, phases, regionId);
generatePhase(subno, phases);
switch (desc.sub[subno].wave) {
case LFOWave::Triangle:
processWave<LFOWave::Triangle>(subno, out, phases.data());
@ -315,13 +308,12 @@ void LFO::processFadeIn(absl::Span<float> out)
impl.fadePosition_ = fadePosition;
}
void LFO::generatePhase(unsigned nth, absl::Span<float> phases, NumericId<Region> regionId)
void LFO::generatePhase(unsigned nth, absl::Span<float> phases)
{
Impl& impl = *impl_;
BufferPool& bufferPool = impl.bufferPool_;
BeatClock* beatClock = impl.beatClock_;
ModMatrix* modMatrix = impl.modMatrix_;
const NumericId<LFO> id { impl.id_ };
const LFODescription& desc = *impl.desc_;
const LFODescription::Sub& sub = desc.sub[nth];
const float samplePeriod = 1.0f / impl.sampleRate_;
@ -337,13 +329,11 @@ void LFO::generatePhase(unsigned nth, absl::Span<float> phases, NumericId<Region
// modulations
const float* beatsMod = nullptr;
const float* freqMod = nullptr;
if (modMatrix && id && regionId) {
if (modMatrix) {
// Note(jpc) we might switch between beats and frequency, if host
// switches play state on and off; continually generate both.
ModKey beatsKey = ModKey::createNXYZ(ModId::LFOBeats, regionId, id.number());
ModKey freqKey = ModKey::createNXYZ(ModId::LFOFrequency, regionId, id.number());
beatsMod = modMatrix->getModulationByKey(beatsKey);
freqMod = modMatrix->getModulationByKey(freqKey);
beatsMod = modMatrix->getModulationByKey(desc.beatsKey);
freqMod = modMatrix->getModulationByKey(desc.freqKey);
}
if (beatClock && beatClock->isPlaying() && beats > 0) {

View file

@ -55,14 +55,11 @@ struct LFODescription;
class LFO {
public:
explicit LFO(
NumericId<LFO> id,
BufferPool& bufferPool,
BeatClock* beatClock = nullptr,
ModMatrix* modMatrix = nullptr);
~LFO();
NumericId<LFO> getId() const noexcept;
/**
Sets the sample rate.
*/
@ -85,7 +82,7 @@ public:
TODO(jpc) frequency modulations
*/
void process(absl::Span<float> out, NumericId<Region> regionId = {});
void process(absl::Span<float> out);
private:
/**
@ -123,7 +120,7 @@ private:
/**
Generate the phase of the N-th generator
*/
void generatePhase(unsigned nth, absl::Span<float> phases, NumericId<Region> regionId);
void generatePhase(unsigned nth, absl::Span<float> phases);
private:
struct Impl;

View file

@ -6,6 +6,7 @@
#pragma once
#include "Defaults.h"
#include "modulations/ModKey.h"
#include <absl/types/optional.h>
#include <vector>
@ -32,6 +33,10 @@ struct LFODescription {
};
absl::optional<StepSequence> seq;
std::vector<Sub> sub;
// modulations
ModKey beatsKey;
ModKey freqKey;
};
} // namespace sfz

View file

@ -955,6 +955,10 @@ bool sfz::Region::parseLFOOpcodeV2(const Opcode& opcode)
const unsigned lfoNumber = lfoNumber1Based - 1;
LFODescription& lfo = lfos[lfoNumber];
//
lfo.beatsKey = ModKey::createNXYZ(ModId::LFOBeats, id, lfoNumber);
lfo.freqKey = ModKey::createNXYZ(ModId::LFOFrequency, id, lfoNumber);
//
auto getOrCreateLFOStep = [&opcode, &lfo]() -> float* {
const unsigned stepNumber1Based = opcode.parameters[1];

View file

@ -1604,8 +1604,7 @@ void Voice::setMaxLFOsPerVoice(size_t numLFOs)
impl.lfos_.resize(numLFOs);
for (size_t i = 0; i < numLFOs; ++i) {
const NumericId<LFO> id { static_cast<int>(i) };
auto lfo = absl::make_unique<LFO>(id, resources.bufferPool, &resources.beatClock, &resources.modMatrix);
auto lfo = absl::make_unique<LFO>(resources.bufferPool, &resources.beatClock, &resources.modMatrix);
lfo->setSampleRate(impl.sampleRate_);
impl.lfos_[i] = std::move(lfo);
}

View file

@ -59,7 +59,7 @@ void LFOSource::generate(const ModKey& sourceKey, NumericId<Voice> voiceId, absl
}
LFO* lfo = voice->getLFO(lfoIndex);
lfo->process(buffer, region->getId());
lfo->process(buffer);
}
} // namespace sfz

View file

@ -28,8 +28,7 @@ static bool computeLFO(DataPoints& dp, const fs::path& sfzPath, double sampleRat
std::vector<std::unique_ptr<sfz::LFO>> lfos(numLfos);
for (size_t l = 0; l < numLfos; ++l) {
const NumericId<sfz::LFO> id { static_cast<int>(l) };
sfz::LFO* lfo = new sfz::LFO(id, resources.bufferPool);
sfz::LFO* lfo = new sfz::LFO(resources.bufferPool);
lfos[l].reset(lfo);
lfo->setSampleRate(sampleRate);
lfo->configure(&desc[l]);