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.

184 lines
3.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. 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() {return "";}
  53. virtual std::vector<int> getInputDeviceIds() {return {};}
  54. virtual std::string getInputDeviceName(int deviceId) {return "";}
  55. virtual InputDevice *subscribeInput(int deviceId, Input *input) {return NULL;}
  56. virtual void unsubscribeInput(int deviceId, Input *input) {}
  57. virtual std::vector<int> getOutputDeviceIds() {return {};}
  58. virtual std::string getOutputDeviceName(int deviceId) {return "";}
  59. virtual OutputDevice *subscribeOutput(int deviceId, Output *output) {return NULL;}
  60. virtual void unsubscribeOutput(int deviceId, Output *output) {}
  61. };
  62. ////////////////////
  63. // Device
  64. ////////////////////
  65. struct Device {
  66. virtual ~Device() {}
  67. };
  68. struct InputDevice : Device {
  69. std::set<Input*> subscribed;
  70. void subscribe(Input *input);
  71. void unsubscribe(Input *input);
  72. void onMessage(Message message);
  73. };
  74. struct OutputDevice : Device {
  75. std::set<Output*> subscribed;
  76. void subscribe(Output *input);
  77. void unsubscribe(Output *input);
  78. virtual void sendMessage(Message message) {}
  79. };
  80. ////////////////////
  81. // Port
  82. ////////////////////
  83. struct Port {
  84. int driverId = -1;
  85. int deviceId = -1;
  86. /* For MIDI output, the channel to output messages.
  87. For MIDI input, the channel to filter.
  88. Set to -1 to allow all MIDI channels (for input).
  89. Zero indexed.
  90. */
  91. int channel = -1;
  92. /** Not owned */
  93. Driver *driver = NULL;
  94. /** Remember to call setDriverId(-1) in subclass destructors. */
  95. virtual ~Port() {}
  96. std::vector<int> getDriverIds();
  97. std::string getDriverName(int driverId);
  98. void setDriverId(int driverId);
  99. virtual std::vector<int> getDeviceIds() = 0;
  100. virtual std::string getDeviceName(int deviceId) = 0;
  101. virtual void setDeviceId(int deviceId) = 0;
  102. virtual std::vector<int> getChannels() = 0;
  103. std::string getChannelName(int channel);
  104. void setChannel(int channel);
  105. json_t *toJson();
  106. void fromJson(json_t *rootJ);
  107. };
  108. struct Input : Port {
  109. /** Not owned */
  110. InputDevice *inputDevice = NULL;
  111. Input();
  112. ~Input();
  113. void reset();
  114. std::vector<int> getDeviceIds() override;
  115. std::string getDeviceName(int deviceId) override;
  116. void setDeviceId(int deviceId) override;
  117. std::vector<int> getChannels() override;
  118. virtual void onMessage(Message message) {}
  119. };
  120. struct InputQueue : Input {
  121. int queueMaxSize = 8192;
  122. std::queue<Message> queue;
  123. void onMessage(Message message) override;
  124. /** If a Message is available, writes `message` and return true */
  125. bool shift(Message *message);
  126. };
  127. struct Output : Port {
  128. /** Not owned */
  129. OutputDevice *outputDevice = NULL;
  130. Output();
  131. ~Output();
  132. void reset();
  133. std::vector<int> getDeviceIds() override;
  134. std::string getDeviceName(int deviceId) override;
  135. void setDeviceId(int deviceId) override;
  136. std::vector<int> getChannels() override;
  137. void sendMessage(Message message);
  138. };
  139. void init();
  140. void destroy();
  141. /** Registers a new MIDI driver. Takes pointer ownership. */
  142. void addDriver(int driverId, Driver *driver);
  143. } // namespace midi
  144. } // namespace rack