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.

177 lines
3.7KB

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