forked from corrados/edrumulus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edrumulus.ino
442 lines (395 loc) · 18.5 KB
/
edrumulus.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
/******************************************************************************\
* Copyright (c) 2020-2022
* Author(s): Volker Fischer
******************************************************************************
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\******************************************************************************/
#define USE_MIDI
#include "edrumulus.h"
#ifdef USE_MIDI
# ifdef ESP_PLATFORM
# include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
# define MYMIDI MIDI
# define MIDI_CONTROL_CHANGE_TYPE midi::ControlChange
# define MIDI_SEND_AFTER_TOUCH sendAfterTouch
# define MIDI_SERIAL 38400
# endif
# ifdef TEENSYDUINO
# define MYMIDI usbMIDI
# define MIDI_CONTROL_CHANGE_TYPE usbMIDI.ControlChange
# define MIDI_SEND_AFTER_TOUCH sendAfterTouchPoly
# endif
#endif
Edrumulus edrumulus;
#ifdef ESP_PLATFORM
const int number_pads = 9; // ESP32
#else
const int number_pads = 8; // Teensy 4.0
#endif
const int status_LED_pin = BOARD_LED_PIN; // internal LED used for overload indicator
const int midi_channel = 10; // default for edrums is 10
const int hihat_pad_idx = 2;
const int hihatctrl_pad_idx = 3;
bool is_status_LED_on = false; // initialization value
int selected_pad = 0; // initialization value
void setup()
{
#ifdef USE_MIDI
MYMIDI.begin();
#endif
#ifdef MIDI_SERIAL
Serial.begin ( MIDI_SERIAL );
#else
Serial.begin ( 115200 );
#endif
#ifdef ESP_PLATFORM
// NOTE: avoid ESP32 GPIO 25/26 for piezo inputs since they are DAC pins which cause an incorrect DC offset
// estimation and DC offset drift which makes the spike cancellation algorithm not working correctly
// analog pins setup: snare | kick | hi-hat | hi-hat-ctrl | crash | tom1 | ride | tom2 | tom3
const int analog_pins[] = { 36, 33, 32, 25, 34, 39, 27, 12, 15 };
const int analog_pins_rimshot[] = { 35, -1, 26, -1, 14, -1, 13, -1, -1 };
#endif
#ifdef TEENSYDUINO
// analog pins setup: snare | kick | hi-hat | hi-hat-ctrl | crash | tom1 | ride | tom2 | tom3
const int analog_pins[] = { 10, 11, 12, 13, 1, 6, 4, 5, 17 };
const int analog_pins_rimshot[] = { 9, -1, 0, -1, 3, 8, 2, 7, 18 };
#endif
edrumulus.setup ( number_pads, analog_pins, analog_pins_rimshot );
read_settings();
// initialize GPIO port for status LED
pinMode ( status_LED_pin, OUTPUT );
}
void preset_settings()
{
// default MIDI note assignments
edrumulus.set_midi_notes ( 0, 38, 40 ); // snare
edrumulus.set_midi_notes ( 1, 36, 36 ); // kick
edrumulus.set_midi_notes ( hihat_pad_idx, 22 /*42*/, 22 );
edrumulus.set_midi_notes_open ( hihat_pad_idx, 26 /*46*/, 26 );
edrumulus.set_midi_notes ( hihatctrl_pad_idx, 44, 44 ); // Hi-Hat pedal hit
edrumulus.set_midi_notes ( 4, 49, 55 ); // crash
edrumulus.set_midi_notes ( 5, 48, 50 ); // tom 1
edrumulus.set_midi_notes ( 6, 51, 53 /*59*/ ); // ride (edge: 59, bell: 53)
edrumulus.set_midi_notes ( 7, 45, 47 ); // tom 2
edrumulus.set_midi_notes ( 8, 43, 58 ); // tom 3
// default drum kit setup
edrumulus.set_pad_type ( 0, Edrumulus::PD80R ); // snare
edrumulus.set_rim_shot_is_used ( 0, true );
edrumulus.set_pos_sense_is_used ( 0, true );
edrumulus.set_pad_type ( 1, Edrumulus::KD7 ); // kick
edrumulus.set_curve ( 1, Edrumulus::LOG2 ); // less dynamic on kick (similar to other drum modules)
edrumulus.set_pad_type ( 2, Edrumulus::CY5 ); // Hi-Hat, using rim switch
edrumulus.set_rim_shot_is_used ( 2, true );
edrumulus.set_pad_type ( 3, Edrumulus::FD8 ); // Hi-Hat-ctrl
edrumulus.set_pad_type ( 4, Edrumulus::CY8 ); // crash, using rim switch
edrumulus.set_rim_shot_is_used ( 4, true );
edrumulus.set_pad_type ( 5, Edrumulus::PD8 ); // tom 1
edrumulus.set_pad_type ( 6, Edrumulus::PD8 ); // ride, using rim switch
edrumulus.set_rim_shot_is_used ( 6, true );
edrumulus.set_pad_type ( 7, Edrumulus::PD8 ); // tom 2
edrumulus.set_pad_type ( 8, Edrumulus::PD8 ); // tom 3
}
void loop()
{
// this function is blocking at the system sampling rate
edrumulus.process();
// status LED handling
if ( edrumulus.get_status_is_overload() || edrumulus.get_status_is_error() )
{
if ( !is_status_LED_on )
{
digitalWrite ( status_LED_pin, HIGH );
is_status_LED_on = true;
}
}
else
{
if ( is_status_LED_on )
{
digitalWrite ( status_LED_pin, LOW );
is_status_LED_on = false;
}
}
#ifdef USE_MIDI
// send MIDI note to drum synthesizer
for ( int pad_idx = 0; pad_idx < number_pads; pad_idx++ )
{
if ( edrumulus.get_peak_found ( pad_idx ) )
{
// get current MIDI note and velocity (maybe note will be overwritten later on)
const int midi_velocity = edrumulus.get_midi_velocity ( pad_idx );
int midi_note = edrumulus.get_midi_note ( pad_idx );
// send midi positional control message if positional sensing is enabled for the current pad
if ( edrumulus.get_pos_sense_is_used ( pad_idx ) )
{
const int midi_pos = edrumulus.get_midi_pos ( pad_idx );
MYMIDI.sendControlChange ( 16, midi_pos, midi_channel ); // positional sensing
}
// send Hi-Hat control message right before each Hi-Hat pad hit
if ( pad_idx == hihat_pad_idx )
{
const int midi_ctrl_ch = edrumulus.get_midi_ctrl_ch ( hihatctrl_pad_idx );
const int midi_ctrl_value = edrumulus.get_midi_ctrl_value ( hihatctrl_pad_idx );
const bool hi_hat_is_open = edrumulus.get_midi_ctrl_is_open ( hihatctrl_pad_idx );
MYMIDI.sendControlChange ( midi_ctrl_ch, midi_ctrl_value, midi_channel );
// if Hi-Hat is open, overwrite MIDI note
if ( hi_hat_is_open )
{
midi_note = edrumulus.get_midi_note_open ( pad_idx );
}
}
MYMIDI.sendNoteOn ( midi_note, midi_velocity, midi_channel ); // (note, velocity, channel)
MYMIDI.sendNoteOff ( midi_note, 0, midi_channel ); // we need a note off
}
if ( edrumulus.get_control_found ( pad_idx ) )
{
const int midi_ctrl_ch = edrumulus.get_midi_ctrl_ch ( pad_idx );
const int midi_ctrl_value = edrumulus.get_midi_ctrl_value ( pad_idx );
MYMIDI.sendControlChange ( midi_ctrl_ch, midi_ctrl_value, midi_channel );
}
if ( edrumulus.get_choke_on_found ( pad_idx ) )
{
// if grabbed edge found, polyphonic aftertouch at 127 is transmitted for all notes of the pad
MYMIDI.MIDI_SEND_AFTER_TOUCH ( edrumulus.get_midi_note_norm ( pad_idx ), 127, midi_channel );
MYMIDI.MIDI_SEND_AFTER_TOUCH ( edrumulus.get_midi_note_rim ( pad_idx ), 127, midi_channel );
if ( pad_idx == hihat_pad_idx )
{
MYMIDI.MIDI_SEND_AFTER_TOUCH ( edrumulus.get_midi_note_open_norm ( pad_idx ), 127, midi_channel );
MYMIDI.MIDI_SEND_AFTER_TOUCH ( edrumulus.get_midi_note_open_rim ( pad_idx ), 127, midi_channel );
}
}
else if ( edrumulus.get_choke_off_found ( pad_idx ) )
{
// if released edge found, polyphonic aftertouch at 0 is transmitted for all notes of the pad
MYMIDI.MIDI_SEND_AFTER_TOUCH ( edrumulus.get_midi_note_norm ( pad_idx ), 0, midi_channel );
MYMIDI.MIDI_SEND_AFTER_TOUCH ( edrumulus.get_midi_note_rim ( pad_idx ), 0, midi_channel );
if ( pad_idx == hihat_pad_idx )
{
MYMIDI.MIDI_SEND_AFTER_TOUCH ( edrumulus.get_midi_note_open_norm ( pad_idx ), 0, midi_channel );
MYMIDI.MIDI_SEND_AFTER_TOUCH ( edrumulus.get_midi_note_open_rim ( pad_idx ), 0, midi_channel );
}
}
}
// receiving MIDI messages to change the pad settings: edrumuluscontrol.m -> loopMIDI -> Hairless MIDI
if ( MYMIDI.read ( midi_channel ) )
{
if ( MYMIDI.getType() == MIDI_CONTROL_CHANGE_TYPE )
{
bool is_used = false;
const int controller = MYMIDI.getData1();
const int value = MYMIDI.getData2();
// controller 102: pad type
if ( controller == 102 )
{
edrumulus.set_pad_type ( selected_pad, static_cast<Edrumulus::Epadtype> ( value ) );
edrumulus.write_setting ( selected_pad, 0, value );
is_used = true;
}
// controller 103: threshold
if ( controller == 103 )
{
edrumulus.set_velocity_threshold ( selected_pad, value );
edrumulus.write_setting ( selected_pad, 1, value );
is_used = true;
}
// controller 104: sensitivity
if ( controller == 104 )
{
edrumulus.set_velocity_sensitivity ( selected_pad, value );
edrumulus.write_setting ( selected_pad, 2, value );
is_used = true;
}
// controller 105: positional sensing threshold
if ( controller == 105 )
{
edrumulus.set_pos_threshold ( selected_pad, value );
edrumulus.write_setting ( selected_pad, 3, value );
is_used = true;
}
// controller 106: positional sensing sensitivity
if ( controller == 106 )
{
edrumulus.set_pos_sensitivity ( selected_pad, value );
edrumulus.write_setting ( selected_pad, 4, value );
is_used = true;
}
// controller 107: rim shot threshold
if ( controller == 107 )
{
edrumulus.set_rim_shot_treshold ( selected_pad, value );
edrumulus.write_setting ( selected_pad, 5, value );
is_used = true;
}
// controller 108: select pad
if ( ( controller == 108 ) && ( value < MAX_NUM_PADS ) )
{
selected_pad = value;
is_used = true;
// on a pad selection, return all parameters of the selected pad
MYMIDI.sendNoteOff ( 102, static_cast<int> ( edrumulus.get_pad_type ( selected_pad ) ), 1 );
MYMIDI.sendNoteOff ( 103, edrumulus.get_velocity_threshold ( selected_pad ), 1 );
MYMIDI.sendNoteOff ( 104, edrumulus.get_velocity_sensitivity ( selected_pad ), 1 );
MYMIDI.sendNoteOff ( 105, edrumulus.get_pos_threshold ( selected_pad ), 1 );
MYMIDI.sendNoteOff ( 106, edrumulus.get_pos_sensitivity ( selected_pad ), 1 );
MYMIDI.sendNoteOff ( 107, edrumulus.get_rim_shot_treshold ( selected_pad ), 1 );
MYMIDI.sendNoteOff ( 108, selected_pad, 1 );
MYMIDI.sendNoteOff ( 109, static_cast<int> ( edrumulus.get_curve ( selected_pad ) ), 1 );
MYMIDI.sendNoteOff ( 110, edrumulus.get_spike_cancel_level(), 1 );
MYMIDI.sendNoteOff ( 111, edrumulus.get_rim_shot_is_used ( selected_pad ) + 2 * edrumulus.get_pos_sense_is_used ( selected_pad ), 1 );
MYMIDI.sendNoteOff ( 112, edrumulus.get_midi_note_norm ( selected_pad ), 1 );
MYMIDI.sendNoteOff ( 113, edrumulus.get_midi_note_rim ( selected_pad ), 1 );
MYMIDI.sendNoteOff ( 114, edrumulus.get_cancellation ( selected_pad ), 1 );
MYMIDI.sendNoteOff ( 126, VERSION_MINOR, 1 );
MYMIDI.sendNoteOff ( 127, VERSION_MAJOR, 1 );
}
// controller 109: MIDI curve type
if ( controller == 109 )
{
edrumulus.set_curve ( selected_pad, static_cast<Edrumulus::Ecurvetype> ( value ) );
edrumulus.write_setting ( selected_pad, 6, value );
is_used = true;
}
// controller 110: spike cancellation level
if ( controller == 110 )
{
edrumulus.set_spike_cancel_level ( value );
edrumulus.write_setting ( number_pads, 0, value );
is_used = true;
}
// controller 111: enable/disable rim shot and positional sensing support
if ( controller == 111 )
{
switch ( value )
{
case 0:
edrumulus.set_rim_shot_is_used ( selected_pad, false );
edrumulus.write_setting ( selected_pad, 7, false );
edrumulus.set_pos_sense_is_used ( selected_pad, false );
edrumulus.write_setting ( selected_pad, 8, false );
break;
case 1:
edrumulus.set_rim_shot_is_used ( selected_pad, true );
edrumulus.write_setting ( selected_pad, 7, true );
edrumulus.set_pos_sense_is_used ( selected_pad, false );
edrumulus.write_setting ( selected_pad, 8, false );
break;
case 2:
edrumulus.set_rim_shot_is_used ( selected_pad, false );
edrumulus.write_setting ( selected_pad, 7, false );
edrumulus.set_pos_sense_is_used ( selected_pad, true );
edrumulus.write_setting ( selected_pad, 8, true );
break;
case 3:
edrumulus.set_rim_shot_is_used ( selected_pad, true );
edrumulus.write_setting ( selected_pad, 7, true );
edrumulus.set_pos_sense_is_used ( selected_pad, true );
edrumulus.write_setting ( selected_pad, 8, true );
break;
}
is_used = true;
}
// controller 112: normal MIDI note
if ( controller == 112 )
{
edrumulus.set_midi_note_norm ( selected_pad, value );
edrumulus.write_setting ( selected_pad, 9, value );
is_used = true;
}
// controller 113: MIDI note for rim
if ( controller == 113 )
{
edrumulus.set_midi_note_rim ( selected_pad, value );
edrumulus.write_setting ( selected_pad, 10, value );
is_used = true;
}
// controller 114: cross talk cancellation
if ( controller == 114 )
{
edrumulus.set_cancellation ( selected_pad, value );
edrumulus.write_setting ( selected_pad, 11, value );
is_used = true;
}
// controller 115: apply preset settings and store these to the EEPROM
if ( controller == 115 )
{
preset_settings();
write_all_settings();
is_used = true;
}
// controller 116: normal MIDI note open (Hi-Hat)
if ( controller == 116 )
{
edrumulus.set_midi_note_open_norm ( selected_pad, value );
edrumulus.write_setting ( selected_pad, 12, value );
is_used = true;
}
// controller 117: MIDI note open (Hi-Hat) for rim
if ( controller == 117 )
{
edrumulus.set_midi_note_open_rim ( selected_pad, value );
edrumulus.write_setting ( selected_pad, 13, value );
is_used = true;
}
// give some feedback that the setting was correctly received
if ( is_used )
{
MYMIDI.sendNoteOff ( controller, value, 1 ); // can be checked, e.g., in the log file
}
}
}
#endif
}
void read_settings()
{
for ( int i = 0; i < number_pads; i++ )
{
// NOTE that it is important that set_pad_type() is called first because it resets all other parameters
edrumulus.set_pad_type ( i, static_cast<Edrumulus::Epadtype> ( edrumulus.read_setting ( i, 0 ) ) );
edrumulus.set_velocity_threshold ( i, edrumulus.read_setting ( i, 1 ) );
edrumulus.set_velocity_sensitivity ( i, edrumulus.read_setting ( i, 2 ) );
edrumulus.set_pos_threshold ( i, edrumulus.read_setting ( i, 3 ) );
edrumulus.set_pos_sensitivity ( i, edrumulus.read_setting ( i, 4 ) );
edrumulus.set_rim_shot_treshold ( i, edrumulus.read_setting ( i, 5 ) );
edrumulus.set_curve ( i, static_cast<Edrumulus::Ecurvetype> ( edrumulus.read_setting ( i, 6 ) ) );
edrumulus.set_rim_shot_is_used ( i, edrumulus.read_setting ( i, 7 ) );
edrumulus.set_pos_sense_is_used ( i, edrumulus.read_setting ( i, 8 ) );
edrumulus.set_midi_note_norm ( i, edrumulus.read_setting ( i, 9 ) );
edrumulus.set_midi_note_rim ( i, edrumulus.read_setting ( i, 10 ) );
edrumulus.set_cancellation ( i, edrumulus.read_setting ( i, 11 ) );
edrumulus.set_midi_note_open_norm ( i, edrumulus.read_setting ( i, 12 ) );
edrumulus.set_midi_note_open_rim ( i, edrumulus.read_setting ( i, 13 ) );
}
edrumulus.set_spike_cancel_level ( edrumulus.read_setting ( number_pads, 0 ) );
}
void write_all_settings()
{
for ( int i = 0; i < number_pads; i++ )
{
edrumulus.write_setting ( i, 0, edrumulus.get_pad_type ( i ) );
edrumulus.write_setting ( i, 1, edrumulus.get_velocity_threshold ( i ) );
edrumulus.write_setting ( i, 2, edrumulus.get_velocity_sensitivity ( i ) );
edrumulus.write_setting ( i, 3, edrumulus.get_pos_threshold ( i ) );
edrumulus.write_setting ( i, 4, edrumulus.get_pos_sensitivity ( i ) );
edrumulus.write_setting ( i, 5, edrumulus.get_rim_shot_treshold ( i ) );
edrumulus.write_setting ( i, 6, edrumulus.get_curve ( i ) );
edrumulus.write_setting ( i, 7, edrumulus.get_rim_shot_is_used ( i ) );
edrumulus.write_setting ( i, 8, edrumulus.get_pos_sense_is_used ( i ) );
edrumulus.write_setting ( i, 9, edrumulus.get_midi_note_norm ( i ) );
edrumulus.write_setting ( i, 10, edrumulus.get_midi_note_rim ( i ) );
edrumulus.write_setting ( i, 11, edrumulus.get_cancellation ( i ) );
edrumulus.write_setting ( i, 12, edrumulus.get_midi_note_open_norm ( i ) );
edrumulus.write_setting ( i, 13, edrumulus.get_midi_note_open_rim ( i ) );
}
edrumulus.write_setting ( number_pads, 0, edrumulus.get_spike_cancel_level() );
}