-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLooper.cpp
600 lines (507 loc) · 16.2 KB
/
Looper.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
#include "MIDIProcessor.h"
#include "Controls.h"
#include "Display.h"
#define _DEBUG
#define IS_NOTE_OFF(_n) (((((_n).velocity&0x7F) == 0x00)?true : false))
byte CharPlay[8] = {
0b00000,
0b10000,
0b11000,
0b11110,
0b11110,
0b11000,
0b10000,
0b00000,
};
byte CharStop[8] = {
0b00000,
0b10010,
0b10010,
0b10010,
0b10010,
0b10010,
0b10010,
0b00000
};
typedef enum
{
eLooperManual, //Detect loops and wait for manual ack
eLooperAuto //Detect loops and auto run
} tLooperMode;
typedef enum
{
eLooperIdle, //Do nothing
eLooperPlaying, //Replay recorded notes
eLooperRecording //Record notes and detect loops
} tLooperStatus;
typedef struct //8 byte struct
{
unsigned int time;
byte note;
byte velocity;
unsigned int duration;
} tNoteEvent;
#define MAX_SAMPLE 32 //Max events in sample (32 notes)
#define MAX_SLOTS 4
byte slotIdx = 0;
typedef struct
{
tNoteEvent aNoteEvents[MAX_SAMPLE];
byte noteIdx; //Current note record index
byte sampleSize; //Complete size of sample
unsigned int repeatDelay; //delay between last not and first note
byte replayIdx; //Current note being played on the loop
unsigned long previousLoopTimestamp; //Ts of last played NoteOff (Note Off are not ordered, so we have no index)
unsigned long firstNoteTimestamp; //Current timestamp for note 0 when playing or recording "when did we play first note ?"
byte bChannel; //MIDI channel for this slot
tLooperStatus slotStatus; //Slot status
} tLooperSlot;
tLooperMode looperMode;
tLooperStatus looperStatus;
tLooperSlot aSlots[MAX_SLOTS];
unsigned long displayTimeout;
//Callbacks for buttons/Knobs
void changeLooperModeCb(byte button, tButtonStatus event, int duration); //Auto/Manual
void slotPlayMuteCb(byte button, tButtonStatus event, int duration); //Start/stop play
void slotRecordCb(byte button, tButtonStatus event, int duration); //Start Recording
void generalPlayStopCb(byte button, tButtonStatus event, int duration); //RePlay previous loop on current slot
void dumpLoopCb(byte button, tButtonStatus event, int duration); //Dump loop contents
void slotSelectCb (byte knob, int value, tKnobRotate rot);
//MIDI event callback
byte NoteCb(byte channel, byte note, byte velocity, unsigned long timestamp);
//Looper mode changes
void SetGlobalMode(tLooperMode mode);
void SetStatus(tLooperStatus lstatus);
void ResetLoop(byte slot);
void ResetPlay(byte playIdx = 0);
void RefreshDisplay(const char * msg = NULL);
void LooperSetup()
{
int i;
MIDIRegisterNoteCb(NoteCb);
DisplayCreateChar(CharPlay, 0);
DisplayCreateChar(CharStop, 1);
memset(aSlots, 0x00, MAX_SLOTS*sizeof(tLooperSlot));
for (i = 0; i < MAX_SLOTS; i++)
ResetLoop(i);
ControlsRegisterButtonCallback(0, eButtonStatus_Released, 0, changeLooperModeCb); //Auto / Manual
#ifdef _DEBUG
ControlsRegisterButtonCallback(0, eButtonStatus_Released, 1000, dumpLoopCb); //Debug : Dump notes
#endif
ControlsRegisterButtonCallback(1, eButtonStatus_Released, 0, slotPlayMuteCb); //Play / Idle
ControlsRegisterButtonCallback(1, eButtonStatus_Released, 1000, slotRecordCb); //Record
ControlsRegisterButtonCallback(2, eButtonStatus_Released, 0, generalPlayStopCb); //RePlay previous loop on current slot
ControlsRegisterKnobCallback(0, slotSelectCb); //Select slot for loop
ControlsNotifyKnob(0); //Force update for init
SetGlobalMode(eLooperAuto);
SetStatus(eLooperIdle);
displayTimeout = 0;
RefreshDisplay();
}
void LooperUpdate()
{
byte s, i;
if (looperStatus != eLooperPlaying) //Nothing to do
return;
unsigned long timestamp = millis();
// Play recorded loops
for (s = 0; s < MAX_SLOTS; s++)
{
tLooperSlot * slot = &aSlots[s];
if (slot->sampleSize && ((slot->slotStatus == eLooperPlaying)||(slot->slotStatus == eLooperIdle)))
{
//A (0ms) B (10ms) C (5ms) D (1ms) E (100ms) A (0ms) ...
//Increase timers ont muted slots to keep sync
//Play note ON events
while ((timestamp > slot->firstNoteTimestamp) && //firstNoteTimestampcan be set to a future date (end loop delay)
((int)(timestamp - slot->firstNoteTimestamp) >= slot->aNoteEvents[slot->replayIdx].time))
{
//On first loop note, make sure there's no pending Note Off to be processed from previous loop
if (!slot->replayIdx)
{
//Flush remaning note off
for (i = 0; i < slot->sampleSize; i++)
{
unsigned int noteOffTs = slot->aNoteEvents[i].time + slot->aNoteEvents[i].duration;
if (slot->previousLoopTimestamp < timestamp + noteOffTs)
{
Serial.write(0x80 | slot->bChannel);
Serial.write(slot->aNoteEvents[i].note);
Serial.write(slot->aNoteEvents[i].velocity);
}
}
slot->previousLoopTimestamp = slot->firstNoteTimestamp;
}
//Play note
if (slot->slotStatus == eLooperPlaying)
{
Serial.write(0x90 | slot->bChannel);
Serial.write(slot->aNoteEvents[slot->replayIdx].note);
Serial.write(slot->aNoteEvents[slot->replayIdx].velocity);
}
slot->replayIdx ++;
//End of loop, start again
if (slot->replayIdx == slot->sampleSize)
{
//Start at idx 0
slot->replayIdx = 0;
//Wait for last note to end using repeatDelay (we don't want to play last note and first one at the same time !)
slot->firstNoteTimestamp = millis() + slot->repeatDelay; //Set sequence ts start
}
}
//Play note off events
for (i = 0; i < slot->sampleSize; i++)
{
unsigned int noteOffTs = slot->aNoteEvents[i].time + slot->aNoteEvents[i].duration;
if ((slot->previousLoopTimestamp < slot->firstNoteTimestamp + noteOffTs) && //not already played
(timestamp >= slot->firstNoteTimestamp + noteOffTs)) //time to play it
{
Serial.write(0x80 | slot->bChannel);
Serial.write(slot->aNoteEvents[i].note);
Serial.write(slot->aNoteEvents[i].velocity);
}
}
slot->previousLoopTimestamp = timestamp;
}
}
//Auto vanish messages after 2s
if (displayTimeout && ((int)(timestamp - displayTimeout) > 2000))
RefreshDisplay();
}
//Updates "sampleSIze" to the longest loop found on given slot
//Returns current position in loop in that case or 0xFF if no loop is detected
byte LoopDetect(tLooperSlot * slot)
{
if (slot->noteIdx < 4) //Need at least 4 notes "AB AB" to detect "AB".
return 0xFF;
if ((slot->aNoteEvents[0].note == slot->aNoteEvents[slot->noteIdx - 2].note) &&
(slot->aNoteEvents[1].note == slot->aNoteEvents[slot->noteIdx - 1].note))
{
//Set sample length
slot->sampleSize = slot->noteIdx - 2;
//Compute delay between last note of the sample and first one
//If we don't do last, first note will be play immediately after last one
slot->repeatDelay = slot->aNoteEvents[slot->noteIdx-2].time - slot->aNoteEvents[slot->noteIdx-3].time;
return 1;//Consider we've been playing note 1 (0, 1 .. and then next is 2)
}
return 0xFF; //Nothing found
}
bool AddNoteOff(tLooperSlot * slot, byte note, unsigned long timestamp)
{
//Note off : try to find correponding Note On and update duration
for (int i = slot->noteIdx - 1; i >= 0; i--)
{
if ((slot->aNoteEvents[i].note == note) && (slot->aNoteEvents[i].duration == 0)) //Corresponding Note On found !
{
//Update note duration
slot->aNoteEvents[i].duration = (int)(timestamp - slot->firstNoteTimestamp) - slot->aNoteEvents[i].time;
return true;
}
}
return false;
}
bool AddNoteOn(tLooperSlot * slot, byte note, byte velocity, unsigned long timestamp)
{
slot->aNoteEvents[slot->noteIdx].note = note;
slot->aNoteEvents[slot->noteIdx].time = (int)(timestamp - slot->firstNoteTimestamp);
slot->aNoteEvents[slot->noteIdx].velocity = velocity;
slot->aNoteEvents[slot->noteIdx].duration = 0; //Note Off event will set duration
slot->noteIdx ++;
return true;
}
//Adds a note on the current slot
//Detects and optimize chords
//returns false on slot full
bool AddNote(tLooperSlot * slot, byte note, byte velocity, byte channel, unsigned long timestamp)
{
if (slot->noteIdx == MAX_SAMPLE) //Slot full
return false;
if ((slot->noteIdx == 0) && (velocity == 0))//Ignore "NoteOff" as first sample event (not an error)
return true;
if (slot->noteIdx == 0) //First note of a loop decides the slot's whole channel (reject channel change during loop record)
{
slot->bChannel = channel;
slot->firstNoteTimestamp = timestamp; //Set beginning of loop timestamp
RefreshDisplay();
}
if (!velocity)
return AddNoteOff(slot, note, timestamp);
else
return AddNoteOn(slot, note, velocity, timestamp);
return true;
}
//Called when Note (on/off) received
//Return : Silent ?
byte NoteCb(byte channel, byte note, byte velocity, unsigned long timestamp)
{
byte loopFound;
tLooperSlot * slot = &aSlots[slotIdx];
DisplayBlinkRed();
if (slot->slotStatus == eLooperIdle) //Simply replay received note
return false;
if (slot->slotStatus == eLooperPlaying) //Simply replay received note FIXME : stop passthrough when just starting replay ?
return false;
//Recording
if (slot->noteIdx == MAX_SAMPLE) //Pattern too long
{
slot->slotStatus = eLooperIdle;
RefreshDisplay((char*)"Too long !");
ResetLoop(slotIdx);
return false;
}
if (!velocity && !slot->noteIdx) //Loop may not start with a NoteOff event ...
return false;
//DisplayBlinkGreen();
if (!AddNote(slot, note, velocity, channel, timestamp))
{
//Cannot add another note on slot
//-> sample must be too long
slot->slotStatus = eLooperIdle;
RefreshDisplay((char*)"Too long !");
ResetLoop(slotIdx);
return false;
}
if ((velocity & 0x7F) == 0x00) //NoteOff is not used to setup a loop
return false;
loopFound = LoopDetect(slot);
if (loopFound == 0xFF)//No loop, continue
return false;
if (looperMode == eLooperAuto)
{
slot->slotStatus = eLooperPlaying;
looperStatus = eLooperPlaying;
RefreshDisplay("Loop Ok !");
ResetPlay(loopFound);
return false;
}
else //Message and wait for manual ack
{
RefreshDisplay("Loop Ready!");
return false;
}
return false;
}
// ######## SLOT SPECIFIC FUNCTIONS #########
//Reset loop contents on current slot
void ResetLoop(byte slot)
{
aSlots[slot].sampleSize = 0;
aSlots[slot].noteIdx = 0;
aSlots[slot].bChannel = 0;
memset(aSlots[slot].aNoteEvents, 0x00, MAX_SAMPLE*sizeof(tNoteEvent));
}
//Reset play indexes
void ResetPlay(byte playIdx)
{
aSlots[slotIdx].replayIdx = playIdx;
aSlots[slotIdx].firstNoteTimestamp = millis() - aSlots[slotIdx].aNoteEvents[playIdx].time; //Compute a fake 1st note timestamp (roll back in time)
}
// ######## GENERAL LOOPER FUNCTIONS #########
//Change looper status (playing/stop)
void SetStatus(tLooperStatus lstatus)
{
switch (lstatus)
{
case eLooperPlaying:
looperStatus = eLooperPlaying;
RefreshDisplay();
break;
case eLooperIdle:
looperStatus = eLooperIdle;
RefreshDisplay();
break;
}
}
//Change Auto/Manual
void SetGlobalMode(tLooperMode mode)
{
looperMode = mode;
RefreshDisplay();
}
//Main Display method
void RefreshDisplay(const char * msg) //7chars max
{
DisplayClear();
/*
X|Auto|Ch07|Sl 4
............Rec.
*/
//1st line
switch (looperStatus)
{
case eLooperIdle:
DisplayWriteChar(1, 0,0);
break;
case eLooperPlaying:
DisplayWriteChar(0, 0,0);
break;
}
DisplayWriteStr((looperMode==eLooperManual)?"|Man |":"|Auto|", 0, 1);
if (aSlots[slotIdx].sampleSize)
{
DisplayWriteStr("Ch00|", 0, 7);
DisplayWriteInt(aSlots[slotIdx].bChannel+1, 0, (aSlots[slotIdx].bChannel>9)?9:10); //Ch01 - Ch16
}
else
{
DisplayWriteStr("ChXX|", 0, 7);
}
DisplayWriteStr("Sl ", 0, 12);
DisplayWriteInt(slotIdx+1, 0, 15);
//2nd line
switch (aSlots[slotIdx].slotStatus)
{
case eLooperIdle:
if (!aSlots[slotIdx].sampleSize)
DisplayWriteStr("Empt", 1, 12);
else
DisplayWriteStr("Mute", 1, 12);
break;
case eLooperPlaying:
DisplayWriteStr("Play", 1, 12);
break;
case eLooperRecording:
DisplayWriteStr("Rec.", 1, 12);
break;
}
//Custom message
if (msg)
{
DisplayWriteStr(msg, 1, 0);
displayTimeout = millis();
}
else
{
displayTimeout = 0;
}
}
//Sends a "All note off" message to stop all pending notes.
void ChannelAllOff(byte channel)
{
//Write 0xB<channel> 0x7B 0x00
Serial.write(0xB0 | channel);
Serial.write(0x7B);
Serial.write((byte)0x00);
}
// ######## BUTTON CALLBACKS #########
//## Button 1 : Change global mode (Play/Stop)
void generalPlayStopCb(byte button, tButtonStatus event, int duration)
{
if (looperStatus == eLooperIdle)
{
SetStatus(eLooperPlaying);
}
else
{
int i;
for (i = 0; i < MAX_SLOTS; i++)
{
if (aSlots[i].slotStatus == eLooperPlaying)
ChannelAllOff(aSlots[i].bChannel);
}
SetStatus(eLooperIdle);
}
}
//## Button 2 : Change slot mode (Play/Stop) or acknowledge loop on manual mode
void slotPlayMuteCb(byte button, tButtonStatus event, int duration) //Change status (start recording)
{
if ((aSlots[slotIdx].slotStatus == eLooperIdle) && aSlots[slotIdx].sampleSize) //Start playing
{
aSlots[slotIdx].slotStatus = eLooperPlaying;
looperStatus = eLooperPlaying;
}
else if (aSlots[slotIdx].slotStatus == eLooperPlaying) //Loop (if loop found)
{
ChannelAllOff(aSlots[slotIdx].bChannel);
aSlots[slotIdx].slotStatus = eLooperIdle;
}
else if ((aSlots[slotIdx].slotStatus == eLooperRecording) && (looperMode == eLooperManual)) //Manual ack
{
if (aSlots[slotIdx].sampleSize) //Manual enable
{
ResetPlay(0); //TODO: start playing at appropriate note !
aSlots[slotIdx].slotStatus = eLooperPlaying;
looperStatus = eLooperPlaying;
}
else //No loop
{
RefreshDisplay("NoLoop!");
return;
}
}
else
{
aSlots[slotIdx].slotStatus = eLooperIdle;
RefreshDisplay("NoLoop!");
return;
}
RefreshDisplay();
}
//## Button 2 (long press) : Switch current slot to Recording status
void slotRecordCb(byte button, tButtonStatus event, int duration) //Start Recording
{
ResetLoop(slotIdx);
aSlots[slotIdx].slotStatus = eLooperRecording;
RefreshDisplay();
}
//## Button 3 : Change looper mode (Auto/manual ack)
void changeLooperModeCb(byte button, tButtonStatus event, int duration) //Change mode
{
switch (looperMode)
{
case eLooperManual:
SetGlobalMode(eLooperAuto);
break;
case eLooperAuto:
SetGlobalMode(eLooperManual);
break;
}
}
// ######## KNOBS CALLBACKS #########
//## Knob 1 : Select slot
void slotSelectCb (byte knob, int value, tKnobRotate rot)
{
byte v = MAX_SLOTS - (value * MAX_SLOTS/1024) - 1;
if (v == slotIdx)
return;
slotIdx = v;
RefreshDisplay();
}
#ifdef _DEBUG
void dumpLoopCb(byte button, tButtonStatus event, int duration)
{
int i;
DisplayClear();
DisplayWriteStr("Debug ... ", 0, 0);
delay(1000);
DisplayWriteStr("Mode : ", 0, 0);
DisplayWriteStr((looperMode==eLooperManual)?"Manual":"Auto", 0, 7);
delay(1000);
DisplayWriteStr("Status : ", 0, 0);
DisplayWriteStr(looperStatus==eLooperIdle?"Idle":(looperStatus==eLooperPlaying?"Playing":"Recording"), 0, 9);
delay(1000);
DisplayWriteStr("SampleSize : ", 0, 0);
DisplayWriteInt(aSlots[slotIdx].sampleSize, 0, 13);
delay(1000);
if (!aSlots[slotIdx].sampleSize)
{
DisplayWriteStr("No Sample", 0, 0);
delay(1000);
return;
}
delay(1000);
DisplayWriteStr("[ ] n ms", 0, 0);
DisplayWriteStr("Dur. : ms", 1, 0);
for (i = 0; i < aSlots[slotIdx].sampleSize; i++)
{
//[4] n67 1234ms
//Dur. : 1200 ms
DisplayWriteInt(i, 0, 1);
DisplayWriteInt(aSlots[slotIdx].aNoteEvents[i].note, 0, 7);
DisplayWriteInt(aSlots[slotIdx].aNoteEvents[i].time, 0, 10);
DisplayWriteInt(aSlots[slotIdx].aNoteEvents[i].duration, 1, 7);
delay(1500);
}
RefreshDisplay();
}
#endif