-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathml_synth_basic_example.ino
541 lines (448 loc) · 11.2 KB
/
ml_synth_basic_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
/*
* Copyright (c) 2023 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_basic_example.ino
* @author Marcel Licence
* @date 17.12.2021
*
* @brief This is the main project file
*
* The project is shown in this video: @see https://youtu.be/WJGOIgaY-1s
*/
#ifdef __CDT_PARSER__
#include <cdt.h>
#endif
#include "config.h"
#include <Arduino.h>
#ifdef ESP32
#include <Wire.h>
#endif
/* requires the ML_SynthTools library: https://github.com/marcel-licence/ML_SynthTools */
#include <ml_arp.h>
#ifdef REVERB_ENABLED
#include <ml_reverb.h>
#endif
#include <ml_delay.h>
#ifdef OLED_OSC_DISP_ENABLED
#include <ml_scope.h>
#endif
#include <ml_types.h>
//#include <ml_midi.h>
#define ML_SYNTH_INLINE_DECLARATION
#include <ml_inline.h>
#undef ML_SYNTH_INLINE_DECLARATION
char shortName[] = "ML_BasicSynth";
void setup()
{
/*
* this code runs once
*/
#ifdef BLINK_LED_PIN
Blink_Setup();
Blink_Fast(1);
#endif
#ifdef ARDUINO_DAISY_SEED
DaisySeed_Setup();
#endif
delay(1500);
Serial.begin(115200);
delay(1500);
Serial.printf("ml_synth_basic_example Copyright (C) 2024 Marcel Licence\n");
Serial.printf("This program comes with ABSOLUTELY NO WARRANTY;\n");
Serial.printf("This is free software, and you are welcome to redistribute it\n");
Serial.printf("under certain conditions; \n");
Serial.printf("Initialize Synth Module\n");
delay(1000);
Synth_Init();
#ifdef REVERB_ENABLED
/*
* Initialize reverb
* The buffer shall be static to ensure that
* the memory will be exclusive available for the reverb module
*/
#ifdef REVERB_STATIC
static float revBuffer[REV_BUFF_SIZE];
#else
static float *revBuffer = (float *)malloc(sizeof(float) * REV_BUFF_SIZE);
#endif
Reverb_Setup(revBuffer);
#endif
#ifdef MAX_DELAY
/*
* Prepare a buffer which can be used for the delay
*/
#ifdef DELAY_STATIC
static int16_t delBuffer1[MAX_DELAY];
static int16_t delBuffer2[MAX_DELAY];
#else
static int16_t *delBuffer1 = (int16_t *)malloc(sizeof(int16_t) * MAX_DELAY);
static int16_t *delBuffer2 = (int16_t *)malloc(sizeof(int16_t) * MAX_DELAY);
#endif
Delay_Init2(delBuffer1, delBuffer2, MAX_DELAY);
#endif
Serial.printf("Initialize Audio Interface\n");
#ifdef ML_BOARD_SETUP
Board_Setup();
#else
Audio_Setup();
Serial.printf("Initialize Midi Module\n");
/*
* setup midi module / rx port
*/
Midi_Setup();
#endif
Arp_Init(24 * 4); /* slowest tempo one step per bar */
#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());
#endif
Serial.printf("Firmware started successfully\n");
#ifdef MIDI_BLE_ENABLED
midi_ble_setup();
#endif
#ifdef MIDI_USB_ENABLED
Midi_Usb_Setup();
#endif
#ifdef NOTE_ON_AFTER_SETUP /* activate this line to get a tone on startup to test the DAC */
Synth_NoteOn(0, 64, 1.0f);
#endif
#if (defined ADC_TO_MIDI_ENABLED) || (defined MIDI_VIA_USB_ENABLED) || (defined OLED_OSC_DISP_ENABLED)
#ifdef ESP32
Core0TaskInit();
#else
#error only supported by ESP32 platform
#endif
#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 _ADC_TO_MIDI_ENABLED
AdcMul_Init();
#endif
#ifdef MIDI_VIA_USB_ENABLED
UsbMidi_Setup();
#endif
#ifdef PRESSURE_SENSOR_ENABLED
PressureSetup();
#endif
}
void Core0TaskLoop()
{
/*
* put your loop stuff for core0 here
*/
#ifdef _ADC_TO_MIDI_ENABLED
#ifdef MIDI_VIA_USB_ENABLED
static uint8_t adc_prescaler = 0;
adc_prescaler++;
if (adc_prescaler > 15) /* use prescaler when USB is active because it is very time consuming */
#endif /* MIDI_VIA_USB_ENABLED */
{
adc_prescaler = 0;
AdcMul_Process();
}
#endif /* ADC_TO_MIDI_ENABLED */
#ifdef MIDI_VIA_USB_ENABLED
UsbMidi_Loop();
#endif
#ifdef _MCP23_MODULE_ENABLED
MCP23_Loop();
#endif
#ifdef OLED_OSC_DISP_ENABLED
ScopeOled_Process();
#endif
#ifdef PRESSURE_SENSOR_ENABLED
PressureLoop();
#endif
}
void Core0Task(void *parameter)
{
Core0TaskSetup();
while (true)
{
Core0TaskLoop();
/* this seems necessary to trigger the watchdog */
delay(1);
yield();
}
}
#endif /* ESP32 */
static uint32_t midi_sync = 0;
void Midi_SyncRecvd(void)
{
midi_sync += 1;
}
void Synth_RealTimeMsg(uint8_t msg)
{
#ifndef MIDI_SYNC_MASTER
switch (msg)
{
case 0xfa: /* start */
Arp_Reset();
break;
case 0xf8: /* Timing Clock */
Midi_SyncRecvd();
break;
}
#endif
}
#ifdef MIDI_SYNC_MASTER
#define MIDI_PPQ 24
#define SAMPLES_PER_MIN (SAMPLE_RATE*60)
static float midi_tempo = 120.0f;
void MidiSyncMasterLoop(void)
{
static float midiDiv = 0;
midiDiv += SAMPLE_BUFFER_SIZE;
if (midiDiv >= (SAMPLES_PER_MIN) / (MIDI_PPQ * midi_tempo))
{
midiDiv -= (SAMPLES_PER_MIN) / (MIDI_PPQ * midi_tempo);
Midi_SyncRecvd();
}
}
void Synth_SetMidiMasterTempo(uint8_t unused __attribute__((unused)), float val)
{
midi_tempo = 60.0f + val * (240.0f - 60.0f);
}
#endif
void Synth_SongPosition(uint16_t pos)
{
Serial.printf("Songpos: %d\n", pos);
if (pos == 0)
{
Arp_Reset();
}
}
void Synth_SongPosReset(uint8_t unused __attribute__((unused)), float var)
{
if (var > 0)
{
Synth_SongPosition(0);
}
}
/*
* use this if something should happen every second
* - you can drive a blinking LED for example
*/
inline void Loop_1Hz(void)
{
#ifdef BLINK_LED_PIN
Blink_Process();
#endif
}
/*
* our main loop
* - all is done in a blocking context
* - do not block the loop otherwise you will get problems with your audio
*/
static float left[SAMPLE_BUFFER_SIZE];
static float right[SAMPLE_BUFFER_SIZE];
#ifdef REVERB_ENABLED
static float mono[SAMPLE_BUFFER_SIZE];
#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_1Hz();
loop_cnt_1hz = 0;
}
#ifdef MIDI_SYNC_MASTER
MidiSyncMasterLoop();
#endif
#ifdef ARP_MODULE_ENABLED
Arp_Process(midi_sync);
midi_sync = 0;
#endif
/*
* MIDI processing
*/
Midi_Process();
#ifdef MIDI_VIA_USB_ENABLED
UsbMidi_ProcessSync();
#endif
#ifdef MIDI_BLE_ENABLED
midi_ble_loop();
#endif
#ifdef MIDI_USB_ENABLED
Midi_Usb_Loop();
#endif
/* zero buffer, otherwise you can pass trough an input signal */
memset(left, 0, sizeof(left));
memset(right, 0, sizeof(right));
#ifdef AUDIO_PASS_THROUGH
Audio_Input(left, right);
#endif
/*
* Process synthesizer core
*/
Synth_Process(left, right, SAMPLE_BUFFER_SIZE);
#ifdef MAX_DELAY
/*
* process delay line
*/
Delay_Process_Buff2(left, right, SAMPLE_BUFFER_SIZE);
#endif
/*
* add some mono reverb
*/
#ifdef REVERB_ENABLED
for (int i = 0; i < SAMPLE_BUFFER_SIZE; i++)
{
mono[i] = 0.5f * (left[i] + right[i]);
}
Reverb_Process(mono, SAMPLE_BUFFER_SIZE);
for (int i = 0; i < SAMPLE_BUFFER_SIZE; i++)
{
left[i] += mono[i];
right[i] += mono[i];
}
#endif
/*
* Output the audio
*/
Audio_Output(left, right);
#ifdef OLED_OSC_DISP_ENABLED
ScopeOled_AddSamples(left, right, SAMPLE_BUFFER_SIZE);
#endif
}
/*
* Callbacks
*/
void Arp_Cb_NoteOn(uint8_t ch, uint8_t note, float vel)
{
Synth_NoteOn(ch, note, vel);
}
void Arp_Cb_NoteOff(uint8_t ch, uint8_t note)
{
Synth_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 */
}
/*
* MIDI via USB Host Module
*/
#ifdef MIDI_VIA_USB_ENABLED
void App_UsbMidiShortMsgReceived(uint8_t *msg)
{
Midi_SendShortMessage(msg);
Midi_HandleShortMsg(msg, 8);
}
#endif
/*
* Test functions
*/
#if defined(I2C_SCL) && defined (I2C_SDA) && (!defined ARDUINO_DISCO_F407VG)
void ScanI2C(void)
{
Wire.begin(I2C_SDA, I2C_SCL);
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