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.

260 lines
4.8KB

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