-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyEvent.h
100 lines (86 loc) · 3.31 KB
/
KeyEvent.h
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
#ifndef MIDI2KB_KEYEVENT_H
#define MIDI2KB_KEYEVENT_H
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
enum class KeyEventType {
Press = 0x90,
Release = 0x80,
Pressure = 0xA0,
PitchBend = 0xE0,
ProgramChange = 0xC0,
ChannelPressure = 0xD0,
ControllerChange = 0xB0,
};
struct KeyEvent {
KeyEventType type;
unsigned char byte1, byte2;
explicit KeyEvent(std::vector<unsigned char> *event_data)
{
type = (KeyEventType) event_data->at(0);
switch (type)
{
case KeyEventType::Press:
case KeyEventType::Release:
case KeyEventType::Pressure:
case KeyEventType::PitchBend:
case KeyEventType::ControllerChange:
byte1 = event_data->at(1);
byte2 = event_data->at(2);
break;
case KeyEventType::ProgramChange:
case KeyEventType::ChannelPressure:
byte1 = event_data->at(1);
byte2 = 0;
break;
default:
{
if(event_data->size() >= 3)
{
byte1 = event_data->at(1);
byte2 = event_data->at(2);
}
else if(event_data->size() == 2)
{
byte1 = event_data->at(1);
byte2 = 0;
}
}
}
}
std::string ToString()
{
std::string tp;
switch (type)
{
case KeyEventType::Press: tp = "PRESS"; break;
case KeyEventType::Release: tp = "RELEASE"; break;
case KeyEventType::Pressure: tp = "PRESSURE"; break;
case KeyEventType::PitchBend: tp = "PITCH_BEND"; break;
case KeyEventType::ProgramChange: tp = "PROGRAM_CHANGE"; break;
case KeyEventType::ChannelPressure: tp = "CHANNEL_PRESSURE"; break;
case KeyEventType::ControllerChange: tp = "CONTROLLER_CHANGE"; break;
default: tp = std::to_string((int) type); break;
}
std::string b1Name, b2Name;
switch (type)
{
case KeyEventType::Press:
case KeyEventType::Release: b1Name = "PITCH"; b2Name = "VELOCITY"; break;
case KeyEventType::Pressure: b1Name = "KEY"; b2Name = "PRESSURE"; break;
case KeyEventType::PitchBend: b1Name = "LSB"; b2Name = "MSB"; break;
case KeyEventType::ProgramChange: b1Name = "PRESET"; b2Name = "BYTE2"; break;
case KeyEventType::ChannelPressure: b1Name = "PRESSURE"; b2Name = "BYTE2"; break;
case KeyEventType::ControllerChange: b1Name = "CONTROLLER"; b2Name = "VALUE"; break;
default: b1Name = "BYTE1"; b2Name = "BYTE2"; break;
}
std::stringstream text;
text << "[KEY EVENT] "
<< "TYPE: " << tp
<< " | " << b1Name << ": " << (unsigned short) byte1
<< " | " << b2Name << ": " << (unsigned short) byte2;
return text.str();
}
};
#endif //MIDI2KB_KEYEVENT_H