@@ -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