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.

226 lines
4.2KB

  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 {return "Computer keyboard";}
  133. std::vector<int> getInputDeviceIds() override {
  134. std::vector<int> deviceIds;
  135. for (int deviceId = 0; deviceId < deviceCount; deviceId++) {
  136. deviceIds.push_back(deviceId);
  137. }
  138. return deviceIds;
  139. }
  140. std::string getInputDeviceName(int deviceId) override {
  141. if (deviceId == 0)
  142. return "QWERTY keyboard (US)";
  143. else if (deviceId == 1)
  144. return "Numpad keyboard (US)";
  145. return "";
  146. }
  147. midi::InputDevice *subscribeInput(int deviceId, midi::Input *input) override {
  148. if (!(0 <= deviceId && deviceId < deviceCount))
  149. return NULL;
  150. devices[deviceId].subscribe(input);
  151. return &devices[deviceId];
  152. }
  153. void unsubscribeInput(int deviceId, midi::Input *input) override {
  154. if (!(0 <= deviceId && deviceId < deviceCount))
  155. return;
  156. devices[deviceId].unsubscribe(input);
  157. }
  158. void onKeyPress(int key) {
  159. for (int deviceId = 0; deviceId < deviceCount; deviceId++) {
  160. devices[deviceId].onKeyPress(key);
  161. }
  162. }
  163. void onKeyRelease(int key) {
  164. for (int deviceId = 0; deviceId < deviceCount; deviceId++) {
  165. devices[deviceId].onKeyRelease(key);
  166. }
  167. }
  168. };
  169. void init() {
  170. driver = new Driver;
  171. midi::addDriver(DRIVER, driver);
  172. }
  173. void press(int key) {
  174. if (!driver)
  175. return;
  176. driver->onKeyPress(key);
  177. }
  178. void release(int key) {
  179. if (!driver)
  180. return;
  181. driver->onKeyRelease(key);
  182. }
  183. } // namespace keyboard
  184. } // namespace rack