mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-09 23:34:20 +00:00
MIDI: Use Spans instead of pointer pairs in more places
This commit is contained in:
parent
fb4f04e4d1
commit
835216c581
7 changed files with 45 additions and 66 deletions
|
|
@ -55,12 +55,11 @@ public:
|
|||
continue from that point in the packet (unless `reset` is called first).
|
||||
*/
|
||||
template <typename PacketCallbackFunction>
|
||||
void dispatch (const uint32_t* begin,
|
||||
const uint32_t* end,
|
||||
void dispatch (Span<const uint32_t> words,
|
||||
double timeStamp,
|
||||
PacketCallbackFunction&& callback)
|
||||
{
|
||||
std::for_each (begin, end, [&] (uint32_t word)
|
||||
for (const auto word : words)
|
||||
{
|
||||
nextPacket[currentPacketLen++] = word;
|
||||
|
||||
|
|
@ -69,7 +68,7 @@ public:
|
|||
callback (View (nextPacket.data()), timeStamp);
|
||||
currentPacketLen = 0;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
|
|
@ -106,14 +105,12 @@ public:
|
|||
|
||||
/** Calls `callback` with a View of each converted packet as it becomes ready.
|
||||
|
||||
@param begin the first byte in a range of bytes representing bytestream-encoded MIDI messages.
|
||||
@param end one-past the last byte in a range of bytes representing bytestream-encoded MIDI messages.
|
||||
@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 (const uint8_t* begin,
|
||||
const uint8_t* end,
|
||||
void dispatch (Span<const std::byte> bytes,
|
||||
double timestamp,
|
||||
PacketCallbackFunction&& callback)
|
||||
{
|
||||
|
|
@ -139,7 +136,7 @@ public:
|
|||
};
|
||||
|
||||
Callback inputCallback { *this, &callback };
|
||||
concatenator.pushMidiData (begin, int (end - begin), timestamp, (void*) nullptr, inputCallback);
|
||||
concatenator.pushMidiData (bytes, timestamp, (void*) nullptr, inputCallback);
|
||||
}
|
||||
|
||||
private:
|
||||
|
|
@ -181,12 +178,11 @@ public:
|
|||
@param callback a callback which will be passed a MidiMessage each time a new message becomes ready.
|
||||
*/
|
||||
template <typename BytestreamMessageCallback>
|
||||
void dispatch (const uint32_t* begin,
|
||||
const uint32_t* end,
|
||||
void dispatch (Span<const uint32_t> words,
|
||||
double timestamp,
|
||||
BytestreamMessageCallback&& callback)
|
||||
{
|
||||
dispatcher.dispatch (begin, end, timestamp, [&] (const View& view, double time)
|
||||
dispatcher.dispatch (words, timestamp, [&] (const View& view, double time)
|
||||
{
|
||||
converter.convert (view, time, callback);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -67,12 +67,10 @@ public:
|
|||
? Utils::MessageKind::commonRealtime
|
||||
: Utils::MessageKind::channelVoice1));
|
||||
|
||||
translator.dispatch (View {packets.data() },
|
||||
0,
|
||||
[&] (const BytestreamMidiView& roundTripped)
|
||||
{
|
||||
expect (equal (m, roundTripped.getMessage()));
|
||||
});
|
||||
translator.dispatch (View { packets.data() }, 0, [&] (const BytestreamMidiView& roundTripped)
|
||||
{
|
||||
expect (equal (m, roundTripped.getMessage()));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -146,13 +144,10 @@ public:
|
|||
Conversion::toMidi1 (ump::BytestreamMidiView (meta), [&] (const auto p) { packets.add (p); });
|
||||
|
||||
MidiBuffer output;
|
||||
converter.dispatch (packets.data(),
|
||||
packets.data() + packets.size(),
|
||||
0,
|
||||
[&] (const BytestreamMidiView& roundTripped)
|
||||
{
|
||||
output.addEvent (roundTripped.getMessage(), int (roundTripped.timestamp));
|
||||
});
|
||||
converter.dispatch (packets, 0, [&] (const BytestreamMidiView& roundTripped)
|
||||
{
|
||||
output.addEvent (roundTripped.getMessage(), int (roundTripped.timestamp));
|
||||
});
|
||||
packets.clear();
|
||||
|
||||
expect (equal (expected, output));
|
||||
|
|
@ -189,13 +184,10 @@ public:
|
|||
}
|
||||
|
||||
MidiBuffer output;
|
||||
converter.dispatch (modifiedPackets.data(),
|
||||
modifiedPackets.data() + modifiedPackets.size(),
|
||||
0,
|
||||
[&] (const BytestreamMidiView& roundTripped)
|
||||
{
|
||||
output.addEvent (roundTripped.getMessage(), int (roundTripped.timestamp));
|
||||
});
|
||||
converter.dispatch (modifiedPackets, 0, [&] (const BytestreamMidiView& roundTripped)
|
||||
{
|
||||
output.addEvent (roundTripped.getMessage(), int (roundTripped.timestamp));
|
||||
});
|
||||
|
||||
// All Utility messages should have been ignored
|
||||
expect (output.getNumEvents() == 1);
|
||||
|
|
@ -227,13 +219,10 @@ public:
|
|||
}
|
||||
|
||||
MidiBuffer output;
|
||||
converter.dispatch (modifiedPackets.data(),
|
||||
modifiedPackets.data() + modifiedPackets.size(),
|
||||
0,
|
||||
[&] (const BytestreamMidiView& roundTripped)
|
||||
{
|
||||
output.addEvent (roundTripped.getMessage(), int (roundTripped.timestamp));
|
||||
});
|
||||
converter.dispatch (modifiedPackets, 0, [&] (const BytestreamMidiView& roundTripped)
|
||||
{
|
||||
output.addEvent (roundTripped.getMessage(), int (roundTripped.timestamp));
|
||||
});
|
||||
|
||||
const auto numOutputs = output.getNumEvents();
|
||||
const auto numInputs = realtimeMessages.getNumEvents();
|
||||
|
|
@ -294,13 +283,10 @@ public:
|
|||
}
|
||||
|
||||
MidiBuffer output;
|
||||
converter.dispatch (modifiedPackets.data(),
|
||||
modifiedPackets.data() + modifiedPackets.size(),
|
||||
0,
|
||||
[&] (const BytestreamMidiView& roundTripped)
|
||||
{
|
||||
output.addEvent (roundTripped.getMessage(), int (roundTripped.timestamp));
|
||||
});
|
||||
converter.dispatch (modifiedPackets, 0, [&] (const BytestreamMidiView& roundTripped)
|
||||
{
|
||||
output.addEvent (roundTripped.getMessage(), int (roundTripped.timestamp));
|
||||
});
|
||||
|
||||
const auto numOutputs = output.getNumEvents();
|
||||
const auto numInputs = realtimeMessages.getNumEvents();
|
||||
|
|
@ -377,13 +363,10 @@ public:
|
|||
|
||||
const auto pushToOutput = [&] (const Packets& p)
|
||||
{
|
||||
converter.dispatch (p.data(),
|
||||
p.data() + p.size(),
|
||||
0,
|
||||
[&] (const BytestreamMidiView& roundTripped)
|
||||
{
|
||||
output.addEvent (roundTripped.getMessage(), int (roundTripped.timestamp));
|
||||
});
|
||||
converter.dispatch (p, 0, [&] (const BytestreamMidiView& roundTripped)
|
||||
{
|
||||
output.addEvent (roundTripped.getMessage(), int (roundTripped.timestamp));
|
||||
});
|
||||
};
|
||||
|
||||
pushToOutput (modifiedPackets);
|
||||
|
|
|
|||
|
|
@ -136,8 +136,8 @@ struct BytestreamToUMPHandler : public BytestreamInputHandler
|
|||
|
||||
void pushMidiData (const void* data, int bytes, double time) override
|
||||
{
|
||||
const auto* ptr = static_cast<const uint8_t*> (data);
|
||||
dispatcher.dispatch (ptr, ptr + bytes, time, [&] (const View& v)
|
||||
const auto* ptr = static_cast<const std::byte*> (data);
|
||||
dispatcher.dispatch (Span { ptr, (size_t) bytes }, time, [&] (const View& v)
|
||||
{
|
||||
recipient.packetReceived (v, time);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ struct U32InputHandler
|
|||
virtual ~U32InputHandler() noexcept = default;
|
||||
|
||||
virtual void reset() = 0;
|
||||
virtual void pushMidiData (const uint32_t* begin, const uint32_t* end, double time) = 0;
|
||||
virtual void pushMidiData (Span<const uint32_t>, double time) = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
@ -89,9 +89,9 @@ struct U32ToBytestreamHandler : public U32InputHandler
|
|||
|
||||
void reset() override { dispatcher.reset(); }
|
||||
|
||||
void pushMidiData (const uint32_t* begin, const uint32_t* end, double time) override
|
||||
void pushMidiData (Span<const uint32_t> words, double time) override
|
||||
{
|
||||
dispatcher.dispatch (begin, end, time, [this] (const BytestreamMidiView& m)
|
||||
dispatcher.dispatch (words, time, [this] (const BytestreamMidiView& m)
|
||||
{
|
||||
callback.handleIncomingMidiMessage (&input, m.getMessage());
|
||||
});
|
||||
|
|
@ -142,9 +142,9 @@ struct U32ToUMPHandler : public U32InputHandler
|
|||
converter.reset();
|
||||
}
|
||||
|
||||
void pushMidiData (const uint32_t* begin, const uint32_t* end, double time) override
|
||||
void pushMidiData (Span<const uint32_t> words, double time) override
|
||||
{
|
||||
dispatcher.dispatch (begin, end, time, [this] (const View& view, double thisTime)
|
||||
dispatcher.dispatch (words, time, [this] (const View& view, double thisTime)
|
||||
{
|
||||
converter.convert (view, [&] (const View& converted)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -641,8 +641,8 @@ namespace CoreMidiHelpers
|
|||
static_assert (sizeof (uint32_t) == sizeof (UInt32)
|
||||
&& alignof (uint32_t) == alignof (UInt32),
|
||||
"If this fails, the cast below will be broken too!");
|
||||
u32InputHandler->pushMidiData (reinterpret_cast<const uint32_t*> (packet->words),
|
||||
reinterpret_cast<const uint32_t*> (packet->words + packet->wordCount),
|
||||
u32InputHandler->pushMidiData ({ reinterpret_cast<const uint32_t*> (packet->words),
|
||||
(size_t) packet->wordCount },
|
||||
time);
|
||||
|
||||
packet = MIDIEventPacketNext (packet);
|
||||
|
|
|
|||
|
|
@ -1593,8 +1593,8 @@ public:
|
|||
|
||||
for (uint32_t i = 0; i < list->numPackets; ++i)
|
||||
{
|
||||
toBytestreamDispatcher.dispatch (reinterpret_cast<const uint32_t*> (packet->words),
|
||||
reinterpret_cast<const uint32_t*> (packet->words + packet->wordCount),
|
||||
toBytestreamDispatcher.dispatch ({ reinterpret_cast<const uint32_t*> (packet->words),
|
||||
(size_t) packet->wordCount },
|
||||
static_cast<double> (packet->timeStamp + inOffsetSampleFrame),
|
||||
[this] (const ump::BytestreamMidiView& message)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1486,8 +1486,8 @@ private:
|
|||
|
||||
for (uint32_t i = 0; i < list.numPackets; ++i)
|
||||
{
|
||||
converter.dispatch (reinterpret_cast<const uint32_t*> (packet->words),
|
||||
reinterpret_cast<const uint32_t*> (packet->words + packet->wordCount),
|
||||
converter.dispatch ({ reinterpret_cast<const uint32_t*> (packet->words),
|
||||
(size_t) packet->wordCount },
|
||||
static_cast<int> (packet->timeStamp - (MIDITimeStamp) startTime),
|
||||
[this] (const ump::BytestreamMidiView& message)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue