-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMIDIReceiver.cpp
47 lines (43 loc) · 1.4 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"
MIDIReceiver* MIDIReceiver::ref=NULL;
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 || status == IMidiMsg::kPitchWheel) {
mMidiQueue.Add(midiMessage);
}
}
//Be called on every sample while we¡¯re generating an audio buffer
void MIDIReceiver::advance() {
while (!mMidiQueue.Empty()) {
IMidiMsg* midiMessage = mMidiQueue.Peek();
if (midiMessage->mOffset > mOffset) break;
IMidiMsg::EStatusMsg status = midiMessage->StatusMsg();
if (status == IMidiMsg::kPitchWheel)
{
pbend[midiMessage->Channel()] = midiMessage->PitchWheel();
pbChanged();
mMidiQueue.Remove();
continue;
}
int channel = midiMessage->Channel();
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;
noteOn(channel, noteNumber, velocity);
}
} else {
if (mKeyStatus[noteNumber] == true) {
mKeyStatus[noteNumber] = false;
mNumKeys -= 1;
noteOff(channel, noteNumber, velocity);
}
}
mMidiQueue.Remove();
}
mOffset++;
}