You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

keyboard.cpp 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #include <keyboard.hpp>
  2. #include <midi.hpp>
  3. #include <window.hpp>
  4. #include <map>
  5. namespace rack {
  6. namespace keyboard {
  7. struct Driver;
  8. static const int DRIVER = -11;
  9. static Driver* driver = NULL;
  10. enum {
  11. CMD_OCTAVE_DOWN = -1,
  12. CMD_OCTAVE_UP = -2,
  13. };
  14. static const int deviceCount = 2;
  15. static const std::vector<std::map<int, int>> deviceKeyNote = {
  16. {
  17. {GLFW_KEY_GRAVE_ACCENT, CMD_OCTAVE_DOWN},
  18. {GLFW_KEY_1, CMD_OCTAVE_UP},
  19. {GLFW_KEY_Z, 0},
  20. {GLFW_KEY_S, 1},
  21. {GLFW_KEY_X, 2},
  22. {GLFW_KEY_D, 3},
  23. {GLFW_KEY_C, 4},
  24. {GLFW_KEY_V, 5},
  25. {GLFW_KEY_G, 6},
  26. {GLFW_KEY_B, 7},
  27. {GLFW_KEY_H, 8},
  28. {GLFW_KEY_N, 9},
  29. {GLFW_KEY_J, 10},
  30. {GLFW_KEY_M, 11},
  31. {GLFW_KEY_COMMA, 12},
  32. {GLFW_KEY_L, 13},
  33. {GLFW_KEY_PERIOD, 14},
  34. {GLFW_KEY_SEMICOLON, 15},
  35. {GLFW_KEY_SLASH, 16},
  36. {GLFW_KEY_Q, 12},
  37. {GLFW_KEY_2, 13},
  38. {GLFW_KEY_W, 14},
  39. {GLFW_KEY_3, 15},
  40. {GLFW_KEY_E, 16},
  41. {GLFW_KEY_R, 17},
  42. {GLFW_KEY_5, 18},
  43. {GLFW_KEY_T, 19},
  44. {GLFW_KEY_6, 20},
  45. {GLFW_KEY_Y, 21},
  46. {GLFW_KEY_7, 22},
  47. {GLFW_KEY_U, 23},
  48. {GLFW_KEY_I, 24},
  49. {GLFW_KEY_9, 25},
  50. {GLFW_KEY_O, 26},
  51. {GLFW_KEY_0, 27},
  52. {GLFW_KEY_P, 28},
  53. {GLFW_KEY_LEFT_BRACKET, 29},
  54. {GLFW_KEY_EQUAL, 30},
  55. {GLFW_KEY_RIGHT_BRACKET, 31},
  56. },
  57. {
  58. {GLFW_KEY_KP_DIVIDE, CMD_OCTAVE_DOWN},
  59. {GLFW_KEY_KP_MULTIPLY, CMD_OCTAVE_UP},
  60. {GLFW_KEY_KP_0, 0},
  61. {GLFW_KEY_KP_DECIMAL, 2},
  62. {GLFW_KEY_KP_ENTER, 3},
  63. {GLFW_KEY_KP_1, 4},
  64. {GLFW_KEY_KP_2, 5},
  65. {GLFW_KEY_KP_3, 6},
  66. {GLFW_KEY_KP_4, 8},
  67. {GLFW_KEY_KP_5, 9},
  68. {GLFW_KEY_KP_6, 10},
  69. {GLFW_KEY_KP_ADD, 11},
  70. {GLFW_KEY_KP_7, 12},
  71. {GLFW_KEY_KP_8, 13},
  72. {GLFW_KEY_KP_9, 14},
  73. },
  74. };
  75. struct InputDevice : midi::InputDevice {
  76. int deviceId;
  77. int octave = 5;
  78. std::map<int, int> pressedNotes;
  79. void onKeyPress(int key) {
  80. // Do nothing if no ports are subscribed
  81. if (subscribed.empty())
  82. return;
  83. auto keyNote = deviceKeyNote[deviceId];
  84. auto it = keyNote.find(key);
  85. if (it == keyNote.end())
  86. return;
  87. int note = it->second;
  88. if (note < 0) {
  89. if (note == CMD_OCTAVE_DOWN)
  90. octave--;
  91. else if (note == CMD_OCTAVE_UP)
  92. octave++;
  93. octave = math::clamp(octave, 0, 9);
  94. return;
  95. }
  96. note += 12 * octave;
  97. if (note > 127)
  98. return;
  99. // MIDI note on
  100. midi::Message msg;
  101. msg.setStatus(0x9);
  102. msg.setNote(note);
  103. msg.setValue(127);
  104. onMessage(msg);
  105. pressedNotes[key] = note;
  106. }
  107. void onKeyRelease(int key) {
  108. // Do nothing if no ports are subscribed
  109. if (subscribed.empty())
  110. return;
  111. auto it = pressedNotes.find(key);
  112. if (it == pressedNotes.end())
  113. return;
  114. int note = it->second;
  115. // MIDI note off
  116. midi::Message msg;
  117. msg.setStatus(0x8);
  118. msg.setNote(note);
  119. msg.setValue(127);
  120. onMessage(msg);
  121. pressedNotes.erase(it);
  122. }
  123. };
  124. struct Driver : midi::Driver {
  125. InputDevice devices[deviceCount];
  126. Driver() {
  127. for (int deviceId = 0; deviceId < deviceCount; deviceId++) {
  128. devices[deviceId].deviceId = deviceId;
  129. }
  130. devices[1].octave = 3;
  131. }
  132. std::string getName() override {
  133. return "Computer keyboard";
  134. }
  135. std::vector<int> getInputDeviceIds() override {
  136. std::vector<int> deviceIds;
  137. for (int deviceId = 0; deviceId < deviceCount; deviceId++) {
  138. deviceIds.push_back(deviceId);
  139. }
  140. return deviceIds;
  141. }
  142. std::string getInputDeviceName(int deviceId) override {
  143. if (deviceId == 0)
  144. return "QWERTY keyboard (US)";
  145. else if (deviceId == 1)
  146. return "Numpad keyboard (US)";
  147. return "";
  148. }
  149. midi::InputDevice* subscribeInput(int deviceId, midi::Input* input) override {
  150. if (!(0 <= deviceId && deviceId < deviceCount))
  151. return NULL;
  152. devices[deviceId].subscribe(input);
  153. return &devices[deviceId];
  154. }
  155. void unsubscribeInput(int deviceId, midi::Input* input) override {
  156. if (!(0 <= deviceId && deviceId < deviceCount))
  157. return;
  158. devices[deviceId].unsubscribe(input);
  159. }
  160. void onKeyPress(int key) {
  161. for (int deviceId = 0; deviceId < deviceCount; deviceId++) {
  162. devices[deviceId].onKeyPress(key);
  163. }
  164. }
  165. void onKeyRelease(int key) {
  166. for (int deviceId = 0; deviceId < deviceCount; deviceId++) {
  167. devices[deviceId].onKeyRelease(key);
  168. }
  169. }
  170. };
  171. void init() {
  172. driver = new Driver;
  173. midi::addDriver(DRIVER, driver);
  174. }
  175. void press(int key) {
  176. if (!driver)
  177. return;
  178. driver->onKeyPress(key);
  179. }
  180. void release(int key) {
  181. if (!driver)
  182. return;
  183. driver->onKeyRelease(key);
  184. }
  185. } // namespace keyboard
  186. } // namespace rack