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.

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