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.

95 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. #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. virtual void setDevice(int device) {}
  47. std::string getChannelName(int channel);
  48. json_t *toJson();
  49. void fromJson(json_t *rootJ);
  50. };
  51. struct MidiInput : MidiIO {
  52. RtMidiIn *rtMidiIn = NULL;
  53. MidiInput();
  54. ~MidiInput();
  55. void setDriver(int driver) override;
  56. void setDevice(int device) override;
  57. virtual void onMessage(MidiMessage message) {}
  58. };
  59. struct MidiInputQueue : MidiInput {
  60. int queueSize = 8192;
  61. std::queue<MidiMessage> queue;
  62. void onMessage(MidiMessage message) override;
  63. /** If a MidiMessage is available, writes `message` and return true */
  64. bool shift(MidiMessage *message);
  65. };
  66. struct MidiOutput : MidiIO {
  67. RtMidiOut *rtMidiOut = NULL;
  68. MidiOutput();
  69. ~MidiOutput();
  70. void setDriver(int driver) override;
  71. void setDevice(int device) override;
  72. };
  73. } // namespace rack