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.

97 lines
1.8KB

  1. #pragma once
  2. #include "util/common.hpp"
  3. #include <queue>
  4. #include <vector>
  5. #include <jansson.h>
  6. #pragma GCC diagnostic push
  7. #ifndef __clang__
  8. #pragma GCC diagnostic ignored "-Wsuggest-override"
  9. #endif
  10. #include "rtmidi/RtMidi.h"
  11. #pragma GCC diagnostic pop
  12. namespace rack {
  13. struct MidiMessage {
  14. uint8_t cmd = 0x00;
  15. uint8_t data1 = 0x00;
  16. uint8_t data2 = 0x00;
  17. uint8_t channel() {
  18. return cmd & 0xf;
  19. }
  20. uint8_t status() {
  21. return (cmd >> 4) & 0xf;
  22. }
  23. uint8_t note() {
  24. return data1 & 0x7f;
  25. }
  26. uint8_t value() {
  27. return data2 & 0x7f;
  28. }
  29. };
  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. RtMidi *rtMidi = NULL;
  40. /** Cached */
  41. std::string deviceName;
  42. virtual ~MidiIO() {}
  43. std::vector<int> getDrivers();
  44. std::string getDriverName(int driver);
  45. virtual void setDriver(int driver) {}
  46. int getDeviceCount();
  47. std::string getDeviceName(int device);
  48. virtual void setDevice(int device) {}
  49. std::string getChannelName(int channel);
  50. json_t *toJson();
  51. void fromJson(json_t *rootJ);
  52. };
  53. struct MidiInput : MidiIO {
  54. RtMidiIn *rtMidiIn = NULL;
  55. MidiInput();
  56. ~MidiInput();
  57. void setDriver(int driver) override;
  58. void setDevice(int device) override;
  59. virtual void onMessage(MidiMessage message) {}
  60. };
  61. struct MidiInputQueue : MidiInput {
  62. int queueSize = 8192;
  63. std::queue<MidiMessage> queue;
  64. void onMessage(MidiMessage message) override;
  65. /** If a MidiMessage is available, writes `message` and return true */
  66. bool shift(MidiMessage *message);
  67. };
  68. struct MidiOutput : MidiIO {
  69. RtMidiOut *rtMidiOut = NULL;
  70. MidiOutput();
  71. ~MidiOutput();
  72. void setDriver(int driver) override;
  73. void setDevice(int device) override;
  74. };
  75. } // namespace rack