From 31e270d8e008addebfd752efbd4a1103f595dedf Mon Sep 17 00:00:00 2001 From: ben Date: Thu, 26 Oct 2017 13:44:09 +0200 Subject: [PATCH] Avoid creating an rtMidi instance if not necessary and make sure it's delete after --- src/core/MidiIO.cpp | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/core/MidiIO.cpp b/src/core/MidiIO.cpp index 8ca37b32..0b502047 100644 --- a/src/core/MidiIO.cpp +++ b/src/core/MidiIO.cpp @@ -49,15 +49,32 @@ void MidiIO::baseFromJson(json_t *rootJ) { } std::vector MidiIO::getDevices() { - /* Note: we could also use an existing interface if one exists */ - RtMidiIn m; - + MidiApi *m = NULL; std::vector names = {}; - for (unsigned int i = 0; i < m.getPortCount(); i++) { - names.push_back(m.getPortName(i)); + if (isPortOpen()) { + if (isOut) { + //m = (RtMidiApi *) midiInMap[this->deviceName]; + return names; + } else { + m = (MidiApi *) midiInMap[this->deviceName]; + } + } else { + try { + m = (MidiApi *) new RtMidiIn(); + } catch (RtMidiError &error) { + fprintf(stderr, "Failed to create RtMidiIn: %s\n", error.getMessage().c_str()); + return names; + } } + for (unsigned int i = 0; i < m->getPortCount(); i++) { + names.push_back(m->getPortName(i)); + } + + if (!isPortOpen()) + delete (m); + return names; } @@ -81,6 +98,13 @@ void MidiIO::openDevice(std::string deviceName) { break; } } + + if (!mw->isPortOpen()) { + fprintf(stderr, "Failed to create RtMidiIn: No such device %s\n", deviceName.c_str()); + this->deviceName = ""; + this->id = -1; + return; + } } catch (RtMidiError &error) { fprintf(stderr, "Failed to create RtMidiIn: %s\n", error.getMessage().c_str()); @@ -238,4 +262,4 @@ void ChannelChoice::onAction() { void ChannelChoice::step() { text = (midiModule->channel >= 0) ? stringf("%d", midiModule->channel + 1) : "All"; -} \ No newline at end of file +}