#pragma once #include #include "Params.h" namespace serum { // =========================================================================== // Free-running LFO with tempo sync, seven shapes (sine/tri/saw/square/S&H/ // step-sequencer/freehand), adjustable phase, fade-in and start delay. // =========================================================================== class LFO { public: static constexpr int kShapePoints = 64; void prepare (double sampleRate) { sr = sampleRate; reset(); } void reset(); void setParams (float rateNorm, bool sync, float beat, int shape, float phase, float fade, float delay); void setTempo (double bpm) noexcept { tempo = bpm; } void setShapeData (const std::vector& data, int steps); float process() noexcept; // advance and return current value float getPhase() const noexcept { return (float) phase; } float getValue() const noexcept { return value; } const std::vector& getShapeData() const noexcept { return shapeBuffer; } int getShapeSteps() const noexcept { return shapeSteps; } private: double sr = 44100.0; double tempo = 120.0; double phase = 0.0; double rateHz = 1.0; float value = 0.0f; int shape = 0; bool sync = false; float beat = 0.25f; double delaySeconds = 0.0; double fadeSeconds = 0.0; double delayCounter = 0.0; // samples remaining before start double fadeCounter = 0.0; // samples elapsed in fade float fadeVal = 1.0f; float prevDelayParam = -1.0f; float prevFadeParam = -1.0f; std::vector shapeBuffer; int shapeSteps = 16; juce::Random rng; float holdValue = 0.0f; double prevPhase = 0.0; float shapeValue() noexcept; }; } // namespace serum