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.

119 lines
2.2KB

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