-
Notifications
You must be signed in to change notification settings - Fork 1
/
synth-oscillator.cpp
197 lines (149 loc) · 4.21 KB
/
synth-oscillator.cpp
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
/*
FM. BISON hybrid FM synthesis -- Oscillator (DCO/LFO).
(C) njdewit technologies (visualizers.nl) & bipolaraudio.nl
MIT license applies, please see https://en.wikipedia.org/wiki/MIT_License or LICENSE in the project root!
*/
#include "synth-oscillator.h"
#include "synth-distort.h"
namespace SFM
{
void Oscillator::Initialize(Waveform form, float frequency, unsigned sampleRate, float phaseShift, float supersawDetune /* = 0.f */, float supersawMix /* = 0.f */)
{
switch (form)
{
case kWhiteNoise:
m_phase.Initialize(1.f, sampleRate);
break;
case kPinkNoise:
m_pinkNoise = PinkNoise();
m_phase.Initialize(1.f, sampleRate);
break;
case kSupersaw:
m_supersaw.Initialize(frequency, sampleRate, supersawDetune, supersawMix);
break;
case kSampleAndHold:
m_sampleAndHold = SampleAndHold(sampleRate);
default:
m_phase.Initialize(frequency, sampleRate, phaseShift);
}
m_form = form;
}
float Oscillator::Sample(float phaseShift)
{
SFM_ASSERT(phaseShift >= 0.f);
constexpr float defaultDuty = 0.25f; // FIXME: parameter?
// These calls are unnecessary for a few waveforms, but as far as they don't show up in a profiler I'll let them be
const float phase = m_phase.Sample();
const float pitch = m_phase.GetPitch(); // For PolyBLEP
const float modulated = (0.f == phaseShift) // Not calling fmodf() certainly warrants a comparison and branch
? phase // Gauranteed to be [0..1]
: fmodf(phase+phaseShift, 1.f);
// Calculate signal (switch statement has never shown up during profiling)
float signal = 0.f;
switch (m_form)
{
case kNone:
signal = 0.f;
break;
/* Supersaw */
case kSupersaw:
signal = m_supersaw.Sample();
break;
/* Band-limited (DCO/LFO) */
case kSine:
signal = oscSine(modulated);
break;
case kCosine:
signal = oscCos(modulated);
break;
case kPolyTriangle:
signal = oscPolyTriangle(modulated, pitch);
break;
case kPolySquare:
signal = oscPolySquare(modulated, pitch);
break;
case kPolySaw:
signal = oscPolySaw(modulated, pitch);
break;
case kPolyRamp:
signal = oscPolyRamp(modulated, pitch);
break;
case kPolyRectifiedSine:
signal = oscPolyRectifiedSine(modulated, pitch);
break;
case kPolyRectangle:
signal = oscPolyRectangle(modulated, pitch, defaultDuty);
break;
case kBump:
signal = Squarepusher(oscSine(modulated), 0.3f);
break;
/*
Approximate a ramp and a saw with a *very* subtle slope (for LFO)
*/
case kSoftRamp:
{
const float ramp = oscSine(modulated + 0.1f*oscSine(modulated));
const float squared = Squarepusher(ramp, 0.4f);
signal = lerpf<float>(ramp, squared, 0.4f);
}
break;
case kSoftSaw:
{
const float saw = oscSine(modulated - 0.1f*oscSine(modulated));
const float squared = Squarepusher(saw, 0.4f);
signal = lerpf<float>(saw, squared, 0.4f);
}
break;
/* Noise */
case kWhiteNoise:
signal = oscWhiteNoise();
break;
case kPinkNoise:
signal = m_pinkNoise.Sample();
break;
/* See synth-oscillator.h */
case kUniRamp:
{
const float ramp = oscSine(modulated + 0.1f*oscSine(modulated));
const float squared = Squarepusher(ramp, 0.4f);
signal = lerpf<float>(ramp, squared, 0.4f);
signal = 0.5f + signal*0.5f;
}
// signal = oscPolyRamp(modulated, pitch);
// signal = 0.5f + signal*0.5f;
break;
/* LFO */
case kRamp:
signal = oscRamp(modulated);
break;
case kSaw:
signal = oscSaw(modulated);
break;
case kSquare:
signal = oscSquare(modulated);
break;
case kTriangle:
signal = oscTriangle(modulated);
break;
case kPulse:
signal = oscPulse(modulated, defaultDuty);
break;
/* S&H */
case kSampleAndHold:
{
const float random = oscWhiteNoise();
signal = m_sampleAndHold.Sample(modulated, random);
}
break;
// Not implemented
default:
signal = oscWhiteNoise();
SFM_ASSERT(false);
break;
}
// I'd like to check the range here as well ([-1..1]) but for ex. pink noise or the supersaw overshoot (FIXME)
FloatAssert(signal);
m_signal = signal;
return signal;
}
}