forked from VCVRack/Rack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyboard.cpp
139 lines (112 loc) · 3.03 KB
/
keyboard.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
128
129
130
131
132
133
134
135
136
137
138
139
#include "keyboard.hpp"
#include <GLFW/glfw3.h>
namespace rack {
static const int KEYBOARD_DRIVER = -11;
static KeyboardDriver *driver = NULL;
void KeyboardInputDevice::onKeyPress(int key) {
int note = -1;
switch (key) {
case GLFW_KEY_Z: note = 0; break;
case GLFW_KEY_S: note = 1; break;
case GLFW_KEY_X: note = 2; break;
case GLFW_KEY_D: note = 3; break;
case GLFW_KEY_C: note = 4; break;
case GLFW_KEY_V: note = 5; break;
case GLFW_KEY_G: note = 6; break;
case GLFW_KEY_B: note = 7; break;
case GLFW_KEY_H: note = 8; break;
case GLFW_KEY_N: note = 9; break;
case GLFW_KEY_J: note = 10; break;
case GLFW_KEY_M: note = 11; break;
case GLFW_KEY_COMMA: note = 12; break;
case GLFW_KEY_L: note = 13; break;
case GLFW_KEY_PERIOD: note = 14; break;
case GLFW_KEY_SEMICOLON: note = 15; break;
case GLFW_KEY_SLASH: note = 16; break;
case GLFW_KEY_Q: note = 12; break;
case GLFW_KEY_2: note = 13; break;
case GLFW_KEY_W: note = 14; break;
case GLFW_KEY_3: note = 15; break;
case GLFW_KEY_E: note = 16; break;
case GLFW_KEY_R: note = 17; break;
case GLFW_KEY_5: note = 18; break;
case GLFW_KEY_T: note = 19; break;
case GLFW_KEY_6: note = 20; break;
case GLFW_KEY_Y: note = 21; break;
case GLFW_KEY_7: note = 22; break;
case GLFW_KEY_U: note = 23; break;
case GLFW_KEY_I: note = 24; break;
case GLFW_KEY_9: note = 25; break;
case GLFW_KEY_O: note = 26; break;
case GLFW_KEY_0: note = 27; break;
case GLFW_KEY_P: note = 28; break;
case GLFW_KEY_LEFT_BRACKET: note = 29; break;
case GLFW_KEY_EQUAL: note = 30; break;
case GLFW_KEY_RIGHT_BRACKET: note = 31; break;
case GLFW_KEY_GRAVE_ACCENT: {
octave--;
} break;
case GLFW_KEY_1: {
octave++;
} break;
default: break;
}
octave = clamp(octave, 0, 9);
if (note < 0)
return;
note += 12 * octave;
if (note > 127)
return;
MidiMessage msg;
msg.cmd = 0x9 << 4;
msg.data1 = note;
msg.data2 = 127;
onMessage(msg);
pressedNotes[key] = note;
}
void KeyboardInputDevice::onKeyRelease(int key) {
auto it = pressedNotes.find(key);
if (it != pressedNotes.end()) {
int note = it->second;
MidiMessage msg;
msg.cmd = 0x8 << 4;
msg.data1 = note;
msg.data2 = 127;
onMessage(msg);
pressedNotes.erase(it);
}
}
std::vector<int> KeyboardDriver::getInputDeviceIds() {
return {0};
}
std::string KeyboardDriver::getInputDeviceName(int deviceId) {
if (deviceId == 0)
return "QWERTY keyboard (US)";
return "";
}
MidiInputDevice *KeyboardDriver::subscribeInputDevice(int deviceId, MidiInput *midiInput) {
if (deviceId != 0)
return NULL;
device.subscribe(midiInput);
return &device;
}
void KeyboardDriver::unsubscribeInputDevice(int deviceId, MidiInput *midiInput) {
if (deviceId != 0)
return;
device.unsubscribe(midiInput);
}
void keyboardInit() {
driver = new KeyboardDriver();
midiDriverAdd(KEYBOARD_DRIVER, driver);
}
void keyboardPress(int key) {
if (!driver)
return;
driver->device.onKeyPress(key);
}
void keyboardRelease(int key) {
if (!driver)
return;
driver->device.onKeyRelease(key);
}
} // namespace rack