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.

262 lines
4.8KB

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