Loading...
Searching...
No Matches
juce_UMPConverters.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
{
struct ToUMP1Converter
{
template <typename Fn>
void convert (const BytestreamMidiView& m, Fn&& fn)
{
Conversion::toMidi1 (m, std::forward<Fn> (fn));
}
template <typename Fn>
void convert (const View& v, Fn&& fn)
{
Conversion::midi2ToMidi1DefaultTranslation (v, std::forward<Fn> (fn));
}
};
struct ToUMP2Converter
{
template <typename Fn>
void convert (const BytestreamMidiView& m, Fn&& fn)
{
Conversion::toMidi1 (m, [&] (const View& v)
{
translator.dispatch (v, fn);
});
}
template <typename Fn>
void convert (const View& v, Fn&& fn)
{
translator.dispatch (v, std::forward<Fn> (fn));
}
void reset()
{
translator.reset();
}
Midi1ToMidi2DefaultTranslator translator;
};
class GenericUMPConverter
{
template <typename This, typename... Args>
static void visit (This& t, Args&&... args)
{
if (t.mode == PacketProtocol::MIDI_1_0)
convertImpl (std::get<0> (t.converters), std::forward<Args> (args)...);
else
convertImpl (std::get<1> (t.converters), std::forward<Args> (args)...);
}
public:
explicit GenericUMPConverter (PacketProtocol m)
: mode (m) {}
void reset()
{
std::get<1> (converters).reset();
}
template <typename Converter, typename Fn>
static void convertImpl (Converter& converter, const BytestreamMidiView& m, Fn&& fn)
{
converter.convert (m, std::forward<Fn> (fn));
}
template <typename Converter, typename Fn>
static void convertImpl (Converter& converter, const View& m, Fn&& fn)
{
converter.convert (m, std::forward<Fn> (fn));
}
template <typename Converter, typename Fn>
static void convertImpl (Converter& converter, Iterator b, Iterator e, Fn&& fn)
{
std::for_each (b, e, [&] (const auto& v)
{
convertImpl (converter, v, fn);
});
}
template <typename Fn>
void convert (const BytestreamMidiView& m, Fn&& fn)
{
visit (*this, m, std::forward<Fn> (fn));
}
template <typename Fn>
void convert (const View& v, Fn&& fn)
{
visit (*this, v, std::forward<Fn> (fn));
}
template <typename Fn>
void convert (Iterator begin, Iterator end, Fn&& fn)
{
visit (*this, begin, end, std::forward<Fn> (fn));
}
PacketProtocol getProtocol() const noexcept { return mode; }
private:
std::tuple<ToUMP1Converter, ToUMP2Converter> converters;
const PacketProtocol mode{};
};
struct ToBytestreamConverter
{
explicit ToBytestreamConverter (int storageSize)
: translator (storageSize) {}
template <typename Fn>
void convert (const MidiMessage& m, Fn&& fn)
{
fn (m);
}
template <typename Fn>
void convert (const View& v, double time, Fn&& fn)
{
Conversion::midi2ToMidi1DefaultTranslation (v, [&] (const View& midi1)
{
translator.dispatch (midi1, time, fn);
});
}
void reset() { translator.reset(); }
Midi1ToBytestreamTranslator translator;
};
} // namespace juce::universal_midi_packets
#endif
juce::MidiMessage
Definition juce_MidiMessage.h:35
juce::universal_midi_packets::Iterator
Definition juce_UMPIterator.h:38
juce::universal_midi_packets::Midi1ToBytestreamTranslator
Definition juce_UMPMidi1ToBytestreamTranslator.h:35
juce::universal_midi_packets::Midi1ToMidi2DefaultTranslator
Definition juce_UMPMidi1ToMidi2DefaultTranslator.h:35
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::midi2ToMidi1DefaultTranslation
static void midi2ToMidi1DefaultTranslation(const View &v, Callback &&callback)
Definition juce_UMPConversion.h:216
juce::universal_midi_packets::Conversion::toMidi1
static void toMidi1(const BytestreamMidiView &m, PacketCallbackFunction &&callback)
Definition juce_UMPConversion.h:79
juce::universal_midi_packets::ToUMP1Converter
Definition juce_UMPConverters.h:34
juce::universal_midi_packets::ToUMP2Converter
Definition juce_UMPConverters.h:55