-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.ino
269 lines (221 loc) · 6.36 KB
/
core.ino
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
265
266
267
268
269
#include "tables.h"
long frames = 0;
// HMMM either saw or tri should always be 0 so these can go to 512
// but what if not?
volatile short sawMix = 0; // 0-341 (~8.4 bits)
volatile short sqrMix = 127; // 0-341
volatile short triMix = 127; // 0-341
void setOscMix()
{
//int envMod = (envelopeShapeDepth * envLevel) >> ENV_BITS;
int lfoMod = (lfoShapeDepth * (lfoLevel>>8) >> (MOD_BITS-8);
//int gateMod = (gateShapeDepth * gateLevel) >> MOD_BITS;
int mx = oscMixSetting + lfoMod; // 0-255
if (mx < 0) { mx = 0; }
if (mx > 255) { mx = 255; }
int x;
if (mx < 128)
{
x = mx*2;
// first half is saw-to-square
sawMix = 255-x;
sqrMix = x;
triMix = 0;
}
else
{
x = (mx-128) * 2;
sawMix = 0;
sqrMix = 255-x;
triMix = x;
}
}
long resoRatio = 3543;
void setResoRatio()
{
int envMod = (envelopeRatioDepth * envLevel) >> ENV_BITS;
// lfo ratio depth is -128 to +127
// lfo level is 0-8M
// -- should the mod depth be unipolar and the mod wave bipolar instead?
// This way lines up well with envelope and gate...
int lfoMod = ( lfoRatioDepth * (lfoLevel>>8)) >> (MOD_BITS-8);
int gateMod = (gateRatioDepth * (gateLevel>>8)) >> (MOD_BITS-8);
int ratioValue = resoRatioSetting + envMod + lfoMod + gateMod;
// oof. reso ratio goes from 0.5 (down one octave,
// which may not be useful) to... 32? up 5 octaves?
// but the resoRatio value is scaled by 1024, so:
// 512 - 32768? Ideally it should probably be
// roughly exponential as well :/
int scale = 16;
int start = 512;
while (ratioValue < 0)
{
scale /= 2;
ratioValue += 32;
start /= 2;
}
while (ratioValue > 32)
{
scale *= 2;
ratioValue -= 32;
start *= 2;
}
resoRatio = start + scale * ratioValue;
if (resoRatio < 128)
{
resoRatio = 128;
}
if (resoRatio > 65536)
{
resoRatio = 65536;
}
}
void tick()
{
frames++;
if ((frames & CONTROL_RATE_MASK) == 0)
{
updateEnvelope();
updateModulators();
}
if ((frames & MIDI_RATE_MASK) == 0)
{
update_midi();
}
// TODO UPDATE MODULATOR LEVELS
tickEnvelope();
lfoLevel += lfoDelta;
if (lfoLevel > MAX_MOD)
{
lfoLevel = MAX_MOD;
}
if (lfoLevel < 0)
{
lfoLevel = 0;
}
gateLevel += gateDelta;
if (gateLevel > MAX_MOD)
{
gateLevel = MAX_MOD;
}
if (gateLevel < 0)
{
gateLevel = 0;
}
// Advance the envelope
envLevel += envRate;
if (envLevel > peakLevel)
{
envLevel = peakLevel;
}
if (envLevel < 0)
{
envLevel = 0;
}
setOscMix();
setResoRatio();
// TODO MODULATE OSC FREQ
// lfoPitchDepth, gatePitchDepth, envPitchDepth
// each should have a max of about +/- 2 semitones or 1.125x
int lfoMod = (lfoPitchDepth * (lfoLevel >> 8)) >> (MOD_BITS-8);
int gateMod = (gatePitchDepth * (gateLevel >> 8)) >> (MOD_BITS-8);
int envMod = (envPitchDepth * (envLevel >> 8)) >> (ENV_BITS-8);
// UGH this is ugly-ass
int pitchFactor = 1024 + lfoMod + gateMod + envMod;
int shift = 10;
deltaWavePhase = deltaWaveBase;
while (deltaWavePhase > 1048575)
{
deltaWavePhase >>= 1;
shift -= 1;
}
while (pitchFactor > 2047)
{
pitchFactor >>= 1;
shift -= 1;
}
deltaWavePhase = (pitchFactor * deltaWaveBase) >> shift;
deltaResoPhase = (deltaWavePhase / 1024) * resoRatio;
// Increment wave phasors
wavePhase += deltaWavePhase;
resoPhase += deltaResoPhase;
// Did the oscillator phase wrap?
if (wavePhase < deltaWavePhase)
{
// Sync the resonator
resoPhase = (wavePhase * deltaResoPhase) / deltaWavePhase;
}
// Get table indices
int waveIx = (wavePhase >> PHASOR_FRACTION_BITS) & TABLE_MASK;
int resoIx = (resoPhase >> PHASOR_FRACTION_BITS) & TABLE_MASK;
// Resonator lookup
unsigned long reso = sine[resoIx] >> 1; // 15 bit unsigned
// Oscillator lookups
unsigned long sawOsc = saw[waveIx]; // 16 bit unsigned
unsigned long triOsc = tri[waveIx];
unsigned long sqrOsc = sqr[waveIx];
// Sum and resonate oscillator A
// 16 bits x 10 bits (mixes sum to < 1024) = 26 bits
unsigned long osc = (sawOsc * sawMix + sqrOsc * sqrMix + triOsc * triMix) >> 11; // 15 bits
int resoDepth = resoDepthSetting << 7;
if (resoDepth < 0) { resoDepth = 0; }
if (resoDepth > 32767) { resoDepth = 32767; }
int resoComplement = 32767-resoDepth;
unsigned long resonated = (((osc * reso) >> 15) * resoDepth) >> 15; // 15 bits
unsigned long unresonated = (osc * resoComplement) >> 15; // 15 bits
unsigned long finalosc = resonated + unresonated; // 16 bits
// TODO gate
long amp = (envLevel >> (ENV_BITS-15)); // 15 bits
// 15 x 15 -> 30 >> 14 = 16u
audioChannel.WriteU16( (amp * (finalosc >> 1)) >> 14 );
//audioChannel.WriteU16( (amp * triOsc) >> 14 );
}
// K-rate update
void updateModulators()
{
lfoPhase += lfoDeltaPhase;
gatePhase += gateDeltaPhase;
set_led( 0, (lfoPhase & 0x40000000) > 0 ? 1 : 0 );
set_led( 2, (gatePhase & 0x40000000) > 0 ? 1 : 0 );
int lfoIx = (lfoPhase >> PHASOR_FRACTION_BITS) & TABLE_MASK;
long lfoTarget = ((long)tri[lfoIx]) << 7; // convert 16 bit to 23 bit
// get there by the next K-rate update
lfoDelta = (lfoTarget - lfoLevel) / CONTROL_RATE_RATIO;
// we just need three bits of the gate phase.
int gateStep = (gatePhase >> 28) & 0x7; // 0-7 which step we're on
// gate pattern is an 8 bit mask, if the bit is set the gate
// target is high for that step.
int stepValue = gatePattern & (1 << gateStep);
unsigned long gateTarget = 0;
if (stepValue > 0)
{
gateTarget = 0x7FFFFF;
set_led(1,0);
}
else
{
gateTarget = 0;
set_led(1,1);
}
gateDelta = (gateTarget-gateLevel) / CONTROL_RATE_RATIO;
// TODO gate contour
updateTestTone();
}
void inc_program()
{
currentProgram--;
if (currentProgram < 0)
{
currentProgram = 127;
}
select_program(currentProgram);
}
void dec_program()
{
currentProgram++;
if (currentProgram > 127)
{
currentProgram = 0;
}
select_program(currentProgram);
}