Browse Source

Avoid creating an rtMidi instance if not necessary and make sure it's

delete after
tags/v0.5.0
ben 7 years ago
parent
commit
31e270d8e0
1 changed files with 30 additions and 6 deletions
  1. +30
    -6
      src/core/MidiIO.cpp

+ 30
- 6
src/core/MidiIO.cpp View File

@@ -49,15 +49,32 @@ void MidiIO::baseFromJson(json_t *rootJ) {
}

std::vector<std::string> MidiIO::getDevices() {
/* Note: we could also use an existing interface if one exists */
RtMidiIn m;

MidiApi *m = NULL;
std::vector<std::string> 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";
}
}

Loading…
Cancel
Save