diff --git a/src/core/MidiIO.cpp b/src/core/MidiIO.cpp index 8ca37b32..a9dc41ce 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; - std::vector names = {}; - for (unsigned int i = 0; i < m.getPortCount(); i++) { - names.push_back(m.getPortName(i)); + if (isOut) { + // TODO + return names; + } + + RtMidiIn *m = NULL; + if (midiInMap.empty()){ + try { + m = new RtMidiIn(); + } catch (RtMidiError &error) { + fprintf(stderr, "Failed to create RtMidiIn: %s\n", error.getMessage().c_str()); + return names; + } + } else { + m = midiInMap.begin()->second; } + 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 +}