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.

205 lines
4.0KB

  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 *longMessage = NULL;
  15. void setSysex(uint8_t *msg, uint8_t len)
  16. {
  17. size=len;
  18. longMessage = msg;
  19. }
  20. void setSize(uint8_t size) {
  21. assert(size <= 3);
  22. this->size = size;
  23. }
  24. uint8_t getChannel() {
  25. return bytes[0] & 0xf;
  26. }
  27. void setChannel(uint8_t channel) {
  28. bytes[0] = (bytes[0] & 0xf0) | (channel & 0xf);
  29. }
  30. uint8_t getStatus() {
  31. return bytes[0] >> 4;
  32. }
  33. void setStatus(uint8_t status) {
  34. bytes[0] = (bytes[0] & 0xf) | (status << 4);
  35. }
  36. uint8_t getNote() {
  37. return bytes[1];
  38. }
  39. void setNote(uint8_t note) {
  40. bytes[1] = note & 0x7f;
  41. }
  42. uint8_t getValue() {
  43. return bytes[2];
  44. }
  45. void setValue(uint8_t value) {
  46. bytes[2] = value & 0x7f;
  47. }
  48. };
  49. ////////////////////
  50. // Driver
  51. ////////////////////
  52. struct InputDevice;
  53. struct Input;
  54. struct OutputDevice;
  55. struct Output;
  56. struct Driver {
  57. virtual ~Driver() {}
  58. virtual std::string getName() {
  59. return "";
  60. }
  61. virtual std::vector<int> getInputDeviceIds() {
  62. return {};
  63. }
  64. virtual std::string getInputDeviceName(int deviceId) {
  65. return "";
  66. }
  67. virtual InputDevice* subscribeInput(int deviceId, Input* input) {
  68. return NULL;
  69. }
  70. virtual void unsubscribeInput(int deviceId, Input* input) {}
  71. virtual std::vector<int> getOutputDeviceIds() {
  72. return {};
  73. }
  74. virtual std::string getOutputDeviceName(int deviceId) {
  75. return "";
  76. }
  77. virtual OutputDevice* subscribeOutput(int deviceId, Output* output) {
  78. return NULL;
  79. }
  80. virtual void unsubscribeOutput(int deviceId, Output* output) {}
  81. };
  82. ////////////////////
  83. // Device
  84. ////////////////////
  85. struct Device {
  86. virtual ~Device() {}
  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. int driverId = -1;
  105. int deviceId = -1;
  106. /* For MIDI output, the channel to output messages.
  107. For MIDI input, the channel to filter.
  108. Set to -1 to allow all MIDI channels (for input).
  109. Zero indexed.
  110. */
  111. int channel = -1;
  112. /** Not owned */
  113. Driver* driver = NULL;
  114. /** Remember to call setDriverId(-1) in subclass destructors. */
  115. virtual ~Port() {}
  116. std::vector<int> getDriverIds();
  117. std::string getDriverName(int driverId);
  118. void setDriverId(int driverId);
  119. virtual std::vector<int> getDeviceIds() = 0;
  120. virtual std::string getDeviceName(int deviceId) = 0;
  121. virtual void setDeviceId(int deviceId) = 0;
  122. virtual std::vector<int> getChannels() = 0;
  123. std::string getChannelName(int channel);
  124. void setChannel(int channel);
  125. json_t* toJson();
  126. void fromJson(json_t* rootJ);
  127. };
  128. struct Input : Port {
  129. /** Not owned */
  130. InputDevice* inputDevice = NULL;
  131. Input();
  132. ~Input();
  133. void reset();
  134. std::vector<int> getDeviceIds() override;
  135. std::string getDeviceName(int deviceId) override;
  136. void setDeviceId(int deviceId) override;
  137. std::vector<int> getChannels() override;
  138. virtual void onMessage(Message message) {}
  139. };
  140. struct InputQueue : Input {
  141. int queueMaxSize = 8192;
  142. std::queue<Message> queue;
  143. void onMessage(Message message) override;
  144. /** If a Message is available, writes `message` and return true */
  145. bool shift(Message* message);
  146. };
  147. struct Output : Port {
  148. /** Not owned */
  149. OutputDevice* outputDevice = NULL;
  150. Output();
  151. ~Output();
  152. void reset();
  153. std::vector<int> getDeviceIds() override;
  154. std::string getDeviceName(int deviceId) override;
  155. void setDeviceId(int deviceId) override;
  156. std::vector<int> getChannels() override;
  157. void sendMessage(Message message);
  158. };
  159. void init();
  160. void destroy();
  161. /** Registers a new MIDI driver. Takes pointer ownership. */
  162. void addDriver(int driverId, Driver* driver);
  163. } // namespace midi
  164. } // namespace rack