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.

206 lines
3.5KB

  1. #if 0
  2. #include <unordered_map>
  3. #include "rack.hpp"
  4. #pragma GCC diagnostic push
  5. #pragma GCC diagnostic ignored "-Wsuggest-override"
  6. #include "rtmidi/RtMidi.h"
  7. #pragma GCC diagnostic pop
  8. using namespace rack;
  9. struct IgnoreFlags {
  10. bool midiSysex = true;
  11. bool midiTime = true;
  12. bool midiSense = true;
  13. };
  14. struct MidiMessage {
  15. std::vector<unsigned char> bytes;
  16. double timeStamp;
  17. MidiMessage() : bytes(0), timeStamp(0.0) {};
  18. };
  19. /**
  20. * This class allows to use one instance of rtMidiIn with
  21. * multiple modules. A MidiIn port will be opened only once while multiple
  22. * instances can use it simultaniously, each receiving all its incoming messages.
  23. */
  24. struct MidiInWrapper : RtMidiIn {
  25. std::unordered_map<int, std::list<MidiMessage>> idMessagesMap;
  26. std::unordered_map<int, IgnoreFlags> ignoresMap;
  27. int uid_c = 0;
  28. MidiInWrapper() : RtMidiIn() {
  29. idMessagesMap = {};
  30. };
  31. int add() {
  32. int id = ++uid_c;
  33. idMessagesMap[id] = {};
  34. ignoresMap[id] = IgnoreFlags();
  35. return id;
  36. }
  37. void erase(int id) {
  38. idMessagesMap.erase(id);
  39. ignoresMap.erase(id);
  40. }
  41. };
  42. /**
  43. * Note: MidiIO is not thread safe which might become
  44. * important in the future
  45. */
  46. struct MidiIO {
  47. private:
  48. static std::unordered_map<std::string, MidiInWrapper *> midiInMap;
  49. /* TODO: add for midi out*/
  50. int id = -1;
  51. std::string deviceName = "";
  52. bool isOut = false;
  53. public:
  54. int channel;
  55. MidiIO(bool isOut = false);
  56. ~MidiIO() {
  57. close();
  58. }
  59. std::vector<std::string> getDevices();
  60. void openDevice(std::string deviceName);
  61. void setIgnores(bool ignoreSysex = true, bool ignoreTime = true, bool ignoreSense = true);
  62. std::string getDeviceName();
  63. void setChannel(int channel);
  64. double getMessage(std::vector<unsigned char> *msg);
  65. json_t *addBaseJson(json_t *rootJ);
  66. void baseFromJson(json_t *rootJ);
  67. bool isPortOpen();
  68. void close();
  69. /* called when midi port is set */
  70. virtual void resetMidi() {}
  71. /* called if a user switches or sets the device (and after this device is initialised)*/
  72. virtual void onDeviceChange() {}
  73. };
  74. struct TransitionSmoother {
  75. enum TransitionFunction {
  76. SMOOTHSTEP,
  77. EXP,
  78. LIN,
  79. };
  80. enum TransitionMode {
  81. DELTA,
  82. CONST,
  83. };
  84. float start;
  85. float end;
  86. float x;
  87. float delta;
  88. float step;
  89. TransitionFunction t;
  90. void set(float start, float end, int l = 1500, TransitionFunction t = LIN, TransitionMode m = DELTA, bool reset = true) {
  91. this->start = start;
  92. this->end = end;
  93. this->delta = end - start;
  94. this->t = t;
  95. if (reset || x >= 1) {
  96. this->x = 0;
  97. }
  98. switch (m) {
  99. case DELTA:
  100. /* If the change is smaller, the transition phase is longer */
  101. this->step = delta > 0 ? delta / l : -delta / l;
  102. break;
  103. case CONST:
  104. this->step = 1.0 / l;
  105. break;
  106. }
  107. }
  108. float next() {
  109. float next = start;
  110. x += step;
  111. if (x >= 1)
  112. return end;
  113. switch (t) {
  114. case SMOOTHSTEP:
  115. next += delta * x * x * (3 - 2 * x);
  116. break;
  117. case EXP:
  118. next += delta * x * x;
  119. break;
  120. case LIN:
  121. next += delta * x;
  122. break;
  123. }
  124. if ((delta > 0 && next > end) || (delta <= 0 && next < end))
  125. return end;
  126. return next;;
  127. }
  128. };
  129. //////////////////////
  130. // MIDI module widgets
  131. //////////////////////
  132. struct MidiItem : MenuItem {
  133. MidiIO *midiModule;
  134. void onAction(EventAction &e) override;
  135. };
  136. struct MidiChoice : ChoiceButton {
  137. MidiIO *midiModule;
  138. void step() override;
  139. void onAction(EventAction &e) override;
  140. };
  141. struct ChannelItem : MenuItem {
  142. MidiIO *midiModule;
  143. int channel;
  144. void onAction(EventAction &e) override;
  145. };
  146. struct ChannelChoice : ChoiceButton {
  147. MidiIO *midiModule;
  148. void step() override;
  149. void onAction(EventAction &e) override;
  150. };
  151. #endif