-
Notifications
You must be signed in to change notification settings - Fork 20
/
esp32_drum_computer.ino
294 lines (238 loc) · 6.34 KB
/
esp32_drum_computer.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
/*
* Copyright (c) 2022 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 27.03.2021
*
* @brief This file is the main project file which can be opened and compiled with arduino
*
* The project is shown in this video: @see https://youtu.be/vvA7vfouk84
*/
#ifdef __CDT_PARSER__
#include <cdt.h>
#endif
/*
* global project configuration
* stays on top of multi-file-compilation
*/
#include "config.h"
#include <Arduino.h>
//#include <WiFi.h>
//#define SPI_DISP_ENABLED
/* requires the ML_SynthTools library: https://github.com/marcel-licence/ML_SynthTools */
#include <ml_reverb.h>
#ifdef OLED_OSC_DISP_ENABLED
#include <ml_scope.h>
#endif
#include <ml_types.h>
#define ML_SYNTH_INLINE_DECLARATION
#include <ml_inline.h>
#undef ML_SYNTH_INLINE_DECLARATION
void setup()
{
// put your setup code here, to run once:
delay(500);
Serial.begin(115200);
Serial.println();
Serial.printf("Loading data\n");
Serial.printf("Firmware started successfully\n");
#ifdef BLINK_LED_PIN
Blink_Setup();
#endif
Audio_Setup();
Sampler_Init();
Effect_Init();
Sequencer_Init();
/*
* setup midi module / rx port
*/
Midi_Setup();
/*
* Initialize reverb
* The buffer shall be static to ensure that
* the memory will be exclusive available for the reverb module
*/
//static float revBuffer[REV_BUFF_SIZE];
static float *revBuffer = (float *)malloc(sizeof(float) * REV_BUFF_SIZE);
Reverb_Setup(revBuffer);
#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());
Serial.printf("Total heap: %d\n", ESP.getHeapSize());
Serial.printf("Free heap: %d\n", ESP.getFreeHeap());
/* 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
#ifdef OLED_OSC_DISP_ENABLED
Core0TaskInit();
#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);
}
inline
void Core0TaskSetup()
{
/*
* init your stuff for core0 here
*/
#ifdef OLED_OSC_DISP_ENABLED
ScopeOled_Setup();
#endif
}
void Core0TaskLoop()
{
/*
* put your loop stuff for core0 here
*/
#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 */
static float fl_sample[SAMPLE_BUFFER_SIZE];
static float fr_sample[SAMPLE_BUFFER_SIZE];
inline void audio_task()
{
#ifdef AUDIO_PASS_THROUGH
memset(fl_sample, 0, sizeof(fl_sample));
memset(fr_sample, 0, sizeof(fr_sample));
#ifdef ESP32_AUDIO_KIT
Audio_Input(fl_sample, fr_sample);
#endif
#else
memset(fl_sample, 0, sizeof(fl_sample));
memset(fr_sample, 0, sizeof(fr_sample));
#endif
/* prepare out samples for processing */
for (int n = 0; n < SAMPLE_BUFFER_SIZE; n++)
{
Sampler_Process(&fl_sample[n], &fr_sample[n]);
Effect_Process(&fl_sample[n], &fr_sample[n]);
Sequencer_Process(&fl_sample[n], &fr_sample[n]);
}
Reverb_Process(fl_sample, SAMPLE_BUFFER_SIZE);
memcpy(fr_sample, fl_sample, sizeof(fr_sample));
Audio_Output(fl_sample, fr_sample);
#ifdef OLED_OSC_DISP_ENABLED
ScopeOled_AddSamples(fl_sample, fr_sample, SAMPLE_BUFFER_SIZE);
#endif
}
inline
void loop_1Hz(void)
{
#ifdef BLINK_LED_PIN
Blink_Process();
#endif
}
void loop()
{
static uint32_t loop_cnt;
loop_cnt += SAMPLE_BUFFER_SIZE;
if (loop_cnt >= SAMPLE_RATE)
{
loop_cnt = 0;
loop_1Hz();
}
Midi_Process();
audio_task();
}
void Synth_SetSlider(uint8_t id, float value)
{
switch (id)
{
case 0:
Sampler_SetPlaybackSpeed(value);
break;
case 1:
Effect_SetBiCutoff(value);
break;
case 2:
Effect_SetBiReso(value);
break;
case 3:
Effect_SetBitCrusher(value);
break;
case 4:
Sampler_SetSoundPitch(value);
break;
default:
break;
}
}
void Synth_SetRotary(uint8_t id, float value)
{
switch (id)
{
case 0:
Sequencer_SetSpeed(value);
break;
case 1:
Sequencer_SetShuffle(value);
break;
default:
break;
}
}
void Synth_NoteOn(uint8_t ch, uint8_t note, float vol)
{
float volF = vol;
volF *= 127;
Sequencer_NoteOn(note, volF);
Sampler_SelectNote(note);
}
void Synth_NoteOff(uint8_t ch, uint8_t note)
{
Sequencer_NoteOff(note);
}