-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
382 lines (328 loc) · 10.4 KB
/
main.c
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include <em_device.h>
#include <em_chip.h>
#include <em_cmu.h>
#include <em_emu.h>
#include <em_gpio.h>
#include <em_timer.h>
#include <math.h>
#include <spidrv.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
#include "midi.h"
#include "synth.h"
#include "sound.h"
#include "channel.h"
#include "usart.h"
#include "button.h"
#include "arpeggiator.h"
#include "timer.h"
#include "leds.h"
/* For arpeggiator */
Arpeggiator arpeggiator;
uint16_t counter = 0;
uint16_t old_BPM = START_BPM;
uint8_t old_notes_per_beat = START_NPB;
float old_gate_time = START_GATE_TIME;
volatile char current_note;
volatile bool event_flag = true;
volatile bool arpeggiator_note_off_flag, arpeggiator_note_on_flag;
int current_reverb_preset;
bool arpeggiator_on = false;
/* For arpeggiator */
void start_arpeggiator() {
start_note_timer();
}
void stop_arpeggiator() {
stop_note_timer();
stop_gate_timer();
}
/* For arpeggiator */
typedef void CommandHandler(char status);
Synth synth = {
.master_volume = F2FP(1.0),
.pan = { .balance = 0, },
.reverb = {
.tau = {3003, 3403, 3905, 4495, 241, 83},
.gain = {
F2FP(0.895),
F2FP(0.883),
F2FP(0.867),
F2FP(0.853),
F2FP(0.7),
F2FP(0.7),
F2FP(0)
},
},
};
Channel channels[16];
Channel *arp_channel = &channels[9];
SPIDRV_HandleData_t synth_spi;
SPIDRV_Init_t synth_spi_init = {
.port = USART0,
.portLocationTx = _USART_ROUTELOC0_TXLOC_LOC0,
.portLocationRx = _USART_ROUTELOC0_RXLOC_LOC0,
.portLocationClk = _USART_ROUTELOC0_CLKLOC_LOC0,
.bitRate = 10000000,
.frameLength = 8,
.type = spidrvMaster,
.bitOrder = spidrvBitOrderLsbFirst,
.clockMode = spidrvClockMode0,
.csControl = spidrvCsControlApplication,
};
Ecode_t ecode = ECODE_OK; /* for debugging */
static void complete_synth_transfer(SPIDRV_Handle_t handle, Ecode_t status, int nbytes) {
if (nbytes != sizeof(synth)) return;
synth_clearcmds(&synth);
GPIO_PinOutSet(gpioPortE, 13);
GPIO_PinOutClear(LED_PORT, LED0);
}
static void abort_synth_transfer(void) {
ecode = SPIDRV_AbortTransfer(&synth_spi);
if (ecode != ECODE_EMDRV_SPIDRV_OK && ecode != ECODE_EMDRV_SPIDRV_IDLE) {
exit(1);
}
}
static void start_synth_transfer(void) {
GPIO_PinOutSet(LED_PORT, LED0);
GPIO_PinOutClear(gpioPortE, 13);
ecode = SPIDRV_MTransmit(&synth_spi, (void *)&synth, sizeof(synth), complete_synth_transfer);
if (ecode != ECODE_EMDRV_SPIDRV_OK) exit(1);
}
static void handle_note_off(char status) {
Channel *c = &channels[status & MIDI_CHANNEL_MASK];
char key = uart_next_valid_byte();
char velocity = uart_next_valid_byte();
if (c == arp_channel) {
remove_held_key(&arpeggiator, key);
}
channel_note_off(c, key, velocity / 127.0);
}
static void handle_note_on(char status) {
Channel *c = &channels[status & MIDI_CHANNEL_MASK];
char key = uart_next_valid_byte();
char velocity = uart_next_valid_byte();
if (c == arp_channel) {
add_held_key(&arpeggiator, key);
} else {
channel_note_on(c, key, velocity / 127.0);
}
}
static void handle_control_change(char status) {
Channel *c = &channels[status & MIDI_CHANNEL_MASK];
char ctrl = uart_next_valid_byte();
char value = uart_next_valid_byte();
switch (ctrl) {
case MIDI_CC_MODULATION_WHEEL:
case MIDI_CC_VOLUME:
c->gain = value / 127.0;
break;
case MIDI_CC_SUSTAIN_KEY:
case MIDI_CC_SUSTAIN_PEDAL:
channel_set_sustain(c, !value);
return;
case MIDI_CC_ALL_SOUND_OFF:
channel_all_notes_off(c, true);
return;
case MIDI_CC_RESET_ALL_CONTROLLERS:
c->gain = 0.5;
c->pitch_bend = 0.0;
c->sustain = false;
break;
case MIDI_CC_ALL_NOTES_OFF:
case MIDI_CC_OMNI_MODE_OFF:
case MIDI_CC_OMNI_MODE_ON:
case MIDI_CC_MONO_MODE_ON:
case MIDI_CC_MONO_MODE_OFF:
channel_all_notes_off(c, false);
return;
}
channel_update_wavegens(c);
}
static void handle_program_change(char status) {
Channel *c = &channels[status & MIDI_CHANNEL_MASK];
char program = uart_next_valid_byte();
if (program >= num_programs) return;
c->program = &programs[(int)program];
channel_update_wavegens(c);
}
static void handle_pitch_bend_change(char status) {
Channel *c = &channels[status & MIDI_CHANNEL_MASK];
char lsb = uart_next_valid_byte();
char msb = uart_next_valid_byte();
c->pitch_bend = (((msb << 7) | lsb) - 0x2000) / (float)0x2000;
channel_update_wavegens(c);
}
void handle_button(uint8_t pin) {
switch(pin) {
case BUTTON_SW1:
if (arpeggiator_on) {
set_gate_time(&arpeggiator, arpeggiator.gate_time - 0.05);
}
else {
// Handle reverb preset browsing (NOTE: abort and perform synth transfer, )
current_reverb_preset = (current_reverb_preset+1) % REVERB_PRESET_LEN;
event_flag = true;
}
break;
case BUTTON_SW2:
if (arpeggiator_on) {
set_gate_time(&arpeggiator, arpeggiator.gate_time + 0.05);
}
else {
if (arpeggiator.dynamic_NPB_switching) {
arpeggiator.dynamic_NPB_switching = false;
set_notes_per_beat(&arpeggiator, 4);
GPIO_PinOutClear(LED_PORT, LED1);
}
else {
arpeggiator.dynamic_NPB_switching = true;
GPIO_PinOutSet(LED_PORT, LED1);
set_notes_per_beat(&arpeggiator, arpeggiator.num_held_keys);
}
}
break;
case BUTTON_SW3:
if (arpeggiator_on) {
set_BPM(&arpeggiator, arpeggiator.BPM - 5);
}
else {
// Browse playback order
set_playback_order(&arpeggiator, (arpeggiator.playback_order + 1) % 4);
}
break;
case BUTTON_SW4:
if (arpeggiator_on) {
set_BPM(&arpeggiator, arpeggiator.BPM + 5);
}
else {
// Funky modulo because num_octaves ranges between 1-3, not 0-2
set_num_octaves(&arpeggiator, (arpeggiator.num_octaves % 3)+1);
}
break;
case BUTTON_SW5:
/* Toggle arpeggiator */
if (!arpeggiator_on) {
arpeggiator_on = true;
GPIO_PinOutSet(LED_PORT, LED2);
}
else {
arpeggiator_on = false;
GPIO_PinOutClear(LED_PORT, LED2);
GPIO_PinOutClear(LED_PORT, LED3);
}
break;
default:
break;
}
}
void update_arpeggiator(void) {
if (arpeggiator_note_off_flag) {
arpeggiator_note_off_flag = false;
channel_note_off(arp_channel, current_note, 0);
}
if (arpeggiator_note_on_flag) {
arpeggiator_note_on_flag = false;
current_note = play_current_note(&arpeggiator);
if (arpeggiator.loop_length == 0 || current_note == 0) {
return;
}
channel_note_on(arp_channel, current_note, 1.0);
}
}
void recv_midi_command(void) {
static CommandHandler *const command_handlers[] = {
[MIDI_NOTE_OFF] = handle_note_off,
[MIDI_NOTE_ON] = handle_note_on,
[MIDI_CONTROL_CHANGE] = handle_control_change,
[MIDI_PROGRAM_CHANGE] = handle_program_change,
[MIDI_PITCH_BEND_CHANGE] = handle_pitch_bend_change,
};
uint8_t byte = uart_next_byte();
int idx;
CommandHandler *handler;
if (byte == 0xFF) return;
if (!(byte & MIDI_STATUS_MASK)) {
warn();
return;
}
idx = (byte & MIDI_COMMAND_MASK) >> MIDI_COMMAND_SHIFT;
if (idx >= lenof(command_handlers)) return;
handler = command_handlers[(int)idx];
if (!handler) return;
handler(byte & ~MIDI_STATUS_MASK);
}
int main(void) {
for (int i = 0; i < num_programs && i < lenof(channels); i++) {
Channel *c = &channels[i];
c->program = &programs[i];
c->gain = 0.5;
}
CHIP_Init();
CMU_ClockEnable(cmuClock_GPIO, true);
GPIO_PinModeSet(gpioPortE, 13, gpioModePushPull, 1);
GPIO_PinModeSet(gpioPortA, 0, gpioModePushPull, 1);
SPIDRV_Init(&synth_spi, &synth_spi_init);
GPIOINT_Init();
button_init();
reset_channels();
uart_init();
__enable_irq();
led_init();
arpeggiator = setup_arpeggiator();
start_arpeggiator();
while (1) {
while (!event_flag) __WFI();
event_flag = false;
abort_synth_transfer();
synth.reverb = reverb_presets[current_reverb_preset];
update_arpeggiator();
recv_midi_command();
start_synth_transfer();
}
}
// Start playing note (send single-note synth struct to FPGA, with start instruction)
// (If implemented, tick metronome)
void TIMER0_IRQHandler(void)
{
// Clear flag for TIMER0 OF interrupt
TIMER_IntClear(TIMER0, TIMER_IF_OF);
// If notes_per_beat, BPM or gate_time has changed
if (arpeggiator.notes_per_beat != old_notes_per_beat || arpeggiator.BPM != old_BPM || arpeggiator.gate_time != old_gate_time) {
set_timer_tops(arpeggiator);
old_notes_per_beat = arpeggiator.notes_per_beat;
old_BPM = arpeggiator.BPM;
old_gate_time = arpeggiator.gate_time;
}
// Increment counter (for metronome handling)
counter++;
// Handles the metronome (currently LED3 toggling)
// Sort of a hack; this will usually cause a jump in the metronome whenever dynamic_NPB_switching is toggled
if (arpeggiator.dynamic_NPB_switching) {
if (arpeggiator.current_note_index == 0) {
GPIO_PinOutToggle(LED_PORT, LED3);
}
}
else if (arpeggiator.notes_per_beat == 1 || counter % arpeggiator.notes_per_beat-1 == 0) {
GPIO_PinOutToggle(LED_PORT, LED3);
}
if (arpeggiator.loop_length == 0) return;
event_flag = arpeggiator_note_on_flag = true;
start_gate_timer();
}
// Stop playing note (send single-note synth struct to FPGA, with stop instruction)
void TIMER1_IRQHandler(void)
{
// Clear flag for TIMER1 OF interrupt
TIMER_IntClear(TIMER1, TIMER_IF_OF);
event_flag = arpeggiator_note_off_flag = true;
// NB: Assumes current_note has not changed since last TIMER0 interrupt.
// Will not hold if gate_time is e.g. greater than 1
stop_gate_timer();
}
void _exit(int status) {
(void) status;
while (1) {}
}