[go: up one dir, main page]

Menu

[r68]: / MolossII / Source / LFO.h  Maximize  Restore  History

Download this file

107 lines (92 with data), 2.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
/**************************************************************************
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;
};