@@ -119,7 +119,7 @@ std::string MidiIO::getDeviceName() { | |||||
} | } | ||||
double MidiIO::getMessage(std::vector<unsigned char> *msg) { | double MidiIO::getMessage(std::vector<unsigned char> *msg) { | ||||
std::vector<unsigned char> next_msg; | |||||
MidiMessage next_msg = MidiMessage(); | |||||
MidiInWrapper *mw = midiInMap[deviceName]; | MidiInWrapper *mw = midiInMap[deviceName]; | ||||
@@ -128,24 +128,23 @@ double MidiIO::getMessage(std::vector<unsigned char> *msg) { | |||||
return 0; | return 0; | ||||
} | } | ||||
double stamp = midiInMap[deviceName]->getMessage(&next_msg); | |||||
next_msg.timeStamp = midiInMap[deviceName]->getMessage(&next_msg.bytes); | |||||
if (next_msg.bytes.size() > 0) { | |||||
for (auto &kv : mw->idMessagesMap) { | |||||
if (next_msg.size() > 0) { | |||||
for (auto kv : mw->idMessagesMap) { | |||||
mw->idMessagesMap[kv.first].push_back(next_msg); | |||||
mw->idStampsMap[kv.first].push_back(stamp); | |||||
kv.second.push_back(next_msg); | |||||
} | } | ||||
} | } | ||||
if (mw->idMessagesMap[id].size() <= 0) { | |||||
*msg = next_msg; | |||||
return stamp; | |||||
if (mw->idMessagesMap[id].size() > 0) { | |||||
next_msg = mw->idMessagesMap[id].front(); | |||||
mw->idMessagesMap[id].pop_front(); | |||||
} | } | ||||
*msg = mw->idMessagesMap[id].front(); | |||||
stamp = mw->idStampsMap[id].front(); | |||||
mw->idMessagesMap[id].pop_front(); | |||||
return stamp; | |||||
*msg = next_msg.bytes; | |||||
return next_msg.timeStamp; | |||||
} | } | ||||
bool MidiIO::isPortOpen() { | bool MidiIO::isPortOpen() { | ||||
@@ -165,7 +164,7 @@ void MidiIO::close() { | |||||
mw->erase(id); | mw->erase(id); | ||||
if (mw->subscribers == 0) { | |||||
if (mw->idMessagesMap.size() == 0) { | |||||
mw->closePort(); | mw->closePort(); | ||||
midiInMap.erase(deviceName); | midiInMap.erase(deviceName); | ||||
} | } | ||||
@@ -11,37 +11,39 @@ struct IgnoreFlags { | |||||
bool midiSense = true; | bool midiSense = true; | ||||
}; | }; | ||||
struct MidiMessage { | |||||
std::vector<unsigned char> bytes; | |||||
double timeStamp; | |||||
MidiMessage(): bytes(0), timeStamp(0.0) {}; | |||||
}; | |||||
/** | /** | ||||
* This class allows to use one instance of rtMidiIn with | * This class allows to use one instance of rtMidiIn with | ||||
* multiple modules. A MidiIn port will be opened only once while multiple | * multiple modules. A MidiIn port will be opened only once while multiple | ||||
* instances can use it simultaniously, each receiving all its incoming messages. | * instances can use it simultaniously, each receiving all its incoming messages. | ||||
*/ | */ | ||||
struct MidiInWrapper : RtMidiIn { | struct MidiInWrapper : RtMidiIn { | ||||
std::unordered_map<int, std::list<MidiMessage>> idMessagesMap; | |||||
std::unordered_map<int, IgnoreFlags> ignoresMap; | std::unordered_map<int, IgnoreFlags> ignoresMap; | ||||
int uid_c = 0; | int uid_c = 0; | ||||
int subscribers = 0; | |||||
MidiInWrapper() : RtMidiIn() { | MidiInWrapper() : RtMidiIn() { | ||||
idMessagesMap = {}; | idMessagesMap = {}; | ||||
idStampsMap = {}; | |||||
}; | }; | ||||
int add() { | int add() { | ||||
int id = ++uid_c; | int id = ++uid_c; | ||||
subscribers++; | |||||
idMessagesMap[id] = {}; | idMessagesMap[id] = {}; | ||||
idStampsMap[id] = {}; | |||||
ignoresMap[id] = IgnoreFlags(); | |||||
return id; | return id; | ||||
} | } | ||||
void erase(int id) { | void erase(int id) { | ||||
subscribers--; | |||||
idMessagesMap.erase(id); | idMessagesMap.erase(id); | ||||
idStampsMap.erase(id); | |||||
ignoresMap.erase(id); | ignoresMap.erase(id); | ||||
} | } | ||||
}; | }; | ||||
@@ -88,7 +90,7 @@ public: | |||||
virtual void resetMidi()=0; | virtual void resetMidi()=0; | ||||
/* called if a user switches or sets the deivce (and after this device is initialised)*/ | /* called if a user switches or sets the deivce (and after this device is initialised)*/ | ||||
virtual void onDeviceChange(){}; | |||||
virtual void onDeviceChange() {}; | |||||
}; | }; | ||||
////////////////////// | ////////////////////// | ||||