chenlh
2026-03-20 f761b6198e2e98f02adb84c83dbf59529e51b2ef
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "LFO.h"
 
 
LFO::LFO()
{    
    
}
 
LFO::LFO(int waveformType)
{    
    setWaveformType(waveformType);
}
 
void LFO::prepare(double sampleRate)
{
    setSampleRate(sampleRate);    
    modCounter = 0.0;
    modCounter90 = 0.25;
    setFrequency(frequency);
}
 
double LFO::getNextOutputSample(int phaseType)
{
    generateNextOutputSample();
 
    switch(phaseType)
    {
        case LFOPhase::Normal:
            return outNormal;
            break;
        case LFOPhase::Inverted:
            return outInverted;
            break;
        case LFOPhase::QuadPhase:
            return outQuadPhase;
            break;
        case LFOPhase::QuadPhaseInverted:
            return outQuadPhaseInverted;
            break;
        default:
            return outNormal;
            break;
    }
}
 
void LFO::setWaveformType(int waveformType)
{
    switch(waveformType)
    {
        case Waveforms::Triangle:
            waveformType = Waveforms::Triangle;
            break;
        case Waveforms::Sine:
            waveformType = Waveforms::Sine;
            break;
        case Waveforms::Saw:
            waveformType = Waveforms::Saw;
            break;
    }
}
 
int LFO::getWaveformType()
{
    return waveformType;
}
 
void LFO::setFrequency(double freq)
{
    frequency = freq;
    phaseInc = frequency / sampleRate;
}
 
double LFO::getFrequency()
{
    return frequency;
}
 
void LFO::setSampleRate(double sampleRate)
{
    this->sampleRate = sampleRate;
}
 
double LFO::getSampleRate()
{
    return sampleRate;
}
 
double LFO::unipolarToBipolar(double value)
{
    return 2.0*value - 1.0;
}
 
void LFO::generateNextOutputSample()
{
    moduloWrap(modCounter, phaseInc);
 
    modCounter90 = modCounter;
 
    moduloAdvanceAndWrap(modCounter90, 0.25);
 
    outNormal = 0.0;
    outInverted = 0.0;
    outQuadPhase = 0.0;
    outQuadPhaseInverted = 0.0;
 
    switch(waveformType)
    {
        case Waveforms::Triangle:
            // Normal Output
            outNormal = unipolarToBipolar(modCounter);
            outNormal = 2.0*fabs(outNormal) - 1.0;            
 
            // 90 Degree phase shift
            outQuadPhase = unipolarToBipolar(modCounter90);
            outQuadPhase = 2.0*fabs(outQuadPhase) - 1.0;            
 
            break;
 
        case Waveforms::Sine:
            double angle;
 
            // Angle Calculation
            angle = modCounter*2.0*PI - PI;
 
            // Normal Output
            outNormal = parabolicSine(-angle);
 
            // 90 Degree shift angle calculation
            angle = modCounter90*2.0*PI - PI;
 
            // 90 Degree phase shift
            outQuadPhase = parabolicSine(-angle);
 
            break;
 
        case Waveforms::Saw:
            // Normal Output
            outNormal = unipolarToBipolar(modCounter);
 
            // 90 Degree Shift
            outQuadPhase = unipolarToBipolar(modCounter90);
 
            break;
 
    }
 
    // Inverted Outputs
 
    // Inverted Normal Output
    outInverted = -outNormal;
 
    // Inverted 90 Degree
    outQuadPhaseInverted = -outQuadPhase;
 
    moduloAdvance(modCounter, phaseInc);
}
 
void LFO::moduloAdvance(double& modCounter, double phaseInc)
{
    modCounter += phaseInc;
}
 
void LFO::moduloWrap(double& modCounter, double phaseInc)
{
    // Positive Frequencies
    if(phaseInc > 0 && modCounter >= 1.0)
        modCounter -= 1.0;
 
    // Negative Frequencies
    if(phaseInc < 0 && modCounter <= 0.0)
        modCounter += 1.0;
}
 
void LFO::moduloAdvanceAndWrap(double& modCounter, double phaseInc)
{
    // Advance
    modCounter += phaseInc;
 
    // Check if Wrap is needed
 
    // Positive Frequencies 
    if(phaseInc > 0 && modCounter >= 1.0)
        modCounter -= 1.0;
 
    // Negative Frequencies
    if(phaseInc < 0 && modCounter <= 0.0)
        modCounter += 1.0;
}
 
double LFO::parabolicSine(double angle)
{
    const double B = 4.0 / PI;
    const double C = -4.0 / (PI* PI);
    const double P = 0.225;
 
    double y = B * angle + C * angle * fabs(angle);
    
    return (P * (y * fabs(y) - y) + y);
}