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.

180 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. uint8_t getChannel() {
  15. return bytes[0] & 0xf;
  16. }
  17. void setChannel(uint8_t channel) {
  18. bytes[0] = (bytes[0] & 0xf0) | (channel & 0xf);
  19. }
  20. uint8_t getStatus() {
  21. return (bytes[0] >> 4) & 0xf;
  22. }
  23. void setStatus(uint8_t status) {
  24. bytes[0] = (bytes[0] & 0xf) | ((status << 4) & 0xf0);
  25. }
  26. uint8_t getNote() {
  27. return bytes[1] & 0x7f;
  28. }
  29. void setNote(uint8_t note) {
  30. bytes[1] = note & 0x7f;
  31. }
  32. uint8_t getValue() {
  33. return bytes[2] & 0x7f;
  34. }
  35. void setValue(uint8_t value) {
  36. bytes[2] = value & 0x7f;
  37. }
  38. };
  39. ////////////////////
  40. // Driver
  41. ////////////////////
  42. struct InputDevice;
  43. struct Input;
  44. struct OutputDevice;
  45. struct Output;
  46. struct Driver {
  47. virtual ~Driver() {}
  48. virtual std::string getName() {return "";}
  49. virtual std::vector<int> getInputDeviceIds() {return {};}
  50. virtual std::string getInputDeviceName(int deviceId) {return "";}
  51. virtual InputDevice *subscribeInput(int deviceId, Input *input) {return NULL;}
  52. virtual void unsubscribeInput(int deviceId, Input *input) {}
  53. virtual std::vector<int> getOutputDeviceIds() {return {};}
  54. virtual std::string getOutputDeviceName(int deviceId) {return "";}
  55. virtual OutputDevice *subscribeOutput(int deviceId, Output *output) {return NULL;}
  56. virtual void unsubscribeOutput(int deviceId, Output *output) {}
  57. };
  58. ////////////////////
  59. // Device
  60. ////////////////////
  61. struct Device {
  62. virtual ~Device() {}
  63. };
  64. struct InputDevice : Device {
  65. std::set<Input*> subscribed;
  66. void subscribe(Input *input);
  67. void unsubscribe(Input *input);
  68. void onMessage(Message message);
  69. };
  70. struct OutputDevice : Device {
  71. std::set<Output*> subscribed;
  72. void subscribe(Output *input);
  73. void unsubscribe(Output *input);
  74. virtual void sendMessage(Message message) {}
  75. };
  76. ////////////////////
  77. // Port
  78. ////////////////////
  79. struct Port {
  80. int driverId = -1;
  81. int deviceId = -1;
  82. /* For MIDI output, the channel to output messages.
  83. For MIDI input, the channel to filter.
  84. Set to -1 to allow all MIDI channels (for input).
  85. Zero indexed.
  86. */
  87. int channel = -1;
  88. /** Not owned */
  89. Driver *driver = NULL;
  90. /** Remember to call setDriverId(-1) in subclass destructors. */
  91. virtual ~Port() {}
  92. std::vector<int> getDriverIds();
  93. std::string getDriverName(int driverId);
  94. void setDriverId(int driverId);
  95. virtual std::vector<int> getDeviceIds() = 0;
  96. virtual std::string getDeviceName(int deviceId) = 0;
  97. virtual void setDeviceId(int deviceId) = 0;
  98. virtual std::vector<int> getChannels() = 0;
  99. std::string getChannelName(int channel);
  100. void setChannel(int channel);
  101. json_t *toJson();
  102. void fromJson(json_t *rootJ);
  103. };
  104. struct Input : Port {
  105. /** Not owned */
  106. InputDevice *inputDevice = NULL;
  107. Input();
  108. ~Input();
  109. void reset();
  110. std::vector<int> getDeviceIds() override;
  111. std::string getDeviceName(int deviceId) override;
  112. void setDeviceId(int deviceId) override;
  113. std::vector<int> getChannels() override;
  114. virtual void onMessage(Message message) {}
  115. };
  116. struct InputQueue : Input {
  117. int queueMaxSize = 8192;
  118. std::queue<Message> queue;
  119. void onMessage(Message message) override;
  120. /** If a Message is available, writes `message` and return true */
  121. bool shift(Message *message);
  122. };
  123. struct Output : Port {
  124. /** Not owned */
  125. OutputDevice *outputDevice = NULL;
  126. Output();
  127. ~Output();
  128. void reset();
  129. std::vector<int> getDeviceIds() override;
  130. std::string getDeviceName(int deviceId) override;
  131. void setDeviceId(int deviceId) override;
  132. std::vector<int> getChannels() override;
  133. void sendMessage(Message message);
  134. };
  135. void init();
  136. void destroy();
  137. /** Registers a new MIDI driver. Takes pointer ownership. */
  138. void addDriver(int driverId, Driver *driver);
  139. } // namespace midi
  140. } // namespace rack