Skip to content

Commit

Permalink
Ensure to generate continuous modulations
Browse files Browse the repository at this point in the history
  • Loading branch information
jpcima committed Nov 15, 2020
1 parent f06ed50 commit 2722908
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/sfizz/LFO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ struct LFO::Impl {
std::array<float, config::maxLFOSubs> subPhases_ {{}};
std::array<float, config::maxLFOSubs> sampleHoldMem_ {{}};
std::array<int, config::maxLFOSubs> sampleHoldState_ {{}};
unsigned countLeft_ = 0;
};

LFO::LFO(NumericId<LFO> id, BufferPool& bufferPool, BeatClock* beatClock, ModMatrix* modMatrix)
Expand Down Expand Up @@ -87,6 +88,8 @@ void LFO::start(unsigned triggerDelay)
impl.delayFramesLeft_ = triggerDelay + delayFrames;

impl.fadePosition_ = (desc.fade > 0) ? 0.0f : 1.0f;

impl.countLeft_ = desc.count;
}

template <>
Expand Down Expand Up @@ -174,8 +177,6 @@ void LFO::processSH(unsigned nth, absl::Span<float> out, const float* phaseIn)
for (size_t i = 0; i < numFrames; ++i) {
out[i] += offset + scale * sampleHoldValue;

// TODO(jpc) lfoN_count: number of repetitions

float phase = phaseIn[i];

int oldState = sampleHoldState;
Expand Down Expand Up @@ -330,16 +331,22 @@ void LFO::generatePhase(unsigned nth, absl::Span<float> phases, NumericId<Region

// TODO(jpc) lfoN_count: number of repetitions

// modulations
const float* beatsMod = nullptr;
const float* freqMod = nullptr;
if (modMatrix && id && regionId) {
// 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);
}

if (beatClock && beatClock->isPlaying() && beats > 0) {
// generate using the beat clock
float beatRatio = (ratio > 0) ? (1.0f / ratio) : 0.0f;

const float* beatsMod = nullptr;
if (modMatrix && id && regionId) {
ModKey beatsKey = ModKey::createNXYZ(ModId::LFOBeats, regionId, id.number());
beatsMod = modMatrix->getModulationByKey(beatsKey);
}

if (!beatsMod)
beatClock->calculatePhase(beats * beatRatio, phases.data());
else {
Expand All @@ -358,12 +365,6 @@ void LFO::generatePhase(unsigned nth, absl::Span<float> phases, NumericId<Region
}
else {
// generate using the frequency
const float* freqMod = nullptr;
if (modMatrix && id && regionId) {
ModKey freqKey = ModKey::createNXYZ(ModId::LFOFrequency, regionId, id.number());
freqMod = modMatrix->getModulationByKey(freqKey);
}

if (!freqMod) {
for (size_t i = 0; i < numFrames; ++i) {
phases[i] = phase;
Expand Down

0 comments on commit 2722908

Please sign in to comment.