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.

239 lines
4.4KB

  1. #pragma once
  2. #include <common.hpp>
  3. #include <vector>
  4. #include <queue>
  5. #include <set>
  6. #include <jansson.h>
  7. namespace rack {
  8. /** MIDI driver
  9. */
  10. namespace midi {
  11. struct Message {
  12. uint8_t size = 3;
  13. uint8_t bytes[3] = {};
  14. uint8_t getSize() {
  15. return size;
  16. }
  17. void setSize(uint8_t size) {
  18. assert(size <= 3);
  19. this->size = size;
  20. }
  21. uint8_t getChannel() {
  22. return bytes[0] & 0xf;
  23. }
  24. void setChannel(uint8_t channel) {
  25. bytes[0] = (bytes[0] & 0xf0) | (channel & 0xf);
  26. }
  27. uint8_t getStatus() {
  28. return bytes[0] >> 4;
  29. }
  30. void setStatus(uint8_t status) {
  31. bytes[0] = (bytes[0] & 0xf) | (status << 4);
  32. }
  33. uint8_t getNote() {
  34. return bytes[1];
  35. }
  36. void setNote(uint8_t note) {
  37. bytes[1] = note & 0x7f;
  38. }
  39. uint8_t getValue() {
  40. return bytes[2];
  41. }
  42. void setValue(uint8_t value) {
  43. bytes[2] = value & 0x7f;
  44. }
  45. };
  46. ////////////////////
  47. // Driver
  48. ////////////////////
  49. struct InputDevice;
  50. struct Input;
  51. struct OutputDevice;
  52. struct Output;
  53. struct Driver {
  54. virtual ~Driver() {}
  55. virtual std::string getName() {
  56. return "";
  57. }
  58. virtual std::vector<int> getInputDeviceIds() {
  59. return {};
  60. }
  61. virtual std::string getInputDeviceName(int deviceId) {
  62. return "";
  63. }
  64. virtual InputDevice* subscribeInput(int deviceId, Input* input) {
  65. return NULL;
  66. }
  67. virtual void unsubscribeInput(int deviceId, Input* input) {}
  68. virtual std::vector<int> getOutputDeviceIds() {
  69. return {};
  70. }
  71. virtual std::string getOutputDeviceName(int deviceId) {
  72. return "";
  73. }
  74. virtual OutputDevice* subscribeOutput(int deviceId, Output* output) {
  75. return NULL;
  76. }
  77. virtual void unsubscribeOutput(int deviceId, Output* output) {}
  78. };
  79. ////////////////////
  80. // Device
  81. ////////////////////
  82. struct Device {
  83. virtual ~Device() {}
  84. virtual std::string getName() {
  85. return "";
  86. }
  87. };
  88. struct InputDevice : Device {
  89. std::set<Input*> subscribed;
  90. void subscribe(Input* input);
  91. void unsubscribe(Input* input);
  92. void onMessage(Message message);
  93. };
  94. struct OutputDevice : Device {
  95. std::set<Output*> subscribed;
  96. void subscribe(Output* input);
  97. void unsubscribe(Output* input);
  98. virtual void sendMessage(Message message) {}
  99. };
  100. ////////////////////
  101. // Port
  102. ////////////////////
  103. struct Port {
  104. /* For MIDI output, the channel to output messages.
  105. For MIDI input, the channel to filter.
  106. Set to -1 to allow all MIDI channels (for input).
  107. Zero indexed.
  108. */
  109. int channel = -1;
  110. // private
  111. int driverId = -1;
  112. int deviceId = -1;
  113. /** Not owned */
  114. Driver* driver = NULL;
  115. Device* device = NULL;
  116. Port();
  117. virtual ~Port();
  118. Driver* getDriver() {
  119. return driver;
  120. }
  121. int getDriverId() {
  122. return driverId;
  123. }
  124. void setDriverId(int driverId);
  125. Device* getDevice() {
  126. return device;
  127. }
  128. virtual std::vector<int> getDeviceIds() = 0;
  129. int getDeviceId() {
  130. return deviceId;
  131. }
  132. virtual void setDeviceId(int deviceId) = 0;
  133. virtual std::string getDeviceName(int deviceId) = 0;
  134. virtual std::vector<int> getChannels() = 0;
  135. int getChannel() {
  136. return channel;
  137. }
  138. void setChannel(int channel);
  139. std::string getChannelName(int channel);
  140. json_t* toJson();
  141. void fromJson(json_t* rootJ);
  142. };
  143. struct Input : Port {
  144. /** Not owned */
  145. InputDevice* inputDevice = NULL;
  146. Input();
  147. ~Input();
  148. void reset();
  149. std::vector<int> getDeviceIds() override {
  150. if (driver)
  151. return driver->getInputDeviceIds();
  152. return {};
  153. }
  154. void setDeviceId(int deviceId) override;
  155. std::string getDeviceName(int deviceId) override {
  156. if (driver)
  157. return driver->getInputDeviceName(deviceId);
  158. return "";
  159. }
  160. std::vector<int> getChannels() override;
  161. virtual void onMessage(Message message) {}
  162. };
  163. struct InputQueue : Input {
  164. int queueMaxSize = 8192;
  165. std::queue<Message> queue;
  166. void onMessage(Message message) override;
  167. /** If a Message is available, writes `message` and return true */
  168. bool shift(Message* message);
  169. };
  170. struct Output : Port {
  171. /** Not owned */
  172. OutputDevice* outputDevice = NULL;
  173. Output();
  174. ~Output();
  175. void reset();
  176. std::vector<int> getDeviceIds() override {
  177. if (driver)
  178. return driver->getOutputDeviceIds();
  179. return {};
  180. }
  181. void setDeviceId(int deviceId) override;
  182. std::string getDeviceName(int deviceId) override {
  183. if (driver)
  184. return driver->getInputDeviceName(deviceId);
  185. return "";
  186. }
  187. std::vector<int> getChannels() override;
  188. void sendMessage(Message message);
  189. };
  190. void init();
  191. void destroy();
  192. /** Registers a new MIDI driver. Takes pointer ownership. */
  193. void addDriver(int driverId, Driver* driver);
  194. std::vector<int> getDriverIds();
  195. Driver* getDriver(int driverId);
  196. } // namespace midi
  197. } // namespace rack