-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlfo.cpp
More file actions
66 lines (54 loc) · 1.35 KB
/
lfo.cpp
File metadata and controls
66 lines (54 loc) · 1.35 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
//
// lfo.cpp
//
#include "lfo.h"
CLFO::CLFO(unsigned sampleRate)
: m_sampleRate(sampleRate),
m_waveSaw(0.0f),
m_waveSquare(0.0f),
m_waveTri(1.0f),
m_frequency(1.0f),
m_phase(0.0f),
m_phaseInc(0.0f)
{
}
void CLFO::Init(float sampleRate)
{
m_sampleRate = sampleRate;
SetFrequency(m_frequency);
m_phase = 0.0f;
}
void CLFO::SetFrequency(float freqHz)
{
m_frequency = freqHz;
m_frequency = (m_frequency<0.01f) ? 0.01f : (m_frequency>30.0f) ? 30.0f : m_frequency;
m_phaseInc = (m_frequency / m_sampleRate); // normalized [0 → 1)
}
void CLFO::Reset(void)
{
m_phase = 0.0f;
}
// ------------------------------------------------------------
// Generate one LFO sample in range -1 → +1
// ------------------------------------------------------------
float CLFO::GetSample(void)
{
float saw = 0.0f;
float square = 0.0f;
float tri = 0.0f;
// triangle //
if (m_phase < 0.25f)
tri = 4.0f * m_phase;
else if (m_phase < 0.75f)
tri = 2.0f - 4.0f * m_phase;
else
tri = -4.0f + 4.0f * m_phase;
// square //
square = (m_phase < 0.5f) ? 1.0f : -1.0f;
// saw //
saw = 2.0f * m_phase - 1.0f;
m_phase += m_phaseInc;
if (m_phase >= 1.0f)
m_phase -= 1.0f;
return (square * m_waveSquare - saw * m_waveSaw + tri * m_waveTri);
}