Skip to content

Commit

Permalink
Fix segfault on closing rack (and variable naming)
Browse files Browse the repository at this point in the history
  • Loading branch information
bontric committed Oct 15, 2017
1 parent 19e0e45 commit 87da77b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
56 changes: 34 additions & 22 deletions src/core/MidiInterface.cpp
Expand Up @@ -52,28 +52,33 @@ void MidiIO::baseFromJson(json_t *rootJ) {

std::vector<std::string> MidiIO::getDevices() {
/* Note: we could also use an existing interface if one exists */
static RtMidiIn *t = new RtMidiIn(RtMidi::UNSPECIFIED, "Rack");
static RtMidiIn * m = new RtMidiIn();

std::vector<std::string> names = {};

for (int i = 0; i < t->getPortCount(); i++) {
names.push_back(t->getPortName(i));
for (int i = 0; i < m->getPortCount(); i++) {
names.push_back(m->getPortName(i));
}

return names;
}

void MidiIO::openDevice(std::string deviceName) {
MidiInWrapper *mw = midiInMap[deviceName];

if (!midiInMap[deviceName]) {
if (id > 0 || deviceName != "") {
close();
}

if (!mw) {
try {
MidiInWrapper *t = new MidiInWrapper();
midiInMap[deviceName] = t;
mw = new MidiInWrapper();
midiInMap[deviceName] = mw;


for (int i = 0; i < t->getPortCount(); i++) {
if (deviceName == t->getPortName(i)) {
t->openPort(i);
for (int i = 0; i < mw->getPortCount(); i++) {
if (deviceName == mw->getPortName(i)) {
mw->openPort(i);
break;
}
}
Expand All @@ -98,44 +103,51 @@ std::string MidiIO::getDeviceName() {
double MidiIO::getMessage(std::vector<unsigned char> *msg) {
std::vector<unsigned char> next_msg;

MidiInWrapper *m = midiInMap[deviceName];
MidiInWrapper *mw = midiInMap[deviceName];

if (!m) {
if (!mw) {
fprintf(stderr, "Device not opened!: %s\n", deviceName.c_str());
return 0;
}

double stamp = midiInMap[deviceName]->getMessage(&next_msg);

if (next_msg.size() > 0) {
for (auto kv : m->idMessagesMap) {
m->idMessagesMap[kv.first].push_back(next_msg);
m->idStampsMap[kv.first].push_back(stamp);
for (auto kv : mw->idMessagesMap) {
mw->idMessagesMap[kv.first].push_back(next_msg);
mw->idStampsMap[kv.first].push_back(stamp);
}
}

if (m->idMessagesMap[id].size() <= 0) {
if (mw->idMessagesMap[id].size() <= 0) {
*msg = next_msg;
return stamp;
}

*msg = m->idMessagesMap[id].front();
stamp = m->idStampsMap[id].front();
m->idMessagesMap[id].pop_front();
*msg = mw->idMessagesMap[id].front();
stamp = mw->idStampsMap[id].front();
mw->idMessagesMap[id].pop_front();
return stamp;
}

bool MidiIO::isPortOpen() {
return midiInMap[deviceName] != NULL;
return id > 0;
}

void MidiIO::close() {
midiInMap[deviceName]->erase(id);
MidiInWrapper * mw = midiInMap[deviceName];
if (!mw){
return;
}

mw->erase(id);

if (midiInMap[deviceName]->subscribers == 0) {
midiInMap[deviceName]->closePort();
if (mw->subscribers == 0) {
mw->closePort();
midiInMap.erase(deviceName);
}

id = -1;
}


Expand Down
4 changes: 2 additions & 2 deletions src/core/MidiInterface.hpp
Expand Up @@ -69,12 +69,12 @@ struct MidiIO {

double getMessage(std::vector<unsigned char> *msg);

bool isPortOpen();

json_t *addBaseJson(json_t *rootJ);

void baseFromJson(json_t *rootJ);

bool isPortOpen();

void close();

/* called when midi port is set */
Expand Down

0 comments on commit 87da77b

Please sign in to comment.