-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.cpp
executable file
·692 lines (641 loc) · 14.3 KB
/
main.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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
/*
This code is a modification of OscPocketD/VASynth, created by Staffan Melin, [email protected].
It was later modified by (Steven @moonfriendsynth) to work with the Daisy Pod.
I (Christopher @Nettech15) have modified it further to run on the Daisy Seed without the use of the Daisy Pod board.
Synth parameters are now controlled by a Miditech i2-61 midi keyboard.
Feel free to copy, modify, and improve this code to match your equipment and sound requirements.
*/
#include "daisy_seed.h"
#include "daisysp.h"
#include "main.h"
#include "vasynth.h"
using namespace daisy;
using namespace daisysp;
// globals
DaisySeed hardware;
MidiUsbHandler midi;
float sysSampleRate;
float sysCallbackRate;
extern uint8_t preset_max;
extern VASynthSetting preset_setting[PRESET_MAX];
uint8_t param_;
float pitch_bend = 1.0f;
float master_tune = 0.0f;
// sound
VASynth vasynth;
uint8_t gPlay = PLAY_ON;
// fx
DelayLine<float, DELAY_MAX> DSY_SDRAM_BSS delay_;
// sequencer
void SequencerPlay(uint16_t);
void SequencerRecord(uint8_t, uint8_t);
void SeqTimer_Config(void);
void writeSram(uint32_t, uint8_t);
uint8_t readSram(uint32_t);
void writeSramWord(uint32_t, uint16_t);
uint16_t readSramWord(uint32_t);
uint16_t seqclock = 0, seqtime = 0, seqmode = 0, seqmsg;
uint16_t seqnote, seqvelocity;
uint32_t seqmem = 0x00010000;
// audio callback
void AudioCallback(AudioHandle::InterleavingInputBuffer in,
AudioHandle::InterleavingOutputBuffer out,
size_t size)
{
float voice_left, voice_right;
for (size_t n = 0; n < size; n += 2)
{
if (gPlay == PLAY_ON)
{
vasynth.Process(&voice_left, &voice_right);
out[n] = voice_left + in[n];
out[n + 1] = voice_right + in[n + 1];
}
else
{
out[n] = 0;
out[n + 1] = 0;
}
}
}
// midi handler
void HandleMidiMessage(MidiEvent m)
{
switch(m.type)
{
case NoteOn:
{
NoteOnEvent p = m.AsNoteOn();
if ((vasynth.midi_channel_ == MIDI_CHANNEL_ALL) || (p.channel == vasynth.midi_channel_))
{
if(seqmode == 1)
{
SequencerRecord((p.note | 0x80), p.velocity);
}
vasynth.NoteOn(p.note, p.velocity);
hardware.SetLed(true);
}
break;
}
case NoteOff:
{
NoteOnEvent p = m.AsNoteOn();
if ((vasynth.midi_channel_ == MIDI_CHANNEL_ALL) || (p.channel == vasynth.midi_channel_))
{
if(seqmode == 1)
{
SequencerRecord(p.note, 0);
}
vasynth.NoteOff(p.note);
hardware.SetLed(false);
}
break;
}
case PitchBend:
{
PitchBendEvent p = m.AsPitchBend();
if ((vasynth.midi_channel_ == MIDI_CHANNEL_ALL) || (p.channel == vasynth.midi_channel_))
{
vasynth.PitchBend(p.value);
}
break;
}
case ControlChange:
{
ControlChangeEvent p = m.AsControlChange();
switch(p.control_number)
{
case 1: // Modulation Wheel
vasynth.lfo_amp_ = ((float)p.value / 127.0f);
vasynth.SetLFO();
break;
case 7: // Data Slider Default (Volume)
switch(param_)
{
case 0: // This is set as the default parameter
{
master_tune = 1.0f - ((float)p.value / 64.0f);
break;
}
case 1:
{
switch(p.value >> 5)
{
case 0:
vasynth.waveform_ = WAVE_TRI;
break;
case 1:
vasynth.waveform_ = WAVE_SAW;
break;
case 2:
vasynth.waveform_ = WAVE_SQUARE;
break;
case 3:
vasynth.waveform_ = WAVE_POLYBLEP_SAW;
break;
}
vasynth.SetWaveform();
break;
}
case 2:
{
switch(p.value >> 5)
{
case 0:
vasynth.osc2_waveform_ = WAVE_TRI;
break;
case 1:
vasynth.osc2_waveform_ = WAVE_SAW;
break;
case 2:
vasynth.osc2_waveform_ = WAVE_SQUARE;
break;
case 3:
vasynth.osc2_waveform_ = WAVE_POLYBLEP_SAW;
break;
}
vasynth.SetWaveform();
break;
}
case 3:
{
vasynth.osc_mix_ = ((float)p.value / 127.0f);
break;
}
case 4:
{
vasynth.osc2_detune_ = ((float)p.value / 255.0f);
break;
}
case 5:
{
vasynth.osc2_transpose_ = (1.0f + ((float)p.value / 127.0f));
break;
}
case 6:
{
vasynth.filter_res_ = ((float)p.value / 127.0f);
vasynth.SetFilter();
break;
}
case 7:
{
vasynth.osc_pw_ = ((float)p.value / 255.0f);
break;
}
case 8:
{
vasynth.osc2_pw_ = ((float)p.value / 255.0f);
break;
}
case 9:
{
vasynth.eg_f_attack_ = ((float)p.value / 127.0f);
vasynth.SetEG();
break;
}
case 10:
{
vasynth.eg_f_decay_ = ((float)p.value / 127.0f);
vasynth.SetEG();
break;
}
case 11:
{
vasynth.eg_f_sustain_ = ((float)p.value / 127.0f);
vasynth.SetEG();
break;
}
case 12:
{
vasynth.eg_f_release_ = ((float)p.value / 127.0f);
vasynth.SetEG();
break;
}
case 13:
{
vasynth.eg_a_attack_ = ((float)p.value / 127.0f);
vasynth.SetEG();
break;
}
case 14:
{
vasynth.eg_a_decay_ = ((float)p.value / 127.0f);
vasynth.SetEG();
break;
}
case 15:
{
vasynth.eg_a_sustain_ = ((float)p.value / 127.0f);
vasynth.SetEG();
break;
}
case 16:
{
vasynth.eg_a_release_ = ((float)p.value / 127.0f);
vasynth.SetEG();
break;
}
case 17:
{
vasynth.vcf_kbd_follow_= ((float)p.value / 127.0f);
break;
}
case 18:
{
vasynth.env_kbd_follow_ = ((float)p.value / 127.0f);
break;
}
case 19:
{
vasynth.vel_select_ = p.value >> 5;
break;
}
case 20:
{
vasynth.eg_f_amount_ = ((float)p.value / 127.0f);
break;
}
case 21:
{
vasynth.lfo_freq_ = ((float)p.value / 127.0f);
vasynth.SetLFO();
break;
}
case 22:
{
vasynth.pwmlfo_freq_ = ((float)p.value / 127.0f);
vasynth.SetPWMLFO();
break;
}
case 23:
{
vasynth.pwmlfo_amp_ = ((float)p.value / 511.0f);
vasynth.SetPWMLFO();
break;
}
case 24:
{
vasynth.pwm2lfo_freq_ = ((float)p.value / 127.0f);
vasynth.SetPWM2LFO();
break;
}
case 25:
{
vasynth.pwm2lfo_amp_ = ((float)p.value / 511.0f);
vasynth.SetPWM2LFO();
break;
}
case 26:
{
vasynth.vcavcflfo_freq_ = ((float)p.value / 127.0f);
vasynth.SetVCAVCFLFO();
break;
}
case 27:
{
vasynth.vcavcflfo_amp_ = ((float)p.value / 127.0f);
vasynth.SetVCAVCFLFO();
break;
}
case 28:
{
switch(p.value >> 5)
{
case 0:
vasynth.vcavcflfo_waveform_ = WAVE_TRI;
break;
case 1:
vasynth.vcavcflfo_waveform_ = WAVE_SAW;
break;
case 2:
vasynth.vcavcflfo_waveform_ = WAVE_SQUARE;
break;
case 3:
vasynth.vcavcflfo_waveform_ = WAVE_POLYBLEP_SAW;
break;
}
vasynth.SetVCAVCFLFO();
break;
}
}
break;
default: break;
}
break;
}
case ProgramChange:
{
ProgramChangeEvent p = m.AsProgramChange();
if ((vasynth.midi_channel_ == MIDI_CHANNEL_ALL) || (p.channel == vasynth.midi_channel_))
{
if(p.program >= 29)
{
switch (p.program)
{
case 35:
{
// Sequencer Mode Record
seqmode = 1;
seqclock = 0;
break;
}
case 36:
{
// Sequencer Mode Record End
seqmode = 2;
// Place a time-stamp and stop marker at the current position and stop the sequencer
writeSramWord(seqmem, seqclock);
seqmem++;
seqmem++;
writeSramWord(seqmem, 0xFFFF);
seqmode = 0;
seqclock = 0;
seqmem = 0x00010000;
break;
}
case 37:
{
// Sequencer Mode Stop
seqmode = 0;
seqclock = 0;
seqmem = 0x00010000;
break;
}
case 38:
{
// Sequencer Mode Play
seqmode = 3;
seqclock = 0;
seqmem = 0x00010000;
break;
}
case 39:
{
vasynth.FlashLoad(0);
break;
}
case 40:
{
vasynth.FlashLoad(1);
break;
}
case 41:
{
vasynth.FlashLoad(2);
break;
}
case 42:
{
vasynth.FlashLoad(3);
break;
}
case 43:
{
vasynth.FlashLoad(4);
break;
}
case 44:
{
vasynth.FlashLoad(5);
break;
}
case 45:
{
vasynth.FlashLoad(6);
break;
}
case 46:
{
vasynth.FlashLoad(7);
break;
}
case 47:
{
vasynth.FlashLoad(8);
break;
}
case 48:
{
vasynth.FlashLoad(9);
break;
}
case 49:
{
vasynth.FlashSave(0);
break;
}
case 50:
{
vasynth.FlashSave(1);
break;
}
case 51:
{
vasynth.FlashSave(2);
break;
}
case 52:
{
vasynth.FlashSave(3);
break;
}
case 53:
{
vasynth.FlashSave(4);
break;
}
case 54:
{
vasynth.FlashSave(5);
break;
}
case 55:
{
vasynth.FlashSave(6);
break;
}
case 56:
{
vasynth.FlashSave(7);
break;
}
case 57:
{
vasynth.FlashSave(8);
break;
}
case 58:
{
vasynth.FlashSave(9);
break;
}
}
}
else
{
vasynth.ProgramChange(p.program);
break;
}
}
}
default: break;
}
}
void SequencerPlay(uint16_t modenum)
{
if(modenum == 0)
{
seqclock = 0;
seqmem = 0x00010000;
return;
}
if(modenum == 3)
{
/* Read the time-stamp from SRAM and compare to seqclock*/
seqtime = readSramWord(seqmem);
while(seqtime == seqclock)
{
/* Move pointer to note data */
seqmem++;
seqmem++;
/* Read the note from SRAM */
seqnote = readSram(seqmem);
seqmsg = seqnote & 0x80;
seqnote = seqnote & 0x7F;
seqmem++;
/* Read the velocity from SRAM */
seqvelocity = readSram(seqmem);
seqmem++;
if(seqvelocity == 0xFF)
{
/* End of sequence reached, reset the position to the beginning and restart */
seqclock = 0;
seqmem = 0x00010000;
return;
}
if(seqmsg)
{
/* Play the note */
vasynth.NoteOn(seqnote, seqvelocity);
}
else
{
/* Stop the note */
vasynth.NoteOff(seqnote);
}
/* Check to see if there are other events at the current sequencer clock */
seqtime = readSramWord(seqmem);
}
}
/* All events processed so increment the sequencer clock */
seqclock++;
}
void SequencerRecord(uint8_t recnote, uint8_t recvelocity)
{
/* Write the timestamp to SRAM */
writeSramWord(seqmem, seqclock);
seqmem++;
seqmem++;
/* Write the note to SRAM */
writeSram(seqmem, recnote);
seqmem++;
/* Write the velocity to SRAM */
writeSram(seqmem, recvelocity);
seqmem++;
if(seqmem > 0x7FFF8)
{
/* There is no more memory left for the sequencer to use */
/* Place a timestamp and stop marker at the current position and select stop mode */
writeSramWord(seqmem, seqclock);
seqmem++;
seqmem++;
writeSramWord(seqmem, 0xFFFF);
seqmode = 0;
seqclock = 0;
seqmem = 0x00010000;
}
}
/* SRAM memory handling routines */
void writeSram(uint32_t l_addr, uint8_t l_data)
{
/* Pointer write on specific location of backup SRAM */
*(volatile uint8_t *) (WRITE_READ_SRAM_ADDR + l_addr) = l_data;
}
uint8_t readSram(uint32_t l_addr)
{
uint8_t i_retval;
/* Pointer write from specific location of backup SRAM */
i_retval = *(volatile uint8_t *) (WRITE_READ_SRAM_ADDR + l_addr);
return i_retval;
}
void writeSramWord(uint32_t l_addr_w, uint16_t l_data_w)
{
/* Pointer write on specific location of backup SRAM */
*(volatile uint16_t *) (WRITE_READ_SRAM_ADDR + l_addr_w) = l_data_w;
}
uint16_t readSramWord(uint32_t l_addr_w)
{
uint16_t i_retval_w;
/* Pointer write from specific location of backup SRAM */
i_retval_w = *(volatile uint16_t *) (WRITE_READ_SRAM_ADDR + l_addr_w);
return i_retval_w;
}
/* Process sequencer requests */
void Callback(void* data)
{
SequencerPlay(seqmode);
}
void SeqTimer_Config(void)
{
/* Create Timer Handle and Config */
TimerHandle tim5;
TimerHandle::Config tim_cfg;
/** TIM5 with IRQ enabled */
tim_cfg.periph = TimerHandle::Config::Peripheral::TIM_5;
tim_cfg.enable_irq = true;
/** Configure frequency (240Hz) */
auto tim_target_freq = 240;
auto tim_base_freq = System::GetPClk2Freq();
tim_cfg.period = tim_base_freq / tim_target_freq;
/** Initialize timer */
tim5.Init(tim_cfg);
tim5.SetCallback(Callback);
/** Start the timer, and generate callbacks at the end of each period */
tim5.Start();
}
int main(void)
{
// init hardware
hardware.Init(true); // true = boost to 480MHz
hardware.SetAudioBlockSize(1);
sysSampleRate = hardware.AudioSampleRate();
sysCallbackRate = hardware.AudioCallbackRate();
// setup vasynth initial values
vasynth.Init();
// load the default patch
vasynth.First(0);
// Initialize USB Midi
MidiUsbHandler::Config midi_cfg;
midi_cfg.transport_config.periph = MidiUsbTransport::Config::INTERNAL;
midi.Init(midi_cfg);
// let everything settle
System::Delay(100);
// Stereo simulator
delay_.Init();
delay_.SetDelay(sysSampleRate * 0.01f);
// Start the sequencer clock
SeqTimer_Config();
// Start calling the audio callback
hardware.StartAudio(AudioCallback);
// Loop forever
for(;;)
{
// handle MIDI Events
midi.Listen();
while(midi.HasEvents())
{
HandleMidiMessage(midi.PopEvent());
}
}
}