-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidi-listener.cpp
44 lines (40 loc) · 1.39 KB
/
midi-listener.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
#include "midi-listener.h"
void MidiListener::poll()
{
if (p_input->available()) {
handleByte(p_input->read()); }
}
/* Callback fired when the Serial provides MidiListener with a byte of data.
Sets this->action if not set. Else, calls handleNote or handleVolume. */
void MidiListener::handleByte(uint8_t byte)
{
if ( note ) // the 3rd byte in the packet indicates the volume
handleVolume( byte );
else if ( action ) // the 2nd byte in the packet indicates the note
handleNote( byte );
else // the 1st byte in the packet indicates NOTE_ON or NOTE_OFF
action = byte;
}
/* Callback fired when handleByte is called and action is set.
Sets this-> note or fires noteOffCallback. */
void MidiListener::handleNote(uint8_t byte)
{
if ( action == MIDI_NOTE_ON )
note = byte;
else if ( action == MIDI_NOTE_OFF )
noteOffCallback( byte );
else // The previous/current input packet was malformed. Treat this byte as the first byte of the packet
action = byte;
}
/* Callback fired when handleByte is called and note is set.
Fires noteOnCallback or noteOffCallback. Clears action and note. */
void MidiListener::handleVolume(uint8_t byte)
{
// fire a callback for NOTE_ON or NOTE_OFF
if (byte) // if volume is not 0
noteOnCallback(note, byte);
else // volume of 0 is equivalent to MIDI_NOTE_OFF
noteOffCallback(note);
// reset
action = note = 0;
}