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_UMPDispatcher.h 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. Parses a raw stream of uint32_t, and calls a user-provided callback every time
  24. a full Universal MIDI Packet is encountered.
  25. @tags{Audio}
  26. */
  27. class Dispatcher
  28. {
  29. public:
  30. /** Clears the dispatcher. */
  31. void reset() { currentPacketLen = 0; }
  32. /** Calls `callback` with a View of each packet encountered in the range delimited
  33. by `begin` and `end`.
  34. If the range ends part-way through a packet, the next call to `dispatch` will
  35. continue from that point in the packet (unless `reset` is called first).
  36. */
  37. template <typename PacketCallbackFunction>
  38. void dispatch (const uint32_t* begin,
  39. const uint32_t* end,
  40. double timeStamp,
  41. PacketCallbackFunction&& callback)
  42. {
  43. std::for_each (begin, end, [&] (uint32_t word)
  44. {
  45. nextPacket[currentPacketLen++] = word;
  46. if (currentPacketLen == Utils::getNumWordsForMessageType (nextPacket.front()))
  47. {
  48. callback (View (nextPacket.data()), timeStamp);
  49. currentPacketLen = 0;
  50. }
  51. });
  52. }
  53. private:
  54. std::array<uint32_t, 4> nextPacket;
  55. size_t currentPacketLen = 0;
  56. };
  57. //==============================================================================
  58. /**
  59. Parses a stream of bytes representing a sequence of bytestream-encoded MIDI 1.0 messages,
  60. converting the messages to UMP format and passing the packets to a user-provided callback
  61. as they become ready.
  62. @tags{Audio}
  63. */
  64. class BytestreamToUMPDispatcher
  65. {
  66. public:
  67. /** Initialises the dispatcher.
  68. Channel messages will be converted to the requested protocol format `pp`.
  69. `storageSize` bytes will be allocated to store incomplete messages.
  70. */
  71. explicit BytestreamToUMPDispatcher (PacketProtocol pp, int storageSize)
  72. : concatenator (storageSize),
  73. converter (pp)
  74. {}
  75. void reset()
  76. {
  77. concatenator.reset();
  78. converter.reset();
  79. }
  80. /** Calls `callback` with a View of each converted packet as it becomes ready.
  81. @param begin the first byte in a range of bytes representing bytestream-encoded MIDI messages.
  82. @param end one-past the last byte in a range of bytes representing bytestream-encoded MIDI messages.
  83. @param timestamp a timestamp to apply to the created packets.
  84. @param callback a callback which will be passed a View pointing to each new packet as it becomes ready.
  85. */
  86. template <typename PacketCallbackFunction>
  87. void dispatch (const uint8_t* begin,
  88. const uint8_t* end,
  89. double timestamp,
  90. PacketCallbackFunction&& callback)
  91. {
  92. using CallbackPtr = decltype (std::addressof (callback));
  93. #if JUCE_MINGW
  94. #define JUCE_MINGW_HIDDEN_VISIBILITY __attribute__ ((visibility ("hidden")))
  95. #else
  96. #define JUCE_MINGW_HIDDEN_VISIBILITY
  97. #endif
  98. struct JUCE_MINGW_HIDDEN_VISIBILITY Callback
  99. {
  100. Callback (BytestreamToUMPDispatcher& d, CallbackPtr c)
  101. : dispatch (d), callbackPtr (c) {}
  102. void handleIncomingMidiMessage (void*, const MidiMessage& msg) const
  103. {
  104. Conversion::toMidi1 (msg, [&] (const View& view)
  105. {
  106. dispatch.converter.convert (view, *callbackPtr);
  107. });
  108. }
  109. void handlePartialSysexMessage (void*, const uint8_t*, int, double) const {}
  110. BytestreamToUMPDispatcher& dispatch;
  111. CallbackPtr callbackPtr = nullptr;
  112. };
  113. #undef JUCE_MINGW_HIDDEN_VISIBILITY
  114. Callback inputCallback { *this, &callback };
  115. concatenator.pushMidiData (begin, int (end - begin), timestamp, (void*) nullptr, inputCallback);
  116. }
  117. private:
  118. MidiDataConcatenator concatenator;
  119. GenericUMPConverter converter;
  120. };
  121. //==============================================================================
  122. /**
  123. Parses a stream of 32-bit words representing a sequence of UMP-encoded MIDI messages,
  124. converting the messages to MIDI 1.0 bytestream format and passing them to a user-provided
  125. callback as they become ready.
  126. @tags{Audio}
  127. */
  128. class ToBytestreamDispatcher
  129. {
  130. public:
  131. /** Initialises the dispatcher.
  132. `storageSize` bytes will be allocated to store incomplete messages.
  133. */
  134. explicit ToBytestreamDispatcher (int storageSize)
  135. : converter (storageSize) {}
  136. /** Clears the dispatcher. */
  137. void reset()
  138. {
  139. dispatcher.reset();
  140. converter.reset();
  141. }
  142. /** Calls `callback` with converted bytestream-formatted MidiMessage whenever
  143. a new message becomes available.
  144. @param begin the first word in a stream of words representing UMP-encoded MIDI packets.
  145. @param end one-past the last word in a stream of words representing UMP-encoded MIDI packets.
  146. @param timestamp a timestamp to apply to converted messages.
  147. @param callback a callback which will be passed a MidiMessage each time a new message becomes ready.
  148. */
  149. template <typename BytestreamMessageCallback>
  150. void dispatch (const uint32_t* begin,
  151. const uint32_t* end,
  152. double timestamp,
  153. BytestreamMessageCallback&& callback)
  154. {
  155. dispatcher.dispatch (begin, end, timestamp, [&] (const View& view, double time)
  156. {
  157. converter.convert (view, time, callback);
  158. });
  159. }
  160. private:
  161. Dispatcher dispatcher;
  162. ToBytestreamConverter converter;
  163. };
  164. }
  165. }
  166. #endif