-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpatch.cpp
312 lines (288 loc) · 10 KB
/
patch.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <inttypes.h>
#include "patch.h"
#include "utils.h"
// Returns two bytes, one register value in each.
uint16_t patchParamRegister(int param) {
// TODO combine with setPatchValue?
switch (param) {
// OSC A
case 0: return 4; // Waveform
case 1: return (2 << 8 | 3);// Pulse width
case 2: return 5; // Attack
case 3: return 5; // Decay
case 4: return 6; // Sustain
case 5: return 6; // Release
case 6: return 23; // Filter enable
// OSC B
case 7: return 11;
case 8: return (9 << 8 | 10);
case 9: return 12;
case 10: return 12;
case 11: return 13;
case 12: return 13;
case 13: return 23;
case 14: return (7 << 8 | 8); // Detune
// OSC C
case 15: return 18;
case 16: return (16 << 8 | 17);
case 17: return 19;
case 18: return 19;
case 19: return 20;
case 20: return 20;
case 21: return 23;
case 22: return (14 << 8 | 15);
// Filter
case 23: return (21 << 8) | 22; // Filter cutoff
case 24: return 23; // Resonance
case 25: return 24; // Filter mode
// General
case 26: return 24; // Volume
}
}
int *loadPatchValuePtr(int param, livePatch *p) {
switch (param) {
case 0: return &(p->patch.waveOscA);
case 1: return &(p->patch.pulseWidthOscA);
case 2: return &(p->patch.attackOscA);
case 3: return &(p->patch.decayOscA);
case 4: return &(p->patch.sustainOscA);
case 5: return &(p->patch.releaseOscA);
case 6: return &(p->patch.filterOscA);
case 7: return &(p->patch.waveOscB);
case 8: return &(p->patch.pulseWidthOscB);
case 9: return &(p->patch.attackOscB);
case 10: return &(p->patch.decayOscB);
case 11: return &(p->patch.sustainOscB);
case 12: return &(p->patch.releaseOscB);
case 13: return &(p->patch.filterOscB);
case 14: return &(p->patch.detuneOscB);
case 15: return &(p->patch.waveOscC);
case 16: return &(p->patch.pulseWidthOscC);
case 17: return &(p->patch.attackOscC);
case 18: return &(p->patch.decayOscC);
case 19: return &(p->patch.sustainOscC);
case 20: return &(p->patch.releaseOscC);
case 21: return &(p->patch.filterOscC);
case 22: return &(p->patch.detuneOscC);
case 23: return &(p->patch.cutoff);
case 24: return &(p->patch.resonance);
case 25: return &(p->patch.mode);
case 26: return &(p->patch.volume);
}
}
int loadPatchValue(int param, livePatch *p) {
int *v = loadPatchValuePtr(param, p);
return *v;
}
void patchUpdateRegister(livePatch *p, int param) {
// TODO support pulseWidth, detune, bypass
switch(param) {
// 0/1: Osc A: frequency (midi)
case 0:
// Osc A: Control Register
//
// 0000 0001 (1) - Gate (midi)
// 0000 0010 (2) - Sync
// 0000 0100 (4) - Ring mod (always with triangle, ie 0x14
// 0000 1000 (8) - Test (not used)
// 0001 0000 (16) - Triangle
// 0010 0000 (32) - Saw
// 0100 0000 (64) - Square
// 1000 0000 (128) - Noise
switch (p->patch.waveOscA) {
case 0: p->registers[4] = 0x10; break;
case 1: p->registers[4] = 0x20; break;
case 2: p->registers[4] = 0x40; break;
case 3: p->registers[4] = 0x14; break;
case 4: p->registers[4] = 0x12; break;
case 5: p->registers[4] = 0x22; break;
case 6: p->registers[4] = 0x42; break;
case 7: p->registers[4] = 0x80; break;
}
break;
case 1:
// Osc A: pulse width
p->registers[2] = p->patch.pulseWidthOscA & 0xFF;
p->registers[3] = p->patch.pulseWidthOscA >> 8;
break;
case 2:
case 3:
// Osc A: Attack, Decay
p->registers[5] = (p->patch.attackOscA << 4) + p->patch.decayOscA;
break;
case 4:
case 5:
// Osc A: Sustain, Release
p->registers[6] = (p->patch.sustainOscA << 4) + p->patch.releaseOscA;
break;
case 6:
// Osc A: Filter enable
if (p->patch.filterOscA == 1) p->registers[23] |= 0x1;
else p->registers[23] &= 0xFE;
break;
// 7/8: Osc B: frequency (midi)
case 7:
// Osc B: Control Register
switch (p->patch.waveOscB) {
case 0: p->registers[11] = 0x10; break;
case 1: p->registers[11] = 0x20; break;
case 2: p->registers[11] = 0x40; break;
case 3: p->registers[11] = 0x14; break;
case 4: p->registers[11] = 0x12; break;
case 5: p->registers[11] = 0x22; break;
case 6: p->registers[11] = 0x42; break;
case 7: p->registers[11] = 0x80; break;
}
break;
case 8:
// Osc B: pulse width
p->registers[9] = p->patch.pulseWidthOscB & 0xFF;
p->registers[10] = p->patch.pulseWidthOscB >> 8;
break;
case 9:
case 10:
// Osc B: Attack, Decay
p->registers[12] = (p->patch.attackOscB << 4) + p->patch.decayOscB;
break;
case 11:
case 12:
// Osc B: Sustain, Release
p->registers[13] = (p->patch.sustainOscB << 4) + p->patch.releaseOscB;
break;
case 13:
// Osc B: Filter enable
if (p->patch.filterOscB == 1) p->registers[23] |= 0x2;
else p->registers[23] &= 0xFD;
break;
case 14:
// Osc B: Detune
break;
// 14/15: Osc C: frequency (midi)
case 15:
// Osc C: Control Register
switch (p->patch.waveOscC) {
case 0: p->registers[18] = 0x10; break;
case 1: p->registers[18] = 0x20; break;
case 2: p->registers[18] = 0x40; break;
case 3: p->registers[18] = 0x14; break;
case 4: p->registers[18] = 0x12; break;
case 5: p->registers[18] = 0x22; break;
case 6: p->registers[18] = 0x42; break;
case 7: p->registers[18] = 0x80; break;
}
break;
case 16:
// Osc C: pulse width (setting to 2048 for now)
p->registers[16] = p->patch.pulseWidthOscC & 0xFF;
p->registers[17] = p->patch.pulseWidthOscC >> 8;
break;
case 17:
case 18:
// Osc C: Attack, Decay
p->registers[19] = (p->patch.attackOscC << 4) + p->patch.decayOscC;
break;
case 19:
case 20:
// Osc C: Sustain, Release
p->registers[20] = (p->patch.sustainOscC << 4) + p->patch.releaseOscC;
break;
case 21:
// Osc C: Filter enable
if (p->patch.filterOscC == 1) p->registers[23] |= 0x2;
else p->registers[23] &= 0xFD;
break;
case 22:
// Osc C: Detune
break;
case 23:
// Filter frequency (11 bits)
p->registers[21] = p->patch.cutoff & 0x7; // 0000 0111
p->registers[22] = p->patch.cutoff >> 3;
break;
case 24:
// Resonance, Routing
p->registers[23] = p->patch.resonance << 4;
// 0100 - OSC C
// 0010 - OSC B
// 0001 - OSC A
p->registers[23] |= 0x7; // 0111
break;
case 25:
case 26:
// Mode / Vol
// 0001 0000 (0x10) - lowpass
// 0010 0000 (0x40) - bandpass
// 0100 0000 (0x20) - highpass
// 0101 0000 (0x50) - notch
switch (p->patch.mode) {
case 0: p->registers[24] = 0x10; break;
case 1: p->registers[24] = 0x40; break;
case 2: p->registers[24] = 0x20; break;
case 3: p->registers[24] = 0x50; break;
}
break;
}
}
void patchToRegisters(livePatch *p) {
for (int i = 0; i < 27; i++) {
patchUpdateRegister(p, i);
}
}
// Update the setting, and the register value.
void setPatchValue(livePatch *p, int param, int val) {
int *v = loadPatchValuePtr(param, p);
*v = val;
patchUpdateRegister(p, param);
}
bool copyPatch(patchSettings *pSrc, livePatch *pDest) {
pDest->patch.waveOscA = pSrc->waveOscA;
pDest->patch.pulseWidthOscA = pSrc->pulseWidthOscA;
pDest->patch.attackOscA = pSrc->attackOscA;
pDest->patch.decayOscA = pSrc->decayOscA;
pDest->patch.sustainOscA = pSrc->sustainOscA;
pDest->patch.releaseOscA = pSrc->releaseOscA;
pDest->patch.filterOscA = pSrc->filterOscA;
pDest->patch.waveOscB = pSrc->waveOscB;
pDest->patch.pulseWidthOscB = pSrc->pulseWidthOscB;
pDest->patch.attackOscB = pSrc->attackOscB;
pDest->patch.decayOscB = pSrc->decayOscB;
pDest->patch.sustainOscB = pSrc->sustainOscB;
pDest->patch.releaseOscB = pSrc->releaseOscB;
pDest->patch.filterOscB = pSrc->filterOscB;
pDest->patch.detuneOscB = pSrc->detuneOscB;
pDest->patch.waveOscC = pSrc->waveOscC;
pDest->patch.pulseWidthOscC = pSrc->pulseWidthOscC;
pDest->patch.attackOscC = pSrc->attackOscC;
pDest->patch.decayOscC = pSrc->decayOscC;
pDest->patch.sustainOscC = pSrc->sustainOscC;
pDest->patch.releaseOscC = pSrc->releaseOscC;
pDest->patch.filterOscC = pSrc->filterOscC;
pDest->patch.detuneOscC = pSrc->detuneOscC;
pDest->patch.cutoff = pSrc->cutoff;
pDest->patch.resonance = pSrc->resonance;
pDest->patch.mode = pSrc->mode;
pDest->patch.volume = pSrc->volume;
pDest->patch.id = pSrc->id;
setString(pSrc->name, pDest->patch.name, PATCHNAME_LEN);
patchToRegisters(pDest);
return true;
}
//void displayRegisters(patch *p) {
// patchToRegisters(p);
//
// int d = 2500;
// lcd.clear();
// for (int i = 0; i < 25; i++) {
// if (i != 0 && i % 7 == 0) {
// delay(d);
// lcd.clear();
// }
// if (p->registers[i] < 16)
// lcd.print(0);
// lcd.print(p->registers[i], HEX);
// lcd.print(' ');
// if (i % 7 == 4)
// lcd.setCursor(0,1);
// }
// delay(d);
//}