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.

159 lines
3.3KB

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