summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2021-08-08 10:34:54 +0200
committerBent Bisballe Nyeng <deva@aasimon.org>2021-08-08 10:34:54 +0200
commitf43935b5c873676a632b23cbfcef45a4431b233d (patch)
treead1473fa34f3da5292e3344a99ed447261d99584 /src
parenta2483a839264369482fff135d33f007ded266d3c (diff)
Add support for a single midi note that plays multiple instruments.
Diffstat (limited to 'src')
-rw-r--r--src/audioinputenginemidi.cc51
-rw-r--r--src/midimapparser.cc3
-rw-r--r--src/midimapper.cc24
-rw-r--r--src/midimapper.h16
4 files changed, 53 insertions, 41 deletions
diff --git a/src/audioinputenginemidi.cc b/src/audioinputenginemidi.cc
index 8732d8d..69aeeb6 100644
--- a/src/audioinputenginemidi.cc
+++ b/src/audioinputenginemidi.cc
@@ -117,32 +117,35 @@ 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);
-
- switch(midi_buffer[0] & NoteMask)
+ auto instruments = mmap.lookup(key);
+ for(const auto& instrument_idx : instruments)
{
- case NoteOff:
- // Ignore for now
- break;
-
- case NoteOn:
- if(velocity != 0 && instrument_idx != -1)
- {
- // maps velocities to [.5/127, 126.5/127]
- auto centered_velocity = (velocity-.5f)/127.0f;
- events.push_back({EventType::OnSet, (std::size_t)instrument_idx,
- offset, centered_velocity});
- }
- break;
-
- case NoteAftertouch:
- if(velocity > 0 && instrument_idx != -1)
+ switch(midi_buffer[0] & NoteMask)
{
- events.push_back({EventType::Choke, (std::size_t)instrument_idx,
- offset, .0f});
+ case NoteOff:
+ // Ignore for now
+ break;
+
+ case NoteOn:
+ if(velocity != 0)
+ {
+ // maps velocities to [.5/127, 126.5/127]
+ auto centered_velocity = (velocity-.5f)/127.0f;
+ events.push_back({EventType::OnSet, (std::size_t)instrument_idx,
+ offset, centered_velocity});
+ }
+ break;
+
+ case NoteAftertouch:
+ if(velocity > 0)
+ {
+ events.push_back({EventType::Choke, (std::size_t)instrument_idx,
+ offset, .0f});
+ }
+ break;
+
+ default:
+ break;
}
- break;
-
- default:
- break;
}
}
diff --git a/src/midimapparser.cc b/src/midimapparser.cc
index 059dfec..363e1d5 100644
--- a/src/midimapparser.cc
+++ b/src/midimapparser.cc
@@ -50,7 +50,8 @@ bool MidiMapParser::parseFile(const std::string& filename)
continue;
}
- midimap[note] = instr;
+ MidimapEntry entry{note, instr};
+ midimap.push_back(entry);
}
return true;
diff --git a/src/midimapper.cc b/src/midimapper.cc
index 9593aae..b9316c5 100644
--- a/src/midimapper.cc
+++ b/src/midimapper.cc
@@ -26,23 +26,25 @@
*/
#include "midimapper.h"
-int MidiMapper::lookup(int note)
+std::vector<int> MidiMapper::lookup(int note_id)
{
- std::lock_guard<std::mutex> guard(mutex);
+ std::vector<int> instruments;
- auto midimap_it = midimap.find(note);
- if(midimap_it == midimap.end())
- {
- return -1;
- }
+ std::lock_guard<std::mutex> guard(mutex);
- auto instrmap_it = instrmap.find(midimap_it->second);
- if(instrmap_it == instrmap.end())
+ for(const auto& map_entry : midimap)
{
- return -1;
+ if(map_entry.note_id == note_id)
+ {
+ auto instrmap_it = instrmap.find(map_entry.instrument_name);
+ if(instrmap_it != instrmap.end())
+ {
+ instruments.push_back(instrmap_it->second);
+ }
+ }
}
- return instrmap_it->second;
+ return instruments;
}
void MidiMapper::swap(instrmap_t& instrmap, midimap_t& midimap)
diff --git a/src/midimapper.h b/src/midimapper.h
index 4673e33..94781d4 100644
--- a/src/midimapper.h
+++ b/src/midimapper.h
@@ -29,16 +29,22 @@
#include <map>
#include <string>
#include <mutex>
+#include <vector>
-typedef std::map<int, std::string> midimap_t;
-typedef std::map<std::string, int> instrmap_t;
+struct MidimapEntry
+{
+ int note_id;
+ std::string instrument_name;
+};
+
+using midimap_t = std::vector<MidimapEntry>;
+using instrmap_t = std::map<std::string, int>;
class MidiMapper
{
public:
- //! Lookup note in map and return its index.
- //! \returns -1 if not found or the note index.
- int lookup(int note);
+ //! Lookup note in map and returns the corresponding instrument index list.
+ std::vector<int> lookup(int note_id);
//! Set new map sets.
void swap(instrmap_t& instrmap, midimap_t& midimap);