1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-09 23:34:20 +00:00

MIDI: Add stronger MessageKind type in UMPUtils.h

This commit is contained in:
reuk 2025-03-05 20:22:43 +00:00
parent 5e590c536b
commit 6d58bf637b
No known key found for this signature in database
6 changed files with 35 additions and 22 deletions

View file

@ -228,7 +228,7 @@ struct Conversion
{
const auto firstWord = v[0];
if (Utils::getMessageType (firstWord) != 0x4)
if (Utils::getMessageType (firstWord) != Utils::MessageKind::channelVoice2)
{
callback (v);
return;
@ -237,7 +237,7 @@ struct Conversion
const auto status = Utils::getStatus (firstWord);
const auto typeAndGroup = ((std::byte { 0x2 } << 0x4) | std::byte { Utils::getGroup (firstWord) });
switch (status)
switch ((uint8_t) status)
{
case 0x8: // note off
case 0x9: // note on
@ -250,10 +250,10 @@ struct Conversion
// 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 == 0x9 && byte3 == std::byte { 0 };
const auto needsCorrection = status == std::byte { 0x9 } && byte3 == std::byte { 0 };
const auto correctedByte = needsCorrection ? std::byte { 1 } : byte3;
const auto shouldIgnore = status == 0xb && [&]
const auto shouldIgnore = status == std::byte { 0xb } && [&]
{
switch (uint8_t (byte2))
{
@ -298,8 +298,8 @@ struct Conversion
case 0x2: // rpn
case 0x3: // nrpn
{
const auto ccX = status == 0x2 ? std::byte { 101 } : std::byte { 99 };
const auto ccY = status == 0x2 ? std::byte { 100 } : std::byte { 98 };
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]);

View file

@ -81,7 +81,7 @@ public:
case 1:
{
// Utility messages don't translate to bytestream format
if (Utils::getMessageType (firstWord) != 0x00)
if (Utils::getMessageType (firstWord) != Utils::MessageKind::utility)
{
const auto message = fromUmp (PacketX1 { firstWord }, time);
callback (BytestreamMidiView (&message));
@ -92,7 +92,7 @@ public:
case 2:
{
if (Utils::getMessageType (firstWord) == 0x3)
if (Utils::getMessageType (firstWord) == Utils::MessageKind::sysex7)
processSysEx (PacketX2 { packet[0], packet[1] }, time, callback);
break;
@ -199,12 +199,12 @@ private:
static bool isJROrNOP (uint32_t word)
{
return Utils::getMessageType (word) == 0x0;
return Utils::getMessageType (word) == Utils::MessageKind::utility;
}
static bool isSysExContinuation (uint32_t word)
{
if (Utils::getMessageType (word) != 0x3)
if (Utils::getMessageType (word) != Utils::MessageKind::sysex7)
return false;
const auto kind = getSysEx7Kind (word);
@ -213,7 +213,7 @@ private:
static bool isSystemRealTime (uint32_t word)
{
return Utils::getMessageType (word) == 0x1 && ((word >> 0x10) & 0xff) >= 0xf8;
return Utils::getMessageType (word) == Utils::MessageKind::commonRealtime && ((word >> 0x10) & 0xff) >= 0xf8;
}
std::vector<std::byte> pendingSysExData;

View file

@ -62,7 +62,7 @@ public:
const auto firstWord = v[0];
const auto messageType = Utils::getMessageType (firstWord);
if (messageType != 0x2)
if (messageType != Utils::MessageKind::channelVoice1)
{
callback (v);
return;
@ -76,7 +76,7 @@ public:
std::byte ((firstWord >> 0x00) & 0x7f),
};
switch (Utils::getStatus (firstWord))
switch ((uint8_t) Utils::getStatus (firstWord))
{
case 0x8:
case 0x9:

View file

@ -37,7 +37,7 @@ namespace juce::universal_midi_packets
uint32_t Utils::getNumWordsForMessageType (uint32_t mt)
{
switch (Utils::getMessageType (mt))
switch ((uint8_t) Utils::getMessageType (mt))
{
case 0x0:
case 0x1:

View file

@ -75,7 +75,7 @@ struct Utils
static constexpr uint8_t get (uint32_t word)
{
return (uint8_t) ((word >> shift) & 0xf);
return (word >> shift) & 0xf;
}
};
@ -94,7 +94,7 @@ struct Utils
static constexpr uint8_t get (uint32_t word)
{
return (uint8_t) ((word >> shift) & 0xff);
return (word >> shift) & 0xff;
}
};
@ -117,10 +117,21 @@ struct Utils
}
};
static constexpr uint8_t getMessageType (uint32_t w) noexcept { return U4<0>::get (w); }
static constexpr uint8_t getGroup (uint32_t w) noexcept { return U4<1>::get (w); }
static constexpr uint8_t getStatus (uint32_t w) noexcept { return U4<2>::get (w); }
static constexpr uint8_t getChannel (uint32_t w) noexcept { return U4<3>::get (w); }
enum class MessageKind : uint8_t
{
utility = 0x0,
commonRealtime = 0x1,
channelVoice1 = 0x2,
sysex7 = 0x3,
channelVoice2 = 0x4,
sysex8 = 0x5,
stream = 0xf,
};
static constexpr MessageKind getMessageType (uint32_t w) noexcept { return MessageKind { U4<0>::get (w) }; }
static constexpr uint8_t getGroup (uint32_t w) noexcept { return U4<1>::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); }
};
} // namespace juce::universal_midi_packets

View file

@ -62,8 +62,10 @@ public:
expect (packets.size() == 1);
// Make sure that the message type is correct
const auto msgType = Utils::getMessageType (packets.data()[0]);
expect (msgType == ((m.getRawData()[0] >> 0x4) == 0xf ? 0x1 : 0x2));
expect (Utils::getMessageType (packets.data()[0])
== ((m.getRawData()[0] >> 0x4) == 0xf
? Utils::MessageKind::commonRealtime
: Utils::MessageKind::channelVoice1));
translator.dispatch (View {packets.data() },
0,