-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi.cpp
77 lines (66 loc) · 2.13 KB
/
midi.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
#include "midi.h"
#include "shiftRegister.h"
#include "setting.h"
extern const bool DEBUG_MODE;
Note scheduled[88] = {{0,0,0,0,0,0,0}};
/* time schema of a note :
*
* solenoid
* position
* | ________
* | / \
* | / \
* | / \
* |__________________ time
*
* pulse
* pwm
* |____ . . . . . . . velocity, varies, max is MAX_PWM
* | |
* | |________ . . . pressedPwm, setting
* | |
* |___________|______ time
* : : :
* : : note off
* : keepTime
* note
* on
*
*/
void onNoteOn(byte channel, byte note, byte velocity) {
if (DEBUG_MODE) Serial.printf("[%u] Note ON chan:%i note:%i vel:%i\n", millis(), channel, note, velocity);
note -= 21; // lower octaves a bit
if (note >= 0 && note < 87) {
if (Setting::scheduleNotes) {
unsigned long on_time = millis();
// planned, triggered, vel-on, vel-stay, time-on, time-stay, time-off
scheduled[note] = {1, 0, velocity, Setting::pressedPWM, on_time, on_time + Setting::pressedDelay, 0};
} else {
activateNote(note, velocity);
}
pressedKeys++;
}
}
void onNoteOff(byte channel, byte note, byte velocity) {
if (DEBUG_MODE) Serial.printf("[%u] Note OFF chan:%i note:%i vel:%i\n", millis(), channel, note, velocity);
note -= 21; // lower octaves a bit
if (note >= 0 && note < 87) {
if (Setting::scheduleNotes) {
scheduled[note].time_off = (millis() - scheduled[note].time_on < Setting::minNoteTime) ? scheduled[note].time_on + Setting::minNoteTime : millis();
} else {
activateNote(note, 0);
}
pressedKeys--;
}
}
void onControlChange(byte channel, byte control, byte value) {
// if (DEBUG_MODE) Serial.printf("[%u] Control: chan:%i ctrl:%i val:%i\n", millis(), channel, control, value);
// handle panic
if (control == 120 || control == 121 || control == 123) { resetNotes(); }
// handle sustain pedal
if (control == 64 && value > 63) { activateNote(87, 127); }
if (control == 64 && value < 64) { activateNote(87, 0); }
}
void checkForMidiUSB() {
usbMIDI.read();
}