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.

139 lines
2.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. 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. // Device
  28. ////////////////////
  29. struct Device {
  30. virtual ~Device() {}
  31. };
  32. struct Input;
  33. struct InputDevice : Device {
  34. std::set<Input*> subscribed;
  35. void subscribe(Input *input);
  36. void unsubscribe(Input *input);
  37. void onMessage(Message message);
  38. };
  39. struct OutputDevice : Device {
  40. // TODO
  41. };
  42. ////////////////////
  43. // Driver
  44. ////////////////////
  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 *subscribeInputDevice(int deviceId, Input *input) {return NULL;}
  51. virtual void unsubscribeInputDevice(int deviceId, Input *input) {}
  52. // virtual std::vector<int> getOutputDeviceIds() = 0;
  53. // virtual std::string getOutputDeviceName(int deviceId) = 0;
  54. // virtual OutputDevice *subscribeOutputDevice(int deviceId, Output *midiOutput) = 0;
  55. // virtual void unsubscribeOutputDevice(int deviceId, Output *midiOutput) = 0;
  56. };
  57. ////////////////////
  58. // IO
  59. ////////////////////
  60. struct IO {
  61. int driverId = -1;
  62. int deviceId = -1;
  63. /* For MIDI output, the channel to output messages.
  64. For MIDI input, the channel to filter.
  65. Set to -1 to allow all MIDI channels (for input).
  66. Zero indexed.
  67. */
  68. int channel = -1;
  69. /** Not owned */
  70. Driver *driver = NULL;
  71. virtual ~IO();
  72. std::vector<int> getDriverIds();
  73. std::string getDriverName(int driverId);
  74. void setDriverId(int driverId);
  75. virtual std::vector<int> getDeviceIds() = 0;
  76. virtual std::string getDeviceName(int deviceId) = 0;
  77. virtual void setDeviceId(int deviceId) = 0;
  78. std::string getChannelName(int channel);
  79. json_t *toJson();
  80. void fromJson(json_t *rootJ);
  81. };
  82. struct Input : IO {
  83. Input();
  84. ~Input();
  85. std::vector<int> getDeviceIds() override;
  86. std::string getDeviceName(int deviceId) override;
  87. void setDeviceId(int deviceId) override;
  88. virtual void onMessage(Message message) {}
  89. };
  90. struct InputQueue : Input {
  91. int queueMaxSize = 8192;
  92. std::queue<Message> queue;
  93. void onMessage(Message message) override;
  94. /** If a Message is available, writes `message` and return true */
  95. bool shift(Message *message);
  96. };
  97. struct Output : IO {
  98. Output();
  99. ~Output();
  100. void setDeviceId(int deviceId) override;
  101. };
  102. void init();
  103. void destroy();
  104. /** Registers a new MIDI driver. Takes pointer ownership. */
  105. void addDriver(int driverId, Driver *driver);
  106. } // namespace midi
  107. } // namespace rack