-
Notifications
You must be signed in to change notification settings - Fork 1
/
synth-stateless-oscillators.h
258 lines (205 loc) · 5.92 KB
/
synth-stateless-oscillators.h
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
FM. BISON hybrid FM synthesis -- Stateless oscillator functions.
(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!
- Phase is [0..1], this range must be adhered to except for oscSine() and oscCos()
- Band-limited (PolyBLEP) oscillators are called 'oscPoly...'
*/
#pragma once
#include "synth-global.h"
namespace SFM
{
/*
Sin/Cos
*/
SFM_INLINE static float oscSine(float phase)
{
return fast_sinf(phase);
}
SFM_INLINE static float oscCos(float phase)
{
return fast_cosf(phase);
}
/* Naive implementations (not band-limited) */
SFM_INLINE static float oscSaw(float phase)
{
SFM_ASSERT(phase >= 0.f && phase <= 1.f);
phase += 0.5f;
return 2.f*phase - 1.f;
}
SFM_INLINE static float oscRamp(float phase)
{
return -1.f*oscSaw(phase);
}
SFM_INLINE static float oscSquare(float phase)
{
SFM_ASSERT(phase >= 0.f && phase <= 1.f);
return phase >= 0.5 ? 1.f : -1.f;
}
SFM_INLINE static float oscTriangle(float phase)
{
SFM_ASSERT(phase >= 0.f && phase <= 1.f);
return -2.f * (fabsf(-1.f + (2.f*phase))-0.5f);
}
SFM_INLINE static float oscPulse(float phase, float duty)
{
SFM_ASSERT(phase >= 0.f && phase <= 1.f);
SFM_ASSERT(duty >= 0.f && duty <= 1.f);
const float cycle = phase;
return (cycle < duty) ? 1.f : -1.f;
}
SFM_INLINE static float oscBox(float phase)
{
SFM_ASSERT(phase >= 0.f && phase <= 1.f);
return phase >= 0.25f && phase <= 0.75f ? 1.f : -1.f;
}
/*
Band-limited (PolyBLEP) oscillators
- Links:
* https://github.com/martinfinke/PolyBLEP
* http://www.kvraudio.com/forum/viewtopic.php?t=375517
* https://dsp.stackexchange.com/questions/54790/polyblamp-anti-aliasing-in-c
- A copy of Martin Finke's implementation can be found @ /3rdparty/PolyBLEP
I've kept this implementation and it's helper functions all in one spot, though I did
rename a few things to keep it consistent with my style
Keep in mind that PolyBLEP is *not* the best solution for band-limited oscillators, but it does a relatively good job for
it's CPU footprint; it gets better the higher the sample rate is
*/
namespace Poly
{
template<typename T> SFM_INLINE static T Squared(const T &value)
{
return value*value;
}
SFM_INLINE static int32_t bitwiseOrZero(float value)
{
return static_cast<int32_t>(value) | 0;
}
// Adapted from "Phaseshaping Oscillator Algorithms for Musical Sound Synthesis" by Jari Kleimola, Victor Lazzarini, Joseph Timoney, and Vesa Valimaki.
// http://www.acoustics.hut.fi/publications/papers/smc2010-phaseshaping/
SFM_INLINE static float BLEP(float point, float dT /* This is, usually, just the pitch of the oscillator */)
{
if (point < dT)
// Discontinuities between 0 & 1
return -Squared(point/dT - 1.f);
else if (point > 1.f - dT)
// Discontinuities between -1 & 0
return Squared((point - 1.f)/dT + 1.f);
else
return 0.f;
}
// By Tale: http://www.kvraudio.com/forum/viewtopic.php?t=375517
SFM_INLINE static float BLEP_by_Tale(float point, float dT)
{
if (point < dT)
{
// Discontinuities between 0 & 1
point /= dT;
return point+point - point*point - 1.f;
}
else if (point > 1.f-dT)
{
// Discontinuities between -1 & 0
point = (point-1.f)/dT;
return point*point + point+point + 1.f;
}
else
return 0.f;
}
SFM_INLINE static float BLAMP(float point, float dT)
{
if (point < dT)
{
point = point/dT - 1.f;
return -1.f / 3.f*Squared(point) * point;
}
else if (point > 1.f - dT)
{
point = (point - 1.f) /dT + 1.f;
return 1.f / 3.f*Squared(point) * point;
}
else
return 0.f;
}
}
SFM_INLINE static float oscPolySquare(float phase, float pitch)
{
SFM_ASSERT(phase >= 0.f && phase <= 1.f);
SFM_ASSERT(pitch > 0.f);
float P1 = phase + 0.5f;
P1 -= Poly::bitwiseOrZero(P1);
float square = phase < 0.5f ? 1.f : -1.f;
square += Poly::BLEP(phase, pitch) - Poly::BLEP(P1, pitch);
return square;
}
SFM_INLINE static float oscPolySaw(float phase, float pitch)
{
SFM_ASSERT(phase >= 0.f && phase <= 1.f);
SFM_ASSERT(pitch > 0.f);
float P1 = phase + 0.5f;
P1 -= Poly::bitwiseOrZero(P1);
float saw = 2.f*P1 - 1.f;
saw -= Poly::BLEP(P1, pitch);
return saw;
}
SFM_INLINE static float oscPolyRamp(float phase, float pitch)
{
SFM_ASSERT(phase >= 0.f && phase <= 1.f);
SFM_ASSERT(pitch > 0.f);
float P1 = phase;
P1 -= Poly::bitwiseOrZero(P1);
float ramp = 1.f - 2.f*P1;
ramp += Poly::BLEP(P1, pitch);
return ramp;
}
SFM_INLINE static float oscPolyTriangle(float phase, float pitch)
{
SFM_ASSERT(phase >= 0.f && phase <= 1.f);
SFM_ASSERT(pitch > 0.f);
float P1 = phase + 0.25f;
float P2 = phase + 0.75f;
P1 -= Poly::bitwiseOrZero(P1);
P2 -= Poly::bitwiseOrZero(P2);
float triangle = phase*4.f;
if (triangle >= 3.f)
{
triangle -= 4.f;
}
else if (triangle > 1.f)
{
triangle = 2.f - triangle;
}
triangle += 4.f * pitch * (Poly::BLAMP(P1, pitch) - Poly::BLAMP(P2, pitch));
return triangle;
}
SFM_INLINE static float oscPolyRectifiedSine(float phase, float pitch)
{
SFM_ASSERT(phase >= 0.f && phase <= 1.f);
SFM_ASSERT(pitch > 0.f);
float P1 = phase + 0.25f;
P1 -= Poly::bitwiseOrZero(P1);
float rectified = 2.f * oscSine(0.5f * P1) - 4.f*0.5f;
rectified += 2.f * pitch * Poly::BLAMP(P1, pitch);
return rectified;
}
SFM_INLINE static float oscPolyRectangle(float phase, float pitch, float width)
{
SFM_ASSERT(phase >= 0.f && phase <= 1.f);
SFM_ASSERT(pitch > 0.f);
SFM_ASSERT(width > 0.f && width <= 1.f);
float P1 = phase + 1.f-width;
P1 -= Poly::bitwiseOrZero(P1);
float rectangle = -2.f*width;
if (phase < width)
rectangle += 2.f;
rectangle += Poly::BLEP(phase, pitch) - Poly::BLEP(P1, pitch);
return rectangle;
}
/*
White noise
*/
SFM_INLINE static float oscWhiteNoise()
{
return mt_randfc();
}
}