-
Notifications
You must be signed in to change notification settings - Fork 1
/
MIDIReceiver.cpp
64 lines (53 loc) · 1.5 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
module;
#include <cmath>;
#include <algorithm>;
#include "IPlugMidi.h"
module megasynth.midi_receiver;
using std::pow;
using std::fill;
using iplug::IMidiMsg;
MIDIReceiver::MIDIReceiver() {
fill(this->keyStatus, this->keyStatus + this->keyCount, false);
}
void MIDIReceiver::flush(int numFrames) {
this->queue.Flush(numFrames);
this->offset = 0;
}
void MIDIReceiver::onMessageReceived(const IMidiMsg &midiMessage) {
auto status = midiMessage.StatusMsg();
if (status == IMidiMsg::EStatusMsg::kNoteOn || status == IMidiMsg::EStatusMsg::kNoteOff) {
this->queue.Add(midiMessage);
}
}
void MIDIReceiver::advance()
{
while (!this->queue.Empty())
{
const IMidiMsg& message = this->queue.Peek();
if (message.mOffset > this->offset)
break;
auto status = message.StatusMsg();
int noteNumber = message.NoteNumber();
int velocity = message.Velocity();
if (status == IMidiMsg::EStatusMsg::kNoteOn && velocity != 0)
{
if (this->keyStatus[noteNumber] == false)
{
this->keyStatus[noteNumber] = true;
this->numKeysPressed++;
noteOn(noteNumber, velocity); // maybe should be outside of "if"
}
}
else // if the message was Note Off or Velocity = 0
{
if (this->keyStatus[noteNumber] == true)
{
this->keyStatus[noteNumber] = false;
this->numKeysPressed--;
this->noteOff(noteNumber, velocity); // again maybe should be outside of "if"
}
}
this->queue.Remove();
}
this->offset++;
}