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.

147 lines
2.9KB

  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. // MidiIO
  27. ////////////////////
  28. struct MidiInputDriver;
  29. struct MidiOutputDriver;
  30. struct MidiInputDevice;
  31. struct MidiOutputDevice;
  32. struct MidiIO {
  33. int driver = -1;
  34. int device = -1;
  35. /* For MIDI output, the channel to output messages.
  36. For MIDI input, the channel to filter.
  37. Set to -1 to allow all MIDI channels (for input).
  38. Zero indexed.
  39. */
  40. int channel = -1;
  41. virtual ~MidiIO() {}
  42. std::vector<int> getDrivers();
  43. std::string getDriverName(int driver);
  44. virtual void setDriver(int driver) = 0;
  45. virtual int getDeviceCount() = 0;
  46. virtual std::string getDeviceName(int device) = 0;
  47. virtual void setDevice(int device) = 0;
  48. std::string getChannelName(int channel);
  49. json_t *toJson();
  50. void fromJson(json_t *rootJ);
  51. };
  52. struct MidiInput : MidiIO {
  53. /** Not owned */
  54. MidiInputDriver *midiInputDriver = NULL;
  55. /** Not owned, must unsubscribe when destructed */
  56. MidiInputDevice *midiInputDevice = NULL;
  57. MidiInput();
  58. ~MidiInput();
  59. void setDriver(int driver) override;
  60. int getDeviceCount() override;
  61. std::string getDeviceName(int device) override;
  62. void setDevice(int device) override;
  63. virtual void onMessage(MidiMessage message) {}
  64. };
  65. struct MidiInputQueue : MidiInput {
  66. int queueSize = 8192;
  67. // TODO Switch to RingBuffer
  68. std::queue<MidiMessage> queue;
  69. void onMessage(MidiMessage message) override;
  70. /** If a MidiMessage is available, writes `message` and return true */
  71. bool shift(MidiMessage *message);
  72. };
  73. struct MidiOutput : MidiIO {
  74. /** Not owned */
  75. MidiOutputDriver *midiOutputDriver = NULL;
  76. /** Not owned, must unsubscribe when destructed */
  77. MidiOutputDevice *midiOutputDevice = NULL;
  78. MidiOutput();
  79. ~MidiOutput();
  80. void setDriver(int driver) override;
  81. void setDevice(int device) override;
  82. };
  83. ////////////////////
  84. // MidiIODriver
  85. ////////////////////
  86. struct MidiIODriver {
  87. virtual ~MidiIODriver() {}
  88. virtual int getDeviceCount() = 0;
  89. virtual std::string getDeviceName(int device) = 0;
  90. };
  91. struct MidiInputDriver : MidiIODriver {
  92. virtual MidiInputDevice *getDevice(int device) = 0;
  93. };
  94. struct MidiOutputDriver : MidiIODriver {
  95. virtual MidiOutputDevice *getDevice(int device) = 0;
  96. };
  97. ////////////////////
  98. // MidiIODevice
  99. ////////////////////
  100. struct MidiIODevice {
  101. virtual ~MidiIODevice() {}
  102. };
  103. struct MidiInputDevice : MidiIODevice {
  104. std::set<MidiInput*> subscribed;
  105. void subscribe(MidiInput *midiInput);
  106. /** Deletes itself if nothing is subscribed */
  107. void unsubscribe(MidiInput *midiInput);
  108. void onMessage(MidiMessage message);
  109. };
  110. struct MidiOutputDevice : MidiIODevice {
  111. // TODO
  112. };
  113. } // namespace rack