From 062d1e074c2e98c107bdf24aaaf2f02c87bcea10 Mon Sep 17 00:00:00 2001 From: Liam Kerr Date: Wed, 12 Aug 2026 01:27:00 +0100 Subject: [PATCH] feat: improve oscillator initialization values and LFO sync implementation with bar tracking --- core/Oscillator.cpp | 22 +++++++++----- plugins/SquidgeFilter/DistrhoPluginInfo.h | 1 + plugins/SquidgeFilter/SquidgeFilter.cpp | 37 ++++++++++------------- 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/core/Oscillator.cpp b/core/Oscillator.cpp index 7cee116..0f4b8a6 100644 --- a/core/Oscillator.cpp +++ b/core/Oscillator.cpp @@ -4,13 +4,17 @@ Oscillator::Oscillator(float initFreq) : freq_(initFreq), gain_(100.0f), waveShape_(kWaveShapeSine), - sampleRate_(48000.f), phase_(0.0f), inc_(0.0f) { + sampleRate_(48000.f), phase_(0.0f), inc_(0.0f) +{ recalc_(); }; -void Oscillator::process(float *out, uint32_t frames) { - for (uint32_t i = 0; i < frames; ++i) { - switch (waveShape_) { +void Oscillator::process(float *out, uint32_t frames) +{ + for (uint32_t i = 0; i < frames; ++i) + { + switch (waveShape_) + { default: case kWaveShapeSine: out[i] = std::sinf(phase_ * 2.0f * M_PI) * gain_; @@ -26,17 +30,21 @@ void Oscillator::reset() { phase_ = 0; } void Oscillator::recalc_() { inc_ = freq_ / sampleRate_; } -void Oscillator::setFrequency(float newFreq) { +void Oscillator::setFrequency(float newFreq) +{ freq_ = std::fmax(newFreq, 0.0f); + recalc_(); } float Oscillator::getFrequency() const { return freq_; }; -void Oscillator::setGain(float newGain) { +void Oscillator::setGain(float newGain) +{ gain_ = newGain > 0 ? newGain : 1.0f; } float Oscillator::getGain() const { return gain_; }; -void Oscillator::setSampleRate(float newSampleRate) { +void Oscillator::setSampleRate(float newSampleRate) +{ sampleRate_ = newSampleRate > 0 ? newSampleRate : 48000.0f; recalc_(); } diff --git a/plugins/SquidgeFilter/DistrhoPluginInfo.h b/plugins/SquidgeFilter/DistrhoPluginInfo.h index 1e69a67..c9adf22 100644 --- a/plugins/SquidgeFilter/DistrhoPluginInfo.h +++ b/plugins/SquidgeFilter/DistrhoPluginInfo.h @@ -15,6 +15,7 @@ #define DISTRHO_PLUGIN_WANT_PROGRAMS 0 #define DISTRHO_PLUGIN_WANT_STATE 0 +#define DISTRHO_PLUGIN_WANT_TIMEPOS 1 #define DISTRHO_PLUGIN_HAS_UI 0 #define DISTRHO_PLUGIN_IS_SYNTH 0 diff --git a/plugins/SquidgeFilter/SquidgeFilter.cpp b/plugins/SquidgeFilter/SquidgeFilter.cpp index 9397f8f..028f04e 100644 --- a/plugins/SquidgeFilter/SquidgeFilter.cpp +++ b/plugins/SquidgeFilter/SquidgeFilter.cpp @@ -22,8 +22,8 @@ public: }; SquidgeFilter() - : Plugin(kParameterCount, 0, 0), filter_(1000.0f, 0.707f, 48000.0f), - lfo_(4.0f) { + : Plugin(kParameterCount, 0, 0), filter_(440.0f, 2.0f, 48000.0f), + lfo_(20.0f), lfoSync_(0.0f) { filter_.setType(Filter::kLowPass); } @@ -43,16 +43,16 @@ protected: parameter.name = "Frequency"; parameter.symbol = "frequency"; parameter.ranges.min = 20.0f; - parameter.ranges.def = 1000.0f; - parameter.ranges.max = 20000.0f; + parameter.ranges.def = 440.0f; + parameter.ranges.max = 10000.0f; parameter.hints = kParameterIsAutomatable | kParameterIsLogarithmic; break; case kParamResonance: parameter.name = "Resonance"; parameter.symbol = "resonance"; - parameter.ranges.min = 0.1f; - parameter.ranges.def = 0.707f; + parameter.ranges.min = 0.9f; + parameter.ranges.def = 2.0f; parameter.ranges.max = 20.0f; parameter.hints = kParameterIsAutomatable; break; @@ -149,31 +149,25 @@ protected: const TimePosition &timePos = getTimePosition(); float targetFreq = 4.0f; - int syncIndex = static_cast(lfoSync_); - - if (syncIndex < 0) - syncIndex = 0; - if (syncIndex > 6) - syncIndex = 6; if (timePos.bbt.valid && timePos.playing && timePos.bbt.beatsPerMinute > 0.0) { + float bpb = static_cast(timePos.bbt.beatsPerBar); float bpm = static_cast(timePos.bbt.beatsPerMinute); + float bar = static_cast(timePos.bbt.bar); - float baseFreq = (bpm / 60.0f) * 4.0f; + if (bar != currentBar_) { + lfo_.reset(); + currentBar_ = bar; + } + + float baseFreq = (bpm / 60.0f) / bpb; static const float multipliers[] = {1.0f, 2.0f, 4.0f, 8.0f, 16.0f, 32.0f, 64.0f}; - targetFreq = baseFreq * multipliers[syncIndex]; - - static int lastBar = -1; - if (timePos.bbt.bar != lastBar) { - lfo_.reset(); - lastBar = timePos.bbt.bar; - } + targetFreq = baseFreq * multipliers[static_cast(lfoSync_)]; } else { - targetFreq = 1.0f; } @@ -193,6 +187,7 @@ private: Filter filter_; Oscillator lfo_; float lfoSync_; + uint32_t currentBar_ = 0; float lfoBuffer_[BUFFER_SIZE]; DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(SquidgeFilter);