diff options
author | Volker Fischer <corrados@users.noreply.github.com> | 2023-10-02 16:12:18 +0200 |
---|---|---|
committer | Volker Fischer <corrados@users.noreply.github.com> | 2023-10-02 16:12:18 +0200 |
commit | 644742033b73eb19485b47277fb1a33343e8b356 (patch) | |
tree | 8bca1202a86428a42445cba8a31ccd7a28e11a19 | |
parent | e559a014f9cc6205bc15b6f09729751b9b14386f (diff) |
introduce controlthresh in MIDI mapping XML to support playing different hi-hat sounds depending on the MIDI controller 4 input
-rw-r--r-- | src/audioinputenginemidi.cc | 7 | ||||
-rw-r--r-- | src/midimapparser.cc | 7 | ||||
-rw-r--r-- | src/midimapparser.h | 2 | ||||
-rw-r--r-- | src/midimapper.cc | 23 | ||||
-rw-r--r-- | src/midimapper.h | 8 |
5 files changed, 41 insertions, 6 deletions
diff --git a/src/audioinputenginemidi.cc b/src/audioinputenginemidi.cc index 503a5c3..4297ced 100644 --- a/src/audioinputenginemidi.cc +++ b/src/audioinputenginemidi.cc @@ -78,7 +78,7 @@ bool AudioInputEngineMidi::loadMidiMap(const std::string& file, instrmap[instruments[i]->getName()] = i; } - mmap.swap(instrmap, midimap_parser.midimap); + mmap.swap(instrmap, midimap_parser.midimap, midimap_parser.midimultimap, midimap_parser.controlthreshmap); midimap = file; is_valid = true; @@ -133,7 +133,7 @@ void AudioInputEngineMidi::processNote(const std::uint8_t* midi_buffer, { auto key = midi_buffer[1]; auto velocity = midi_buffer[2]; - auto instrument_idx = mmap.lookup(key); + auto instrument_idx = mmap.lookup(key, hihat_controller); if(velocity != 0 && instrument_idx != -1) { // maps velocities to [.5/127, 126.5/127] @@ -141,6 +141,8 @@ void AudioInputEngineMidi::processNote(const std::uint8_t* midi_buffer, events.push_back({ EventType::OnSet, (std::size_t)instrument_idx, offset, centered_velocity, positional_information }); + +/* // quick hack, add 1000000 to offset to transport hi-hat controller value auto instrument_idx1 = mmap.lookup(hihat_midi_key); if(instrument_idx == instrument_idx1) @@ -151,6 +153,7 @@ if(instrument_idx == instrument_idx1) 1000000 + hihat_controller, .0f, .0f }); } } +*/ } } diff --git a/src/midimapparser.cc b/src/midimapparser.cc index 059dfec..551af0a 100644 --- a/src/midimapparser.cc +++ b/src/midimapparser.cc @@ -45,12 +45,19 @@ bool MidiMapParser::parseFile(const std::string& filename) constexpr int bad_value = 10000; auto note = map_node.attribute("note").as_int(bad_value); auto instr = map_node.attribute("instr").as_string(); + auto controlthresh = map_node.attribute("controlthresh").as_int(bad_value); if(std::string(instr) == "" || note == bad_value) { continue; } + if(controlthresh != bad_value) + { + controlthreshmap[note][instr] = controlthresh; + } + midimap[note] = instr; + midimultimap.insert(std::make_pair(note, instr)); } return true; diff --git a/src/midimapparser.h b/src/midimapparser.h index d2f2ddd..061c619 100644 --- a/src/midimapparser.h +++ b/src/midimapparser.h @@ -35,4 +35,6 @@ public: bool parseFile(const std::string& filename); midimap_t midimap; + midimultimap_t midimultimap; + controlthreshmap_t controlthreshmap; }; diff --git a/src/midimapper.cc b/src/midimapper.cc index 9593aae..12c1452 100644 --- a/src/midimapper.cc +++ b/src/midimapper.cc @@ -26,7 +26,7 @@ */ #include "midimapper.h" -int MidiMapper::lookup(int note) +int MidiMapper::lookup(int note, int controller) { std::lock_guard<std::mutex> guard(mutex); @@ -42,15 +42,34 @@ int MidiMapper::lookup(int note) return -1; } + if(controller >= 0 && midimultimap.count(note) > 1) + { + // find instrument where controller is above threshold with smallest distance to threshold + int diff = 10000; + std::string instr = controlthreshmap[note].begin()->first; + for(auto& c : controlthreshmap[note]) + { + int cur_diff = controller - c.second; + if(cur_diff >= 0 && cur_diff < diff) + { + diff = cur_diff; + instr = c.first; + } + } + instrmap_it = instrmap.find(instr); + } + return instrmap_it->second; } -void MidiMapper::swap(instrmap_t& instrmap, midimap_t& midimap) +void MidiMapper::swap(instrmap_t& instrmap, midimap_t& midimap, midimultimap_t& midimultimap, controlthreshmap_t& controlthreshmap) { std::lock_guard<std::mutex> guard(mutex); std::swap(this->instrmap, instrmap); std::swap(this->midimap, midimap); + std::swap(this->midimultimap, midimultimap); + std::swap(this->controlthreshmap, controlthreshmap); } const midimap_t& MidiMapper::getMap() diff --git a/src/midimapper.h b/src/midimapper.h index 4673e33..659d9c8 100644 --- a/src/midimapper.h +++ b/src/midimapper.h @@ -31,23 +31,27 @@ #include <mutex> typedef std::map<int, std::string> midimap_t; +typedef std::multimap<int, std::string> midimultimap_t; typedef std::map<std::string, int> instrmap_t; +typedef std::map<int, std::map<std::string, int> > controlthreshmap_t; class MidiMapper { public: //! Lookup note in map and return its index. //! \returns -1 if not found or the note index. - int lookup(int note); + int lookup(int note, int controller = -1); //! Set new map sets. - void swap(instrmap_t& instrmap, midimap_t& midimap); + void swap(instrmap_t& instrmap, midimap_t& midimap, midimultimap_t& midimultimap, controlthreshmap_t& controlthreshmap); const midimap_t& getMap(); private: instrmap_t instrmap; midimap_t midimap; + midimultimap_t midimultimap; + controlthreshmap_t controlthreshmap; std::mutex mutex; }; |