Loading...
Searching...
No Matches
juce_UMPDispatcher.h
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
#ifndef DOXYGEN
namespace juce::universal_midi_packets
{
class Dispatcher
{
public:
void reset() { currentPacketLen = 0; }
template <typename PacketCallbackFunction>
void dispatch (const uint32_t* begin,
const uint32_t* end,
double timeStamp,
PacketCallbackFunction&& callback)
{
std::for_each (begin, end, [&] (uint32_t word)
{
nextPacket[currentPacketLen++] = word;
if (currentPacketLen == Utils::getNumWordsForMessageType (nextPacket.front()))
{
callback (View (nextPacket.data()), timeStamp);
currentPacketLen = 0;
}
});
}
private:
std::array<uint32_t, 4> nextPacket;
size_t currentPacketLen = 0;
};
//==============================================================================
class BytestreamToUMPDispatcher
{
public:
explicit BytestreamToUMPDispatcher (PacketProtocol pp, int storageSize)
: concatenator (storageSize),
converter (pp)
{}
void reset()
{
concatenator.reset();
converter.reset();
}
template <typename PacketCallbackFunction>
void dispatch (const uint8_t* begin,
const uint8_t* end,
double timestamp,
PacketCallbackFunction&& callback)
{
using CallbackPtr = decltype (std::addressof (callback));
#if JUCE_MINGW
#define JUCE_MINGW_HIDDEN_VISIBILITY __attribute__ ((visibility ("hidden")))
#else
#define JUCE_MINGW_HIDDEN_VISIBILITY
#endif
struct JUCE_MINGW_HIDDEN_VISIBILITY Callback
{
Callback (BytestreamToUMPDispatcher& d, CallbackPtr c)
: dispatch (d), callbackPtr (c) {}
void handleIncomingMidiMessage (void*, const MidiMessage& msg) const
{
Conversion::toMidi1 (BytestreamMidiView (&msg), [&] (const View& view)
{
dispatch.converter.convert (view, *callbackPtr);
});
}
void handlePartialSysexMessage (void*, const uint8_t*, int, double) const {}
BytestreamToUMPDispatcher& dispatch;
CallbackPtr callbackPtr = nullptr;
};
#undef JUCE_MINGW_HIDDEN_VISIBILITY
Callback inputCallback { *this, &callback };
concatenator.pushMidiData (begin, int (end - begin), timestamp, (void*) nullptr, inputCallback);
}
private:
MidiDataConcatenator concatenator;
GenericUMPConverter converter;
};
//==============================================================================
class ToBytestreamDispatcher
{
public:
explicit ToBytestreamDispatcher (int storageSize)
: converter (storageSize) {}
void reset()
{
dispatcher.reset();
converter.reset();
}
template <typename BytestreamMessageCallback>
void dispatch (const uint32_t* begin,
const uint32_t* end,
double timestamp,
BytestreamMessageCallback&& callback)
{
dispatcher.dispatch (begin, end, timestamp, [&] (const View& view, double time)
{
converter.convert (view, time, callback);
});
}
private:
Dispatcher dispatcher;
ToBytestreamConverter converter;
};
} // namespace juce::universal_midi_packets
#endif
juce::MidiDataConcatenator
Definition juce_MidiDataConcatenator.h:34
juce::MidiMessage
Definition juce_MidiMessage.h:35
juce::universal_midi_packets::BytestreamToUMPDispatcher::dispatch
void dispatch(const uint8_t *begin, const uint8_t *end, double timestamp, PacketCallbackFunction &&callback)
Definition juce_UMPDispatcher.h:104
juce::universal_midi_packets::BytestreamToUMPDispatcher::BytestreamToUMPDispatcher
BytestreamToUMPDispatcher(PacketProtocol pp, int storageSize)
Definition juce_UMPDispatcher.h:85
juce::universal_midi_packets::Dispatcher
Definition juce_UMPDispatcher.h:35
juce::universal_midi_packets::Dispatcher::dispatch
void dispatch(const uint32_t *begin, const uint32_t *end, double timeStamp, PacketCallbackFunction &&callback)
Definition juce_UMPDispatcher.h:47
juce::universal_midi_packets::Dispatcher::reset
void reset()
Definition juce_UMPDispatcher.h:38
juce::universal_midi_packets::GenericUMPConverter
Definition juce_UMPConverters.h:88
juce::universal_midi_packets::ToBytestreamDispatcher::ToBytestreamDispatcher
ToBytestreamDispatcher(int storageSize)
Definition juce_UMPDispatcher.h:162
juce::universal_midi_packets::ToBytestreamDispatcher::dispatch
void dispatch(const uint32_t *begin, const uint32_t *end, double timestamp, BytestreamMessageCallback &&callback)
Definition juce_UMPDispatcher.h:181
juce::universal_midi_packets::ToBytestreamDispatcher::reset
void reset()
Definition juce_UMPDispatcher.h:166
juce::universal_midi_packets::View
Definition juce_UMPView.h:44
juce::universal_midi_packets::BytestreamMidiView
Definition juce_UMPConversion.h:33
juce::universal_midi_packets::Conversion::toMidi1
static void toMidi1(const BytestreamMidiView &m, PacketCallbackFunction &&callback)
Definition juce_UMPConversion.h:79
juce::universal_midi_packets::ToBytestreamConverter
Definition juce_UMPConverters.h:160
juce::universal_midi_packets::Utils::getNumWordsForMessageType
static uint32_t getNumWordsForMessageType(uint32_t)
Definition juce_UMPUtils.cpp:26