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.

93 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. uint8_t note() {
  22. return data1 & 0x7f;
  23. }
  24. uint8_t value() {
  25. return data2 & 0x7f;
  26. }
  27. };
  28. struct MidiIO {
  29. int driver = -1;
  30. int device = -1;
  31. /* For MIDI output, the channel to output messages.
  32. For MIDI input, the channel to filter.
  33. Set to -1 to allow all MIDI channels (for input).
  34. Zero indexed.
  35. */
  36. int channel = -1;
  37. RtMidi *rtMidi = NULL;
  38. /** Cached */
  39. std::string deviceName;
  40. virtual ~MidiIO() {}
  41. std::vector<int> getDrivers();
  42. std::string getDriverName(int driver);
  43. virtual void setDriver(int driver) {}
  44. int getDeviceCount();
  45. std::string getDeviceName(int device);
  46. void setDevice(int device);
  47. std::string getChannelName(int channel);
  48. json_t *toJson();
  49. void fromJson(json_t *rootJ);
  50. virtual void onMessage(MidiMessage message) {}
  51. };
  52. struct MidiInput : MidiIO {
  53. RtMidiIn *rtMidiIn = NULL;
  54. MidiInput();
  55. ~MidiInput();
  56. void setDriver(int driver) override;
  57. };
  58. struct MidiInputQueue : MidiInput {
  59. int queueSize = 8192;
  60. std::queue<MidiMessage> queue;
  61. void onMessage(MidiMessage message) override;
  62. /** If a MidiMessage is available, writes `message` and return true */
  63. bool shift(MidiMessage *message);
  64. };
  65. struct MidiOutput : MidiIO {
  66. RtMidiOut *rtMidiOut = NULL;
  67. MidiOutput();
  68. ~MidiOutput();
  69. void setDriver(int driver) override;
  70. };
  71. } // namespace rack