OpenShot Audio Library | OpenShotAudio  0.6.0
juce_UMPDispatcher.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2022 - Raw Material Software Limited
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  The code included in this file is provided under the terms of the ISC license
11  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12  To use, copy, modify, and/or distribute this software for any purpose with or
13  without fee is hereby granted provided that the above copyright notice and
14  this permission notice appear in all copies.
15 
16  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18  DISCLAIMED.
19 
20  ==============================================================================
21 */
22 
23 #ifndef DOXYGEN
24 
25 namespace juce::universal_midi_packets
26 {
27 
35 {
36 public:
38  void reset() { currentPacketLen = 0; }
39 
46  template <typename PacketCallbackFunction>
47  void dispatch (const uint32_t* begin,
48  const uint32_t* end,
49  double timeStamp,
50  PacketCallbackFunction&& callback)
51  {
52  std::for_each (begin, end, [&] (uint32_t word)
53  {
54  nextPacket[currentPacketLen++] = word;
55 
56  if (currentPacketLen == Utils::getNumWordsForMessageType (nextPacket.front()))
57  {
58  callback (View (nextPacket.data()), timeStamp);
59  currentPacketLen = 0;
60  }
61  });
62  }
63 
64 private:
65  std::array<uint32_t, 4> nextPacket;
66  size_t currentPacketLen = 0;
67 };
68 
69 //==============================================================================
78 {
79 public:
85  explicit BytestreamToUMPDispatcher (PacketProtocol pp, int storageSize)
86  : concatenator (storageSize),
87  converter (pp)
88  {}
89 
90  void reset()
91  {
92  concatenator.reset();
93  converter.reset();
94  }
95 
103  template <typename PacketCallbackFunction>
104  void dispatch (const uint8_t* begin,
105  const uint8_t* end,
106  double timestamp,
107  PacketCallbackFunction&& callback)
108  {
109  using CallbackPtr = decltype (std::addressof (callback));
110 
111  #if JUCE_MINGW
112  #define JUCE_MINGW_HIDDEN_VISIBILITY __attribute__ ((visibility ("hidden")))
113  #else
114  #define JUCE_MINGW_HIDDEN_VISIBILITY
115  #endif
116 
117  struct JUCE_MINGW_HIDDEN_VISIBILITY Callback
118  {
119  Callback (BytestreamToUMPDispatcher& d, CallbackPtr c)
120  : dispatch (d), callbackPtr (c) {}
121 
122  void handleIncomingMidiMessage (void*, const MidiMessage& msg) const
123  {
124  Conversion::toMidi1 (BytestreamMidiView (&msg), [&] (const View& view)
125  {
126  dispatch.converter.convert (view, *callbackPtr);
127  });
128  }
129 
130  void handlePartialSysexMessage (void*, const uint8_t*, int, double) const {}
131 
133  CallbackPtr callbackPtr = nullptr;
134  };
135 
136  #undef JUCE_MINGW_HIDDEN_VISIBILITY
137 
138  Callback inputCallback { *this, &callback };
139  concatenator.pushMidiData (begin, int (end - begin), timestamp, (void*) nullptr, inputCallback);
140  }
141 
142 private:
143  MidiDataConcatenator concatenator;
144  GenericUMPConverter converter;
145 };
146 
147 //==============================================================================
156 {
157 public:
162  explicit ToBytestreamDispatcher (int storageSize)
163  : converter (storageSize) {}
164 
166  void reset()
167  {
168  dispatcher.reset();
169  converter.reset();
170  }
171 
180  template <typename BytestreamMessageCallback>
181  void dispatch (const uint32_t* begin,
182  const uint32_t* end,
183  double timestamp,
184  BytestreamMessageCallback&& callback)
185  {
186  dispatcher.dispatch (begin, end, timestamp, [&] (const View& view, double time)
187  {
188  converter.convert (view, time, callback);
189  });
190  }
191 
192 private:
193  Dispatcher dispatcher;
194  ToBytestreamConverter converter;
195 };
196 
197 } // namespace juce::universal_midi_packets
198 
199 #endif
void dispatch(const uint8_t *begin, const uint8_t *end, double timestamp, PacketCallbackFunction &&callback)
BytestreamToUMPDispatcher(PacketProtocol pp, int storageSize)
void dispatch(const uint32_t *begin, const uint32_t *end, double timeStamp, PacketCallbackFunction &&callback)
void dispatch(const uint32_t *begin, const uint32_t *end, double timestamp, BytestreamMessageCallback &&callback)
static void toMidi1(const BytestreamMidiView &m, PacketCallbackFunction &&callback)
static uint32_t getNumWordsForMessageType(uint32_t)