адаптация s13bby/osc под vst и gui. (вайбкод)
вайбкод потому что мне в моменте стало понятно. что проще было сразу писать на фреймворке JUCE, чем адаптировать консольное приложение на MiniAudio. обновляться данный репозиторий не будет. СmakeLists адаптирован онли под linux.
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
#include "juce_UMPProtocols.h"
|
||||
#include "juce_UMPUtils.h"
|
||||
#include "juce_UMPacket.h"
|
||||
#include "juce_UMPSysEx7.h"
|
||||
#include "juce_UMPView.h"
|
||||
#include "juce_UMPIterator.h"
|
||||
#include "juce_UMPackets.h"
|
||||
#include "juce_UMPFactory.h"
|
||||
#include "juce_UMPConversion.h"
|
||||
#include "juce_UMPMidi1ToBytestreamTranslator.h"
|
||||
#include "juce_UMPMidi1ToMidi2DefaultTranslator.h"
|
||||
#include "juce_UMPConverters.h"
|
||||
#include "juce_UMPDispatcher.h"
|
||||
#include "juce_UMPStringUtils.h"
|
||||
|
||||
/** @cond */
|
||||
namespace juce
|
||||
{
|
||||
namespace ump = universal_midi_packets;
|
||||
}
|
||||
/** @endcond */
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce::universal_midi_packets
|
||||
{
|
||||
|
||||
/**
|
||||
Holds a UMP group, and a span of bytes that were received or are to be
|
||||
sent on that group. Helpful when working with sysex messages.
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
struct BytesOnGroup
|
||||
{
|
||||
uint8_t group{};
|
||||
Span<const std::byte> bytes;
|
||||
};
|
||||
|
||||
} // namespace juce::universal_midi_packets
|
||||
@@ -0,0 +1,361 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
/** @cond */
|
||||
namespace juce::universal_midi_packets
|
||||
{
|
||||
|
||||
/** Represents a MIDI message on bytestream transport that happened at a particular time.
|
||||
|
||||
Unlike MidiMessage, BytestreamMidiView is non-owning.
|
||||
*/
|
||||
struct BytestreamMidiView
|
||||
{
|
||||
constexpr BytestreamMidiView (Span<const std::byte> bytesIn, double timestampIn)
|
||||
: bytes (bytesIn), timestamp (timestampIn) {}
|
||||
|
||||
/** Creates a view over the provided message.
|
||||
|
||||
Note that the argument is a pointer, not a reference, in order to avoid taking a reference
|
||||
to a temporary.
|
||||
*/
|
||||
explicit BytestreamMidiView (const MidiMessage* msg)
|
||||
: bytes (msg->asSpan()),
|
||||
timestamp (msg->getTimeStamp()) {}
|
||||
|
||||
explicit BytestreamMidiView (const MidiMessageMetadata msg)
|
||||
: bytes (msg.asSpan()),
|
||||
timestamp (msg.samplePosition) {}
|
||||
|
||||
MidiMessage getMessage() const
|
||||
{
|
||||
return MidiMessage (bytes.data(), (int) bytes.size(), timestamp);
|
||||
}
|
||||
|
||||
MidiMessageMetadata getMidiMessageMetadata() const
|
||||
{
|
||||
return MidiMessageMetadata { reinterpret_cast<const uint8*> (bytes.data()),
|
||||
(int) bytes.size(),
|
||||
(int) timestamp };
|
||||
}
|
||||
|
||||
Span<const std::byte> bytes;
|
||||
double timestamp = 0.0;
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Functions to assist conversion of UMP messages to/from other formats,
|
||||
especially older 'bytestream' formatted MidiMessages.
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
struct Conversion
|
||||
{
|
||||
/** Converts 7-bit data (the most significant bit of each byte must be unset) to a series of
|
||||
Universal MIDI Packets.
|
||||
*/
|
||||
template <typename PacketCallbackFunction>
|
||||
static void umpFrom7BitData (BytesOnGroup msg, PacketCallbackFunction&& callback)
|
||||
{
|
||||
// If this is hit, non-7-bit data was supplied.
|
||||
// Maybe you forgot to trim the leading/trailing bytes that delimit a bytestream SysEx message.
|
||||
jassert (std::all_of (msg.bytes.begin(), msg.bytes.end(), [] (std::byte b) { return (b & std::byte { 0x80 }) == std::byte{}; }));
|
||||
|
||||
Factory::splitIntoPackets (msg.bytes, 6, [&] (SysEx7::Kind kind, Span<const std::byte> bytesThisTime)
|
||||
{
|
||||
const auto packet = Factory::Detail::makeSysEx (msg.group, kind, bytesThisTime);
|
||||
const uint32_t paddedPacket[] { packet[0], packet[1], 0, 0 };
|
||||
callback (View (paddedPacket));
|
||||
});
|
||||
}
|
||||
|
||||
/** Converts from a MIDI 1 bytestream to MIDI 1 on Universal MIDI Packets.
|
||||
|
||||
@param bytes the bytes in a single well-formed bytestream MIDI message
|
||||
@param callback a function that accepts a single View argument. This may be called several
|
||||
times for each invocation of toMidi1 if the bytestream message converts
|
||||
to multiple Universal MIDI Packets.
|
||||
*/
|
||||
template <typename PacketCallbackFunction>
|
||||
static void toMidi1 (const BytesOnGroup& groupBytes, PacketCallbackFunction&& callback)
|
||||
{
|
||||
const auto size = groupBytes.bytes.size();
|
||||
|
||||
if (size <= 0)
|
||||
return;
|
||||
|
||||
const auto* data = groupBytes.bytes.data();
|
||||
const auto firstByte = data[0];
|
||||
|
||||
if (firstByte != std::byte { 0xf0 })
|
||||
{
|
||||
const auto mask = [size]() -> uint32_t
|
||||
{
|
||||
switch (size)
|
||||
{
|
||||
case 0: return 0xff000000;
|
||||
case 1: return 0xffff0000;
|
||||
case 2: return 0xffffff00;
|
||||
case 3: return 0xffffffff;
|
||||
}
|
||||
|
||||
// This function can only handle a single bytestream MIDI message at a time!
|
||||
jassertfalse;
|
||||
return 0x00000000;
|
||||
}();
|
||||
|
||||
const auto extraByte = ((((firstByte & std::byte { 0xf0 }) == std::byte { 0xf0 }) ? std::byte { 0x1 } : std::byte { 0x2 }) << 0x4);
|
||||
const std::byte group { (uint8_t) (groupBytes.group & 0xf) };
|
||||
const uint32_t packet[] { mask & Utils::bytesToWord (extraByte | group, data[0], data[1], data[2]), 0, 0, 0 };
|
||||
callback (View (packet));
|
||||
return;
|
||||
}
|
||||
|
||||
umpFrom7BitData ({ groupBytes.group, Span (data + 1, size - 2) }, std::forward<PacketCallbackFunction> (callback));
|
||||
}
|
||||
|
||||
/** Widens a 7-bit MIDI 1.0 value to a 8-bit MIDI 2.0 value. */
|
||||
static uint8_t scaleTo8 (uint8_t word7Bit)
|
||||
{
|
||||
const auto shifted = (uint8_t) (word7Bit << 0x1);
|
||||
const auto repeat = (uint8_t) (word7Bit & 0x3f);
|
||||
const auto mask = (uint8_t) (word7Bit <= 0x40 ? 0x0 : 0xff);
|
||||
return (uint8_t) (shifted | ((repeat >> 5) & mask));
|
||||
}
|
||||
|
||||
/** Widens a 7-bit MIDI 1.0 value to a 16-bit MIDI 2.0 value. */
|
||||
static uint16_t scaleTo16 (uint8_t word7Bit)
|
||||
{
|
||||
const auto shifted = (uint16_t) (word7Bit << 0x9);
|
||||
const auto repeat = (uint16_t) (word7Bit & 0x3f);
|
||||
const auto mask = (uint16_t) (word7Bit <= 0x40 ? 0x0 : 0xffff);
|
||||
return (uint16_t) (shifted | (((repeat << 3) | (repeat >> 3)) & mask));
|
||||
}
|
||||
|
||||
/** Widens a 14-bit MIDI 1.0 value to a 16-bit MIDI 2.0 value. */
|
||||
static uint16_t scaleTo16 (uint16_t word14Bit)
|
||||
{
|
||||
const auto shifted = (uint16_t) (word14Bit << 0x2);
|
||||
const auto repeat = (uint16_t) (word14Bit & 0x1fff);
|
||||
const auto mask = (uint16_t) (word14Bit <= 0x2000 ? 0x0 : 0xffff);
|
||||
return (uint16_t) (shifted | ((repeat >> 11) & mask));
|
||||
}
|
||||
|
||||
/** Widens a 7-bit MIDI 1.0 value to a 32-bit MIDI 2.0 value. */
|
||||
static uint32_t scaleTo32 (uint8_t word7Bit)
|
||||
{
|
||||
const auto shifted = (uint32_t) (word7Bit << 0x19);
|
||||
const auto repeat = (uint32_t) (word7Bit & 0x3f);
|
||||
const auto mask = (uint32_t) (word7Bit <= 0x40 ? 0x0 : 0xffffffff);
|
||||
return (uint32_t) (shifted | (((repeat << 19)
|
||||
| (repeat << 13)
|
||||
| (repeat << 7)
|
||||
| (repeat << 1)
|
||||
| (repeat >> 5)) & mask));
|
||||
}
|
||||
|
||||
/** Widens a 14-bit MIDI 1.0 value to a 32-bit MIDI 2.0 value. */
|
||||
static uint32_t scaleTo32 (uint16_t word14Bit)
|
||||
{
|
||||
const auto shifted = (uint32_t) (word14Bit << 0x12);
|
||||
const auto repeat = (uint32_t) (word14Bit & 0x1fff);
|
||||
const auto mask = (uint32_t) (word14Bit <= 0x2000 ? 0x0 : 0xffffffff);
|
||||
return (uint32_t) (shifted | (((repeat << 5) | (repeat >> 8)) & mask));
|
||||
}
|
||||
|
||||
/** Narrows a 16-bit MIDI 2.0 value to a 7-bit MIDI 1.0 value. */
|
||||
static uint8_t scaleTo7 (uint8_t word8Bit) { return (uint8_t) (word8Bit >> 1); }
|
||||
|
||||
/** Narrows a 16-bit MIDI 2.0 value to a 7-bit MIDI 1.0 value. */
|
||||
static uint8_t scaleTo7 (uint16_t word16Bit) { return (uint8_t) (word16Bit >> 9); }
|
||||
|
||||
/** Narrows a 32-bit MIDI 2.0 value to a 7-bit MIDI 1.0 value. */
|
||||
static uint8_t scaleTo7 (uint32_t word32Bit) { return (uint8_t) (word32Bit >> 25); }
|
||||
|
||||
/** Narrows a 32-bit MIDI 2.0 value to a 14-bit MIDI 1.0 value. */
|
||||
static uint16_t scaleTo14 (uint16_t word16Bit) { return (uint16_t) (word16Bit >> 2); }
|
||||
|
||||
/** Narrows a 32-bit MIDI 2.0 value to a 14-bit MIDI 1.0 value. */
|
||||
static uint16_t scaleTo14 (uint32_t word32Bit) { return (uint16_t) (word32Bit >> 18); }
|
||||
|
||||
/** Converts UMP messages which may include MIDI 2.0 channel voice messages into
|
||||
equivalent MIDI 1.0 messages (still in UMP format).
|
||||
|
||||
`callback` is a function that accepts a single View argument and will be
|
||||
called with each converted packet.
|
||||
|
||||
Note that not all MIDI 2.0 messages have MIDI 1.0 equivalents, so such
|
||||
messages will be ignored.
|
||||
*/
|
||||
template <typename Callback>
|
||||
static void midi2ToMidi1DefaultTranslation (const View& v, Callback&& callback)
|
||||
{
|
||||
const auto firstWord = v[0];
|
||||
|
||||
if (Utils::getMessageType (firstWord) != Utils::MessageKind::channelVoice2)
|
||||
{
|
||||
callback (v);
|
||||
return;
|
||||
}
|
||||
|
||||
const auto status = Utils::getStatus (firstWord);
|
||||
const auto typeAndGroup = ((std::byte { 0x2 } << 0x4) | std::byte { Utils::getGroup (firstWord) });
|
||||
|
||||
switch ((uint8_t) status)
|
||||
{
|
||||
case 0x8: // note off
|
||||
case 0x9: // note on
|
||||
case 0xa: // poly pressure
|
||||
case 0xb: // control change
|
||||
{
|
||||
const auto statusAndChannel = std::byte ((firstWord >> 0x10) & 0xff);
|
||||
const auto byte2 = std::byte ((firstWord >> 0x08) & 0xff);
|
||||
const auto byte3 = std::byte { scaleTo7 (v[1]) };
|
||||
|
||||
// If this is a note-on, and the scaled byte is 0,
|
||||
// the scaled velocity should be 1 instead of 0
|
||||
const auto needsCorrection = status == std::byte { 0x9 } && byte3 == std::byte { 0 };
|
||||
const auto correctedByte = needsCorrection ? std::byte { 1 } : byte3;
|
||||
|
||||
const auto shouldIgnore = status == std::byte { 0xb } && [&]
|
||||
{
|
||||
switch (uint8_t (byte2))
|
||||
{
|
||||
case 0:
|
||||
case 6:
|
||||
case 32:
|
||||
case 38:
|
||||
case 98:
|
||||
case 99:
|
||||
case 100:
|
||||
case 101:
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}();
|
||||
|
||||
if (shouldIgnore)
|
||||
return;
|
||||
|
||||
const PacketX1 packet { Utils::bytesToWord (typeAndGroup,
|
||||
statusAndChannel,
|
||||
byte2,
|
||||
correctedByte) };
|
||||
callback (View (packet.data()));
|
||||
return;
|
||||
}
|
||||
|
||||
case 0xd: // channel pressure
|
||||
{
|
||||
const auto statusAndChannel = std::byte ((firstWord >> 0x10) & 0xff);
|
||||
const auto byte2 = std::byte { scaleTo7 (v[1]) };
|
||||
|
||||
const PacketX1 packet { Utils::bytesToWord (typeAndGroup,
|
||||
statusAndChannel,
|
||||
byte2,
|
||||
std::byte { 0 }) };
|
||||
callback (View (packet.data()));
|
||||
return;
|
||||
}
|
||||
|
||||
case 0x2: // rpn
|
||||
case 0x3: // nrpn
|
||||
{
|
||||
const auto ccX = status == std::byte { 0x2 } ? std::byte { 101 } : std::byte { 99 };
|
||||
const auto ccY = status == std::byte { 0x2 } ? std::byte { 100 } : std::byte { 98 };
|
||||
const auto statusAndChannel = std::byte ((0xb << 0x4) | Utils::getChannel (firstWord));
|
||||
const auto data = scaleTo14 (v[1]);
|
||||
|
||||
const PacketX1 packets[]
|
||||
{
|
||||
PacketX1 { Utils::bytesToWord (typeAndGroup, statusAndChannel, ccX, std::byte ((firstWord >> 0x8) & 0x7f)) },
|
||||
PacketX1 { Utils::bytesToWord (typeAndGroup, statusAndChannel, ccY, std::byte ((firstWord >> 0x0) & 0x7f)) },
|
||||
PacketX1 { Utils::bytesToWord (typeAndGroup, statusAndChannel, std::byte { 6 }, std::byte ((data >> 0x7) & 0x7f)) },
|
||||
PacketX1 { Utils::bytesToWord (typeAndGroup, statusAndChannel, std::byte { 38 }, std::byte ((data >> 0x0) & 0x7f)) },
|
||||
};
|
||||
|
||||
for (const auto& packet : packets)
|
||||
callback (View (packet.data()));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
case 0xc: // program change / bank select
|
||||
{
|
||||
if (firstWord & 1)
|
||||
{
|
||||
const auto statusAndChannel = std::byte ((0xb << 0x4) | Utils::getChannel (firstWord));
|
||||
const auto secondWord = v[1];
|
||||
|
||||
const PacketX1 packets[]
|
||||
{
|
||||
PacketX1 { Utils::bytesToWord (typeAndGroup, statusAndChannel, std::byte { 0 }, std::byte ((secondWord >> 0x8) & 0x7f)) },
|
||||
PacketX1 { Utils::bytesToWord (typeAndGroup, statusAndChannel, std::byte { 32 }, std::byte ((secondWord >> 0x0) & 0x7f)) },
|
||||
};
|
||||
|
||||
for (const auto& packet : packets)
|
||||
callback (View (packet.data()));
|
||||
}
|
||||
|
||||
const auto statusAndChannel = std::byte ((0xc << 0x4) | Utils::getChannel (firstWord));
|
||||
const PacketX1 packet { Utils::bytesToWord (typeAndGroup,
|
||||
statusAndChannel,
|
||||
std::byte ((v[1] >> 0x18) & 0x7f),
|
||||
std::byte { 0 }) };
|
||||
callback (View (packet.data()));
|
||||
return;
|
||||
}
|
||||
|
||||
case 0xe: // pitch bend
|
||||
{
|
||||
const auto data = scaleTo14 (v[1]);
|
||||
const auto statusAndChannel = std::byte ((firstWord >> 0x10) & 0xff);
|
||||
const PacketX1 packet { Utils::bytesToWord (typeAndGroup,
|
||||
statusAndChannel,
|
||||
std::byte (data & 0x7f),
|
||||
std::byte ((data >> 7) & 0x7f)) };
|
||||
callback (View (packet.data()));
|
||||
return;
|
||||
}
|
||||
|
||||
default: // other message types do not translate
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace juce::universal_midi_packets
|
||||
/** @endcond */
|
||||
@@ -0,0 +1,214 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
/** @cond */
|
||||
namespace juce::universal_midi_packets
|
||||
{
|
||||
/**
|
||||
Allows conversion from bytestream- or Universal MIDI Packet-formatted
|
||||
messages to MIDI 1.0 messages in UMP format.
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
struct ToUMP1Converter
|
||||
{
|
||||
template <typename Fn>
|
||||
void convert (const BytesOnGroup& 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));
|
||||
}
|
||||
|
||||
void reset() {}
|
||||
};
|
||||
|
||||
/**
|
||||
Allows conversion from bytestream- or Universal MIDI Packet-formatted
|
||||
messages to MIDI 2.0 messages in UMP format.
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
struct ToUMP2Converter
|
||||
{
|
||||
template <typename Fn>
|
||||
void convert (const BytesOnGroup& 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;
|
||||
};
|
||||
|
||||
/**
|
||||
Allows conversion from bytestream- or Universal MIDI Packet-formatted
|
||||
messages to UMP format.
|
||||
|
||||
The packet protocol can be selected using the constructor parameter.
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
class GenericUMPConverter
|
||||
{
|
||||
using Converters = std::variant<ToUMP1Converter, ToUMP2Converter>;
|
||||
|
||||
template <typename This, typename Fn>
|
||||
static void visit (This& t, Fn&& fn)
|
||||
{
|
||||
if (auto* converter1 = std::get_if<ToUMP1Converter> (&t.converters))
|
||||
fn (*converter1);
|
||||
else if (auto* converter2 = std::get_if<ToUMP2Converter> (&t.converters))
|
||||
fn (*converter2);
|
||||
}
|
||||
|
||||
public:
|
||||
explicit GenericUMPConverter (PacketProtocol m)
|
||||
: converters (m == PacketProtocol::MIDI_1_0 ? Converters (ToUMP1Converter()) : Converters (ToUMP2Converter())) {}
|
||||
|
||||
void reset()
|
||||
{
|
||||
visit (*this, [] (auto& c) { c.reset(); });
|
||||
}
|
||||
|
||||
template <typename Converter, typename Fn>
|
||||
static void convertImpl (Converter& converter, const BytesOnGroup& 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 Converter, typename Fn>
|
||||
static void convertImpl (Converter& converter, const Packets& packets, Fn&& fn)
|
||||
{
|
||||
convertImpl (converter, packets.begin(), packets.end(), std::forward<Fn> (fn));
|
||||
}
|
||||
|
||||
template <typename Fn>
|
||||
void convert (const BytesOnGroup& m, Fn&& fn)
|
||||
{
|
||||
visit (*this, [&] (auto& c) { convertImpl (c, m, std::forward<Fn> (fn)); });
|
||||
}
|
||||
|
||||
template <typename Fn>
|
||||
void convert (const View& v, Fn&& fn)
|
||||
{
|
||||
visit (*this, [&] (auto& c) { convertImpl (c, v, std::forward<Fn> (fn)); });
|
||||
}
|
||||
|
||||
template <typename Fn>
|
||||
void convert (Iterator begin, Iterator end, Fn&& fn)
|
||||
{
|
||||
visit (*this, [&] (auto& c) { convertImpl (c, begin, end, std::forward<Fn> (fn)); });
|
||||
}
|
||||
|
||||
template <typename Fn>
|
||||
void convert (const Packets& packets, Fn&& fn)
|
||||
{
|
||||
visit (*this, [&] (auto& c) { convertImpl (c, packets, std::forward<Fn> (fn)); });
|
||||
}
|
||||
|
||||
PacketProtocol getProtocol() const noexcept
|
||||
{
|
||||
return std::holds_alternative<ToUMP1Converter> (converters) ? PacketProtocol::MIDI_1_0
|
||||
: PacketProtocol::MIDI_2_0;
|
||||
}
|
||||
|
||||
private:
|
||||
Converters converters;
|
||||
};
|
||||
|
||||
/**
|
||||
Allows conversion from bytestream- or Universal MIDI Packet-formatted
|
||||
messages to bytestream format.
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
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(); }
|
||||
|
||||
SingleGroupMidi1ToBytestreamTranslator translator;
|
||||
};
|
||||
} // namespace juce::universal_midi_packets
|
||||
/** @endcond */
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce::universal_midi_packets
|
||||
{
|
||||
|
||||
auto DeviceInfo::tie() const
|
||||
{
|
||||
return std::tie (manufacturer, family, modelNumber, revision);
|
||||
}
|
||||
|
||||
bool DeviceInfo::operator== (const DeviceInfo& other) const
|
||||
{
|
||||
return tie() == other.tie();
|
||||
}
|
||||
|
||||
bool DeviceInfo::operator!= (const DeviceInfo& other) const
|
||||
{
|
||||
return tie() != other.tie();
|
||||
}
|
||||
|
||||
} // namespace juce::universal_midi_packets
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce::universal_midi_packets
|
||||
{
|
||||
|
||||
/**
|
||||
Holds MIDI device info that may be required by certain UMP messages and
|
||||
MIDI-CI messages.
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
struct DeviceInfo
|
||||
{
|
||||
std::array<std::byte, 3> manufacturer;
|
||||
std::array<std::byte, 2> family; ///< LSB first
|
||||
std::array<std::byte, 2> modelNumber; ///< LSB first
|
||||
std::array<std::byte, 4> revision;
|
||||
|
||||
private:
|
||||
auto tie() const;
|
||||
|
||||
public:
|
||||
bool operator== (const DeviceInfo& other) const;
|
||||
bool operator!= (const DeviceInfo& other) const;
|
||||
|
||||
static constexpr auto marshallingVersion = std::nullopt;
|
||||
|
||||
template <typename Archive, typename This>
|
||||
static auto serialise (Archive& archive, This& t)
|
||||
{
|
||||
return archive (named ("manufacturer", t.manufacturer),
|
||||
named ("family", t.family),
|
||||
named ("modelNumber", t.modelNumber),
|
||||
named ("revision", t.revision));
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace juce::universal_midi_packets
|
||||
@@ -0,0 +1,207 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
/** @cond */
|
||||
namespace juce::universal_midi_packets
|
||||
{
|
||||
|
||||
/**
|
||||
Parses a raw stream of uint32_t, and calls a user-provided callback every time
|
||||
a full Universal MIDI Packet is encountered.
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
class Dispatcher
|
||||
{
|
||||
public:
|
||||
/** Clears the dispatcher. */
|
||||
void reset() { currentPacketLen = 0; }
|
||||
|
||||
/** Calls `callback` with a View of each packet encountered in the range delimited
|
||||
by `begin` and `end`.
|
||||
|
||||
If the range ends part-way through a packet, the next call to `dispatch` will
|
||||
continue from that point in the packet (unless `reset` is called first).
|
||||
*/
|
||||
template <typename PacketCallbackFunction>
|
||||
void dispatch (Span<const uint32_t> words,
|
||||
double timeStamp,
|
||||
PacketCallbackFunction&& callback)
|
||||
{
|
||||
for (const auto word : words)
|
||||
{
|
||||
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;
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Parses a stream of bytes representing a sequence of bytestream-encoded MIDI 1.0 messages,
|
||||
converting the messages to UMP format and passing the packets to a user-provided callback
|
||||
as they become ready.
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
class BytestreamToUMPDispatcher
|
||||
{
|
||||
public:
|
||||
/** Initialises the dispatcher.
|
||||
|
||||
Channel messages will be converted to the requested protocol format `pp`.
|
||||
`storageSize` bytes will be allocated to store incomplete messages.
|
||||
*/
|
||||
BytestreamToUMPDispatcher (uint8_t targetGroup, PacketProtocol pp, int storageSize)
|
||||
: concatenator (storageSize),
|
||||
converter (pp),
|
||||
group (targetGroup)
|
||||
{}
|
||||
|
||||
void reset()
|
||||
{
|
||||
concatenator.reset();
|
||||
converter.reset();
|
||||
}
|
||||
|
||||
/** Calls `callback` with a View of each converted packet as it becomes ready.
|
||||
|
||||
@param bytes range of bytes representing bytestream-encoded MIDI messages.
|
||||
@param timestamp a timestamp to apply to the created packets.
|
||||
@param callback a callback which will be passed a View pointing to each new packet as it becomes ready.
|
||||
*/
|
||||
template <typename PacketCallbackFunction>
|
||||
void dispatch (Span<const std::byte> bytes,
|
||||
double timestamp,
|
||||
PacketCallbackFunction&& callback)
|
||||
{
|
||||
using CallbackPtr = decltype (std::addressof (callback));
|
||||
|
||||
struct Callback
|
||||
{
|
||||
Callback (BytestreamToUMPDispatcher& d, CallbackPtr c)
|
||||
: dispatch (d), callbackPtr (c) {}
|
||||
|
||||
void handleIncomingMidiMessage (void*, const MidiMessage& msg) const
|
||||
{
|
||||
Conversion::toMidi1 ({ dispatch.group, msg.asSpan() }, [&] (const View& view)
|
||||
{
|
||||
dispatch.converter.convert (view, [&] (const View& v)
|
||||
{
|
||||
(*callbackPtr) (v, msg.getTimeStamp());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
void handlePartialSysexMessage (void*, const uint8*, int, double) const {}
|
||||
|
||||
BytestreamToUMPDispatcher& dispatch;
|
||||
CallbackPtr callbackPtr = nullptr;
|
||||
};
|
||||
|
||||
Callback inputCallback { *this, &callback };
|
||||
concatenator.pushMidiData (bytes, timestamp, (void*) nullptr, inputCallback);
|
||||
}
|
||||
|
||||
PacketProtocol getProtocol() const
|
||||
{
|
||||
return converter.getProtocol();
|
||||
}
|
||||
|
||||
private:
|
||||
MidiDataConcatenator concatenator;
|
||||
GenericUMPConverter converter;
|
||||
uint8_t group{};
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Parses a stream of 32-bit words representing a sequence of UMP-encoded MIDI messages,
|
||||
converting the messages to MIDI 1.0 bytestream format and passing them to a user-provided
|
||||
callback as they become ready.
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
class ToBytestreamDispatcher
|
||||
{
|
||||
public:
|
||||
/** Initialises the dispatcher.
|
||||
|
||||
`storageSize` bytes will be allocated to store incomplete messages.
|
||||
*/
|
||||
explicit ToBytestreamDispatcher (int storageSize)
|
||||
: converter (storageSize) {}
|
||||
|
||||
/** Clears the dispatcher. */
|
||||
void reset()
|
||||
{
|
||||
dispatcher.reset();
|
||||
converter.reset();
|
||||
}
|
||||
|
||||
/** Calls `callback` with converted bytestream-formatted MidiMessage whenever
|
||||
a new message becomes available.
|
||||
|
||||
@param begin the first word in a stream of words representing UMP-encoded MIDI packets.
|
||||
@param end one-past the last word in a stream of words representing UMP-encoded MIDI packets.
|
||||
@param timestamp a timestamp to apply to converted messages.
|
||||
@param callback a callback which will be passed a MidiMessage each time a new message becomes ready.
|
||||
*/
|
||||
template <typename BytestreamMessageCallback>
|
||||
void dispatch (Span<const uint32_t> words,
|
||||
double timestamp,
|
||||
BytestreamMessageCallback&& callback)
|
||||
{
|
||||
dispatcher.dispatch (words, timestamp, [&] (const View& view, double time)
|
||||
{
|
||||
converter.convert (view, time, callback);
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
Dispatcher dispatcher;
|
||||
ToBytestreamConverter converter;
|
||||
};
|
||||
|
||||
} // namespace juce::universal_midi_packets
|
||||
/** @endcond */
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce::universal_midi_packets
|
||||
{
|
||||
|
||||
auto EndpointInfo::tie() const
|
||||
{
|
||||
return std::tie (versionMajor, versionMinor, numFunctionBlocks, flags);
|
||||
}
|
||||
|
||||
bool EndpointInfo::operator== (const EndpointInfo& other) const
|
||||
{
|
||||
return tie() == other.tie();
|
||||
}
|
||||
|
||||
bool EndpointInfo::operator!= (const EndpointInfo& other) const
|
||||
{
|
||||
return tie() != other.tie();
|
||||
}
|
||||
|
||||
} // namespace juce::universal_midi_packets
|
||||
@@ -0,0 +1,914 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
/** @cond */
|
||||
namespace juce::universal_midi_packets
|
||||
{
|
||||
|
||||
/**
|
||||
Holds the data from a stream configuration notification message, with strong types.
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
class StreamConfiguration
|
||||
{
|
||||
public:
|
||||
[[nodiscard]] StreamConfiguration withProtocol (PacketProtocol p) const { return withFlag (isMidi2, p == PacketProtocol::MIDI_2_0); }
|
||||
[[nodiscard]] StreamConfiguration withTransmitTimestamp (bool b) const { return withFlag (transmitTimestamp, b); }
|
||||
[[nodiscard]] StreamConfiguration withReceiveTimestamp (bool b) const { return withFlag (receiveTimestamp, b); }
|
||||
|
||||
/** The protocol in use by the endpoint. This protocol will be used for sending and receiving messages. */
|
||||
[[nodiscard]] PacketProtocol getProtocol() const { return getFlag (isMidi2) ? PacketProtocol::MIDI_2_0 : PacketProtocol::MIDI_1_0; }
|
||||
/** True if this endpoint intends to send JR timestamps. */
|
||||
[[nodiscard]] bool getTransmitTimestamp() const { return getFlag (transmitTimestamp); }
|
||||
/** True if this endpoint expects to receive JR timestamps. */
|
||||
[[nodiscard]] bool getReceiveTimestamp() const { return getFlag (receiveTimestamp); }
|
||||
|
||||
bool operator== (const StreamConfiguration& other) const { return options == other.options; }
|
||||
bool operator!= (const StreamConfiguration& other) const { return options != other.options; }
|
||||
|
||||
private:
|
||||
enum Flags
|
||||
{
|
||||
isMidi2 = 1 << 0,
|
||||
transmitTimestamp = 1 << 1,
|
||||
receiveTimestamp = 1 << 2,
|
||||
};
|
||||
|
||||
StreamConfiguration withFlag (Flags f, bool value) const
|
||||
{
|
||||
return withMember (*this, &StreamConfiguration::options, value ? (options | f) : (options & ~f));
|
||||
}
|
||||
|
||||
bool getFlag (Flags f) const
|
||||
{
|
||||
return (options & f) != 0;
|
||||
}
|
||||
|
||||
int options = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
Holds the data from an endpoint info notification message, with strong types.
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
class EndpointInfo
|
||||
{
|
||||
auto tie() const;
|
||||
|
||||
public:
|
||||
[[nodiscard]] EndpointInfo withVersion (uint8_t major, uint8_t minor) const
|
||||
{
|
||||
return withMember (withMember (*this, &EndpointInfo::versionMinor, minor), &EndpointInfo::versionMajor, major);
|
||||
}
|
||||
|
||||
[[nodiscard]] EndpointInfo withNumFunctionBlocks (uint8_t x) const
|
||||
{
|
||||
return withMember (*this, &EndpointInfo::numFunctionBlocks, x);
|
||||
}
|
||||
|
||||
[[nodiscard]] EndpointInfo withStaticFunctionBlocks (bool b) const { return withFlag (staticFunctionBlocks, b); }
|
||||
[[nodiscard]] EndpointInfo withMidi1Support (bool b) const { return withFlag (supportsMidi1, b); }
|
||||
[[nodiscard]] EndpointInfo withMidi2Support (bool b) const { return withFlag (supportsMidi2, b); }
|
||||
[[nodiscard]] EndpointInfo withReceiveJRSupport (bool b) const { return withFlag (supportsReceiveJR, b); }
|
||||
[[nodiscard]] EndpointInfo withTransmitJRSupport (bool b) const { return withFlag (supportsTransmitJR, b); }
|
||||
|
||||
/** The major version byte. */
|
||||
[[nodiscard]] uint8_t getVersionMajor() const { return versionMajor; }
|
||||
/** The minor version byte. */
|
||||
[[nodiscard]] uint8_t getVersionMinor() const { return versionMinor; }
|
||||
/** The number of function blocks declared on this endpoint. */
|
||||
[[nodiscard]] uint8_t getNumFunctionBlocks() const { return numFunctionBlocks; }
|
||||
/** True if the function block configuration cannot change. */
|
||||
[[nodiscard]] bool hasStaticFunctionBlocks() const { return getFlag (staticFunctionBlocks); }
|
||||
/** True if this endpoint is capable of supporting the MIDI 1.0 protocol. */
|
||||
[[nodiscard]] bool hasMidi1Support() const { return getFlag (supportsMidi1); }
|
||||
/** True if this endpoint is capable of supporting the MIDI 2.0 protocol. */
|
||||
[[nodiscard]] bool hasMidi2Support() const { return getFlag (supportsMidi2); }
|
||||
/** True if this endpoint is capable of receiving JR timestamps. */
|
||||
[[nodiscard]] bool hasReceiveJRSupport() const { return getFlag (supportsReceiveJR); }
|
||||
/** True if this endpoint is capable of transmitting JR timestamps. */
|
||||
[[nodiscard]] bool hasTransmitJRSupport() const { return getFlag (supportsTransmitJR); }
|
||||
|
||||
bool operator== (const EndpointInfo& other) const;
|
||||
bool operator!= (const EndpointInfo& other) const;
|
||||
|
||||
private:
|
||||
enum Flags
|
||||
{
|
||||
staticFunctionBlocks = 1 << 0,
|
||||
supportsMidi1 = 1 << 1,
|
||||
supportsMidi2 = 1 << 2,
|
||||
supportsReceiveJR = 1 << 3,
|
||||
supportsTransmitJR = 1 << 4,
|
||||
};
|
||||
|
||||
EndpointInfo withFlag (Flags f, bool value) const
|
||||
{
|
||||
return withMember (*this, &EndpointInfo::flags, (uint8_t) (value ? (flags | f) : (flags & ~f)));
|
||||
}
|
||||
|
||||
bool getFlag (Flags f) const
|
||||
{
|
||||
return (flags & f) != 0;
|
||||
}
|
||||
|
||||
uint8_t versionMajor, versionMinor, numFunctionBlocks, flags;
|
||||
};
|
||||
|
||||
/** Directions that can apply to a Function Block or Group Terminal Block. */
|
||||
enum class BlockDirection : uint8_t
|
||||
{
|
||||
unknown = 0b00, ///< Block direction is unknown or undeclared
|
||||
receiver = 0b01, ///< Block is a receiver of messages
|
||||
sender = 0b10, ///< Block is a sender of messages
|
||||
bidirectional = 0b11, ///< Block both sends and receives messages
|
||||
};
|
||||
|
||||
/** UI hints that can apply to a Function Block or Group Terminal Block. */
|
||||
enum class BlockUiHint : uint8_t
|
||||
{
|
||||
unknown = 0b00, ///< Block direction is unknown or undeclared
|
||||
receiver = 0b01, ///< Block is a receiver of messages
|
||||
sender = 0b10, ///< Block is a sender of messages
|
||||
bidirectional = 0b11, ///< Block both sends and receives messages
|
||||
};
|
||||
|
||||
/** Describes how a MIDI 1.0 port maps to a given Block, if applicable. */
|
||||
enum class BlockMIDI1ProxyKind : uint8_t
|
||||
{
|
||||
inapplicable = 0b00, ///< Block does not represent a MIDI 1.0 port
|
||||
unrestrictedBandwidth = 0b01, ///< Block represents a MIDI 1.0 port and can handle high bandwidth
|
||||
restrictedBandwidth = 0b10, ///< Block represents a MIDI 1.0 port that requires restricted bandwidth
|
||||
};
|
||||
|
||||
/**
|
||||
Holds the data from a function block info notification message, with strong types.
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
class BlockInfo
|
||||
{
|
||||
public:
|
||||
[[nodiscard]] BlockInfo withEnabled (bool x) const { return withMember (*this, &BlockInfo::enabled, x); }
|
||||
[[nodiscard]] BlockInfo withUiHint (BlockUiHint x) const { return withMember (*this, &BlockInfo::flags, replaceBits<4, 2> (flags, (uint8_t) x)); }
|
||||
[[nodiscard]] BlockInfo withMIDI1ProxyKind (BlockMIDI1ProxyKind x) const { return withMember (*this, &BlockInfo::flags, replaceBits<2, 2> (flags, (uint8_t) x)); }
|
||||
[[nodiscard]] BlockInfo withDirection (BlockDirection x) const { return withMember (*this, &BlockInfo::flags, replaceBits<0, 2> (flags, (uint8_t) x)); }
|
||||
[[nodiscard]] BlockInfo withFirstGroup (uint8_t x) const { return withMember (*this, &BlockInfo::firstGroup, x); }
|
||||
[[nodiscard]] BlockInfo withNumGroups (uint8_t x) const { return withMember (*this, &BlockInfo::numGroups, x); }
|
||||
[[nodiscard]] BlockInfo withCiVersion (uint8_t x) const { return withMember (*this, &BlockInfo::ciVersion, x); }
|
||||
[[nodiscard]] BlockInfo withMaxSysex8Streams (uint8_t x) const { return withMember (*this, &BlockInfo::numSysex8Streams, x); }
|
||||
|
||||
/** True if the block is enabled/active, false otherwise. */
|
||||
bool isEnabled() const { return enabled; }
|
||||
/** The directionality of the block, for display to the user. */
|
||||
BlockUiHint getUiHint() const { return (BlockUiHint) getBits<4, 2> (flags); }
|
||||
/** The kind of MIDI 1.0 proxy represented by this block, if any. */
|
||||
BlockMIDI1ProxyKind getMIDI1ProxyKind() const { return (BlockMIDI1ProxyKind) getBits<2, 2> (flags); }
|
||||
/** The actual directionality of the block. */
|
||||
BlockDirection getDirection() const { return (BlockDirection) getBits<0, 2> (flags); }
|
||||
/** The zero-based index of the first group in the block. */
|
||||
uint8_t getFirstGroup() const { return firstGroup; }
|
||||
/** The number of groups contained in the block, must be one or greater. */
|
||||
uint8_t getNumGroups() const { return numGroups; }
|
||||
/** The CI version supported by this block. Implies a bidirectional block. */
|
||||
uint8_t getCiVersion() const { return ciVersion; }
|
||||
/** The number of simultaneous SysEx8 streams supported on this block. */
|
||||
uint8_t getMaxSysex8Streams() const { return numSysex8Streams; }
|
||||
|
||||
bool operator== (const BlockInfo& other) const
|
||||
{
|
||||
const auto tie = [] (auto& x)
|
||||
{
|
||||
return std::tuple (x.enabled, x.flags, x.firstGroup, x.numGroups, x.ciVersion, x.numSysex8Streams);
|
||||
};
|
||||
return tie (*this) == tie (other);
|
||||
}
|
||||
|
||||
bool operator!= (const BlockInfo& other) const
|
||||
{
|
||||
return ! operator== (other);
|
||||
}
|
||||
|
||||
private:
|
||||
template <auto position, auto numBits, typename Value>
|
||||
static Value replaceBits (Value value, Value replacement)
|
||||
{
|
||||
constexpr auto mask = ((Value) 1 << numBits) - 1;
|
||||
const auto maskedValue = value & ~(mask << position);
|
||||
return (Value) (maskedValue | (replacement << position));
|
||||
}
|
||||
|
||||
template <auto position, auto numBits, typename Value>
|
||||
static Value getBits (Value value)
|
||||
{
|
||||
constexpr auto mask = ((Value) 1 << numBits) - 1;
|
||||
return (value >> position) & mask;
|
||||
}
|
||||
|
||||
uint8_t enabled{};
|
||||
uint8_t flags{};
|
||||
uint8_t firstGroup{};
|
||||
uint8_t numGroups{};
|
||||
uint8_t ciVersion{};
|
||||
uint8_t numSysex8Streams{};
|
||||
};
|
||||
|
||||
/**
|
||||
This struct holds functions that can be used to create different kinds
|
||||
of Universal MIDI Packet.
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
struct Factory
|
||||
{
|
||||
template <typename Callback>
|
||||
static void splitIntoPackets (Span<const std::byte> bytes, size_t bytesPerPacket, Callback&& callback)
|
||||
{
|
||||
const auto numPackets = (bytes.size() / bytesPerPacket) + ((bytes.size() % bytesPerPacket) != 0);
|
||||
auto* dataOffset = bytes.data();
|
||||
|
||||
if (numPackets <= 1)
|
||||
{
|
||||
callback (SysEx7::Kind::complete, bytes);
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto i = static_cast<ssize_t> (bytes.size()); i > 0; i -= (ssize_t) bytesPerPacket, dataOffset += bytesPerPacket)
|
||||
{
|
||||
const auto kind = [&]
|
||||
{
|
||||
if (i == (ssize_t) bytes.size())
|
||||
return SysEx7::Kind::begin;
|
||||
|
||||
if (i <= (ssize_t) bytesPerPacket)
|
||||
return SysEx7::Kind::end;
|
||||
|
||||
return SysEx7::Kind::continuation;
|
||||
}();
|
||||
|
||||
const auto bytesNow = std::min ((ssize_t) bytesPerPacket, i);
|
||||
callback (kind, Span (dataOffset, (size_t) bytesNow));
|
||||
}
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
struct Detail
|
||||
{
|
||||
static PacketX1 makeSystem() { return PacketX1{}.withMessageType (Utils::MessageKind::commonRealtime); }
|
||||
static PacketX1 makeV1() { return PacketX1{}.withMessageType (Utils::MessageKind::channelVoice1); }
|
||||
static PacketX2 makeV2() { return PacketX2{}.withMessageType (Utils::MessageKind::channelVoice2); }
|
||||
static PacketX4 makeStream() { return PacketX4{}.withMessageType (Utils::MessageKind::stream); }
|
||||
|
||||
static PacketX2 makeSysEx (uint8_t group, SysEx7::Kind status, Span<const std::byte> data)
|
||||
{
|
||||
jassert (data.size() <= 6);
|
||||
|
||||
std::array<std::byte, 8> bytes { {} };
|
||||
bytes[0] = std::byte (0x3 << 0x4) | std::byte (group);
|
||||
bytes[1] = std::byte ((uint8_t) status << 0x4) | std::byte (data.size());
|
||||
|
||||
std::memcpy (bytes.data() + 2, data.data(), data.size());
|
||||
|
||||
std::array<uint32_t, 2> words;
|
||||
|
||||
for (const auto [index, word] : enumerate (words))
|
||||
word = ByteOrder::bigEndianInt (bytes.data() + 4 * index);
|
||||
|
||||
return PacketX2 { words };
|
||||
}
|
||||
|
||||
static PacketX4 makeSysEx8 (uint8_t group,
|
||||
std::byte status,
|
||||
uint8_t dataStart,
|
||||
Span<const std::byte> data)
|
||||
{
|
||||
jassert (data.size() <= (size_t) (16 - dataStart));
|
||||
|
||||
std::array<std::byte, 16> bytes{{}};
|
||||
bytes[0] = std::byte (0x5 << 0x4) | std::byte (group);
|
||||
bytes[1] = std::byte (status << 0x4) | std::byte (data.size());
|
||||
|
||||
std::memcpy (bytes.data() + dataStart, data.data(), data.size());
|
||||
|
||||
std::array<uint32_t, 4> words;
|
||||
|
||||
for (const auto [index, word] : enumerate (words))
|
||||
word = ByteOrder::bigEndianInt (bytes.data() + 4 * index);
|
||||
|
||||
return PacketX4 { words };
|
||||
}
|
||||
|
||||
static PacketX4 makePacketX4 (Span<const std::byte> header,
|
||||
Span<const std::byte> data)
|
||||
{
|
||||
jassert (data.size() <= 14);
|
||||
|
||||
std::array<std::byte, 16> bytes{{}};
|
||||
std::copy (header.begin(), header.end(), bytes.begin());
|
||||
std::copy (data.begin(), data.end(), std::next (bytes.begin(), (ptrdiff_t) header.size()));
|
||||
|
||||
std::array<uint32_t, 4> words{};
|
||||
|
||||
size_t index = 0;
|
||||
|
||||
for (auto& word : words)
|
||||
word = ByteOrder::bigEndianInt (bytes.data() + 4 * index++);
|
||||
|
||||
return PacketX4 { words };
|
||||
}
|
||||
|
||||
static PacketX4 makeStreamSubpacket (std::byte status,
|
||||
SysEx7::Kind kind,
|
||||
Span<const std::byte> data)
|
||||
{
|
||||
jassert (data.size() <= 14);
|
||||
const std::byte header[] { std::byte (0xf0) | std::byte ((uint8_t) kind << 2), status, };
|
||||
return makePacketX4 (header, data);
|
||||
}
|
||||
|
||||
static PacketX4 makeStreamConfiguration (StreamConfiguration options)
|
||||
{
|
||||
return Detail::makeStream().withU8<0x2> (options.getProtocol() == PacketProtocol::MIDI_2_0 ? 0x2 : 0x1)
|
||||
.withU8<0x3> ((options.getReceiveTimestamp() ? 0x2 : 0x0) | (options.getTransmitTimestamp() ? 0x1 : 0x0));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
static PacketX1 makeNoop (uint8_t group)
|
||||
{
|
||||
return PacketX1{}.withGroup (group);
|
||||
}
|
||||
|
||||
static PacketX1 makeJRClock (uint8_t group, uint16_t time)
|
||||
{
|
||||
return PacketX1 { time }.withStatus (std::byte { 1 }).withGroup (group);
|
||||
}
|
||||
|
||||
static PacketX1 makeJRTimestamp (uint8_t group, uint16_t time)
|
||||
{
|
||||
return PacketX1 { time }.withStatus (std::byte { 2 }).withGroup (group);
|
||||
}
|
||||
|
||||
static PacketX1 makeTimeCode (uint8_t group, uint8_t code)
|
||||
{
|
||||
return Detail::makeSystem().withGroup (group)
|
||||
.withU8<1> (0xf1)
|
||||
.withU8<2> (code & 0x7f);
|
||||
}
|
||||
|
||||
static PacketX1 makeSongPositionPointer (uint8_t group, uint16_t pos)
|
||||
{
|
||||
return Detail::makeSystem().withGroup (group)
|
||||
.withU8<1> (0xf2)
|
||||
.withU8<2> (pos & 0x7f)
|
||||
.withU8<3> ((pos >> 7) & 0x7f);
|
||||
}
|
||||
|
||||
static PacketX1 makeSongSelect (uint8_t group, uint8_t song)
|
||||
{
|
||||
return Detail::makeSystem().withGroup (group)
|
||||
.withU8<1> (0xf3)
|
||||
.withU8<2> (song & 0x7f);
|
||||
}
|
||||
|
||||
static PacketX1 makeTuneRequest (uint8_t group)
|
||||
{
|
||||
return Detail::makeSystem().withGroup (group)
|
||||
.withU8<1> (0xf6);
|
||||
}
|
||||
|
||||
static PacketX1 makeTimingClock (uint8_t group)
|
||||
{
|
||||
return Detail::makeSystem().withGroup (group)
|
||||
.withU8<1> (0xf8);
|
||||
}
|
||||
|
||||
static PacketX1 makeStart (uint8_t group)
|
||||
{
|
||||
return Detail::makeSystem().withGroup (group)
|
||||
.withU8<1> (0xfa);
|
||||
}
|
||||
|
||||
static PacketX1 makeContinue (uint8_t group)
|
||||
{
|
||||
return Detail::makeSystem().withGroup (group)
|
||||
.withU8<1> (0xfb);
|
||||
}
|
||||
|
||||
static PacketX1 makeStop (uint8_t group)
|
||||
{
|
||||
return Detail::makeSystem().withGroup (group)
|
||||
.withU8<1> (0xfc);
|
||||
}
|
||||
|
||||
static PacketX1 makeActiveSensing (uint8_t group)
|
||||
{
|
||||
return Detail::makeSystem().withGroup (group)
|
||||
.withU8<1> (0xfe);
|
||||
}
|
||||
|
||||
static PacketX1 makeReset (uint8_t group)
|
||||
{
|
||||
return Detail::makeSystem().withGroup (group)
|
||||
.withU8<1> (0xff);
|
||||
}
|
||||
|
||||
static PacketX1 makeNoteOffV1 (uint8_t group,
|
||||
uint8_t channel,
|
||||
uint8_t note,
|
||||
uint8_t velocity)
|
||||
{
|
||||
return Detail::makeV1().withGroup (group)
|
||||
.withStatus (std::byte { 0x8 })
|
||||
.withChannel (channel)
|
||||
.withU8<2> (note & 0x7f)
|
||||
.withU8<3> (velocity & 0x7f);
|
||||
}
|
||||
|
||||
static PacketX1 makeNoteOnV1 (uint8_t group,
|
||||
uint8_t channel,
|
||||
uint8_t note,
|
||||
uint8_t velocity)
|
||||
{
|
||||
return Detail::makeV1().withGroup (group)
|
||||
.withStatus (std::byte { 0x9 })
|
||||
.withChannel (channel)
|
||||
.withU8<2> (note & 0x7f)
|
||||
.withU8<3> (velocity & 0x7f);
|
||||
}
|
||||
|
||||
static PacketX1 makePolyPressureV1 (uint8_t group,
|
||||
uint8_t channel,
|
||||
uint8_t note,
|
||||
uint8_t pressure)
|
||||
{
|
||||
return Detail::makeV1().withGroup (group)
|
||||
.withStatus (std::byte { 0xa })
|
||||
.withChannel (channel)
|
||||
.withU8<2> (note & 0x7f)
|
||||
.withU8<3> (pressure & 0x7f);
|
||||
}
|
||||
|
||||
static PacketX1 makeControlChangeV1 (uint8_t group,
|
||||
uint8_t channel,
|
||||
uint8_t controller,
|
||||
uint8_t value)
|
||||
{
|
||||
return Detail::makeV1().withGroup (group)
|
||||
.withStatus (std::byte { 0xb })
|
||||
.withChannel (channel)
|
||||
.withU8<2> (controller & 0x7f)
|
||||
.withU8<3> (value & 0x7f);
|
||||
}
|
||||
|
||||
static PacketX1 makeProgramChangeV1 (uint8_t group,
|
||||
uint8_t channel,
|
||||
uint8_t program)
|
||||
{
|
||||
return Detail::makeV1().withGroup (group)
|
||||
.withStatus (std::byte { 0xc })
|
||||
.withChannel (channel)
|
||||
.withU8<2> (program & 0x7f);
|
||||
}
|
||||
|
||||
static PacketX1 makeChannelPressureV1 (uint8_t group,
|
||||
uint8_t channel,
|
||||
uint8_t pressure)
|
||||
{
|
||||
return Detail::makeV1().withGroup (group)
|
||||
.withStatus (std::byte { 0xd })
|
||||
.withChannel (channel)
|
||||
.withU8<2> (pressure & 0x7f);
|
||||
}
|
||||
|
||||
static PacketX1 makePitchBend (uint8_t group,
|
||||
uint8_t channel,
|
||||
uint16_t pitchbend)
|
||||
{
|
||||
return Detail::makeV1().withGroup (group)
|
||||
.withStatus (std::byte { 0xe })
|
||||
.withChannel (channel)
|
||||
.withU8<2> (pitchbend & 0x7f)
|
||||
.withU8<3> ((pitchbend >> 7) & 0x7f);
|
||||
}
|
||||
|
||||
static PacketX2 makeSysExIn1Packet (uint8_t group,
|
||||
Span<const std::byte> data)
|
||||
{
|
||||
return Detail::makeSysEx (group, SysEx7::Kind::complete, data);
|
||||
}
|
||||
|
||||
static PacketX2 makeSysExStart (uint8_t group,
|
||||
Span<const std::byte> data)
|
||||
{
|
||||
return Detail::makeSysEx (group, SysEx7::Kind::begin, data);
|
||||
}
|
||||
|
||||
static PacketX2 makeSysExContinue (uint8_t group,
|
||||
Span<const std::byte> data)
|
||||
{
|
||||
return Detail::makeSysEx (group, SysEx7::Kind::continuation, data);
|
||||
}
|
||||
|
||||
static PacketX2 makeSysExEnd (uint8_t group,
|
||||
Span<const std::byte> data)
|
||||
{
|
||||
return Detail::makeSysEx (group, SysEx7::Kind::end, data);
|
||||
}
|
||||
|
||||
static PacketX2 makeRegisteredPerNoteControllerV2 (uint8_t group,
|
||||
uint8_t channel,
|
||||
uint8_t note,
|
||||
uint8_t controller,
|
||||
uint32_t data)
|
||||
{
|
||||
return Detail::makeV2().withGroup (group)
|
||||
.withStatus (std::byte { 0x0 })
|
||||
.withChannel (channel)
|
||||
.withU8<2> (note & 0x7f)
|
||||
.withU8<3> (controller & 0x7f)
|
||||
.withU32<1> (data);
|
||||
}
|
||||
|
||||
static PacketX2 makeAssignablePerNoteControllerV2 (uint8_t group,
|
||||
uint8_t channel,
|
||||
uint8_t note,
|
||||
uint8_t controller,
|
||||
uint32_t data)
|
||||
{
|
||||
return Detail::makeV2().withGroup (group)
|
||||
.withStatus (std::byte { 0x1 })
|
||||
.withChannel (channel)
|
||||
.withU8<2> (note & 0x7f)
|
||||
.withU8<3> (controller & 0x7f)
|
||||
.withU32<1> (data);
|
||||
}
|
||||
|
||||
static PacketX2 makeRegisteredControllerV2 (uint8_t group,
|
||||
uint8_t channel,
|
||||
uint8_t bank,
|
||||
uint8_t index,
|
||||
uint32_t data)
|
||||
{
|
||||
return Detail::makeV2().withGroup (group)
|
||||
.withStatus (std::byte { 0x2 })
|
||||
.withChannel (channel)
|
||||
.withU8<2> (bank & 0x7f)
|
||||
.withU8<3> (index & 0x7f)
|
||||
.withU32<1> (data);
|
||||
}
|
||||
|
||||
static PacketX2 makeAssignableControllerV2 (uint8_t group,
|
||||
uint8_t channel,
|
||||
uint8_t bank,
|
||||
uint8_t index,
|
||||
uint32_t data)
|
||||
{
|
||||
return Detail::makeV2().withGroup (group)
|
||||
.withStatus (std::byte { 0x3 })
|
||||
.withChannel (channel)
|
||||
.withU8<2> (bank & 0x7f)
|
||||
.withU8<3> (index & 0x7f)
|
||||
.withU32<1> (data);
|
||||
}
|
||||
|
||||
static PacketX2 makeRelativeRegisteredControllerV2 (uint8_t group,
|
||||
uint8_t channel,
|
||||
uint8_t bank,
|
||||
uint8_t index,
|
||||
uint32_t data)
|
||||
{
|
||||
return Detail::makeV2().withGroup (group)
|
||||
.withStatus (std::byte { 0x4 })
|
||||
.withChannel (channel)
|
||||
.withU8<2> (bank & 0x7f)
|
||||
.withU8<3> (index & 0x7f)
|
||||
.withU32<1> (data);
|
||||
}
|
||||
|
||||
static PacketX2 makeRelativeAssignableControllerV2 (uint8_t group,
|
||||
uint8_t channel,
|
||||
uint8_t bank,
|
||||
uint8_t index,
|
||||
uint32_t data)
|
||||
{
|
||||
return Detail::makeV2().withGroup (group)
|
||||
.withStatus (std::byte { 0x5 })
|
||||
.withChannel (channel)
|
||||
.withU8<2> (bank & 0x7f)
|
||||
.withU8<3> (index & 0x7f)
|
||||
.withU32<1> (data);
|
||||
}
|
||||
|
||||
static PacketX2 makePerNotePitchBendV2 (uint8_t group,
|
||||
uint8_t channel,
|
||||
uint8_t note,
|
||||
uint32_t data)
|
||||
{
|
||||
return Detail::makeV2().withGroup (group)
|
||||
.withStatus (std::byte { 0x6 })
|
||||
.withChannel (channel)
|
||||
.withU8<2> (note & 0x7f)
|
||||
.withU32<1> (data);
|
||||
}
|
||||
|
||||
enum class NoteAttributeKind : uint8_t
|
||||
{
|
||||
none = 0x00,
|
||||
manufacturer = 0x01,
|
||||
profile = 0x02,
|
||||
pitch7_9 = 0x03
|
||||
};
|
||||
|
||||
static PacketX2 makeNoteOffV2 (uint8_t group,
|
||||
uint8_t channel,
|
||||
uint8_t note,
|
||||
NoteAttributeKind attribute,
|
||||
uint16_t velocity,
|
||||
uint16_t attributeValue)
|
||||
{
|
||||
return Detail::makeV2().withGroup (group)
|
||||
.withStatus (std::byte { 0x8 })
|
||||
.withChannel (channel)
|
||||
.withU8<2> (note & 0x7f)
|
||||
.withU8<3> ((uint8_t) attribute)
|
||||
.withU16<2> (velocity)
|
||||
.withU16<3> (attributeValue);
|
||||
}
|
||||
|
||||
static PacketX2 makeNoteOnV2 (uint8_t group,
|
||||
uint8_t channel,
|
||||
uint8_t note,
|
||||
NoteAttributeKind attribute,
|
||||
uint16_t velocity,
|
||||
uint16_t attributeValue)
|
||||
{
|
||||
return Detail::makeV2().withGroup (group)
|
||||
.withStatus (std::byte { 0x9 })
|
||||
.withChannel (channel)
|
||||
.withU8<2> (note & 0x7f)
|
||||
.withU8<3> ((uint8_t) attribute)
|
||||
.withU16<2> (velocity)
|
||||
.withU16<3> (attributeValue);
|
||||
}
|
||||
|
||||
static PacketX2 makePolyPressureV2 (uint8_t group,
|
||||
uint8_t channel,
|
||||
uint8_t note,
|
||||
uint32_t data)
|
||||
{
|
||||
return Detail::makeV2().withGroup (group)
|
||||
.withStatus (std::byte { 0xa })
|
||||
.withChannel (channel)
|
||||
.withU8<2> (note & 0x7f)
|
||||
.withU32<1> (data);
|
||||
}
|
||||
|
||||
static PacketX2 makeControlChangeV2 (uint8_t group,
|
||||
uint8_t channel,
|
||||
uint8_t controller,
|
||||
uint32_t data)
|
||||
{
|
||||
return Detail::makeV2().withGroup (group)
|
||||
.withStatus (std::byte { 0xb })
|
||||
.withChannel (channel)
|
||||
.withU8<2> (controller & 0x7f)
|
||||
.withU32<1> (data);
|
||||
}
|
||||
|
||||
static PacketX2 makeProgramChangeV2 (uint8_t group,
|
||||
uint8_t channel,
|
||||
uint8_t optionFlags,
|
||||
uint8_t program,
|
||||
uint8_t bankMsb,
|
||||
uint8_t bankLsb)
|
||||
{
|
||||
return Detail::makeV2().withGroup (group)
|
||||
.withStatus (std::byte { 0xc })
|
||||
.withChannel (channel)
|
||||
.withU8<3> (optionFlags)
|
||||
.withU8<4> (program)
|
||||
.withU8<6> (bankMsb)
|
||||
.withU8<7> (bankLsb);
|
||||
}
|
||||
|
||||
static PacketX2 makeChannelPressureV2 (uint8_t group,
|
||||
uint8_t channel,
|
||||
uint32_t data)
|
||||
{
|
||||
return Detail::makeV2().withGroup (group)
|
||||
.withStatus (std::byte { 0xd })
|
||||
.withChannel (channel)
|
||||
.withU32<1> (data);
|
||||
}
|
||||
|
||||
static PacketX2 makePitchBendV2 (uint8_t group,
|
||||
uint8_t channel,
|
||||
uint32_t data)
|
||||
{
|
||||
return Detail::makeV2().withGroup (group)
|
||||
.withStatus (std::byte { 0xe })
|
||||
.withChannel (channel)
|
||||
.withU32<1> (data);
|
||||
}
|
||||
|
||||
static PacketX2 makePerNoteManagementV2 (uint8_t group,
|
||||
uint8_t channel,
|
||||
uint8_t note,
|
||||
std::byte optionFlags)
|
||||
{
|
||||
return Detail::makeV2().withGroup (group)
|
||||
.withStatus (std::byte { 0xf })
|
||||
.withChannel (channel)
|
||||
.withU8<2> (note)
|
||||
.withU8<3> (uint8_t (optionFlags));
|
||||
}
|
||||
|
||||
|
||||
static PacketX4 makeSysEx8in1Packet (uint8_t group,
|
||||
uint8_t streamId,
|
||||
Span<const std::byte> data)
|
||||
{
|
||||
return Detail::makeSysEx8 (group, std::byte { 0x0 }, 3, data).withU8<2> (streamId);
|
||||
}
|
||||
|
||||
static PacketX4 makeSysEx8Start (uint8_t group,
|
||||
uint8_t streamId,
|
||||
Span<const std::byte> data)
|
||||
{
|
||||
return Detail::makeSysEx8 (group, std::byte { 0x1 }, 3, data).withU8<2> (streamId);
|
||||
}
|
||||
|
||||
static PacketX4 makeSysEx8Continue (uint8_t group,
|
||||
uint8_t streamId,
|
||||
Span<const std::byte> data)
|
||||
{
|
||||
return Detail::makeSysEx8 (group, std::byte { 0x2 }, 3, data).withU8<2> (streamId);
|
||||
}
|
||||
|
||||
static PacketX4 makeSysEx8End (uint8_t group,
|
||||
uint8_t streamId,
|
||||
Span<const std::byte> data)
|
||||
{
|
||||
return Detail::makeSysEx8 (group, std::byte { 0x3 }, 3, data).withU8<2> (streamId);
|
||||
}
|
||||
|
||||
static PacketX4 makeEndpointDiscovery (uint8_t versionMajor,
|
||||
uint8_t versionMinor,
|
||||
std::byte filterBitmap)
|
||||
{
|
||||
return Detail::makeStream().withU8<2> (versionMajor)
|
||||
.withU8<3> (versionMinor)
|
||||
.withU8<7> ((uint8_t) filterBitmap);
|
||||
}
|
||||
|
||||
static PacketX4 makeEndpointInfoNotification (const EndpointInfo& info)
|
||||
{
|
||||
return Detail::makeStream().withU8<1> (1)
|
||||
.withU8<2> (info.getVersionMajor())
|
||||
.withU8<3> (info.getVersionMinor())
|
||||
.withU8<4> (info.getNumFunctionBlocks() | (info.hasStaticFunctionBlocks() ? 0x80 : 0x00))
|
||||
.withU8<6> ((info.hasMidi1Support() ? 0x1 : 0x0) | (info.hasMidi2Support() ? 0x2 : 0x0))
|
||||
.withU8<7> ((info.hasTransmitJRSupport() ? 0x1 : 0x0) | (info.hasReceiveJRSupport() ? 0x2 : 0x0));
|
||||
}
|
||||
|
||||
static PacketX4 makeFunctionBlockDiscovery (uint8_t block, std::byte filterBitmap)
|
||||
{
|
||||
return Detail::makeStream().withU8<1> (0x10)
|
||||
.withU8<2> (block)
|
||||
.withU8<3> ((uint8_t) filterBitmap);
|
||||
}
|
||||
|
||||
static PacketX4 makeDeviceIdentityNotification (DeviceInfo info)
|
||||
{
|
||||
return Detail::makeStream().withU8<0x1> (2)
|
||||
.withU8<0x5> ((uint8_t) info.manufacturer[0])
|
||||
.withU8<0x6> ((uint8_t) info.manufacturer[1])
|
||||
.withU8<0x7> ((uint8_t) info.manufacturer[2])
|
||||
.withU8<0x8> ((uint8_t) info.family[0])
|
||||
.withU8<0x9> ((uint8_t) info.family[1])
|
||||
.withU8<0xa> ((uint8_t) info.modelNumber[0])
|
||||
.withU8<0xb> ((uint8_t) info.modelNumber[1])
|
||||
.withU8<0xc> ((uint8_t) info.revision[0])
|
||||
.withU8<0xd> ((uint8_t) info.revision[1])
|
||||
.withU8<0xe> ((uint8_t) info.revision[2])
|
||||
.withU8<0xf> ((uint8_t) info.revision[3]);
|
||||
}
|
||||
|
||||
template <typename Fn>
|
||||
static bool makeEndpointNameNotification (const String& bytes, Fn&& fn)
|
||||
{
|
||||
constexpr auto maxSize = 98;
|
||||
|
||||
if (maxSize <= bytes.getNumBytesAsUTF8())
|
||||
return false;
|
||||
|
||||
const Span byteSpan { reinterpret_cast<const std::byte*> (bytes.toRawUTF8()), bytes.getNumBytesAsUTF8() };
|
||||
|
||||
splitIntoPackets (byteSpan, 14, [&] (SysEx7::Kind kind, Span<const std::byte> bytesThisTime)
|
||||
{
|
||||
const auto packet = Detail::makeStreamSubpacket (std::byte (3), kind, bytesThisTime);
|
||||
fn (View (packet.data()));
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename Fn>
|
||||
static bool makeProductInstanceIdNotification (const String& bytes, Fn&& fn)
|
||||
{
|
||||
constexpr auto maxSize = 42;
|
||||
|
||||
if (maxSize < bytes.getNumBytesAsUTF8())
|
||||
return false;
|
||||
|
||||
const Span byteSpan { reinterpret_cast<const std::byte*> (bytes.toRawUTF8()), bytes.getNumBytesAsUTF8() };
|
||||
|
||||
splitIntoPackets (byteSpan, 14, [&] (SysEx7::Kind kind, Span<const std::byte> bytesThisTime)
|
||||
{
|
||||
const auto packet = Detail::makeStreamSubpacket (std::byte (4), kind, bytesThisTime);
|
||||
fn (View (packet.data()));
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename Fn>
|
||||
static bool makeFunctionBlockNameNotification (uint8_t index, const String& bytes, Fn&& fn)
|
||||
{
|
||||
constexpr auto maxSize = 91;
|
||||
|
||||
if (maxSize < bytes.getNumBytesAsUTF8())
|
||||
return false;
|
||||
|
||||
const Span byteSpan { reinterpret_cast<const std::byte*> (bytes.toRawUTF8()), bytes.getNumBytesAsUTF8() };
|
||||
|
||||
splitIntoPackets (byteSpan, 13, [&] (SysEx7::Kind kind, Span<const std::byte> bytesThisTime)
|
||||
{
|
||||
const std::byte header[] { std::byte (0xf0) | std::byte ((uint8_t) kind << 2), std::byte (0x12), std::byte (index) };
|
||||
fn (View (Detail::makePacketX4 (header, bytesThisTime).data()));
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static PacketX4 makeFunctionBlockInfoNotification (uint8_t index, const BlockInfo& info)
|
||||
{
|
||||
const auto flags = ((uint8_t) info.getDirection() << 0)
|
||||
| ((uint8_t) info.getMIDI1ProxyKind() << 2)
|
||||
| ((uint8_t) info.getUiHint() << 4);
|
||||
return Detail::makeStream().withU8<0x1> (0x11)
|
||||
.withU8<0x2> ((uint8_t) (index | (info.isEnabled() << 7)))
|
||||
.withU8<0x3> ((uint8_t) flags)
|
||||
.withU8<0x4> (info.getFirstGroup())
|
||||
.withU8<0x5> (info.getNumGroups())
|
||||
.withU8<0x6> (info.getCiVersion())
|
||||
.withU8<0x7> (info.getMaxSysex8Streams());
|
||||
}
|
||||
|
||||
static PacketX4 makeStreamConfigurationRequest (StreamConfiguration options)
|
||||
{
|
||||
return Detail::makeStreamConfiguration (options).withU8<0x1> (5);
|
||||
}
|
||||
|
||||
static PacketX4 makeStreamConfigurationNotification (StreamConfiguration options)
|
||||
{
|
||||
return Detail::makeStreamConfiguration (options).withU8<0x1> (6);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace juce::universal_midi_packets
|
||||
/** @endcond */
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce::universal_midi_packets
|
||||
{
|
||||
|
||||
Iterator::Iterator (const uint32_t* ptr, [[maybe_unused]] size_t words) noexcept
|
||||
: view (ptr)
|
||||
#if JUCE_DEBUG
|
||||
, wordsRemaining (words)
|
||||
#endif
|
||||
{
|
||||
}
|
||||
|
||||
} // namespace juce::universal_midi_packets
|
||||
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
/** @cond */
|
||||
namespace juce::universal_midi_packets
|
||||
{
|
||||
|
||||
/**
|
||||
Enables iteration over a collection of Universal MIDI Packets stored as
|
||||
a contiguous range of 32-bit words.
|
||||
|
||||
This iterator is used by Packets to allow access to the messages
|
||||
that it contains.
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
class Iterator
|
||||
{
|
||||
public:
|
||||
/** Creates an invalid (singular) iterator. */
|
||||
Iterator() noexcept = default;
|
||||
|
||||
/** Creates an iterator pointing at `ptr`. */
|
||||
explicit Iterator (const uint32_t* ptr, size_t words) noexcept;
|
||||
|
||||
using difference_type = std::iterator_traits<const uint32_t*>::difference_type;
|
||||
using value_type = View;
|
||||
using reference = const View&;
|
||||
using pointer = const View*;
|
||||
using iterator_category = std::forward_iterator_tag;
|
||||
|
||||
/** Moves this iterator to the next packet in the range. */
|
||||
Iterator& operator++() noexcept
|
||||
{
|
||||
const auto increment = view.size();
|
||||
|
||||
#if JUCE_DEBUG
|
||||
// If you hit this, the memory region contained a truncated or otherwise
|
||||
// malformed Universal MIDI Packet.
|
||||
// The Iterator can only be used on regions containing complete packets!
|
||||
jassert (increment <= wordsRemaining);
|
||||
wordsRemaining -= increment;
|
||||
#endif
|
||||
|
||||
view = View (view.data() + increment);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** Moves this iterator to the next packet in the range,
|
||||
returning the value of the iterator before it was
|
||||
incremented.
|
||||
*/
|
||||
Iterator operator++ (int) noexcept
|
||||
{
|
||||
auto copy = *this;
|
||||
++(*this);
|
||||
return copy;
|
||||
}
|
||||
|
||||
/** Returns true if this iterator points to the same address
|
||||
as another iterator.
|
||||
*/
|
||||
bool operator== (const Iterator& other) const noexcept
|
||||
{
|
||||
return view == other.view;
|
||||
}
|
||||
|
||||
/** Returns false if this iterator points to the same address
|
||||
as another iterator.
|
||||
*/
|
||||
bool operator!= (const Iterator& other) const noexcept
|
||||
{
|
||||
return ! operator== (other);
|
||||
}
|
||||
|
||||
/** Returns a reference to a View of the packet currently
|
||||
pointed-to by this iterator.
|
||||
|
||||
The View can be queried for its size and content.
|
||||
*/
|
||||
reference operator*() const noexcept { return view; }
|
||||
|
||||
/** Returns a pointer to a View of the packet currently
|
||||
pointed-to by this iterator.
|
||||
|
||||
The View can be queried for its size and content.
|
||||
*/
|
||||
pointer operator->() const noexcept { return &view; }
|
||||
|
||||
private:
|
||||
View view;
|
||||
|
||||
#if JUCE_DEBUG
|
||||
size_t wordsRemaining = 0;
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace juce::universal_midi_packets
|
||||
/** @endcond */
|
||||
+305
@@ -0,0 +1,305 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
/** @cond */
|
||||
namespace juce::universal_midi_packets
|
||||
{
|
||||
|
||||
/**
|
||||
Extracts from a series of Universal MIDI Packets the bytes that are also meaningful in the
|
||||
bytestream MIDI 1.0 format.
|
||||
*/
|
||||
class SingleGroupMidi1ToBytestreamExtractor
|
||||
{
|
||||
public:
|
||||
void reset()
|
||||
{
|
||||
sysexInProgress = false;
|
||||
}
|
||||
|
||||
/** Converts a Universal MIDI Packet using the MIDI 1.0 Protocol to
|
||||
an equivalent MidiMessage. If the packet doesn't convert to a single bytestream message
|
||||
(as may be the case for long sysex7 data), then the the callback will be passed just the
|
||||
sysex bytes in the current packet. To reconstruct the entire sysex message, the caller
|
||||
can bytes that are marked as ongoingSysex, and process the full message once the callback
|
||||
receives bytes that are marked as lastSysex.
|
||||
|
||||
@param packet a packet which is using the MIDI 1.0 Protocol.
|
||||
@param time the timestamp to be applied to these messages.
|
||||
@param callback a callback that will be called with each converted MidiMessage.
|
||||
*/
|
||||
template <typename MessageCallback>
|
||||
void dispatch (const View& packet, double time, MessageCallback&& callback)
|
||||
{
|
||||
const auto firstWord = *packet.data();
|
||||
|
||||
if (sysexInProgress && shouldPacketTerminateSysExEarly (firstWord))
|
||||
{
|
||||
// unexpected end of last sysex
|
||||
callback (SysexExtractorCallbackKind::lastSysex, Span<const std::byte>());
|
||||
sysexInProgress = false;
|
||||
}
|
||||
|
||||
switch (packet.size())
|
||||
{
|
||||
case 1:
|
||||
{
|
||||
// Utility messages don't translate to bytestream format
|
||||
if (Utils::getMessageType (firstWord) != Utils::MessageKind::utility)
|
||||
{
|
||||
const auto converted = fromUmp (PacketX1 { firstWord }, time);
|
||||
callback (SysexExtractorCallbackKind::notSysex,
|
||||
Span (unalignedPointerCast<const std::byte*> (converted.getRawData()),
|
||||
(size_t) converted.getRawDataSize()));
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 2:
|
||||
{
|
||||
if (Utils::getMessageType (firstWord) == Utils::MessageKind::sysex7)
|
||||
processSysEx (PacketX2 { packet[0], packet[1] }, callback);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 3: // no 3-word packets in the current spec
|
||||
case 4: // no 4-word packets translate to bytestream format
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/** Converts from a Universal MIDI Packet to MIDI 1 bytestream format.
|
||||
|
||||
This is only capable of converting a single Universal MIDI Packet to
|
||||
an equivalent bytestream MIDI message. This function cannot understand
|
||||
multi-packet messages, like SysEx7 messages.
|
||||
|
||||
To convert multi-packet messages, use `Midi1ToBytestreamTranslator`
|
||||
to convert from a UMP MIDI 1.0 stream, or `ToBytestreamDispatcher`
|
||||
to convert from both MIDI 2.0 and MIDI 1.0.
|
||||
*/
|
||||
static MidiMessage fromUmp (const PacketX1& m, double time = 0)
|
||||
{
|
||||
const auto word = m.front();
|
||||
jassert (Utils::getNumWordsForMessageType (word) == 1);
|
||||
|
||||
const std::array<std::byte, 3> bytes { { std::byte ((word >> 0x10) & 0xff),
|
||||
std::byte ((word >> 0x08) & 0xff),
|
||||
std::byte ((word >> 0x00) & 0xff) } };
|
||||
const auto numBytes = MidiMessage::getMessageLengthFromFirstByte ((uint8_t) bytes.front());
|
||||
return MidiMessage (bytes.data(), numBytes, time);
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename MessageCallback>
|
||||
void processSysEx (const PacketX2& packet, MessageCallback&& callback)
|
||||
{
|
||||
const std::array<std::byte, 1> initial { std::byte { 0xf0 } }, final { std::byte { 0xf7 } };
|
||||
std::array<std::byte, 8> storage{};
|
||||
size_t validBytes = 0;
|
||||
|
||||
const auto pushBytes = [&] (const Span<const std::byte> b)
|
||||
{
|
||||
std::copy (b.begin(), b.end(), storage.data() + validBytes);
|
||||
validBytes += b.size();
|
||||
};
|
||||
|
||||
const auto pushPacket = [&] (const PacketX2& p)
|
||||
{
|
||||
const auto newBytes = SysEx7::getDataBytes (p);
|
||||
pushBytes (Span<const std::byte> (newBytes.data.data(), newBytes.size));
|
||||
};
|
||||
|
||||
const auto kind = getSysEx7Kind (packet[0]);
|
||||
|
||||
if ( ( sysexInProgress && (kind == SysEx7::Kind::begin || kind == SysEx7::Kind::complete))
|
||||
|| (! sysexInProgress && (kind == SysEx7::Kind::continuation || kind == SysEx7::Kind::end)))
|
||||
{
|
||||
// Malformed SysEx, drop progress and return
|
||||
callback (SysexExtractorCallbackKind::lastSysex, Span<const std::byte>());
|
||||
sysexInProgress = false;
|
||||
return;
|
||||
}
|
||||
|
||||
switch (kind)
|
||||
{
|
||||
case SysEx7::Kind::complete:
|
||||
pushBytes (Span (initial));
|
||||
pushPacket (packet);
|
||||
pushBytes (Span (final));
|
||||
break;
|
||||
|
||||
case SysEx7::Kind::begin:
|
||||
pushBytes (Span (initial));
|
||||
pushPacket (packet);
|
||||
break;
|
||||
|
||||
case SysEx7::Kind::continuation:
|
||||
pushPacket (packet);
|
||||
break;
|
||||
|
||||
case SysEx7::Kind::end:
|
||||
pushPacket (packet);
|
||||
pushBytes (Span (final));
|
||||
break;
|
||||
}
|
||||
|
||||
sysexInProgress = sysexInProgress ? (kind == SysEx7::Kind::continuation)
|
||||
: (kind == SysEx7::Kind::begin);
|
||||
const auto callbackKind = sysexInProgress ? SysexExtractorCallbackKind::ongoingSysex
|
||||
: SysexExtractorCallbackKind::lastSysex;
|
||||
callback (callbackKind, Span (storage.data(), validBytes));
|
||||
}
|
||||
|
||||
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) == Utils::MessageKind::utility;
|
||||
}
|
||||
|
||||
static bool isSysExContinuation (uint32_t word)
|
||||
{
|
||||
if (Utils::getMessageType (word) != Utils::MessageKind::sysex7)
|
||||
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) == Utils::MessageKind::commonRealtime && ((word >> 0x10) & 0xff) >= 0xf8;
|
||||
}
|
||||
|
||||
bool sysexInProgress = false;
|
||||
};
|
||||
|
||||
/**
|
||||
Parses a raw stream of uint32_t holding a series of Universal MIDI Packets using
|
||||
the MIDI 1.0 Protocol, converting to plain (non-UMP) MidiMessages.
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
class SingleGroupMidi1ToBytestreamTranslator
|
||||
{
|
||||
public:
|
||||
/** Ensures that there is room in the internal buffer for a sysex message of at least
|
||||
initialBufferSize bytes.
|
||||
*/
|
||||
explicit SingleGroupMidi1ToBytestreamTranslator (int initialBufferSize)
|
||||
{
|
||||
pendingSysExData.reserve (size_t (initialBufferSize));
|
||||
}
|
||||
|
||||
/** Clears the concatenator. */
|
||||
void reset()
|
||||
{
|
||||
extractor.reset();
|
||||
pendingSysExData.clear();
|
||||
pendingSysExTime = 0.0;
|
||||
}
|
||||
|
||||
/** Converts a Universal MIDI Packet using the MIDI 1.0 Protocol to
|
||||
an equivalent MidiMessage. Accumulates SysEx packets into a single
|
||||
MidiMessage, as appropriate.
|
||||
|
||||
@param packet a packet which is using the MIDI 1.0 Protocol.
|
||||
@param time the timestamp to be applied to these messages.
|
||||
@param callback a callback which will be called with each converted MidiMessage.
|
||||
*/
|
||||
template <typename MessageCallback>
|
||||
void dispatch (const View& packet, double time, MessageCallback&& callback)
|
||||
{
|
||||
extractor.dispatch (packet, time, [&] (SysexExtractorCallbackKind kind, Span<const std::byte> bytes)
|
||||
{
|
||||
switch (kind)
|
||||
{
|
||||
case SysexExtractorCallbackKind::notSysex:
|
||||
callback (BytesOnGroup { 0, bytes }, time);
|
||||
return;
|
||||
|
||||
case SysexExtractorCallbackKind::ongoingSysex:
|
||||
{
|
||||
if (pendingSysExData.empty())
|
||||
pendingSysExTime = time;
|
||||
|
||||
pendingSysExData.insert (pendingSysExData.end(), bytes.begin(), bytes.end());
|
||||
return;
|
||||
}
|
||||
|
||||
case SysexExtractorCallbackKind::lastSysex:
|
||||
{
|
||||
if (pendingSysExData.empty())
|
||||
pendingSysExTime = time;
|
||||
|
||||
pendingSysExData.insert (pendingSysExData.end(), bytes.begin(), bytes.end());
|
||||
|
||||
if (pendingSysExData.empty())
|
||||
return;
|
||||
|
||||
// If this is not true, then the sysex message was truncated somehow and we
|
||||
// probably shouldn't allow it to propagate
|
||||
if (pendingSysExData.back() == std::byte { 0xf7 })
|
||||
callback (BytesOnGroup { 0, Span<const std::byte> (pendingSysExData) }, pendingSysExTime);
|
||||
|
||||
pendingSysExData.clear();
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
SingleGroupMidi1ToBytestreamExtractor extractor;
|
||||
std::vector<std::byte> pendingSysExData;
|
||||
double pendingSysExTime = 0.0;
|
||||
};
|
||||
|
||||
} // namespace juce::universal_midi_packets
|
||||
/** @endcond */
|
||||
+210
@@ -0,0 +1,210 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce::universal_midi_packets
|
||||
{
|
||||
|
||||
PacketX2 Midi1ToMidi2DefaultTranslator::processNoteOnOrOff (const HelperValues helpers)
|
||||
{
|
||||
const auto velocity = helpers.byte2;
|
||||
const auto needsConversion = (helpers.byte0 & std::byte { 0xf0 }) == std::byte { 0x90 } && velocity == std::byte { 0 };
|
||||
const auto firstByte = needsConversion ? (std::byte { 0x80 } | (helpers.byte0 & std::byte { 0xf }))
|
||||
: helpers.byte0;
|
||||
|
||||
return PacketX2
|
||||
{
|
||||
Utils::bytesToWord (helpers.typeAndGroup, firstByte, helpers.byte1, std::byte { 0 }),
|
||||
(uint32_t) (Conversion::scaleTo16 (uint8_t (velocity)) << 0x10)
|
||||
};
|
||||
}
|
||||
|
||||
PacketX2 Midi1ToMidi2DefaultTranslator::processPolyPressure (const HelperValues helpers)
|
||||
{
|
||||
return PacketX2
|
||||
{
|
||||
Utils::bytesToWord (helpers.typeAndGroup, helpers.byte0, helpers.byte1, std::byte { 0 }),
|
||||
Conversion::scaleTo32 (uint8_t (helpers.byte2))
|
||||
};
|
||||
}
|
||||
|
||||
bool Midi1ToMidi2DefaultTranslator::processControlChange (const HelperValues helpers,
|
||||
PacketX2& packet)
|
||||
{
|
||||
const auto statusAndChannel = helpers.byte0;
|
||||
const auto cc = uint8_t (helpers.byte1);
|
||||
|
||||
const auto shouldAccumulate = [&]
|
||||
{
|
||||
switch (cc)
|
||||
{
|
||||
case 6:
|
||||
case 38:
|
||||
case 98:
|
||||
case 99:
|
||||
case 100:
|
||||
case 101:
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}();
|
||||
|
||||
const auto group = (uint8_t) (helpers.typeAndGroup & std::byte { 0xf });
|
||||
const auto channel = (uint8_t) (statusAndChannel & std::byte { 0xf });
|
||||
const auto byte = helpers.byte2;
|
||||
|
||||
if (shouldAccumulate)
|
||||
{
|
||||
auto& accumulator = groupAccumulators[group][channel];
|
||||
|
||||
if (accumulator.addByte (cc, byte))
|
||||
{
|
||||
const auto& bytes = accumulator.getBytes();
|
||||
const auto bank = bytes[0];
|
||||
const auto index = bytes[1];
|
||||
const auto msb = bytes[2];
|
||||
const auto lsb = bytes[3];
|
||||
|
||||
const auto value = uint16_t ((uint16_t (msb & std::byte { 0x7f }) << 7) | uint16_t (lsb & std::byte { 0x7f }));
|
||||
|
||||
const auto newStatus = accumulator.getKind() == PnKind::nrpn ? std::byte { 0x3 } : std::byte { 0x2 };
|
||||
|
||||
packet = PacketX2
|
||||
{
|
||||
Utils::bytesToWord (helpers.typeAndGroup, std::byte ((newStatus << 0x4) | std::byte { channel }), bank, index),
|
||||
Conversion::scaleTo32 (value)
|
||||
};
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (cc == 0)
|
||||
{
|
||||
groupBanks[group][channel].setMsb (byte);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (cc == 32)
|
||||
{
|
||||
groupBanks[group][channel].setLsb (byte);
|
||||
return false;
|
||||
}
|
||||
|
||||
packet = PacketX2
|
||||
{
|
||||
Utils::bytesToWord (helpers.typeAndGroup, statusAndChannel, std::byte { cc }, std::byte { 0 }),
|
||||
Conversion::scaleTo32 (uint8_t (helpers.byte2))
|
||||
};
|
||||
return true;
|
||||
}
|
||||
|
||||
PacketX2 Midi1ToMidi2DefaultTranslator::processProgramChange (const HelperValues helpers) const
|
||||
{
|
||||
const auto group = (uint8_t) (helpers.typeAndGroup & std::byte { 0xf });
|
||||
const auto channel = (uint8_t) (helpers.byte0 & std::byte { 0xf });
|
||||
const auto bank = groupBanks[group][channel];
|
||||
const auto valid = bank.isValid();
|
||||
|
||||
return PacketX2
|
||||
{
|
||||
Utils::bytesToWord (helpers.typeAndGroup,
|
||||
helpers.byte0,
|
||||
std::byte { 0 },
|
||||
valid ? std::byte { 1 } : std::byte { 0 }),
|
||||
Utils::bytesToWord (helpers.byte1,
|
||||
std::byte { 0 },
|
||||
valid ? std::byte { bank.getMsb() } : std::byte { 0 },
|
||||
valid ? std::byte { bank.getLsb() } : std::byte { 0 })
|
||||
};
|
||||
}
|
||||
|
||||
PacketX2 Midi1ToMidi2DefaultTranslator::processChannelPressure (const HelperValues helpers)
|
||||
{
|
||||
return PacketX2
|
||||
{
|
||||
Utils::bytesToWord (helpers.typeAndGroup, helpers.byte0, std::byte { 0 }, std::byte { 0 }),
|
||||
Conversion::scaleTo32 (uint8_t (helpers.byte1))
|
||||
};
|
||||
}
|
||||
|
||||
PacketX2 Midi1ToMidi2DefaultTranslator::processPitchBend (const HelperValues helpers)
|
||||
{
|
||||
const auto lsb = helpers.byte1;
|
||||
const auto msb = helpers.byte2;
|
||||
const auto value = uint16_t (uint16_t (msb) << 7 | uint16_t (lsb));
|
||||
|
||||
return PacketX2
|
||||
{
|
||||
Utils::bytesToWord (helpers.typeAndGroup, helpers.byte0, std::byte { 0 }, std::byte { 0 }),
|
||||
Conversion::scaleTo32 (value)
|
||||
};
|
||||
}
|
||||
|
||||
bool Midi1ToMidi2DefaultTranslator::PnAccumulator::addByte (uint8_t cc, std::byte byte)
|
||||
{
|
||||
const auto isStart = cc == 99 || cc == 101;
|
||||
|
||||
if (isStart)
|
||||
{
|
||||
kind = cc == 99 ? PnKind::nrpn : PnKind::rpn;
|
||||
index = 0;
|
||||
}
|
||||
|
||||
bytes[index] = byte;
|
||||
|
||||
const auto shouldContinue = [&]
|
||||
{
|
||||
switch (index)
|
||||
{
|
||||
case 0: return isStart;
|
||||
case 1: return kind == PnKind::nrpn ? cc == 98 : cc == 100;
|
||||
case 2: return cc == 6;
|
||||
case 3: return cc == 38;
|
||||
}
|
||||
|
||||
return false;
|
||||
}();
|
||||
|
||||
index = shouldContinue ? index + 1 : 0;
|
||||
|
||||
if (index != bytes.size())
|
||||
return false;
|
||||
|
||||
index = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace juce::universal_midi_packets
|
||||
+198
@@ -0,0 +1,198 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
/** @cond */
|
||||
namespace juce::universal_midi_packets
|
||||
{
|
||||
|
||||
/**
|
||||
Translates a series of MIDI 1 Universal MIDI Packets to corresponding MIDI 2
|
||||
packets.
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
class Midi1ToMidi2DefaultTranslator
|
||||
{
|
||||
public:
|
||||
Midi1ToMidi2DefaultTranslator() = default;
|
||||
|
||||
/** Converts MIDI 1 Universal MIDI Packets to corresponding MIDI 2 packets,
|
||||
calling `callback` with each converted packet.
|
||||
|
||||
In some cases (such as RPN/NRPN messages) multiple MIDI 1 packets will
|
||||
convert to a single MIDI 2 packet. In these cases, the translator will
|
||||
accumulate the full message internally, and send a single callback with
|
||||
the completed message, once all the individual MIDI 1 packets have been
|
||||
processed.
|
||||
*/
|
||||
template <typename PacketCallback>
|
||||
void dispatch (const View& v, PacketCallback&& callback)
|
||||
{
|
||||
const auto firstWord = v[0];
|
||||
const auto messageType = Utils::getMessageType (firstWord);
|
||||
|
||||
if (messageType != Utils::MessageKind::channelVoice1)
|
||||
{
|
||||
callback (v);
|
||||
return;
|
||||
}
|
||||
|
||||
const HelperValues helperValues
|
||||
{
|
||||
std::byte (0x40 | Utils::getGroup (firstWord)),
|
||||
std::byte ((firstWord >> 0x10) & 0xff),
|
||||
std::byte ((firstWord >> 0x08) & 0x7f),
|
||||
std::byte ((firstWord >> 0x00) & 0x7f),
|
||||
};
|
||||
|
||||
switch ((uint8_t) Utils::getStatus (firstWord))
|
||||
{
|
||||
case 0x8:
|
||||
case 0x9:
|
||||
{
|
||||
const auto packet = processNoteOnOrOff (helperValues);
|
||||
callback (View (packet.data()));
|
||||
return;
|
||||
}
|
||||
|
||||
case 0xa:
|
||||
{
|
||||
const auto packet = processPolyPressure (helperValues);
|
||||
callback (View (packet.data()));
|
||||
return;
|
||||
}
|
||||
|
||||
case 0xb:
|
||||
{
|
||||
PacketX2 packet;
|
||||
|
||||
if (processControlChange (helperValues, packet))
|
||||
callback (View (packet.data()));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
case 0xc:
|
||||
{
|
||||
const auto packet = processProgramChange (helperValues);
|
||||
callback (View (packet.data()));
|
||||
return;
|
||||
}
|
||||
|
||||
case 0xd:
|
||||
{
|
||||
const auto packet = processChannelPressure (helperValues);
|
||||
callback (View (packet.data()));
|
||||
return;
|
||||
}
|
||||
|
||||
case 0xe:
|
||||
{
|
||||
const auto packet = processPitchBend (helperValues);
|
||||
callback (View (packet.data()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
groupAccumulators = {};
|
||||
groupBanks = {};
|
||||
}
|
||||
|
||||
private:
|
||||
enum class PnKind { nrpn, rpn };
|
||||
|
||||
struct HelperValues
|
||||
{
|
||||
std::byte typeAndGroup;
|
||||
std::byte byte0;
|
||||
std::byte byte1;
|
||||
std::byte byte2;
|
||||
};
|
||||
|
||||
static PacketX2 processNoteOnOrOff (HelperValues helpers);
|
||||
static PacketX2 processPolyPressure (HelperValues helpers);
|
||||
|
||||
bool processControlChange (HelperValues helpers, PacketX2& packet);
|
||||
|
||||
PacketX2 processProgramChange (HelperValues helpers) const;
|
||||
|
||||
static PacketX2 processChannelPressure (HelperValues helpers);
|
||||
static PacketX2 processPitchBend (HelperValues helpers);
|
||||
|
||||
class PnAccumulator
|
||||
{
|
||||
public:
|
||||
bool addByte (uint8_t cc, std::byte byte);
|
||||
|
||||
const std::array<std::byte, 4>& getBytes() const noexcept { return bytes; }
|
||||
PnKind getKind() const noexcept { return kind; }
|
||||
|
||||
private:
|
||||
std::array<std::byte, 4> bytes;
|
||||
uint8_t index = 0;
|
||||
PnKind kind = PnKind::nrpn;
|
||||
};
|
||||
|
||||
class Bank
|
||||
{
|
||||
public:
|
||||
bool isValid() const noexcept { return (msb & std::byte { 0x80 }) == std::byte { 0 }; }
|
||||
|
||||
std::byte getMsb() const noexcept { return msb & std::byte (0x7f); }
|
||||
std::byte getLsb() const noexcept { return lsb & std::byte (0x7f); }
|
||||
|
||||
void setMsb (std::byte i) noexcept { msb = i & std::byte (0x7f); }
|
||||
void setLsb (std::byte i) noexcept { msb &= std::byte (0x7f); lsb = i & std::byte (0x7f); }
|
||||
|
||||
private:
|
||||
// We use the top bit to indicate whether this bank is valid.
|
||||
// After reading the spec, it's not clear how we should determine whether
|
||||
// there are valid values, so we'll just assume that the bank is valid
|
||||
// once either the lsb or msb have been written.
|
||||
std::byte msb { 0x80 };
|
||||
std::byte lsb { 0x00 };
|
||||
};
|
||||
|
||||
using ChannelAccumulators = std::array<PnAccumulator, 16>;
|
||||
std::array<ChannelAccumulators, 16> groupAccumulators;
|
||||
|
||||
using ChannelBanks = std::array<Bank, 16>;
|
||||
std::array<ChannelBanks, 16> groupBanks;
|
||||
};
|
||||
|
||||
} // namespace juce::universal_midi_packets
|
||||
/** @endcond */
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
/** @cond */
|
||||
namespace juce::universal_midi_packets
|
||||
{
|
||||
|
||||
/** Kinds of MIDI message transport.
|
||||
*/
|
||||
enum class Transport : uint8_t
|
||||
{
|
||||
bytestream, ///< A stream of variable-length messages. Suitable for MIDI 1.0.
|
||||
ump, ///< A stream of 32-bit words. Suitable for MIDI-1UP and MIDI 2.0.
|
||||
};
|
||||
|
||||
/** The kinds of MIDI protocol that can be formatted into Universal MIDI Packets.
|
||||
*/
|
||||
enum class PacketProtocol : uint8_t
|
||||
{
|
||||
MIDI_1_0,
|
||||
MIDI_2_0,
|
||||
};
|
||||
|
||||
/** All kinds of MIDI protocol understood by JUCE.
|
||||
*/
|
||||
enum class MidiProtocol : uint8_t
|
||||
{
|
||||
bytestream,
|
||||
UMP_MIDI_1_0,
|
||||
UMP_MIDI_2_0,
|
||||
};
|
||||
|
||||
} // namespace juce::universal_midi_packets
|
||||
/** @endcond */
|
||||
@@ -0,0 +1,248 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce::universal_midi_packets
|
||||
{
|
||||
|
||||
String StringUtils::getHexString (const View& view)
|
||||
{
|
||||
auto result = String::toHexString (*view.data());
|
||||
|
||||
std::for_each (std::next (view.begin()), view.end(), [&] (uint32_t word)
|
||||
{
|
||||
result << ' ' << String::toHexString (word);
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static String getUtilityMessageDescription (const PacketX1& p)
|
||||
{
|
||||
const auto word = p.front();
|
||||
|
||||
switch ((uint8_t) Utils::getStatus (word))
|
||||
{
|
||||
case 0x0: return "NOOP";
|
||||
case 0x1: return "JR Clock " + String::toHexString ((word >> 0x10) & 0xff);
|
||||
case 0x2: return "JR Timestamp " + String::toHexString ((word >> 0x10) & 0xff);
|
||||
}
|
||||
|
||||
jassertfalse;
|
||||
return {};
|
||||
}
|
||||
|
||||
static String getPacketDescription (const PacketX1& p)
|
||||
{
|
||||
switch (Utils::getMessageType (p.front()))
|
||||
{
|
||||
case Utils::MessageKind::utility: return "Utility: " + getUtilityMessageDescription (p);
|
||||
case Utils::MessageKind::commonRealtime: return "System: " + SingleGroupMidi1ToBytestreamExtractor::fromUmp (p).getDescription();
|
||||
case Utils::MessageKind::channelVoice1: return "MIDI 1.0 Channel Voice: " + SingleGroupMidi1ToBytestreamExtractor::fromUmp (p).getDescription();
|
||||
|
||||
case Utils::MessageKind::channelVoice2:
|
||||
case Utils::MessageKind::sysex7:
|
||||
case Utils::MessageKind::sysex8:
|
||||
case Utils::MessageKind::stream:
|
||||
break;
|
||||
}
|
||||
|
||||
jassertfalse;
|
||||
return {};
|
||||
}
|
||||
|
||||
static String getData64MessageDescription (const PacketX2& p)
|
||||
{
|
||||
const auto bytes = SysEx7::getDataBytes (p);
|
||||
|
||||
String byteString;
|
||||
|
||||
std::for_each (bytes.data.data(), bytes.data.data() + bytes.size, [&] (std::byte byte)
|
||||
{
|
||||
byteString << String::toHexString ((uint8_t) byte);
|
||||
});
|
||||
|
||||
switch (SysEx7::Kind (Utils::getStatus (p.front())))
|
||||
{
|
||||
case SysEx7::Kind::complete: return "Full: " + byteString;
|
||||
case SysEx7::Kind::begin: return "Start: " + byteString;
|
||||
case SysEx7::Kind::continuation: return "Continue: " + byteString;
|
||||
case SysEx7::Kind::end: return "End: " + byteString;
|
||||
}
|
||||
|
||||
jassertfalse;
|
||||
return {};
|
||||
}
|
||||
|
||||
static String getMidi2ChannelVoiceDescription (const PacketX2& p)
|
||||
{
|
||||
const auto getNoteName = [&] { return MidiMessage::getMidiNoteName ((p[0] >> 0x8) & 0xff, true, true, 3); };
|
||||
const auto getByte3 = [&] { return String::toHexString ((p[0] >> 0x8) & 0xff); };
|
||||
const auto getByte4 = [&] { return String::toHexString ((p[0] >> 0x0) & 0xff); };
|
||||
|
||||
const auto getVelocity = [&] { return String::toHexString ((p[1] >> 0x10) & 0xffff); };
|
||||
const auto getAttribute = [&] { return String::toHexString ((p[1] >> 0x00) & 0xffff); };
|
||||
|
||||
const auto getChannel = [&] { return String::toHexString (p.getChannel()); };
|
||||
|
||||
switch ((uint8_t) Utils::getStatus (p[0]))
|
||||
{
|
||||
case 0x0: return "Registered Per-Note Controller " + getByte4()
|
||||
+ " Channel " + getChannel()
|
||||
+ " Note " + getNoteName()
|
||||
+ " Data " + String::toHexString (p[1]);
|
||||
case 0x1: return "Assignable Per-Note Controller " + getByte4()
|
||||
+ " Channel " + getChannel()
|
||||
+ " Note " + getNoteName()
|
||||
+ " Data " + String::toHexString (p[1]);
|
||||
case 0x2: return "Registered Controller Bank " + getByte3()
|
||||
+ " Channel " + getChannel()
|
||||
+ " Index " + getByte4()
|
||||
+ " Data " + String::toHexString (p[1]);
|
||||
case 0x3: return "Assignable Controller Bank " + getByte3()
|
||||
+ " Channel " + getChannel()
|
||||
+ " Index " + getByte4()
|
||||
+ " Data " + String::toHexString (p[1]);
|
||||
case 0x4: return "Relative Registered Controller Bank " + getByte3()
|
||||
+ " Channel " + getChannel()
|
||||
+ " Index " + getByte4()
|
||||
+ " Data " + String::toHexString (p[1]);
|
||||
case 0x5: return "Relative Assignable Controller Bank " + getByte3()
|
||||
+ " Channel " + getChannel()
|
||||
+ " Index " + getByte4()
|
||||
+ " Data " + String::toHexString (p[1]);
|
||||
case 0x6: return "Per-Note Pitch Bend Note " + getNoteName()
|
||||
+ " Channel " + getChannel()
|
||||
+ " Data " + String::toHexString (p[1]);
|
||||
case 0x8: return "Note Off " + getNoteName()
|
||||
+ " Channel " + getChannel()
|
||||
+ " Attribute Type " + getByte4()
|
||||
+ " Velocity " + getVelocity()
|
||||
+ " Attribute Data " + getAttribute();
|
||||
case 0x9: return "Note On " + getNoteName()
|
||||
+ " Channel " + getChannel()
|
||||
+ " Attribute Type " + getByte4()
|
||||
+ " Velocity " + getVelocity()
|
||||
+ " Attribute Data " + getAttribute();
|
||||
case 0xa: return "Poly Pressure Note " + getNoteName()
|
||||
+ " Channel " + getChannel()
|
||||
+ " Data " + String::toHexString (p[1]);
|
||||
case 0xb: return "Control Change " + getByte3()
|
||||
+ " Channel " + getChannel()
|
||||
+ " Data " + String::toHexString (p[1]);
|
||||
case 0xc: return "Program Change Options " + getByte4()
|
||||
+ " Channel " + getChannel()
|
||||
+ " Program " + String::toHexString ((p[1] >> 0x18) & 0xff)
|
||||
+ " Bank MSB " + String::toHexString ((p[1] >> 0x08) & 0xff)
|
||||
+ " Bank LSB " + String::toHexString ((p[1] >> 0x00) & 0xff);
|
||||
case 0xd: return "Channel Pressure " + String::toHexString (p[1])
|
||||
+ " Channel " + getChannel();
|
||||
case 0xe: return "Pitch Bend " + String::toHexString (p[1])
|
||||
+ " Channel " + getChannel();
|
||||
case 0xf: return "Per-Note Management Note " + getNoteName()
|
||||
+ " Channel " + getChannel()
|
||||
+ " Options " + getByte4();
|
||||
}
|
||||
|
||||
jassertfalse;
|
||||
return {};
|
||||
}
|
||||
|
||||
static String getPacketDescription (const PacketX2& p)
|
||||
{
|
||||
switch (Utils::getMessageType (p.front()))
|
||||
{
|
||||
case Utils::MessageKind::sysex7: return "Data 64-Bit: " + getData64MessageDescription (p);
|
||||
case Utils::MessageKind::channelVoice2: return "MIDI 2.0 Channel Voice: " + getMidi2ChannelVoiceDescription (p);
|
||||
|
||||
case Utils::MessageKind::utility:
|
||||
case Utils::MessageKind::channelVoice1:
|
||||
case Utils::MessageKind::sysex8:
|
||||
case Utils::MessageKind::commonRealtime:
|
||||
case Utils::MessageKind::stream:
|
||||
break;
|
||||
}
|
||||
|
||||
jassertfalse;
|
||||
return {};
|
||||
}
|
||||
|
||||
static String getPacketDescription (const PacketX3&)
|
||||
{
|
||||
jassertfalse;
|
||||
return {};
|
||||
}
|
||||
|
||||
static String getPacketDescription (const PacketX4& p)
|
||||
{
|
||||
switch (Utils::getMessageType (p[0]))
|
||||
{
|
||||
case Utils::MessageKind::sysex8: return "Data 128 Bit"; // TODO
|
||||
case Utils::MessageKind::stream: return "Stream Configuration"; // TODO
|
||||
|
||||
case Utils::MessageKind::utility:
|
||||
case Utils::MessageKind::sysex7:
|
||||
case Utils::MessageKind::channelVoice1:
|
||||
case Utils::MessageKind::channelVoice2:
|
||||
case Utils::MessageKind::commonRealtime:
|
||||
break;
|
||||
}
|
||||
|
||||
jassertfalse;
|
||||
return {};
|
||||
}
|
||||
|
||||
String StringUtils::getDescription (const View& v)
|
||||
{
|
||||
const auto group = (String ("Group ") + String (Utils::getGroup (*v.data()))) + ' ';
|
||||
|
||||
switch (v.size())
|
||||
{
|
||||
case 1:
|
||||
return group + getPacketDescription (PacketX1 { v[0] });
|
||||
|
||||
case 2:
|
||||
return group + getPacketDescription (PacketX2 { v[0], v[1] });
|
||||
|
||||
case 3:
|
||||
return group + getPacketDescription (PacketX3 { v[0], v[1], v[2] });
|
||||
|
||||
case 4:
|
||||
return group + getPacketDescription (PacketX4 { v[0], v[1], v[2], v[3] });
|
||||
}
|
||||
|
||||
jassertfalse;
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace juce::universal_midi_packets
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce::universal_midi_packets
|
||||
{
|
||||
|
||||
/**
|
||||
Holds utility functions for extracting textual representations of Universal
|
||||
MIDI Packets.
|
||||
*/
|
||||
struct StringUtils
|
||||
{
|
||||
/** Return a textual description of this packet. */
|
||||
static String getHexString (const View&);
|
||||
|
||||
/** Return a textual description of this packet. */
|
||||
static String getDescription (const View&);
|
||||
|
||||
private:
|
||||
StringUtils() = default;
|
||||
};
|
||||
|
||||
} // namespace juce::universal_midi_packets
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce::universal_midi_packets
|
||||
{
|
||||
|
||||
uint32_t SysEx7::getNumPacketsRequiredForDataSize (uint32_t size)
|
||||
{
|
||||
constexpr auto denom = 6;
|
||||
return (size / denom) + ((size % denom) != 0);
|
||||
}
|
||||
|
||||
SysEx7::PacketBytes SysEx7::getDataBytes (const PacketX2& packet)
|
||||
{
|
||||
const auto numBytes = Utils::getChannel (packet[0]);
|
||||
constexpr uint8_t maxBytes = 6;
|
||||
jassert (numBytes <= maxBytes);
|
||||
|
||||
return
|
||||
{
|
||||
{ { std::byte { packet.getU8<2>() },
|
||||
std::byte { packet.getU8<3>() },
|
||||
std::byte { packet.getU8<4>() },
|
||||
std::byte { packet.getU8<5>() },
|
||||
std::byte { packet.getU8<6>() },
|
||||
std::byte { packet.getU8<7>() } } },
|
||||
jmin (numBytes, maxBytes)
|
||||
};
|
||||
}
|
||||
|
||||
} // namespace juce::universal_midi_packets
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
/** @cond */
|
||||
namespace juce::universal_midi_packets
|
||||
{
|
||||
|
||||
/**
|
||||
This struct acts as a single-file namespace for Universal MIDI Packet
|
||||
functionality related to 7-bit SysEx.
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
struct SysEx7
|
||||
{
|
||||
/** Returns the number of 64-bit packets required to hold a series of
|
||||
SysEx bytes.
|
||||
|
||||
The number passed to this function should exclude the leading/trailing
|
||||
SysEx bytes used in an old midi bytestream, as these are not required
|
||||
when using Universal MIDI Packets.
|
||||
*/
|
||||
static uint32_t getNumPacketsRequiredForDataSize (uint32_t);
|
||||
|
||||
/** The different kinds of UMP SysEx-7 message. */
|
||||
enum class Kind : uint8_t
|
||||
{
|
||||
/** The whole message fits in a single 2-word packet. */
|
||||
complete = 0,
|
||||
|
||||
/** The packet begins a SysEx message that will continue in subsequent packets. */
|
||||
begin = 1,
|
||||
|
||||
/** The packet is a continuation of an ongoing SysEx message. */
|
||||
continuation = 2,
|
||||
|
||||
/** The packet terminates an ongoing SysEx message. */
|
||||
end = 3
|
||||
};
|
||||
|
||||
/** Holds the bytes from a single SysEx-7 packet. */
|
||||
struct PacketBytes
|
||||
{
|
||||
std::array<std::byte, 6> data;
|
||||
uint8_t size;
|
||||
};
|
||||
|
||||
/** Extracts the data bytes from a 64-bit data message. */
|
||||
static PacketBytes getDataBytes (const PacketX2& packet);
|
||||
};
|
||||
|
||||
} // namespace juce::universal_midi_packets
|
||||
/** @endcond */
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce::universal_midi_packets
|
||||
{
|
||||
|
||||
uint32_t Utils::getNumWordsForMessageType (uint32_t mt)
|
||||
{
|
||||
switch ((uint8_t) Utils::getMessageType (mt))
|
||||
{
|
||||
case 0x0:
|
||||
case 0x1:
|
||||
case 0x2:
|
||||
case 0x6:
|
||||
case 0x7:
|
||||
return 1;
|
||||
case 0x3:
|
||||
case 0x4:
|
||||
case 0x8:
|
||||
case 0x9:
|
||||
case 0xa:
|
||||
return 2;
|
||||
case 0xb:
|
||||
case 0xc:
|
||||
return 3;
|
||||
case 0x5:
|
||||
case 0xd:
|
||||
case 0xe:
|
||||
case 0xf:
|
||||
return 4;
|
||||
}
|
||||
|
||||
jassertfalse;
|
||||
return 1;
|
||||
}
|
||||
|
||||
} // namespace juce::universal_midi_packets
|
||||
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
/** @cond */
|
||||
namespace juce::universal_midi_packets
|
||||
{
|
||||
|
||||
/**
|
||||
Helpful types and functions for interacting with Universal MIDI Packets.
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
struct Utils
|
||||
{
|
||||
/** Joins 4 bytes into a single 32-bit word. */
|
||||
static constexpr uint32_t bytesToWord (std::byte a, std::byte b, std::byte c, std::byte d)
|
||||
{
|
||||
return uint32_t (a) << 0x18
|
||||
| uint32_t (b) << 0x10
|
||||
| uint32_t (c) << 0x08
|
||||
| uint32_t (d) << 0x00;
|
||||
}
|
||||
|
||||
/** Returns the expected number of 32-bit words in a Universal MIDI Packet, given
|
||||
the first word of the packet.
|
||||
|
||||
The result will be between 1 and 4 inclusive.
|
||||
A result of 1 means that the word is itself a complete packet.
|
||||
*/
|
||||
static uint32_t getNumWordsForMessageType (uint32_t);
|
||||
|
||||
/**
|
||||
Helper functions for setting/getting 4-bit ranges inside a 32-bit word.
|
||||
*/
|
||||
template <size_t Index>
|
||||
struct U4
|
||||
{
|
||||
static constexpr uint32_t shift = (uint32_t) 0x1c - Index * 4;
|
||||
|
||||
static constexpr uint32_t set (uint32_t word, uint8_t value)
|
||||
{
|
||||
return (word & ~((uint32_t) 0xf << shift)) | (uint32_t) ((value & 0xf) << shift);
|
||||
}
|
||||
|
||||
static constexpr uint8_t get (uint32_t word)
|
||||
{
|
||||
return (word >> shift) & 0xf;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
Helper functions for setting/getting 8-bit ranges inside a 32-bit word.
|
||||
*/
|
||||
template <size_t Index>
|
||||
struct U8
|
||||
{
|
||||
static constexpr uint32_t shift = (uint32_t) 0x18 - Index * 8;
|
||||
|
||||
static constexpr uint32_t set (uint32_t word, uint8_t value)
|
||||
{
|
||||
return (word & ~((uint32_t) 0xff << shift)) | (uint32_t) (value << shift);
|
||||
}
|
||||
|
||||
static constexpr uint8_t get (uint32_t word)
|
||||
{
|
||||
return (word >> shift) & 0xff;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
Helper functions for setting/getting 16-bit ranges inside a 32-bit word.
|
||||
*/
|
||||
template <size_t Index>
|
||||
struct U16
|
||||
{
|
||||
static constexpr uint32_t shift = (uint32_t) 0x10 - Index * 16;
|
||||
|
||||
static constexpr uint32_t set (uint32_t word, uint16_t value)
|
||||
{
|
||||
return (word & ~((uint32_t) 0xffff << shift)) | (uint32_t) (value << shift);
|
||||
}
|
||||
|
||||
static constexpr uint16_t get (uint32_t word)
|
||||
{
|
||||
return (uint16_t) ((word >> shift) & 0xffff);
|
||||
}
|
||||
};
|
||||
|
||||
enum class MessageKind : uint8_t
|
||||
{
|
||||
utility = 0x0,
|
||||
commonRealtime = 0x1,
|
||||
channelVoice1 = 0x2,
|
||||
sysex7 = 0x3,
|
||||
channelVoice2 = 0x4,
|
||||
sysex8 = 0x5,
|
||||
stream = 0xf,
|
||||
};
|
||||
|
||||
static constexpr bool hasGroup (MessageKind k)
|
||||
{
|
||||
return ! isGroupless (k);
|
||||
}
|
||||
|
||||
static constexpr bool isGroupless (MessageKind k)
|
||||
{
|
||||
return k == MessageKind::utility || k == MessageKind::stream;
|
||||
}
|
||||
|
||||
static constexpr MessageKind getMessageType (uint32_t w) noexcept { return MessageKind { U4<0>::get (w) }; }
|
||||
static constexpr std::byte getStatus (uint32_t w) noexcept { return std::byte { U4<2>::get (w) }; }
|
||||
static constexpr uint8_t getChannel (uint32_t w) noexcept { return U4<3>::get (w); }
|
||||
static constexpr uint8_t getGroup (uint32_t w) noexcept { return U4<1>::get (w); }
|
||||
};
|
||||
|
||||
} // namespace juce::universal_midi_packets
|
||||
/** @endcond */
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce::universal_midi_packets
|
||||
{
|
||||
|
||||
uint32_t View::size() const noexcept
|
||||
{
|
||||
jassert (ptr != nullptr);
|
||||
return Utils::getNumWordsForMessageType (*ptr);
|
||||
}
|
||||
|
||||
} // namespace juce::universal_midi_packets
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
/** @cond */
|
||||
namespace juce::universal_midi_packets
|
||||
{
|
||||
|
||||
/**
|
||||
Points to a single Universal MIDI Packet.
|
||||
|
||||
The packet must be well-formed for member functions to work correctly.
|
||||
|
||||
Specifically, the constructor argument must be the beginning of a region of
|
||||
uint32_t that contains at least `getNumWordsForMessageType (*data)` items,
|
||||
where `data` is the constructor argument.
|
||||
|
||||
NOTE: Instances of this class do not own the memory that they point to!
|
||||
If you need to store a packet pointed-to by a View for later use, copy
|
||||
the view contents to a Packets collection, or use the Utils::PacketX types.
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
class View
|
||||
{
|
||||
public:
|
||||
/** Create an invalid view. */
|
||||
View() noexcept = default;
|
||||
|
||||
/** Create a view of the packet starting at address `d`. */
|
||||
explicit View (const uint32_t* data) noexcept : ptr (data) {}
|
||||
|
||||
/** Get a pointer to the first word in the Universal MIDI Packet currently
|
||||
pointed-to by this view.
|
||||
*/
|
||||
const uint32_t* data() const noexcept { return ptr; }
|
||||
|
||||
/** Get the number of 32 bit words (between 1 and 4 inclusive) in the Universal
|
||||
MIDI Packet currently pointed-to by this view.
|
||||
*/
|
||||
uint32_t size() const noexcept;
|
||||
|
||||
/** Get a specific word from this packet.
|
||||
|
||||
Passing an `index` that is greater than or equal to the result of `size`
|
||||
will cause undefined behaviour.
|
||||
*/
|
||||
const uint32_t& operator[] (size_t index) const noexcept { return ptr[index]; }
|
||||
|
||||
/** Get an iterator pointing to the first word in the packet. */
|
||||
const uint32_t* begin() const noexcept { return ptr; }
|
||||
const uint32_t* cbegin() const noexcept { return ptr; }
|
||||
|
||||
/** Get an iterator pointing one-past the last word in the packet. */
|
||||
const uint32_t* end() const noexcept { return ptr + size(); }
|
||||
const uint32_t* cend() const noexcept { return ptr + size(); }
|
||||
|
||||
/** Return true if this view is pointing to the same address as another view. */
|
||||
bool operator== (const View& other) const noexcept { return ptr == other.ptr; }
|
||||
|
||||
/** Return false if this view is pointing to the same address as another view. */
|
||||
bool operator!= (const View& other) const noexcept { return ! operator== (other); }
|
||||
|
||||
private:
|
||||
const uint32_t* ptr = nullptr;
|
||||
};
|
||||
|
||||
} // namespace juce::universal_midi_packets
|
||||
/** @endcond */
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,211 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
/** @cond */
|
||||
namespace juce::universal_midi_packets
|
||||
{
|
||||
|
||||
/**
|
||||
Holds a single Universal MIDI Packet.
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
template <size_t numWords>
|
||||
class Packet
|
||||
{
|
||||
public:
|
||||
Packet() = default;
|
||||
|
||||
template <size_t w = numWords, std::enable_if_t<w == 1, int> = 0>
|
||||
Packet (uint32_t a)
|
||||
: contents { { a } }
|
||||
{
|
||||
jassert (Utils::getNumWordsForMessageType (a) == 1);
|
||||
}
|
||||
|
||||
template <size_t w = numWords, std::enable_if_t<w == 2, int> = 0>
|
||||
Packet (uint32_t a, uint32_t b)
|
||||
: contents { { a, b } }
|
||||
{
|
||||
jassert (Utils::getNumWordsForMessageType (a) == 2);
|
||||
}
|
||||
|
||||
template <size_t w = numWords, std::enable_if_t<w == 3, int> = 0>
|
||||
Packet (uint32_t a, uint32_t b, uint32_t c)
|
||||
: contents { { a, b, c } }
|
||||
{
|
||||
jassert (Utils::getNumWordsForMessageType (a) == 3);
|
||||
}
|
||||
|
||||
template <size_t w = numWords, std::enable_if_t<w == 4, int> = 0>
|
||||
Packet (uint32_t a, uint32_t b, uint32_t c, uint32_t d)
|
||||
: contents { { a, b, c, d } }
|
||||
{
|
||||
jassert (Utils::getNumWordsForMessageType (a) == 4);
|
||||
}
|
||||
|
||||
template <size_t w, std::enable_if_t<w == numWords, int> = 0>
|
||||
explicit Packet (const std::array<uint32_t, w>& fullPacket)
|
||||
: contents (fullPacket)
|
||||
{
|
||||
jassert (Utils::getNumWordsForMessageType (fullPacket.front()) == numWords);
|
||||
}
|
||||
|
||||
Packet withMessageType (Utils::MessageKind type) const noexcept
|
||||
{
|
||||
return withU4<0> (uint8_t (type));
|
||||
}
|
||||
|
||||
Packet withGroup (uint8_t group) const noexcept
|
||||
{
|
||||
return withU4<1> (group);
|
||||
}
|
||||
|
||||
Packet withStatus (std::byte status) const noexcept
|
||||
{
|
||||
return withU4<2> (uint8_t (status));
|
||||
}
|
||||
|
||||
Packet withChannel (uint8_t channel) const noexcept
|
||||
{
|
||||
return withU4<3> (channel);
|
||||
}
|
||||
|
||||
Utils::MessageKind getMessageType() const noexcept { return Utils::MessageKind (getU4<0>()); }
|
||||
|
||||
uint8_t getGroup() const noexcept { return getU4<1>(); }
|
||||
|
||||
std::byte getStatus() const noexcept { return getU4<2>(); }
|
||||
|
||||
uint8_t getChannel() const noexcept { return getU4<3>(); }
|
||||
|
||||
template <size_t index>
|
||||
Packet withU4 (uint8_t value) const noexcept
|
||||
{
|
||||
constexpr auto word = index / 8;
|
||||
auto copy = *this;
|
||||
std::get<word> (copy.contents) = Utils::U4<index % 8>::set (copy.template getU32<word>(), value);
|
||||
return copy;
|
||||
}
|
||||
|
||||
template <size_t index>
|
||||
Packet withU8 (uint8_t value) const noexcept
|
||||
{
|
||||
constexpr auto word = index / 4;
|
||||
auto copy = *this;
|
||||
std::get<word> (copy.contents) = Utils::U8<index % 4>::set (copy.template getU32<word>(), value);
|
||||
return copy;
|
||||
}
|
||||
|
||||
template <size_t index>
|
||||
Packet withU16 (uint16_t value) const noexcept
|
||||
{
|
||||
constexpr auto word = index / 2;
|
||||
auto copy = *this;
|
||||
std::get<word> (copy.contents) = Utils::U16<index % 2>::set (copy.template getU32<word>(), value);
|
||||
return copy;
|
||||
}
|
||||
|
||||
template <size_t index>
|
||||
Packet withU32 (uint32_t value) const noexcept
|
||||
{
|
||||
auto copy = *this;
|
||||
std::get<index> (copy.contents) = value;
|
||||
return copy;
|
||||
}
|
||||
|
||||
template <size_t index>
|
||||
uint8_t getU4() const noexcept
|
||||
{
|
||||
return Utils::U4<index % 8>::get (this->template getU32<index / 8>());
|
||||
}
|
||||
|
||||
template <size_t index>
|
||||
uint8_t getU8() const noexcept
|
||||
{
|
||||
return Utils::U8<index % 4>::get (this->template getU32<index / 4>());
|
||||
}
|
||||
|
||||
template <size_t index>
|
||||
uint16_t getU16() const noexcept
|
||||
{
|
||||
return Utils::U16<index % 2>::get (this->template getU32<index / 2>());
|
||||
}
|
||||
|
||||
template <size_t index>
|
||||
uint32_t getU32() const noexcept
|
||||
{
|
||||
return std::get<index> (contents);
|
||||
}
|
||||
|
||||
bool operator== (const Packet& other) const
|
||||
{
|
||||
return contents == other.contents;
|
||||
}
|
||||
|
||||
bool operator!= (const Packet& other) const
|
||||
{
|
||||
return contents != other.contents;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
using Contents = std::array<uint32_t, numWords>;
|
||||
|
||||
using const_iterator = typename Contents::const_iterator;
|
||||
|
||||
const_iterator begin() const noexcept { return contents.begin(); }
|
||||
const_iterator cbegin() const noexcept { return contents.begin(); }
|
||||
|
||||
const_iterator end() const noexcept { return contents.end(); }
|
||||
const_iterator cend() const noexcept { return contents.end(); }
|
||||
|
||||
const uint32_t* data() const noexcept { return contents.data(); }
|
||||
size_t size() const noexcept { return contents.size(); }
|
||||
|
||||
const uint32_t& front() const noexcept { return contents.front(); }
|
||||
const uint32_t& back() const noexcept { return contents.back(); }
|
||||
|
||||
const uint32_t& operator[] (size_t index) const noexcept { return contents[index]; }
|
||||
|
||||
private:
|
||||
Contents contents { {} };
|
||||
};
|
||||
|
||||
using PacketX1 = Packet<1>;
|
||||
using PacketX2 = Packet<2>;
|
||||
using PacketX3 = Packet<3>;
|
||||
using PacketX4 = Packet<4>;
|
||||
|
||||
} // namespace juce::universal_midi_packets
|
||||
/** @endcond */
|
||||
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
/** @cond */
|
||||
namespace juce::universal_midi_packets
|
||||
{
|
||||
|
||||
/**
|
||||
Holds a collection of Universal MIDI Packets.
|
||||
|
||||
Unlike MidiBuffer, this collection does not store any additional information
|
||||
(e.g. timestamps) alongside the raw messages.
|
||||
|
||||
If timestamps are required, these can be added to the container in UMP format,
|
||||
as Jitter Reduction Utility messages.
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
class Packets
|
||||
{
|
||||
public:
|
||||
/** Adds a single packet to the collection.
|
||||
|
||||
The View must be valid for this to work. If the view
|
||||
points to a malformed message, or if the view points to a region
|
||||
too short for the contained message, this call will result in
|
||||
undefined behaviour.
|
||||
*/
|
||||
void add (const View& v) { storage.insert (storage.end(), v.cbegin(), v.cend()); }
|
||||
|
||||
void add (const PacketX1& p) { addImpl (p); }
|
||||
void add (const PacketX2& p) { addImpl (p); }
|
||||
void add (const PacketX3& p) { addImpl (p); }
|
||||
void add (const PacketX4& p) { addImpl (p); }
|
||||
|
||||
/** Pre-allocates space for at least `numWords` 32-bit words in this collection. */
|
||||
void reserve (size_t numWords) { storage.reserve (numWords); }
|
||||
|
||||
/** Removes all previously-added packets from this collection. */
|
||||
void clear() { storage.clear(); }
|
||||
|
||||
/** Gets an iterator pointing to the first packet in this collection. */
|
||||
Iterator cbegin() const noexcept { return Iterator (data(), size()); }
|
||||
Iterator begin() const noexcept { return cbegin(); }
|
||||
|
||||
/** Gets an iterator pointing one-past the last packet in this collection. */
|
||||
Iterator cend() const noexcept { return Iterator (data() + size(), 0); }
|
||||
Iterator end() const noexcept { return cend(); }
|
||||
|
||||
/** Gets a pointer to the contents of the collection as a range of raw 32-bit words. */
|
||||
const uint32_t* data() const noexcept { return storage.data(); }
|
||||
|
||||
/** Returns the number of uint32_t words in storage.
|
||||
|
||||
Note that this is likely to be larger than the number of packets
|
||||
currently being stored, as some packets span multiple words.
|
||||
*/
|
||||
size_t size() const noexcept { return storage.size(); }
|
||||
|
||||
private:
|
||||
template <size_t numWords>
|
||||
void addImpl (const Packet<numWords>& p)
|
||||
{
|
||||
jassert (Utils::getNumWordsForMessageType (p[0]) == numWords);
|
||||
add (View (p.data()));
|
||||
}
|
||||
|
||||
std::vector<uint32_t> storage;
|
||||
};
|
||||
|
||||
} // namespace juce::universal_midi_packets
|
||||
/** @endcond */
|
||||
Reference in New Issue
Block a user