Audio plugin host https://kx.studio/carla
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.

juce_UMPConverters.h 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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
  19. {
  20. namespace universal_midi_packets
  21. {
  22. /**
  23. Allows conversion from bytestream- or Universal MIDI Packet-formatted
  24. messages to MIDI 1.0 messages in UMP format.
  25. @tags{Audio}
  26. */
  27. struct ToUMP1Converter
  28. {
  29. template <typename Fn>
  30. void convert (const MidiMessage& m, Fn&& fn)
  31. {
  32. Conversion::toMidi1 (m, std::forward<Fn> (fn));
  33. }
  34. template <typename Fn>
  35. void convert (const View& v, Fn&& fn)
  36. {
  37. Conversion::midi2ToMidi1DefaultTranslation (v, std::forward<Fn> (fn));
  38. }
  39. };
  40. /**
  41. Allows conversion from bytestream- or Universal MIDI Packet-formatted
  42. messages to MIDI 2.0 messages in UMP format.
  43. @tags{Audio}
  44. */
  45. struct ToUMP2Converter
  46. {
  47. template <typename Fn>
  48. void convert (const MidiMessage& m, Fn&& fn)
  49. {
  50. Conversion::toMidi1 (m, [&] (const View& v)
  51. {
  52. translator.dispatch (v, fn);
  53. });
  54. }
  55. template <typename Fn>
  56. void convert (const View& v, Fn&& fn)
  57. {
  58. translator.dispatch (v, std::forward<Fn> (fn));
  59. }
  60. void reset()
  61. {
  62. translator.reset();
  63. }
  64. Midi1ToMidi2DefaultTranslator translator;
  65. };
  66. /**
  67. Allows conversion from bytestream- or Universal MIDI Packet-formatted
  68. messages to UMP format.
  69. The packet protocol can be selected using the constructor parameter.
  70. @tags{Audio}
  71. */
  72. class GenericUMPConverter
  73. {
  74. public:
  75. explicit GenericUMPConverter (PacketProtocol m)
  76. : mode (m) {}
  77. void reset()
  78. {
  79. std::get<1> (converters).reset();
  80. }
  81. template <typename Fn>
  82. void convert (const MidiMessage& m, Fn&& fn)
  83. {
  84. switch (mode)
  85. {
  86. case PacketProtocol::MIDI_1_0: return std::get<0> (converters).convert (m, std::forward<Fn> (fn));
  87. case PacketProtocol::MIDI_2_0: return std::get<1> (converters).convert (m, std::forward<Fn> (fn));
  88. }
  89. }
  90. template <typename Fn>
  91. void convert (const View& v, Fn&& fn)
  92. {
  93. switch (mode)
  94. {
  95. case PacketProtocol::MIDI_1_0: return std::get<0> (converters).convert (v, std::forward<Fn> (fn));
  96. case PacketProtocol::MIDI_2_0: return std::get<1> (converters).convert (v, std::forward<Fn> (fn));
  97. }
  98. }
  99. template <typename Fn>
  100. void convert (Iterator begin, Iterator end, Fn&& fn)
  101. {
  102. std::for_each (begin, end, [&] (const View& v)
  103. {
  104. convert (v, fn);
  105. });
  106. }
  107. PacketProtocol getProtocol() const noexcept { return mode; }
  108. private:
  109. std::tuple<ToUMP1Converter, ToUMP2Converter> converters;
  110. const PacketProtocol mode{};
  111. };
  112. /**
  113. Allows conversion from bytestream- or Universal MIDI Packet-formatted
  114. messages to bytestream format.
  115. @tags{Audio}
  116. */
  117. struct ToBytestreamConverter
  118. {
  119. explicit ToBytestreamConverter (int storageSize)
  120. : translator (storageSize) {}
  121. template <typename Fn>
  122. void convert (const MidiMessage& m, Fn&& fn)
  123. {
  124. fn (m);
  125. }
  126. template <typename Fn>
  127. void convert (const View& v, double time, Fn&& fn)
  128. {
  129. Conversion::midi2ToMidi1DefaultTranslation (v, [&] (const View& midi1)
  130. {
  131. translator.dispatch (midi1, time, fn);
  132. });
  133. }
  134. void reset() { translator.reset(); }
  135. Midi1ToBytestreamTranslator translator;
  136. };
  137. }
  138. }
  139. #endif