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.

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