-
Notifications
You must be signed in to change notification settings - Fork 4
/
ml_synth_multi_saw_example.ino
608 lines (497 loc) · 12.4 KB
/
ml_synth_multi_saw_example.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
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
/*
* Copyright (c) 2024 Marcel Licence
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Dieses Programm ist Freie Software: Sie können es unter den Bedingungen
* der GNU General Public License, wie von der Free Software Foundation,
* Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren
* veröffentlichten Version, weiter verteilen und/oder modifizieren.
*
* Dieses Programm wird in der Hoffnung bereitgestellt, dass es nützlich sein wird, jedoch
* OHNE JEDE GEWÄHR,; sogar ohne die implizite
* Gewähr der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
* Siehe die GNU General Public License für weitere Einzelheiten.
*
* Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
* Programm erhalten haben. Wenn nicht, siehe <https://www.gnu.org/licenses/>.
*/
/**
* @file ml_synth_multi_saw_example.ino
* @author Marcel Licence
* @date 26.11.2021
*
* @brief This is the main project file to test the ML_SynthLibrary (multi saw module)
* It should be compatible with the Raspberry Pi Pico and all other RP2040 platforms
*
* @see https://youtu.be/kcf597op8o4
*/
#ifdef __CDT_PARSER__
#include <cdt.h>
#endif
#include "config.h"
#include <Arduino.h>
#include <Wire.h>
/*
* Library can be found on https://github.com/marcel-licence/ML_SynthTools
*/
#include <ml_types.h>
#include <ml_alg.h>
#include <ml_waveform.h>
#include <ml_filter.h>
#include <ml_multi_saw.h>
#include <ml_slicer.h>
#ifdef ARP_MODULE_ENABLED
#include <ml_arp.h>
#endif
#ifdef REVERB_ENABLED
#include <ml_reverb.h>
#endif
#ifdef MAX_DELAY_Q
#include <ml_delay_q.h>
#endif
#ifdef OLED_OSC_DISP_ENABLED
#include <ml_scope.h>
#endif
/* centralized modules */
#define ML_SYNTH_INLINE_DECLARATION
#include <ml_inline.h>
#undef ML_SYNTH_INLINE_DECLARATION
#if (defined ARDUINO_GENERIC_F407VGTX) || (defined ARDUINO_DISCO_F407VG)
#include <Wire.h> /* todo remove, just for scanning */
#endif
char shortName[] = "ML_MultiSaw";
float static_sine[WAVEFORM_CNT];
float *sine = static_sine;
struct filterCoeffQ16T mainFiltC;
struct filterProcQ16T mainFiltL;
struct filterProcQ16T mainFiltR;
void setup()
{
for (int i = 0; i < WAVEFORM_CNT; i++)
{
float val = (float)sin(i * 2.0 * PI / WAVEFORM_CNT);
sine[i] = val;
}
Filter_Init(&mainFiltL, &mainFiltC);
Filter_Init(&mainFiltR, &mainFiltC);
/*
* this code runs once
*/
#ifdef MIDI_USB_ENABLED
Midi_Usb_Setup();
#endif
#ifdef BLINK_LED_PIN
Blink_Setup();
Blink_Fast(3);
#endif
#ifdef ARDUINO_DAISY_SEED
DaisySeed_Setup();
#endif
delay(2500);
#ifdef SWAP_SERIAL
/* only one hw serial use this for ESP */
Serial.begin(115200);
delay(500);
#else
Serial.begin(SERIAL_BAUDRATE);
#endif
Serial.println();
Serial.printf("Loading data\n");
Serial.printf("Multi Saw Synth Example\n");
#ifdef ESP8266
Midi_Setup();
#endif
Serial.printf("Initialize Audio Interface\n");
Audio_Setup();
#ifdef TEENSYDUINO
Teensy_Setup();
#else
#ifdef ARDUINO_SEEED_XIAO_M0
pinMode(7, INPUT_PULLUP);
Midi_Setup();
pinMode(LED_BUILTIN, OUTPUT);
#else
#ifndef ESP8266 /* otherwise it will break audio output */
Midi_Setup();
#endif
#endif
#endif
#ifdef REVERB_ENABLED
/*
* Initialize reverb
* The buffer shall be static to ensure that
* the memory will be exclusive available for the reverb module
*/
static float *revBuffer = (float *)malloc(sizeof(float) * REV_BUFF_SIZE);
Reverb_Setup(revBuffer);
#endif
#ifdef MAX_DELAY_Q
/*
* Prepare a buffer which can be used for the delay
*/
#ifdef ESP32
static int16_t *delBuffer1 = (int16_t *)malloc(sizeof(int16_t) * MAX_DELAY);
static int16_t *delBuffer2 = (int16_t *)malloc(sizeof(int16_t) * MAX_DELAY);
#else
static int16_t delBuffer1[MAX_DELAY];
static int16_t delBuffer2[MAX_DELAY];
#endif
DelayQ_Init2(delBuffer1, delBuffer2, MAX_DELAY_Q);
#endif
#ifdef MIDI_BLE_ENABLED
midi_ble_setup();
#endif
#ifdef USB_HOST_ENABLED
Usb_Host_Midi_setup();
#endif
#ifdef ESP32
Serial.printf("ESP.getFreeHeap() %d\n", ESP.getFreeHeap());
Serial.printf("ESP.getMinFreeHeap() %d\n", ESP.getMinFreeHeap());
Serial.printf("ESP.getHeapSize() %d\n", ESP.getHeapSize());
Serial.printf("ESP.getMaxAllocHeap() %d\n", ESP.getMaxAllocHeap());
/* PSRAM will be fully used by the looper */
Serial.printf("Total PSRAM: %d\n", ESP.getPsramSize());
Serial.printf("Free PSRAM: %d\n", ESP.getFreePsram());
#endif
#if (defined OLED_OSC_DISP_ENABLED) && (defined TEENSYDUINO)
ScopeOled_Setup();
#endif
#ifdef USB_HOST_ENABLED
Usb_Host_Midi_setup();
#endif
#ifdef ARP_MODULE_ENABLED
Arp_Init(1750); /* some quick tempo */
Arp_SelectSequence(1, 1);
Arp_Active();
#endif
Slicer_SetType(0, 0);
Slicer_SetDecay(0, 127);
Slicer_SetDepth(0, 0);
Serial.printf("Wait for multi saw module to be initialized\n");
MultiSawSynth_Init(SAMPLE_RATE);
Serial.printf("Multi saw module ready\n");
MultiSawSynth_SetDetune(32);
MultiSawSynth_SetCount(127);
#ifdef NOTE_ON_AFTER_SETUP
App_NoteOn(0, 64, 127);
#endif
#ifdef MIDI_STREAM_PLAYER_ENABLED
MidiStreamPlayer_Init();
char midiFile[] = "/song.mid";
MidiStreamPlayer_PlayMidiFile_fromLittleFS(midiFile, 1);
#endif
#if (defined MIDI_VIA_USB_ENABLED) || (defined OLED_OSC_DISP_ENABLED)
#ifdef ESP32
Core0TaskInit();
#else
//#error only supported by ESP32 platform
#endif
#endif
#ifdef ADC_ENABLED
AdcSetup();
#endif
}
#ifdef ESP32
/*
* Core 0
*/
/* this is used to add a task to core 0 */
TaskHandle_t Core0TaskHnd;
inline
void Core0TaskInit()
{
/* we need a second task for the terminal output */
xTaskCreatePinnedToCore(Core0Task, "CoreTask0", 8000, NULL, 999, &Core0TaskHnd, 0);
}
void Core0TaskSetup()
{
/*
* init your stuff for core0 here
*/
#ifdef OLED_OSC_DISP_ENABLED
ScopeOled_Setup();
#endif
#ifdef MIDI_VIA_USB_ENABLED
UsbMidi_Setup();
#endif
}
void Core0TaskLoop()
{
/*
* put your loop stuff for core0 here
*/
#ifdef MIDI_VIA_USB_ENABLED
UsbMidi_Loop();
#endif
#ifdef OLED_OSC_DISP_ENABLED
ScopeOled_Process();
#endif
}
void Core0Task(void *parameter)
{
Core0TaskSetup();
while (true)
{
Core0TaskLoop();
/* this seems necessary to trigger the watchdog */
delay(1);
yield();
}
}
#endif /* ESP32 */
void loop_1Hz()
{
#ifdef CYCLE_MODULE_ENABLED
CyclePrint();
#endif
#ifdef BLINK_LED_PIN
Blink_Process();
#endif
}
void loop()
{
static int loop_cnt_1hz = 0; /*!< counter to allow 1Hz loop cycle */
#ifdef SAMPLE_BUFFER_SIZE
loop_cnt_1hz += SAMPLE_BUFFER_SIZE;
#else
loop_cnt_1hz += 1; /* in case only one sample will be processed per loop cycle */
#endif
if (loop_cnt_1hz >= SAMPLE_RATE)
{
loop_cnt_1hz -= SAMPLE_RATE;
loop_1Hz();
}
#ifdef ADC_ENABLED
AdcCheck();
#endif
#ifdef ARP_MODULE_ENABLED
Arp_Process(1);
#endif
/*
* MIDI processing
*/
Midi_Process();
#ifdef MIDI_VIA_USB_ENABLED
UsbMidi_ProcessSync();
#endif
#ifdef MIDI_STREAM_PLAYER_ENABLED
MidiStreamPlayer_Tick(SAMPLE_BUFFER_SIZE);
#endif
#ifdef MIDI_BLE_ENABLED
midi_ble_loop();
#endif
#ifdef USB_HOST_ENABLED
Usb_Host_Midi_loop();
#endif
#ifdef MIDI_USB_ENABLED
Midi_Usb_Loop();
#endif
/*
* And finally the audio stuff
*/
Q1_14 left[SAMPLE_BUFFER_SIZE];
Q1_14 right[SAMPLE_BUFFER_SIZE];
memset(left, 0, sizeof(left));
memset(right, 0, sizeof(left));
MultiSawSynth_Process(left, right, SAMPLE_BUFFER_SIZE);
Filter_Process_Buffer(left, &mainFiltL, SAMPLE_BUFFER_SIZE);
Filter_Process_Buffer(right, &mainFiltR, SAMPLE_BUFFER_SIZE);
Slicer_Process(left, right, SAMPLE_BUFFER_SIZE);
#ifdef MAX_DELAY_Q
DelayQ_Process_Buff(&left[0].s16, &right[0].s16, &left[0].s16, &right[0].s16, SAMPLE_BUFFER_SIZE);
#endif
/*
* Output the audio
*/
Audio_Output(left, right);
}
void App_NoteOn(uint8_t ch, uint8_t note, uint8_t vel)
{
#ifdef ARP_MODULE_ENABLED
Arp_NoteOn(ch, note, vel);
#else
MultiSawSynth_NoteOn(ch, note, vel);
#endif
}
void App_NoteOff(uint8_t ch, uint8_t note)
{
#ifdef ARP_MODULE_ENABLED
Arp_NoteOff(ch, note);
#else
MultiSawSynth_NoteOff(ch, note);
#endif
}
void App_NoteSetPitch(uint8_t ch, uint8_t note, uint32_t pitch)
{
MultiSawSynth_NoteSetPitch(ch, note, pitch);
}
void App_NoteSetVolume(uint8_t ch, uint8_t note, uint16_t volume)
{
MultiSawSynth_NoteSetVolume(ch, note, volume);
}
void App_PitchBend(uint8_t ch, uint16_t bend)
{
MultiSawSynth_PitchBend(ch, bend);
}
void App_ModulationWheel(uint8_t ch, uint8_t value)
{
/* not implemented yet */
}
/*
* MIDI via USB Host Module
*/
#ifdef MIDI_VIA_USB_ENABLED
void App_UsbMidiShortMsgReceived(uint8_t *msg)
{
#ifdef MIDI_TX2_PIN
Midi_SendShortMessage(msg);
#endif
Midi_HandleShortMsg(msg, 8);
}
#endif
/*
* MIDI callbacks
*/
inline void App_ButtonA(uint8_t setting, uint8_t value)
{
if (value > 0)
{
MultiSawSynth_SetProfile(setting);
}
}
inline void App_ButtonCh(uint8_t setting, uint8_t value)
{
if (value > 0)
{
MultiSawSynth_SetCurrentCh(setting);
}
}
inline void App_ButtonSlicer(uint8_t setting, uint8_t value)
{
if (value > 0)
{
Slicer_SetType(0, setting);
}
}
inline void App_SliderSawCtrl(uint8_t id, uint8_t value)
{
switch (id)
{
case 0:
MultiSawSynth_SetDetune(value);
break;
case 1:
MultiSawSynth_SetCount(value);
break;
case 2:
MultiSawSynth_SetProfile(value);
break;
case 3:
{
if (value < 30)
{
value = 30;
}
if (value > 126)
{
value = 126;
}
float c = value;
c *= 1.0f / 127.0f;
Filter_Calculate(c, 0.5f, &mainFiltC);
}
break;
}
}
/*
* Arp Callbacks
*/
void Arp_Cb_NoteOn(uint8_t ch, uint8_t note, float vel)
{
MultiSawSynth_NoteOn(ch, note, vel);
}
void Arp_Cb_NoteOff(uint8_t ch, uint8_t note)
{
MultiSawSynth_NoteOff(ch, note);
}
void Arp_Status_ValueChangedInt(const char *msg, int value)
{
// Status_ValueChangedInt(msg, value);
}
void Arp_Status_LogMessage(const char *msg)
{
//Status_LogMessage(msg);
}
void Arp_Status_ValueChangedFloat(const char *msg, float value)
{
// Status_ValueChangedFloat(msg, value);
}
void Arp_Cb_Step(uint8_t step)
{
/* ignore */
}
#if defined(I2C_SCL) && defined(I2C_SDA)
void ScanI2C(void)
{
#ifdef ARDUINO_GENERIC_F407VGTX
Wire.setSDA(I2C_SDA);
Wire.setSCL(I2C_SCL);
Wire.begin();//I2C_SDA, I2C_SCL);
#else
Wire.begin();
#endif
byte address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for (address = 1; address < 127; address++)
{
byte r_error;
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
r_error = Wire.endTransmission();
if (r_error == 0)
{
Serial.print("I2C device found at address 0x");
if (address < 16)
{
Serial.print("0");
}
Serial.print(address, HEX);
Serial.println(" !");
nDevices++;
}
else if (r_error == 4)
{
Serial.print("Unknown error at address 0x");
if (address < 16)
{
Serial.print("0");
}
Serial.println(address, HEX);
}
}
if (nDevices == 0)
{
Serial.println("No I2C devices found\n");
}
else
{
Serial.println("done\n");
}
}
#endif /* (defined ARDUINO_GENERIC_F407VGTX) || (defined ARDUINO_DISCO_F407VG) */