This repository has been archived by the owner on Dec 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmidi_input.cpp
127 lines (114 loc) · 3.25 KB
/
midi_input.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
#include "midi_input.h"
void MIDIInput::setNoteOnCallback(void (*callback)(uint8_t, uint8_t, uint8_t))
{
MIDINoteOnCallback = callback;
}
void MIDIInput::setNoteOffCallback(void (*callback)(uint8_t, uint8_t, uint8_t))
{
MIDINoteOffCallback = callback;
}
void MIDIInput::setCCCallback(void (*callback)(uint8_t, uint8_t, uint8_t))
{
MIDICCCallback = callback;
}
void MIDIInput::process()
{
uint8_t mb = uart_getc (_uart);
if ((mb >= 0x80) && (mb <= 0xEF))
{
// MIDI Voice Category Message.
// Action: Start handling Running Status
MIDIRunningStatus = mb;
MIDINote = 0;
MIDILevel = 0;
}
else if ((mb >= 0xF0) && (mb <= 0xF7))
{
// MIDI System Common Category Message.
// Action: Reset Running Status.
MIDIRunningStatus = 0;
}
else if ((mb >= 0xF8) && (mb <= 0xFF))
{
// System Real-Time Message.
// Action: Ignore these.
return;
}
else
{
// MIDI Data
if (MIDIRunningStatus == 0)
// No record of what state we're in, so can go no further
return;
if (MIDIRunningStatus >> 4 == (0x8))
{
uint8_t channel = MIDIRunningStatus & 0x0F;
// Note OFF Received
if (MIDINote == 0)
{
// Store the note number
MIDINote = mb;
}
else
{
// Already have the note, so store the level
MIDILevel = mb;
if (MIDINoteOffCallback)
MIDINoteOffCallback(MIDINote, MIDILevel, channel);
MIDINote = 0;
MIDILevel = 0;
}
}
else if (MIDIRunningStatus >> 4 == (0x9))
{
uint8_t channel = MIDIRunningStatus & 0x0F;
// Note ON Received
if (MIDINote == 0)
{
// Store the note number
MIDINote = mb;
}
else
{
// Already have the note, so store the level
MIDILevel = mb;
if (MIDILevel == 0)
{
if (MIDINoteOffCallback)
MIDINoteOffCallback(MIDINote, MIDILevel, channel);
}
else
{
if (MIDINoteOnCallback)
MIDINoteOnCallback(MIDINote, MIDILevel, channel);
MIDINote = 0;
MIDILevel = 0;
}
}
}
else if (MIDIRunningStatus >> 4 == (0xB))
{
uint8_t channel = MIDIRunningStatus & 0x0F;
// Control Change Received
if (MIDINote == 0)
{
// Store the control number
MIDINote = mb;
}
else
{
// Already have the control, so store the level
MIDILevel = mb;
if (MIDICCCallback)
MIDICCCallback(MIDINote, MIDILevel, channel);
MIDINote = 0;
MIDILevel = 0;
}
}
else
{
// This is a MIDI command we aren't handling right now
return;
}
}
}