The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

186 lines
5.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. #ifndef DOXYGEN
  18. namespace juce::universal_midi_packets
  19. {
  20. /**
  21. Allows conversion from bytestream- or Universal MIDI Packet-formatted
  22. messages to MIDI 1.0 messages in UMP format.
  23. @tags{Audio}
  24. */
  25. struct ToUMP1Converter
  26. {
  27. template <typename Fn>
  28. void convert (const BytestreamMidiView& m, Fn&& fn)
  29. {
  30. Conversion::toMidi1 (m, std::forward<Fn> (fn));
  31. }
  32. template <typename Fn>
  33. void convert (const View& v, Fn&& fn)
  34. {
  35. Conversion::midi2ToMidi1DefaultTranslation (v, std::forward<Fn> (fn));
  36. }
  37. };
  38. /**
  39. Allows conversion from bytestream- or Universal MIDI Packet-formatted
  40. messages to MIDI 2.0 messages in UMP format.
  41. @tags{Audio}
  42. */
  43. struct ToUMP2Converter
  44. {
  45. template <typename Fn>
  46. void convert (const BytestreamMidiView& m, Fn&& fn)
  47. {
  48. Conversion::toMidi1 (m, [&] (const View& v)
  49. {
  50. translator.dispatch (v, fn);
  51. });
  52. }
  53. template <typename Fn>
  54. void convert (const View& v, Fn&& fn)
  55. {
  56. translator.dispatch (v, std::forward<Fn> (fn));
  57. }
  58. void reset()
  59. {
  60. translator.reset();
  61. }
  62. Midi1ToMidi2DefaultTranslator translator;
  63. };
  64. /**
  65. Allows conversion from bytestream- or Universal MIDI Packet-formatted
  66. messages to UMP format.
  67. The packet protocol can be selected using the constructor parameter.
  68. @tags{Audio}
  69. */
  70. class GenericUMPConverter
  71. {
  72. template <typename This, typename... Args>
  73. static void visit (This& t, Args&&... args)
  74. {
  75. if (t.mode == PacketProtocol::MIDI_1_0)
  76. convertImpl (std::get<0> (t.converters), std::forward<Args> (args)...);
  77. else
  78. convertImpl (std::get<1> (t.converters), std::forward<Args> (args)...);
  79. }
  80. public:
  81. explicit GenericUMPConverter (PacketProtocol m)
  82. : mode (m) {}
  83. void reset()
  84. {
  85. std::get<1> (converters).reset();
  86. }
  87. template <typename Converter, typename Fn>
  88. static void convertImpl (Converter& converter, const BytestreamMidiView& m, Fn&& fn)
  89. {
  90. converter.convert (m, std::forward<Fn> (fn));
  91. }
  92. template <typename Converter, typename Fn>
  93. static void convertImpl (Converter& converter, const View& m, Fn&& fn)
  94. {
  95. converter.convert (m, std::forward<Fn> (fn));
  96. }
  97. template <typename Converter, typename Fn>
  98. static void convertImpl (Converter& converter, Iterator b, Iterator e, Fn&& fn)
  99. {
  100. std::for_each (b, e, [&] (const auto& v)
  101. {
  102. convertImpl (converter, v, fn);
  103. });
  104. }
  105. template <typename Fn>
  106. void convert (const BytestreamMidiView& m, Fn&& fn)
  107. {
  108. visit (*this, m, std::forward<Fn> (fn));
  109. }
  110. template <typename Fn>
  111. void convert (const View& v, Fn&& fn)
  112. {
  113. visit (*this, v, std::forward<Fn> (fn));
  114. }
  115. template <typename Fn>
  116. void convert (Iterator begin, Iterator end, Fn&& fn)
  117. {
  118. visit (*this, begin, end, std::forward<Fn> (fn));
  119. }
  120. PacketProtocol getProtocol() const noexcept { return mode; }
  121. private:
  122. std::tuple<ToUMP1Converter, ToUMP2Converter> converters;
  123. const PacketProtocol mode{};
  124. };
  125. /**
  126. Allows conversion from bytestream- or Universal MIDI Packet-formatted
  127. messages to bytestream format.
  128. @tags{Audio}
  129. */
  130. struct ToBytestreamConverter
  131. {
  132. explicit ToBytestreamConverter (int storageSize)
  133. : translator (storageSize) {}
  134. template <typename Fn>
  135. void convert (const MidiMessage& m, Fn&& fn)
  136. {
  137. fn (m);
  138. }
  139. template <typename Fn>
  140. void convert (const View& v, double time, Fn&& fn)
  141. {
  142. Conversion::midi2ToMidi1DefaultTranslation (v, [&] (const View& midi1)
  143. {
  144. translator.dispatch (midi1, time, fn);
  145. });
  146. }
  147. void reset() { translator.reset(); }
  148. Midi1ToBytestreamTranslator translator;
  149. };
  150. } // namespace juce::universal_midi_packets
  151. #endif