diff --git a/examples/Metronome/ExamplePluginMetronome.cpp b/examples/Metronome/ExamplePluginMetronome.cpp index cc0f40cd..cb76ca3d 100644 --- a/examples/Metronome/ExamplePluginMetronome.cpp +++ b/examples/Metronome/ExamplePluginMetronome.cpp @@ -46,13 +46,15 @@ public: - Single-pole IIR low-pass filter - which is the correct formula for the decay coefficient? https://dsp.stackexchange.com/questions/54086/single-pole-iir-low-pass-filter-which-is-the-correct-formula-for-the-decay-coe */ - void setCutoff(float sampleRate, float cutoffHz) { + void setCutoff(const float sampleRate, const float cutoffHz) + { double omega_c = 2.0 * M_PI * cutoffHz / sampleRate; double y = 1.0 - std::cos(omega_c); kp = float(-y + std::sqrt((y + 2.0) * y)); } - float process(float input) { + inline float process(const float input) + { return value += kp * (input - value); } }; @@ -77,6 +79,7 @@ public: cent(0), decayTime(0.2f) { + sampleRateChanged(sampleRate); } protected: @@ -158,7 +161,7 @@ protected: case 0: parameter.name = "Gain"; parameter.hints |= kParameterIsLogarithmic; - parameter.ranges.min = 0.0f; + parameter.ranges.min = 0.001f; parameter.ranges.max = 1.0f; parameter.ranges.def = 0.5f; break; @@ -236,6 +239,17 @@ protected: /* -------------------------------------------------------------------------------------------------------- * Process */ + /** + Activate this plugin. + We use this to reset our filter states. + */ + void activate() override + { + deltaPhaseSmoother.value = 0.0f; + envelopeSmoother.value = 0.0f; + gainSmoother.value = gain; + } + /** Run/process function for plugins without MIDI input. `inputs` is commented out because this plugin has no inputs. @@ -274,8 +288,8 @@ protected: phase = 0.0f; deltaPhaseSmoother.value = deltaPhase; - gainSmoother.value = 1.0f; envelopeSmoother.value = 0.0f; + gainSmoother.value = 0.0f; } for (uint32_t i = 0; i < frames; ++i) @@ -336,8 +350,8 @@ private: float decay; // Coefficient to decay envelope in a frame. Smoother deltaPhaseSmoother; - Smoother gainSmoother; Smoother envelopeSmoother; + Smoother gainSmoother; // Parameters. float gain; diff --git a/examples/Metronome/README.md b/examples/Metronome/README.md index 7937b714..26dce863 100644 --- a/examples/Metronome/README.md +++ b/examples/Metronome/README.md @@ -2,7 +2,7 @@ This example will show tempo sync in DPF.
-This plugin will output sine wave at the start of every beat.
+This plugin will output a sine wave at the start of every beat.
The pitch of sine wave is 1 octave higher at the start of every bar.
4 parameters are avaialble: @@ -17,15 +17,15 @@ To calculate frames to the next beat from the start of current audio buffer, fol ```c++ const TimePosition& timePos(getTimePosition()); -if (timePos.bbt.valid) { +if (timePos.bbt.valid) +{ double secondsPerBeat = 60.0 / timePos.bbt.beatsPerMinute; double framesPerBeat = sampleRate * secondsPerBeat; double beatFraction = timePos.bbt.tick / timePos.bbt.ticksPerBeat; - uint32_t framesToNextBeat = beatFraction == 0.0 - ? 0 - : static_cast(framesPerBeat * (1.0 - beatFraction)); - + uint32_t framesToNextBeat = d_isZero(beatFraction) + ? 0 + : static_cast(framesPerBeat * (1.0 - beatFraction)); // ... } ```