-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMIDIReceiver.cpp
47 lines (43 loc) · 1.67 KB
/
MIDIReceiver.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
#include "MIDIReceiver.h"
void MIDIReceiver::onMessageReceived(IMidiMsg* midiMessage) {
IMidiMsg::EStatusMsg status = midiMessage->StatusMsg();
// We're only interested in Note On/Off messages (not CC, pitch, etc.)
if(status == IMidiMsg::kNoteOn || status == IMidiMsg::kNoteOff) {
mMidiQueue.Add(midiMessage);
}
}
void MIDIReceiver::advance() {
while (!mMidiQueue.Empty()) {
IMidiMsg* midiMessage = mMidiQueue.Peek();
if (midiMessage->mOffset > mOffset) break;
IMidiMsg::EStatusMsg status = midiMessage->StatusMsg();
int noteNumber = midiMessage->NoteNumber();
int velocity = midiMessage->Velocity();
// There are only note on/off messages in the queue, see ::OnMessageReceived
if (status == IMidiMsg::kNoteOn && velocity) {
if(mKeyStatus[noteNumber] == false) {
mKeyStatus[noteNumber] = true;
mNumKeys += 1;
}
// A key pressed later overrides any previously pressed key:
if (noteNumber != mLastNoteNumber) {
mLastNoteNumber = noteNumber;
mLastFrequency = noteNumberToFrequency(mLastNoteNumber);
mLastVelocity = velocity;
}
} else {
if(mKeyStatus[noteNumber] == true) {
mKeyStatus[noteNumber] = false;
mNumKeys -= 1;
}
// If the last note was released, nothing should play:
if (noteNumber == mLastNoteNumber) {
mLastNoteNumber = -1;
mLastFrequency = -1;
mLastVelocity = 0;
}
}
mMidiQueue.Remove();
}
mOffset++;
}