-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoscillator.cpp
More file actions
264 lines (215 loc) · 6.71 KB
/
Copy pathoscillator.cpp
File metadata and controls
264 lines (215 loc) · 6.71 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
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
259
260
261
262
263
264
//
// oscillator.cpp
//
#include <math.h>
#include "oscillator.h"
#include "sdload.h"
const float s_KeyFrequency[128] =
{
8.17580, 8.66196, 9.17702, 9.72272, 10.3009, 10.9134, 11.5623, 12.2499, 12.9783, 13.7500,
14.5676, 15.4339, 16.3516, 17.3239, 18.3540, 19.4454, 20.6017, 21.8268, 23.1247, 24.4997,
25.9565, 27.5000, 29.1352, 30.8677, 32.7032, 34.6478, 36.7081, 38.8909, 41.2034, 43.6535,
46.2493, 48.9994, 51.9131, 55.0000, 58.2705, 61.7354, 65.4064, 69.2957, 73.4162, 77.7817,
82.4069, 87.3071, 92.4986, 97.9989, 103.826, 110.000, 116.541, 123.471, 130.813, 138.591,
146.832, 155.563, 164.814, 174.614, 184.997, 195.998, 207.652, 220.000, 233.082, 246.942,
261.626, 277.183, 293.665, 311.127, 329.628, 349.228, 369.994, 391.995, 415.305, 440.000,
466.164, 493.883, 523.251, 554.365, 587.330, 622.254, 659.255, 698.456, 739.989, 783.991,
830.609, 880.000, 932.328, 987.767, 1046.50, 1108.73, 1174.66, 1244.51, 1318.51, 1396.91,
1479.98, 1567.98, 1661.22, 1760.00, 1864.66, 1975.53, 2093.00, 2217.46, 2349.32, 2489.02,
2637.02, 2793.83, 2959.96, 3135.96, 3322.44, 3520.00, 3729.31, 3951.07, 4186.01, 4434.92,
4698.64, 4978.03, 5274.04, 5587.65, 5919.91, 6271.93, 6644.88, 7040.00, 7458.62, 7902.13,
8372.02, 8869.84, 9397.27, 9956.06, 10548.1, 11175.3, 11839.8, 12543.9
};
static inline float KeyToFreq(unsigned key)
{
return s_KeyFrequency[key];
}
COscillator::COscillator()
: m_waveformSaw(0.0f),
m_waveformSquare(0.0f),
m_waveformTri(0.0f),
m_phase(0.0f),
m_phaseInc(0.0f),
m_phaseOffset(0.0f),
m_pulseWidth(0.5f),
m_frequency(0.0f),
m_currentFrequency(0.0f),
m_targetFrequency(0.0f),
m_glideTime(1.0f),
m_glide(false),
m_tableIndex(0),
m_tableFrac(0.0f),
m_osc2Kbd(1),
m_osc2Low(0),
m_sync(false)
{
}
void COscillator::Reset()
{
m_phase = 0.0f;
m_sync = false;
}
void COscillator::SetGlideTime(float time)
{
m_glide = (bool)time; // Store original MIDI value for no-glide detection
// Calculate actual glide time in seconds (efficient linear approximation)
if (time >= 1.0f) {
m_glideTime = 0.5f; // time = 1.0f -> 0.5 seconds
} else if (time <= 0.001f) {
m_glideTime = time; // time = 0.001f -> 0.001 seconds
} else {
// Linear interpolation between 0.001 and 0.5 seconds
m_glideTime = 0.001f + (time - 0.001f) * (0.5f - 0.001f) / (1.0f - 0.001f);
}
// Efficient exponential smoothing: k = 1 - exp(-1/(glideTime * sampleRate))
// Using approximation: k ≈ 1/(glideTime * sampleRate) for small values
float k = 1.0f / (m_glideTime * 48000.0f);
if (k > 1.0f) k = 1.0f;
if (k < 0.0001f) k = 0.0001f;
m_glideTime = k;
}
// --- Frequency to Table mapping ---
void COscillator::SetFrequency(unsigned key, float transpose, float fine, float lfo, float polyMod, unsigned sampleRate)
{
m_note = key;
// Calculate target frequency
float targetFreq;
if (!m_osc2Kbd && !m_osc2Low)
targetFreq = 440.0f + transpose * 35.0f;
if (!m_osc2Kbd && m_osc2Low)
targetFreq = 12.1f + transpose;
if (m_osc2Kbd && !m_osc2Low)
{
m_note += transpose;
targetFreq = 440.0f * powf(2.0f, (m_note-69)/12.0f);
}
if (m_osc2Kbd && m_osc2Low)
targetFreq = KeyToFreq(m_note)/100.0f + 12.1f + transpose;
float f = fine * 0.11892621789f - 0.05946309436f;
f = f * targetFreq;
targetFreq += f;
float mod = (lfo + polyMod) * targetFreq;
targetFreq += mod;
if (targetFreq < 0.0f)
targetFreq = 0.0f;
// Apply glide
if (!m_glide) // No glide - MIDI value is zero
{
m_frequency = targetFreq;
m_currentFrequency = targetFreq;
m_targetFrequency = targetFreq;
}
else
{
// Exponential glide
if (m_targetFrequency != targetFreq) {
// New target frequency
m_targetFrequency = targetFreq;
// Start from current position for glide (works in both legato and poly mode)
// If m_currentFrequency is 0, it will glide from 0 to target
}
// Apply exponential glide
float freqDiff = m_targetFrequency - m_currentFrequency;
m_currentFrequency += freqDiff * m_glideTime;
// Check if glide is complete (within 0.01% tolerance)
if (fabsf(freqDiff) < m_targetFrequency * 0.0001f) {
m_currentFrequency = m_targetFrequency;
}
m_frequency = m_currentFrequency;
}
m_phaseInc = m_frequency / sampleRate;
}
void COscillator::SetPulseWidth(float pw)
{
m_pulseWidth = pw;
m_pulseWidth = (m_pulseWidth<0.1f) ? 0.1f : (m_pulseWidth>0.9f) ? 0.9f : m_pulseWidth;
}
/////////////////////////////////
// Poly Blep Oscillator //
/////////////////////////////////
inline float COscillator::PolyBlep(float t, float dt)
{
if (t < dt) {
float x = t / dt;
return x + x - x * x - 1.0f;
}
else if (t > 1.0f - dt) {
float x = (t - 1.0f) / dt;
return x * x + x + x + 1.0f;
}
return 0.0f;
}
//https://dsp.stackexchange.com/questions/54790/polyblamp-anti-aliasing-in-c
inline float COscillator::PolyBlamp(float t, float dt)
{
float y = 0.0f;
if (t < dt)
{
float x = t / dt;
float x2 = x * x;
float x3 = x2 * x;
y += x3 / 6.0f;
}
else if (t > 1.0f - dt)
{
float x = (t - 1.0f) / dt;
float x2 = x * x;
float x3 = x2 * x;
y -= x3 / 6.0f;
}
return y * dt;
}
float COscillator::GetTri()
{
// Naive triangle
float dt = m_phaseInc;
float t = m_phase < 0.5f ? m_phase : 1.0f - m_phase;
float tri = 4.0f * t - 1.0f;
// slope discontinuities at t = 0, 0.5
float x = t + 0.5f;
if (x >= 1.0f) x -= 1.0f;
if (x < 0.0f) x += 1.0f;
tri += PolyBlamp(t, dt);
tri -= PolyBlamp(x, dt);
return tri;
}
float COscillator::GetSaw()
{
float phase = m_phase; // 0..1
float dt = m_phaseInc;
// Naive saw
float out = 2.0f * phase - 1.0f;
// Fix discontinuity at phase wrap
out -= PolyBlep(phase, dt);
return out;
}
float COscillator::GetSquare()
{
float phase = m_phase;
float dt = m_phaseInc;
float pw = m_pulseWidth; // 0..1
float out = (phase < pw) ? 1.0f : -1.0f;
// Apply polyBLEP at rising and falling edges
out += PolyBlep(phase, dt); // rising edge
float t2 = phase - pw;
if (t2 < 0.0f)
t2 += 1.0f;
out -= PolyBlep(t2, dt); // falling edge
return out;
}
float COscillator::GetSample()
{
float sample = 0.0f;
float sawSample = GetSaw();
float squareSample = GetSquare();
float triSample = GetTri();
sample = m_waveformSaw * sawSample - m_waveformSquare * squareSample +
m_waveformTri * triSample;
m_phase += m_phaseInc;
if (m_phase >= 1.0f)
{
m_phase -= 1.0f;
m_sync = true;
}
return sample;
}