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.

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