Loading...
Searching...
No Matches
juce_UMPMidi1ToBytestreamTranslator.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 Midi1ToBytestreamTranslator
{
public:
explicit Midi1ToBytestreamTranslator (int initialBufferSize)
{
pendingSysExData.reserve (size_t (initialBufferSize));
}
void reset()
{
pendingSysExData.clear();
pendingSysExTime = 0.0;
}
template <typename MessageCallback>
void dispatch (const View& packet, double time, MessageCallback&& callback)
{
const auto firstWord = *packet.data();
if (! pendingSysExData.empty() && shouldPacketTerminateSysExEarly (firstWord))
pendingSysExData.clear();
switch (packet.size())
{
case 1:
{
// Utility messages don't translate to bytestream format
if (Utils::getMessageType (firstWord) != 0x00)
{
const auto message = fromUmp (PacketX1 { firstWord }, time);
callback (BytestreamMidiView (&message));
}
break;
}
case 2:
{
if (Utils::getMessageType (firstWord) == 0x3)
processSysEx (PacketX2 { packet[0], packet[1] }, time, callback);
break;
}
case 3: // no 3-word packets in the current spec
case 4: // no 4-word packets translate to bytestream format
default:
break;
}
}
static MidiMessage fromUmp (const PacketX1& m, double time = 0)
{
const auto word = m.front();
jassert (Utils::getNumWordsForMessageType (word) == 1);
const std::array<uint8_t, 3> bytes { { uint8_t ((word >> 0x10) & 0xff),
uint8_t ((word >> 0x08) & 0xff),
uint8_t ((word >> 0x00) & 0xff) } };
const auto numBytes = MidiMessage::getMessageLengthFromFirstByte (bytes.front());
return MidiMessage (bytes.data(), numBytes, time);
}
private:
template <typename MessageCallback>
void processSysEx (const PacketX2& packet,
double time,
MessageCallback&& callback)
{
switch (getSysEx7Kind (packet[0]))
{
case SysEx7::Kind::complete:
startSysExMessage (time);
pushBytes (packet);
terminateSysExMessage (callback);
break;
case SysEx7::Kind::begin:
startSysExMessage (time);
pushBytes (packet);
break;
case SysEx7::Kind::continuation:
if (pendingSysExData.empty())
break;
pushBytes (packet);
break;
case SysEx7::Kind::end:
if (pendingSysExData.empty())
break;
pushBytes (packet);
terminateSysExMessage (callback);
break;
}
}
void pushBytes (const PacketX2& packet)
{
const auto bytes = SysEx7::getDataBytes (packet);
pendingSysExData.insert (pendingSysExData.end(),
bytes.data.begin(),
bytes.data.begin() + bytes.size);
}
void startSysExMessage (double time)
{
pendingSysExTime = time;
pendingSysExData.push_back (std::byte { 0xf0 });
}
template <typename MessageCallback>
void terminateSysExMessage (MessageCallback&& callback)
{
pendingSysExData.push_back (std::byte { 0xf7 });
callback (BytestreamMidiView (pendingSysExData, pendingSysExTime));
pendingSysExData.clear();
}
static bool shouldPacketTerminateSysExEarly (uint32_t firstWord)
{
return ! (isSysExContinuation (firstWord)
|| isSystemRealTime (firstWord)
|| isJROrNOP (firstWord));
}
static SysEx7::Kind getSysEx7Kind (uint32_t word)
{
return SysEx7::Kind ((word >> 0x14) & 0xf);
}
static bool isJROrNOP (uint32_t word)
{
return Utils::getMessageType (word) == 0x0;
}
static bool isSysExContinuation (uint32_t word)
{
if (Utils::getMessageType (word) != 0x3)
return false;
const auto kind = getSysEx7Kind (word);
return kind == SysEx7::Kind::continuation || kind == SysEx7::Kind::end;
}
static bool isSystemRealTime (uint32_t word)
{
return Utils::getMessageType (word) == 0x1 && ((word >> 0x10) & 0xff) >= 0xf8;
}
std::vector<std::byte> pendingSysExData;
double pendingSysExTime = 0.0;
};
} // namespace juce::universal_midi_packets
#endif
juce::MidiMessage
Definition juce_MidiMessage.h:35
juce::MidiMessage::getMessageLengthFromFirstByte
static int getMessageLengthFromFirstByte(uint8 firstByte) noexcept
Definition juce_MidiMessage.cpp:104
juce::universal_midi_packets::Midi1ToBytestreamTranslator::Midi1ToBytestreamTranslator
Midi1ToBytestreamTranslator(int initialBufferSize)
Definition juce_UMPMidi1ToBytestreamTranslator.h:40
juce::universal_midi_packets::Midi1ToBytestreamTranslator::dispatch
void dispatch(const View &packet, double time, MessageCallback &&callback)
Definition juce_UMPMidi1ToBytestreamTranslator.h:61
juce::universal_midi_packets::Midi1ToBytestreamTranslator::fromUmp
static MidiMessage fromUmp(const PacketX1 &m, double time=0)
Definition juce_UMPMidi1ToBytestreamTranslator.h:107
juce::universal_midi_packets::Midi1ToBytestreamTranslator::reset
void reset()
Definition juce_UMPMidi1ToBytestreamTranslator.h:46
juce::universal_midi_packets::View
Definition juce_UMPView.h:44
juce::universal_midi_packets::View::size
uint32_t size() const noexcept
Definition juce_UMPView.cpp:26
juce::universal_midi_packets::View::data
const uint32_t * data() const noexcept
Definition juce_UMPView.h:55
juce::universal_midi_packets::BytestreamMidiView
Definition juce_UMPConversion.h:33
juce::universal_midi_packets::SysEx7::Kind
Kind
Definition juce_UMPSysEx7.h:47
juce::universal_midi_packets::SysEx7::Kind::end
@ end
Definition juce_UMPSysEx7.h:58
juce::universal_midi_packets::SysEx7::Kind::continuation
@ continuation
Definition juce_UMPSysEx7.h:55
juce::universal_midi_packets::SysEx7::Kind::begin
@ begin
Definition juce_UMPSysEx7.h:52
juce::universal_midi_packets::SysEx7::Kind::complete
@ complete
Definition juce_UMPSysEx7.h:49
juce::universal_midi_packets::SysEx7::getDataBytes
static PacketBytes getDataBytes(const PacketX2 &packet)
Definition juce_UMPSysEx7.cpp:32
juce::universal_midi_packets::Utils::getNumWordsForMessageType
static uint32_t getNumWordsForMessageType(uint32_t)
Definition juce_UMPUtils.cpp:26