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.

87 lines
1.7KB

  1. #pragma once
  2. #include "util/common.hpp"
  3. #include <queue>
  4. #include <vector>
  5. #include <jansson.h>
  6. #pragma GCC diagnostic push
  7. #pragma GCC diagnostic ignored "-Wsuggest-override"
  8. #include "rtmidi/RtMidi.h"
  9. #pragma GCC diagnostic pop
  10. namespace rack {
  11. struct MidiMessage {
  12. uint8_t cmd = 0x00;
  13. uint8_t data1 = 0x00;
  14. uint8_t data2 = 0x00;
  15. uint8_t channel() {
  16. return cmd & 0xf;
  17. }
  18. uint8_t status() {
  19. return (cmd >> 4) & 0xf;
  20. }
  21. };
  22. struct MidiIO {
  23. int driver = -1;
  24. int device = -1;
  25. /* For MIDI output, the channel to output messages.
  26. For MIDI input, the channel to filter.
  27. Set to -1 to allow all MIDI channels (for input).
  28. Zero indexed.
  29. */
  30. int channel = -1;
  31. RtMidi *rtMidi = NULL;
  32. /** Cached */
  33. std::string deviceName;
  34. virtual ~MidiIO();
  35. std::vector<int> getDrivers();
  36. std::string getDriverName(int driver);
  37. virtual void setDriver(int driver) {}
  38. int getDeviceCount();
  39. std::string getDeviceName(int device);
  40. void setDevice(int device);
  41. std::string getChannelName(int channel);
  42. /** Returns whether the audio stream is open and running */
  43. bool isActive();
  44. json_t *toJson();
  45. void fromJson(json_t *rootJ);
  46. };
  47. struct MidiInput : MidiIO {
  48. RtMidiIn *rtMidiIn = NULL;
  49. MidiInput();
  50. void setDriver(int driver) override;
  51. virtual void onMessage(const MidiMessage &message) {}
  52. };
  53. struct MidiInputQueue : MidiInput {
  54. int queueSize = 8192;
  55. std::queue<MidiMessage> queue;
  56. void onMessage(const MidiMessage &message) override;
  57. /** If a MidiMessage is available, writes `message` and return true */
  58. bool shift(MidiMessage *message);
  59. };
  60. struct MidiOutput : MidiIO {
  61. RtMidiOut *rtMidiOut = NULL;
  62. MidiOutput();
  63. void setDriver(int driver) override;
  64. };
  65. } // namespace rack