Rework MIDI handling, remove output queue
Signed-off-by: falkTX <falktx@falktx.com>
This commit is contained in:
parent
a560b3726f
commit
95c1d3ccfb
3 changed files with 159 additions and 103 deletions
|
@ -311,8 +311,8 @@ class CardinalPlugin : public CardinalBasePlugin
|
|||
// for base/context handling
|
||||
bool fIsActive;
|
||||
CardinalAudioDevice* fCurrentAudioDevice;
|
||||
CardinalMidiInputDevice* fCurrentMidiInput;
|
||||
CardinalMidiOutputDevice* fCurrentMidiOutput;
|
||||
CardinalMidiInputDevice** fCurrentMidiInputs;
|
||||
CardinalMidiOutputDevice** fCurrentMidiOutputs;
|
||||
uint64_t fPreviousFrame;
|
||||
Mutex fDeviceMutex;
|
||||
|
||||
|
@ -327,8 +327,8 @@ public:
|
|||
fAudioBufferOut(nullptr),
|
||||
fIsActive(false),
|
||||
fCurrentAudioDevice(nullptr),
|
||||
fCurrentMidiInput(nullptr),
|
||||
fCurrentMidiOutput(nullptr),
|
||||
fCurrentMidiInputs(nullptr),
|
||||
fCurrentMidiOutputs(nullptr),
|
||||
fPreviousFrame(0)
|
||||
{
|
||||
fWindowParameters[kWindowParameterShowTooltips] = 1.0f;
|
||||
|
@ -397,19 +397,21 @@ public:
|
|||
fInitializer->oscPlugin = nullptr;
|
||||
#endif
|
||||
|
||||
{
|
||||
const MutexLocker cml(fDeviceMutex);
|
||||
fCurrentAudioDevice = nullptr;
|
||||
fCurrentMidiInput = nullptr;
|
||||
fCurrentMidiOutput = nullptr;
|
||||
}
|
||||
|
||||
{
|
||||
const ScopedContext sc(this);
|
||||
context->patch->clear();
|
||||
delete context;
|
||||
}
|
||||
|
||||
{
|
||||
const MutexLocker cml(fDeviceMutex);
|
||||
fCurrentAudioDevice = nullptr;
|
||||
delete[] fCurrentMidiInputs;
|
||||
fCurrentMidiInputs = nullptr;
|
||||
delete[] fCurrentMidiOutputs;
|
||||
fCurrentMidiOutputs = nullptr;
|
||||
}
|
||||
|
||||
if (! fAutosavePath.empty())
|
||||
rack::system::removeRecursively(fAutosavePath);
|
||||
}
|
||||
|
@ -434,18 +436,6 @@ protected:
|
|||
return fCurrentAudioDevice == nullptr;
|
||||
}
|
||||
|
||||
bool canAssignMidiInputDevice() const noexcept override
|
||||
{
|
||||
const MutexLocker cml(fDeviceMutex);
|
||||
return fCurrentMidiInput == nullptr;
|
||||
}
|
||||
|
||||
bool canAssignMidiOutputDevice() const noexcept override
|
||||
{
|
||||
const MutexLocker cml(fDeviceMutex);
|
||||
return fCurrentMidiOutput == nullptr;
|
||||
}
|
||||
|
||||
void assignAudioDevice(CardinalAudioDevice* const dev) noexcept override
|
||||
{
|
||||
DISTRHO_SAFE_ASSERT_RETURN(fCurrentAudioDevice == nullptr,);
|
||||
|
@ -454,22 +444,6 @@ protected:
|
|||
fCurrentAudioDevice = dev;
|
||||
}
|
||||
|
||||
void assignMidiInputDevice(CardinalMidiInputDevice* const dev) noexcept override
|
||||
{
|
||||
DISTRHO_SAFE_ASSERT_RETURN(fCurrentMidiInput == nullptr,);
|
||||
|
||||
const MutexLocker cml(fDeviceMutex);
|
||||
fCurrentMidiInput = dev;
|
||||
}
|
||||
|
||||
void assignMidiOutputDevice(CardinalMidiOutputDevice* const dev) noexcept override
|
||||
{
|
||||
DISTRHO_SAFE_ASSERT_RETURN(fCurrentMidiOutput == nullptr,);
|
||||
|
||||
const MutexLocker cml(fDeviceMutex);
|
||||
fCurrentMidiOutput = dev;
|
||||
}
|
||||
|
||||
bool clearAudioDevice(CardinalAudioDevice* const dev) noexcept override
|
||||
{
|
||||
const MutexLocker cml(fDeviceMutex);
|
||||
|
@ -481,26 +455,96 @@ protected:
|
|||
return true;
|
||||
}
|
||||
|
||||
bool clearMidiInputDevice(CardinalMidiInputDevice* const dev) noexcept override
|
||||
void assignMidiInputDevice(CardinalMidiInputDevice* const dev) noexcept override
|
||||
{
|
||||
const MutexLocker cml(fDeviceMutex);
|
||||
CardinalMidiInputDevice** const oldDevs = fCurrentMidiInputs;
|
||||
|
||||
if (fCurrentMidiInput != dev)
|
||||
return false;
|
||||
uint numDevs = 0;
|
||||
if (oldDevs != nullptr)
|
||||
{
|
||||
while (oldDevs[numDevs] != nullptr)
|
||||
++numDevs;
|
||||
}
|
||||
|
||||
fCurrentMidiInput = nullptr;
|
||||
return true;
|
||||
CardinalMidiInputDevice** const newDevs = new CardinalMidiInputDevice*[numDevs + 2];
|
||||
|
||||
for (uint i=0; i<numDevs; ++i)
|
||||
newDevs[i] = oldDevs[i];
|
||||
|
||||
newDevs[numDevs+0] = dev;
|
||||
newDevs[numDevs+1] = nullptr;
|
||||
|
||||
{
|
||||
const MutexLocker cml(fDeviceMutex);
|
||||
fCurrentMidiInputs = newDevs;
|
||||
}
|
||||
|
||||
delete[] oldDevs;
|
||||
}
|
||||
|
||||
bool clearMidiOutputDevice(CardinalMidiOutputDevice* const dev) noexcept override
|
||||
void assignMidiOutputDevice(CardinalMidiOutputDevice* const dev) noexcept override
|
||||
{
|
||||
CardinalMidiOutputDevice** const oldDevs = fCurrentMidiOutputs;
|
||||
|
||||
uint numDevs = 0;
|
||||
if (oldDevs != nullptr)
|
||||
{
|
||||
while (oldDevs[numDevs] != nullptr)
|
||||
++numDevs;
|
||||
}
|
||||
|
||||
CardinalMidiOutputDevice** const newDevs = new CardinalMidiOutputDevice*[numDevs + 2];
|
||||
|
||||
for (uint i=0; i<numDevs; ++i)
|
||||
newDevs[i] = oldDevs[i];
|
||||
|
||||
newDevs[numDevs+0] = dev;
|
||||
newDevs[numDevs+1] = nullptr;
|
||||
|
||||
{
|
||||
const MutexLocker cml(fDeviceMutex);
|
||||
fCurrentMidiOutputs = newDevs;
|
||||
}
|
||||
|
||||
delete[] oldDevs;
|
||||
}
|
||||
|
||||
void clearMidiInputDevice(CardinalMidiInputDevice* const dev) noexcept override
|
||||
{
|
||||
const MutexLocker cml(fDeviceMutex);
|
||||
|
||||
if (fCurrentMidiOutput != dev)
|
||||
return false;
|
||||
CardinalMidiInputDevice** const inputs = fCurrentMidiInputs;
|
||||
DISTRHO_SAFE_ASSERT_RETURN(inputs != nullptr,);
|
||||
|
||||
fCurrentMidiOutput = nullptr;
|
||||
return true;
|
||||
for (uint i=0; inputs[i] != nullptr; ++i)
|
||||
{
|
||||
CardinalMidiInputDevice* const input = inputs[i];
|
||||
if (input != dev)
|
||||
continue;
|
||||
for (; inputs[i+1] != nullptr; ++i)
|
||||
inputs[i] = inputs[i+1];
|
||||
inputs[i] = nullptr;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void clearMidiOutputDevice(CardinalMidiOutputDevice* const dev) noexcept override
|
||||
{
|
||||
const MutexLocker cml(fDeviceMutex);
|
||||
|
||||
CardinalMidiOutputDevice** const outputs = fCurrentMidiOutputs;
|
||||
DISTRHO_SAFE_ASSERT_RETURN(outputs != nullptr,);
|
||||
|
||||
for (uint i=0; outputs[i] != nullptr; ++i)
|
||||
{
|
||||
CardinalMidiOutputDevice* const output = outputs[i];
|
||||
if (output != dev)
|
||||
continue;
|
||||
for (; outputs[i+1] != nullptr; ++i)
|
||||
outputs[i] = outputs[i+1];
|
||||
outputs[i] = nullptr;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------------------
|
||||
|
@ -858,8 +902,11 @@ protected:
|
|||
fPreviousFrame = timePos.frame;
|
||||
}
|
||||
|
||||
if (fCurrentMidiInput != nullptr)
|
||||
fCurrentMidiInput->handleMessagesFromHost(midiEvents, midiEventCount);
|
||||
if (CardinalMidiInputDevice** inputs = fCurrentMidiInputs)
|
||||
{
|
||||
for (;*inputs != nullptr; ++inputs)
|
||||
(*inputs)->handleMessagesFromHost(midiEvents, midiEventCount);
|
||||
}
|
||||
|
||||
if (fCurrentAudioDevice != nullptr)
|
||||
{
|
||||
|
@ -900,9 +947,6 @@ protected:
|
|||
std::memset(outputs[0], 0, sizeof(float)*frames);
|
||||
std::memset(outputs[1], 0, sizeof(float)*frames);
|
||||
}
|
||||
|
||||
if (fCurrentMidiOutput != nullptr)
|
||||
fCurrentMidiOutput->processMessages();
|
||||
}
|
||||
|
||||
void bufferSizeChanged(const uint32_t newBufferSize) override
|
||||
|
|
|
@ -117,14 +117,12 @@ public:
|
|||
~CardinalBasePlugin() override {}
|
||||
virtual bool isActive() const noexcept = 0;
|
||||
virtual bool canAssignAudioDevice() const noexcept = 0;
|
||||
virtual bool canAssignMidiInputDevice() const noexcept = 0;
|
||||
virtual bool canAssignMidiOutputDevice() const noexcept = 0;
|
||||
virtual bool clearAudioDevice(CardinalAudioDevice* dev) noexcept = 0;
|
||||
virtual void assignAudioDevice(CardinalAudioDevice* dev) noexcept = 0;
|
||||
virtual void assignMidiInputDevice(CardinalMidiInputDevice* dev) noexcept = 0;
|
||||
virtual void assignMidiOutputDevice(CardinalMidiOutputDevice* dev) noexcept = 0;
|
||||
virtual bool clearAudioDevice(CardinalAudioDevice* dev) noexcept = 0;
|
||||
virtual bool clearMidiInputDevice(CardinalMidiInputDevice* dev) noexcept = 0;
|
||||
virtual bool clearMidiOutputDevice(CardinalMidiOutputDevice* dev) noexcept = 0;
|
||||
virtual void clearMidiInputDevice(CardinalMidiInputDevice* dev) noexcept = 0;
|
||||
virtual void clearMidiOutputDevice(CardinalMidiOutputDevice* dev) noexcept = 0;
|
||||
};
|
||||
|
||||
#ifndef HEADLESS
|
||||
|
|
|
@ -211,44 +211,70 @@ struct CardinalMidiInputDevice : rack::midi::InputDevice
|
|||
struct CardinalMidiOutputDevice : rack::midi::OutputDevice
|
||||
{
|
||||
CardinalBasePlugin* const fPlugin;
|
||||
MidiEvent fQueue[128];
|
||||
Mutex fQueueMutex;
|
||||
uint8_t fQueueIndex;
|
||||
|
||||
CardinalMidiOutputDevice(CardinalBasePlugin* const plugin)
|
||||
: fPlugin(plugin),
|
||||
fQueueIndex(0) {}
|
||||
: fPlugin(plugin) {}
|
||||
|
||||
std::string getName() override
|
||||
{
|
||||
return "Cardinal";
|
||||
}
|
||||
|
||||
void processMessages()
|
||||
{
|
||||
const MutexLocker cml(fQueueMutex);
|
||||
|
||||
for (uint8_t i=0; i<fQueueIndex; ++i)
|
||||
fPlugin->writeMidiEvent(fQueue[i]);
|
||||
|
||||
fQueueIndex = 0;
|
||||
}
|
||||
|
||||
void sendMessage(const rack::midi::Message& message) override
|
||||
{
|
||||
if (message.bytes.size() < 3) // FIXME
|
||||
return;
|
||||
if ((message.bytes[0] & 0xf0) == 0xf0)
|
||||
return;
|
||||
if (fQueueIndex == 128)
|
||||
return;
|
||||
const size_t size = message.bytes.size();
|
||||
DISTRHO_SAFE_ASSERT_RETURN(size > 0,);
|
||||
|
||||
const MutexLocker cml(fQueueMutex);
|
||||
|
||||
MidiEvent& event(fQueue[fQueueIndex++]);
|
||||
MidiEvent event;
|
||||
event.frame = message.frame < 0 ? 0 : (message.frame - fPlugin->context->engine->getBlockFrame());
|
||||
event.size = 3; // FIXME
|
||||
|
||||
switch (message.bytes[0] & 0xF0)
|
||||
{
|
||||
case 0x80:
|
||||
case 0x90:
|
||||
case 0xA0:
|
||||
case 0xB0:
|
||||
case 0xE0:
|
||||
event.size = 3;
|
||||
break;
|
||||
case 0xC0:
|
||||
case 0xD0:
|
||||
event.size = 2;
|
||||
break;
|
||||
case 0xF0:
|
||||
switch (message.bytes[0] & 0x0F)
|
||||
{
|
||||
case 0x0:
|
||||
case 0x4:
|
||||
case 0x5:
|
||||
case 0x7:
|
||||
case 0x9:
|
||||
case 0xD:
|
||||
// unsupported
|
||||
return;
|
||||
case 0x1:
|
||||
case 0x2:
|
||||
case 0x3:
|
||||
case 0xE:
|
||||
event.size = 3;
|
||||
break;
|
||||
case 0x6:
|
||||
case 0x8:
|
||||
case 0xA:
|
||||
case 0xB:
|
||||
case 0xC:
|
||||
case 0xF:
|
||||
event.size = 1;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
DISTRHO_SAFE_ASSERT_RETURN(size >= event.size,);
|
||||
|
||||
std::memcpy(event.data, message.bytes.data(), event.size);
|
||||
|
||||
fPlugin->writeMidiEvent(event);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -301,12 +327,8 @@ struct CardinalMidiDriver : rack::midi::Driver
|
|||
CardinalBasePlugin* const plugin = reinterpret_cast<CardinalBasePlugin*>(pluginContext->plugin);
|
||||
DISTRHO_SAFE_ASSERT_RETURN(plugin != nullptr, nullptr);
|
||||
|
||||
if (! plugin->canAssignMidiInputDevice())
|
||||
throw rack::Exception("Plugin driver only allows one midi input device to be used simultaneously");
|
||||
|
||||
CardinalMidiInputDevice* const device = new CardinalMidiInputDevice(plugin);
|
||||
device->subscribe(input);
|
||||
|
||||
plugin->assignMidiInputDevice(device);
|
||||
return device;
|
||||
}
|
||||
|
@ -319,12 +341,8 @@ struct CardinalMidiDriver : rack::midi::Driver
|
|||
CardinalBasePlugin* const plugin = reinterpret_cast<CardinalBasePlugin*>(pluginContext->plugin);
|
||||
DISTRHO_SAFE_ASSERT_RETURN(plugin != nullptr, nullptr);
|
||||
|
||||
if (! plugin->canAssignMidiOutputDevice())
|
||||
throw rack::Exception("Plugin driver only allows one midi output device to be used simultaneously");
|
||||
|
||||
CardinalMidiOutputDevice* const device = new CardinalMidiOutputDevice(plugin);
|
||||
device->subscribe(output);
|
||||
|
||||
plugin->assignMidiOutputDevice(device);
|
||||
return device;
|
||||
}
|
||||
|
@ -340,11 +358,9 @@ struct CardinalMidiDriver : rack::midi::Driver
|
|||
CardinalBasePlugin* const plugin = reinterpret_cast<CardinalBasePlugin*>(pluginContext->plugin);
|
||||
DISTRHO_SAFE_ASSERT_RETURN(plugin != nullptr,);
|
||||
|
||||
if (plugin->clearMidiInputDevice(device))
|
||||
{
|
||||
device->unsubscribe(input);
|
||||
delete device;
|
||||
}
|
||||
plugin->clearMidiInputDevice(device);
|
||||
device->unsubscribe(input);
|
||||
delete device;
|
||||
}
|
||||
|
||||
void unsubscribeOutput(int, rack::midi::Output* const output) override
|
||||
|
@ -358,11 +374,9 @@ struct CardinalMidiDriver : rack::midi::Driver
|
|||
CardinalBasePlugin* const plugin = reinterpret_cast<CardinalBasePlugin*>(pluginContext->plugin);
|
||||
DISTRHO_SAFE_ASSERT_RETURN(plugin != nullptr,);
|
||||
|
||||
if (plugin->clearMidiOutputDevice(device))
|
||||
{
|
||||
device->unsubscribe(output);
|
||||
delete device;
|
||||
}
|
||||
plugin->clearMidiOutputDevice(device);
|
||||
device->unsubscribe(output);
|
||||
delete device;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue