commit 56cbf3b3ca3ab5ec7677d30978cf796a41302789
parent 1241390730867aaff6e0b748fc18a323513a966c
Author: falkTX <falktx@falktx.com>
Date: Wed, 13 Jul 2022 12:12:36 +0100
Fix MIDI handling in standalone native bridges without real MIDI
Diffstat:
2 files changed, 41 insertions(+), 30 deletions(-)
diff --git a/distrho/src/jackbridge/NativeBridge.hpp b/distrho/src/jackbridge/NativeBridge.hpp
@@ -53,6 +53,9 @@ struct NativeBridge {
float* audioBuffers[DISTRHO_PLUGIN_NUM_INPUTS + DISTRHO_PLUGIN_NUM_OUTPUTS];
float* audioBufferStorage;
#endif
+#if DISTRHO_PLUGIN_WANT_MIDI_INPUT || DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
+ bool midiAvailable;
+#endif
#if DISTRHO_PLUGIN_WANT_MIDI_INPUT
static constexpr const uint32_t kMaxMIDIInputMessageSize = 3;
uint8_t midiDataStorage[kMaxMIDIInputMessageSize];
@@ -78,6 +81,9 @@ struct NativeBridge {
, audioBuffers()
, audioBufferStorage(nullptr)
#endif
+ #if DISTRHO_PLUGIN_WANT_MIDI_INPUT || DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
+ , midiAvailable(false)
+ #endif
{
#if DISTRHO_PLUGIN_NUM_INPUTS+DISTRHO_PLUGIN_NUM_OUTPUTS > 0
std::memset(audioBuffers, 0, sizeof(audioBuffers));
@@ -109,7 +115,6 @@ struct NativeBridge {
}
virtual bool supportsBufferSizeChanges() const { return false; }
- virtual bool supportsMIDI() const { return false; }
virtual bool isMIDIEnabled() const { return false; }
virtual bool requestAudioInput() { return false; }
virtual bool requestBufferSizeChange(uint32_t) { return false; }
@@ -120,22 +125,34 @@ struct NativeBridge {
return bufferSize;
}
+ bool supportsMIDI() const noexcept
+ {
+ #if DISTRHO_PLUGIN_WANT_MIDI_INPUT || DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
+ return midiAvailable;
+ #else
+ return false;
+ #endif
+ }
+
uint32_t getEventCount()
{
#if DISTRHO_PLUGIN_WANT_MIDI_INPUT
- // NOTE: this function is only called once per run
- midiInBufferCurrent.copyFromAndClearOther(midiInBufferPending);
- return midiInBufferCurrent.getReadableDataSize() / (kMaxMIDIInputMessageSize + 1u);
- #else
- return 0;
+ if (midiAvailable)
+ {
+ // NOTE: this function is only called once per run
+ midiInBufferCurrent.copyFromAndClearOther(midiInBufferPending);
+ return midiInBufferCurrent.getReadableDataSize() / (kMaxMIDIInputMessageSize + 1u);
+ }
#endif
+
+ return 0;
}
bool getEvent(jack_midi_event_t* const event)
{
#if DISTRHO_PLUGIN_WANT_MIDI_INPUT
// NOTE: this function is called for all events in index succession
- if (midiInBufferCurrent.getReadableDataSize() >= (kMaxMIDIInputMessageSize + 1u))
+ if (midiAvailable && midiInBufferCurrent.getReadableDataSize() >= (kMaxMIDIInputMessageSize + 1u))
{
event->time = 0; // TODO
event->size = midiInBufferCurrent.readByte();
@@ -151,7 +168,8 @@ struct NativeBridge {
void clearEventBuffer()
{
#if DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
- midiOutBuffer.clearData();
+ if (midiAvailable)
+ midiOutBuffer.clearData();
#endif
}
@@ -159,23 +177,28 @@ struct NativeBridge {
{
if (size > 3)
return false;
+
#if DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
- if (midiOutBuffer.writeByte(size) && midiOutBuffer.writeCustomData(data, size))
+ if (midiAvailable)
{
- bool fail = false;
- // align
- switch (size)
+ if (midiOutBuffer.writeByte(size) && midiOutBuffer.writeCustomData(data, size))
{
- case 1: fail |= !midiOutBuffer.writeByte(0);
- // fall-through
- case 2: fail |= !midiOutBuffer.writeByte(0);
+ bool fail = false;
+ // align
+ switch (size)
+ {
+ case 1: fail |= !midiOutBuffer.writeByte(0);
+ // fall-through
+ case 2: fail |= !midiOutBuffer.writeByte(0);
+ }
+ fail |= !midiOutBuffer.writeUInt(time);
+ midiOutBuffer.commitWrite();
+ return !fail;
}
- fail |= !midiOutBuffer.writeUInt(time);
midiOutBuffer.commitWrite();
- return !fail;
}
- midiOutBuffer.commitWrite();
#endif
+
return false;
// maybe unused
(void)data;
diff --git a/distrho/src/jackbridge/WebBridge.hpp b/distrho/src/jackbridge/WebBridge.hpp
@@ -28,9 +28,6 @@ struct WebBridge : NativeBridge {
#if DISTRHO_PLUGIN_NUM_OUTPUTS > 0
bool playbackAvailable = false;
#endif
-#if DISTRHO_PLUGIN_WANT_MIDI_INPUT || DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
- bool midiAvailable = false;
-#endif
bool active = false;
double timestamp = 0;
@@ -330,15 +327,6 @@ struct WebBridge : NativeBridge {
return true;
}
- bool supportsMIDI() const override
- {
- #if DISTRHO_PLUGIN_WANT_MIDI_INPUT || DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
- return midiAvailable;
- #else
- return false;
- #endif
- }
-
bool isMIDIEnabled() const override
{
#if DISTRHO_PLUGIN_WANT_MIDI_INPUT || DISTRHO_PLUGIN_WANT_MIDI_OUTPUT