Skip to content

Commit d2d9d11

Browse files
committed
Fix issue with duplicated MIDI output
1 parent 00b31be commit d2d9d11

3 files changed

Lines changed: 22 additions & 11 deletions

File tree

Source/PluginEditor.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,6 @@ PluginEditor::PluginEditor(PluginProcessor& p)
310310

311311
connectionMessageDisplay = std::make_unique<ConnectionMessageDisplay>(this);
312312

313-
// This cannot be done in MidiDeviceManager's constructor because SettingsFile is not yet initialised at that time
314-
pd->getMidiDeviceManager().loadMidiSettings();
315-
316313
ObjectThemeManager::get()->updateTheme(pd);
317314

318315
addChildComponent(nvgSurface);

Source/PluginProcessor.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ PluginProcessor::PluginProcessor()
189189
setLatencySamples(pd::Instance::getBlockSize());
190190
settingsFile->startChangeListener();
191191

192+
midiDeviceManager.loadMidiSettings();
193+
192194
sendMessagesFromQueue();
193195
startDSP();
194196
}

Source/Utility/MidiDeviceManager.h

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,16 @@ class MidiDeviceManager final : public ChangeListener
157157
if (isInput) {
158158
auto* device = moveMidiDevice<MidiInput>(inputPorts, identifier, port + 1);
159159
if (!device && shouldBeEnabled) {
160-
if (auto midiIn = MidiInput::openDevice(identifier, this)) {
161-
auto* input = inputPorts[port + 1].devices.add(midiIn.release());
162-
input->start();
163-
inputPorts[port + 1].enabled = true;
160+
auto& targetPort = inputPorts[port + 1];
161+
bool alreadyExists = std::any_of(targetPort.devices.begin(), targetPort.devices.end(),
162+
[&](MidiInput const* d) { return d && d->getIdentifier() == identifier; });
163+
164+
if (!alreadyExists) {
165+
if (auto midiIn = MidiInput::openDevice(identifier, this)) {
166+
auto* input = targetPort.devices.add(midiIn.release());
167+
input->start();
168+
targetPort.enabled = true;
169+
}
164170
}
165171
} else if (device && shouldBeEnabled) {
166172
device->start();
@@ -171,10 +177,16 @@ class MidiDeviceManager final : public ChangeListener
171177
auto* device = moveMidiDevice<MidiOutput>(outputPorts, identifier, port + 1);
172178

173179
if (!device && shouldBeEnabled) {
174-
if (auto midiOut = MidiOutput::openDevice(identifier)) {
175-
auto* output = outputPorts[port + 1].devices.add(midiOut.release());
176-
output->startBackgroundThread();
177-
outputPorts[port + 1].enabled = true;
180+
auto& targetPort = outputPorts[port + 1];
181+
bool alreadyExists = std::any_of(targetPort.devices.begin(), targetPort.devices.end(),
182+
[&](MidiOutput const* d) { return d && d->getIdentifier() == identifier; });
183+
184+
if (!alreadyExists) {
185+
if (auto midiOut = MidiOutput::openDevice(identifier)) {
186+
auto* output = targetPort.devices.add(midiOut.release());
187+
output->startBackgroundThread();
188+
targetPort.enabled = true;
189+
}
178190
}
179191
} else if (device && shouldBeEnabled) {
180192
device->startBackgroundThread();

0 commit comments

Comments
 (0)