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.

198 lines
3.9KB

  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. };
  94. ////////////////////
  95. // Port
  96. ////////////////////
  97. struct Port {
  98. int driverId = -1;
  99. int deviceId = -1;
  100. /* For MIDI output, the channel to output messages.
  101. For MIDI input, the channel to filter.
  102. Set to -1 to allow all MIDI channels (for input).
  103. Zero indexed.
  104. */
  105. int channel = -1;
  106. /** Not owned */
  107. Driver* driver = NULL;
  108. /** Remember to call setDriverId(-1) in subclass destructors. */
  109. virtual ~Port() {}
  110. std::vector<int> getDriverIds();
  111. std::string getDriverName(int driverId);
  112. void setDriverId(int driverId);
  113. virtual std::vector<int> getDeviceIds() = 0;
  114. virtual std::string getDeviceName(int deviceId) = 0;
  115. virtual void setDeviceId(int deviceId) = 0;
  116. virtual std::vector<int> getChannels() = 0;
  117. std::string getChannelName(int channel);
  118. void setChannel(int channel);
  119. json_t* toJson();
  120. void fromJson(json_t* rootJ);
  121. };
  122. struct Input : Port {
  123. /** Not owned */
  124. InputDevice* inputDevice = NULL;
  125. Input();
  126. ~Input();
  127. void reset();
  128. std::vector<int> getDeviceIds() override;
  129. std::string getDeviceName(int deviceId) override;
  130. void setDeviceId(int deviceId) override;
  131. std::vector<int> getChannels() override;
  132. virtual void onMessage(Message message) {}
  133. };
  134. struct InputQueue : Input {
  135. int queueMaxSize = 8192;
  136. std::queue<Message> queue;
  137. void onMessage(Message message) override;
  138. /** If a Message is available, writes `message` and return true */
  139. bool shift(Message* message);
  140. };
  141. struct Output : Port {
  142. /** Not owned */
  143. OutputDevice* outputDevice = NULL;
  144. Output();
  145. ~Output();
  146. void reset();
  147. std::vector<int> getDeviceIds() override;
  148. std::string getDeviceName(int deviceId) override;
  149. void setDeviceId(int deviceId) override;
  150. std::vector<int> getChannels() override;
  151. void sendMessage(Message message);
  152. };
  153. void init();
  154. void destroy();
  155. /** Registers a new MIDI driver. Takes pointer ownership. */
  156. void addDriver(int driverId, Driver* driver);
  157. } // namespace midi
  158. } // namespace rack