Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix stuck notes in native MIDI handling
Signed-off-by: falkTX <falktx@falktx.com>
  • Loading branch information
falkTX committed Aug 27, 2023
1 parent 1fc0c6b commit cb630fa
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
13 changes: 8 additions & 5 deletions distrho/src/jackbridge/NativeBridge.hpp
Expand Up @@ -73,6 +73,7 @@ struct NativeBridge {
#endif
#if DISTRHO_PLUGIN_WANT_MIDI_INPUT
static constexpr const uint32_t kMaxMIDIInputMessageSize = 3;
static constexpr const uint32_t kRingBufferMessageSize = 1u /*+ sizeof(double)*/ + kMaxMIDIInputMessageSize;
uint8_t midiDataStorage[kMaxMIDIInputMessageSize];
HeapRingBuffer midiInBufferCurrent;
HeapRingBuffer midiInBufferPending;
Expand Down Expand Up @@ -158,7 +159,7 @@ struct NativeBridge {
{
// NOTE: this function is only called once per run
midiInBufferCurrent.copyFromAndClearOther(midiInBufferPending);
return midiInBufferCurrent.getReadableDataSize() / (kMaxMIDIInputMessageSize + 1u);
return midiInBufferCurrent.getReadableDataSize() / kRingBufferMessageSize;
}
#endif

Expand All @@ -169,10 +170,12 @@ struct NativeBridge {
{
#if DISTRHO_PLUGIN_WANT_MIDI_INPUT
// NOTE: this function is called for all events in index succession
if (midiAvailable && midiInBufferCurrent.getReadableDataSize() >= (kMaxMIDIInputMessageSize + 1u))
if (midiAvailable && midiInBufferCurrent.getReadableDataSize() >= kRingBufferMessageSize)
{
event->time = 0; // TODO
event->size = midiInBufferCurrent.readByte();
event->size = midiInBufferCurrent.readByte();
// TODO timestamp
// const double timestamp = midiInBufferCurrent.readDouble();
event->time = 0;
event->buffer = midiDataStorage;
return midiInBufferCurrent.readCustomData(midiDataStorage, kMaxMIDIInputMessageSize);
}
Expand All @@ -186,7 +189,7 @@ struct NativeBridge {
{
#if DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
if (midiAvailable)
midiOutBuffer.clearData();
midiOutBuffer.flush();
#endif
}

Expand Down
7 changes: 4 additions & 3 deletions distrho/src/jackbridge/RtAudioBridge.hpp
Expand Up @@ -396,17 +396,18 @@ struct RtAudioBridge : NativeBridge {
}

#if defined(RTMIDI_API_TYPE) && DISTRHO_PLUGIN_WANT_MIDI_INPUT
static void RtMidiCallback(double /*timeStamp*/, std::vector<uchar>* message, void* userData)
static void RtMidiCallback(double /*timestamp*/, std::vector<uchar>* const message, void* const userData)
{
const size_t len = message->size();
DISTRHO_SAFE_ASSERT_RETURN(len > 0 && len <= kMaxMIDIInputMessageSize,);

RtAudioBridge* const self = static_cast<RtAudioBridge*>(userData);

// TODO timestamp handling
self->midiInBufferPending.writeByte(static_cast<uint8_t>(len));
// TODO timestamp
// self->midiInBufferPending.writeDouble(timestamp);
self->midiInBufferPending.writeCustomData(message->data(), len);
for (uint8_t i=0; i<len; ++i)
for (uint8_t i=len; i<kMaxMIDIInputMessageSize; ++i)
self->midiInBufferPending.writeByte(0);
self->midiInBufferPending.commitWrite();
}
Expand Down
9 changes: 6 additions & 3 deletions distrho/src/jackbridge/WebBridge.hpp
Expand Up @@ -506,15 +506,18 @@ struct WebBridge : NativeBridge {
}

#if DISTRHO_PLUGIN_WANT_MIDI_INPUT
static void WebMIDICallback(void* const userData, uint8_t* const data, const int len, const double timestamp)
static void WebMIDICallback(void* const userData, uint8_t* const data, const int len, double /*timestamp*/)
{
DISTRHO_SAFE_ASSERT_RETURN(len > 0 && len <= (int)kMaxMIDIInputMessageSize,);

WebBridge* const self = static_cast<WebBridge*>(userData);

// TODO timestamp handling
self->midiInBufferPending.writeByte(static_cast<uint8_t>(len));
self->midiInBufferPending.writeCustomData(data, kMaxMIDIInputMessageSize);
// TODO timestamp
// self->midiInBufferPending.writeDouble(timestamp);
self->midiInBufferPending.writeCustomData(data, len);
for (uint8_t i=len; i<kMaxMIDIInputMessageSize; ++i)
self->midiInBufferPending.writeByte(0);
self->midiInBufferPending.commitWrite();
}
#endif
Expand Down

0 comments on commit cb630fa

Please sign in to comment.