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.

142 lines
2.8KB

  1. #pragma once
  2. #include "util/common.hpp"
  3. #include <vector>
  4. #include <queue>
  5. #include <set>
  6. #include <jansson.h>
  7. namespace rack {
  8. struct MidiMessage {
  9. uint8_t cmd = 0x00;
  10. uint8_t data1 = 0x00;
  11. uint8_t data2 = 0x00;
  12. uint8_t channel() {
  13. return cmd & 0xf;
  14. }
  15. uint8_t status() {
  16. return (cmd >> 4) & 0xf;
  17. }
  18. uint8_t note() {
  19. return data1 & 0x7f;
  20. }
  21. uint8_t value() {
  22. return data2 & 0x7f;
  23. }
  24. };
  25. ////////////////////
  26. // MidiIODevice
  27. ////////////////////
  28. struct MidiIODevice {
  29. virtual ~MidiIODevice() {}
  30. };
  31. struct MidiInput;
  32. struct MidiInputDevice : MidiIODevice {
  33. std::set<MidiInput*> subscribed;
  34. void subscribe(MidiInput *midiInput);
  35. void unsubscribe(MidiInput *midiInput);
  36. void onMessage(MidiMessage message);
  37. };
  38. struct MidiOutputDevice : MidiIODevice {
  39. // TODO
  40. };
  41. ////////////////////
  42. // MidiIODriver
  43. ////////////////////
  44. struct MidiIODriver {
  45. virtual ~MidiIODriver() {}
  46. virtual std::vector<int> getDeviceIds() = 0;
  47. virtual std::string getDeviceName(int deviceId) = 0;
  48. };
  49. struct MidiInputDriver : MidiIODriver {
  50. virtual MidiInputDevice *getDevice(int deviceId) = 0;
  51. };
  52. struct MidiOutputDriver : MidiIODriver {
  53. virtual MidiOutputDevice *getDevice(int deviceId) = 0;
  54. };
  55. ////////////////////
  56. // MidiIO
  57. ////////////////////
  58. struct MidiIO {
  59. int driverId = -1;
  60. int deviceId = -1;
  61. /* For MIDI output, the channel to output messages.
  62. For MIDI input, the channel to filter.
  63. Set to -1 to allow all MIDI channels (for input).
  64. Zero indexed.
  65. */
  66. int channel = -1;
  67. virtual ~MidiIO() {}
  68. std::vector<int> getDriverIds();
  69. std::string getDriverName(int driverId);
  70. virtual void setDriverId(int driverId) = 0;
  71. virtual std::vector<int> getDeviceIds() = 0;
  72. virtual std::string getDeviceName(int deviceId) = 0;
  73. virtual void setDeviceId(int deviceId) = 0;
  74. std::string getChannelName(int channel);
  75. json_t *toJson();
  76. void fromJson(json_t *rootJ);
  77. };
  78. struct MidiInput : MidiIO {
  79. /** Not owned */
  80. MidiInputDriver *driver = NULL;
  81. /** Not owned, must unsubscribe when destructed */
  82. MidiInputDevice *device = NULL;
  83. MidiInput();
  84. ~MidiInput();
  85. void setDriverId(int driverId) override;
  86. std::vector<int> getDeviceIds() override;
  87. std::string getDeviceName(int deviceId) override;
  88. void setDeviceId(int deviceId) override;
  89. virtual void onMessage(MidiMessage message) {}
  90. };
  91. struct MidiInputQueue : MidiInput {
  92. int queueSize = 8192;
  93. // TODO Switch to RingBuffer
  94. std::queue<MidiMessage> queue;
  95. void onMessage(MidiMessage message) override;
  96. /** If a MidiMessage is available, writes `message` and return true */
  97. bool shift(MidiMessage *message);
  98. };
  99. struct MidiOutput : MidiIO {
  100. /** Not owned */
  101. MidiOutputDriver *driver = NULL;
  102. /** Not owned, must unsubscribe when destructed */
  103. MidiOutputDevice *device = NULL;
  104. MidiOutput();
  105. ~MidiOutput();
  106. void setDriverId(int driverId) override;
  107. void setDeviceId(int deviceId) override;
  108. };
  109. } // namespace rack