/**************************************************************************
Copyright (C) 2019 Arnaud Champenois arthelion@free.fr
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/
#pragma once
#include "WaveGenerator.h"
#include "Utils.h"
/*!
* The LFO outputs a varying triangular signal between -1 and 1, with a delay
* The oscillation begins with a small ramp before the delay.
*
*/
class LFO {
public:
LFO() {}
~LFO() {}
void init(double sampligfreq) {
triGenerator.init(sampligfreq);
}
/*!
* Resets the LFO to the beginning, when delay starts.
* Doesn't change the phase itself which starts where it was.
* It continues to live after the note is off
*/
void noteOn() {
phase = 0;
}
void resetGeneratorPhase() {
phase = 0;
triGenerator.resetPhase();
}
/*!
* Sets the LFO ramp delay time
*
* \param time Time in seconds
*/
void setDelay(double time) {
delayPhase = time*triGenerator.getSamplingFreq();
lfoRampSamples = LFORamp * triGenerator.getSamplingFreq();
delayRampPhase = delayPhase - lfoRampSamples;
}
/*!
* Next sample at the given frequency
*
* \param frequency Current LFO frequency (which can be modulated)
* \return A Value between -1.0 and 1.0
*/
double next(double frequency) {
if (phase < delayRampPhase) {
phase += 1.0;
triGenerator.next(frequency);
return 0;
}
else if (phase < delayPhase) {
double value = ((phase- delayRampPhase) / lfoRampSamples)*triGenerator.next(frequency);
phase += 1.0;
return value;
}
else {
phase += 1.0;
return triGenerator.next(frequency);
}
}
void skipDelay() {
if (phase < delayPhase)
phase = delayPhase;
}
/*!
* Move the generator phase of a given number of samples, with a frequency
*
* \param freq The generator frequency
* \param samples The number of samples to go forward
*/
void move(double freq, double samples) {
triGenerator.move(freq, samples);
}
private:
TriangleAliased triGenerator;
double delayPhase = 0.0; // Delay in number of samples
double delayRampPhase = 0.0;
double phase = 0.0; // Current phase in sample count
double lfoRampSamples = 0.0;
};