summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2019-04-08 17:42:00 +0200
committerBent Bisballe Nyeng <deva@aasimon.org>2019-04-08 18:08:43 +0200
commit723343269d161b2bba7aee4597357ffc32f26f04 (patch)
tree65b25278bcf029ce5f34420a0caaee634b0c9c04
parent10d483417fa70bb4c357d598f3bdfcf4f0b75f21 (diff)
Move midi note process to one common place in AudioInputEngineMidi::processNote and use it in the four input engines (plugin, midifile, ossmidi and jackmidi)
-rw-r--r--drumgizmo/input/jackmidi.cc20
-rw-r--r--drumgizmo/input/midifile.cc27
-rw-r--r--drumgizmo/input/ossmidi.cc25
-rw-r--r--plugin/drumgizmo_plugin.cc30
-rw-r--r--src/audioinputenginemidi.cc49
-rw-r--r--src/audioinputenginemidi.h5
6 files changed, 68 insertions, 88 deletions
diff --git a/drumgizmo/input/jackmidi.cc b/drumgizmo/input/jackmidi.cc
index 8459e67..cf4dc6c 100644
--- a/drumgizmo/input/jackmidi.cc
+++ b/drumgizmo/input/jackmidi.cc
@@ -30,9 +30,6 @@
#include "cpp11fix.h" // required for c++11
#include "jackmidi.h"
-static int const NOTE_ON = 0x90;
-static int const NOTE_MASK = 0xF0;
-
JackMidiInputEngine::JackMidiInputEngine(JackClient& client)
: AudioInputEngineMidi{}
, JackProcess{}
@@ -119,22 +116,7 @@ void JackMidiInputEngine::process(jack_nframes_t num_frames)
{
jack_midi_event_t event;
jack_midi_event_get(&event, buffer, i);
- if(event.size != 3)
- {
- continue;
- }
- if((event.buffer[0] & NOTE_MASK) != NOTE_ON)
- {
- continue;
- }
- int key = event.buffer[1];
- int velocity = event.buffer[2];
- printf("Event key:%d vel:%d\n", key, velocity);
- int k = mmap.lookup(key);
- if(k != -1 && velocity)
- {
- events.push_back({EventType::OnSet, (size_t)k, event.time, velocity / 127.f});
- }
+ processNote(event.buffer, event.size, event.time, events);
}
jack_midi_clear_buffer(buffer);
pos += num_frames;
diff --git a/drumgizmo/input/midifile.cc b/drumgizmo/input/midifile.cc
index 562ed37..c0b8e73 100644
--- a/drumgizmo/input/midifile.cc
+++ b/drumgizmo/input/midifile.cc
@@ -29,9 +29,6 @@
#include "midifile.h"
-static int const NOTE_ON = 0x90;
-static int const NOTE_MASK = 0xF0;
-
MidifileInputEngine::MidifileInputEngine()
: AudioInputEngineMidi{}
, smf{nullptr}
@@ -147,26 +144,12 @@ void MidifileInputEngine::run(size_t pos, size_t len, std::vector<event_t>& even
{
if(!smf_event_is_metadata(current_event))
{
- if((current_event->midi_buffer_length == 3) &&
- ((current_event->midi_buffer[0] & NOTE_MASK) == NOTE_ON) &&
- (track == -1 || current_event->track_number == track) &&
- current_event->midi_buffer[2] > 0)
+ if(track == -1 || current_event->track_number == track)
{
- int key = current_event->midi_buffer[1];
- int velocity = current_event->midi_buffer[2];
-
- events.emplace_back();
- auto& event = events.back();
- event.type = EventType::OnSet;
- size_t evpos = current_event->time_seconds * (samplerate / speed);
- event.offset = evpos - pos;
-
- int i = mmap.lookup(key);
- if(i != -1)
- {
- event.instrument = i;
- event.velocity = velocity / 127.0;
- }
+ processNote(current_event->midi_buffer,
+ current_event->midi_buffer_length,
+ current_event->time_seconds * (samplerate / speed),
+ events);
}
}
diff --git a/drumgizmo/input/ossmidi.cc b/drumgizmo/input/ossmidi.cc
index 325a83d..7c7757e 100644
--- a/drumgizmo/input/ossmidi.cc
+++ b/drumgizmo/input/ossmidi.cc
@@ -32,10 +32,6 @@
#include <iostream>
-static int const NOTE_ON = 0x90;
-static int const NOTE_MASK = 0xF0;
-
-
OSSInputEngine::OSSInputEngine()
: AudioInputEngineMidi{}
, dev{"/dev/midi"}
@@ -100,21 +96,12 @@ void OSSInputEngine::run(size_t pos, size_t len, std::vector<event_t>& events)
unsigned char buf[128];
if ((l = read (fd, buf, sizeof (buf))) != -1)
{
- int masked_note = buf[0] & NOTE_MASK;
- if (masked_note == NOTE_ON) {
- int note = buf[1];
- int velocity = buf[2];
- event_t event;
- event.instrument = mmap.lookup(note);
- if(event.instrument != -1)
- {
- event.velocity = velocity / 127.0;
- event.type = 0;
- event.offset = 0;
- events.push_back(event);
- }
- }
- } else if (errno != EAGAIN) {
+ processNote(buf, l,
+ 0, // No time information available? play as soon as possible
+ events);
+ }
+ else if (errno != EAGAIN)
+ {
std::cerr << "Error code: " << errno << std::endl;
std::cerr << std::strerror(errno) << std::endl;
}
diff --git a/plugin/drumgizmo_plugin.cc b/plugin/drumgizmo_plugin.cc
index 67436af..40121e2 100644
--- a/plugin/drumgizmo_plugin.cc
+++ b/plugin/drumgizmo_plugin.cc
@@ -372,34 +372,8 @@ void DrumGizmoPlugin::Input::run(size_t pos, size_t len, std::vector<event_t>& e
for(auto& event : *plugin.input_events)
{
- switch(event.type)
- {
- case MidiEventType::NoteOn:
- {
- int i = mmap.lookup(event.key);
- if(event.velocity != 0 && (i != -1))
- {
- events.push_back({EventType::OnSet, (size_t)i,
- (size_t)event.getTime(), event.velocity / 127.0f});
- }
- }
- break;
-
- case MidiEventType::Aftertouch:
- {
- int i = mmap.lookup(event.key);
- if(event.velocity == 0 && i != -1)
- {
- events.push_back({EventType::Choke, (size_t)i,
- (size_t)event.getTime(), .0f});
- }
- }
- break;
-
- case MidiEventType::NoteOff:
- case MidiEventType::Unknown:
- break;
- }
+ processNote((const std::uint8_t*)event.getData(), event.getSize(),
+ event.getTime(), events);
}
}
diff --git a/src/audioinputenginemidi.cc b/src/audioinputenginemidi.cc
index 6e76f19..7b8cbcb 100644
--- a/src/audioinputenginemidi.cc
+++ b/src/audioinputenginemidi.cc
@@ -95,3 +95,52 @@ bool AudioInputEngineMidi::isValid() const
{
return is_valid;
}
+
+// Note types:
+static const std::uint8_t NoteOff = 0x80;
+static const std::uint8_t NoteOn = 0x90;
+static const std::uint8_t NoteAftertouch = 0xA0;
+
+// Note type mask:
+static int const NoteMask = 0xF0;
+
+void AudioInputEngineMidi::processNote(const std::uint8_t* midi_buffer,
+ std::size_t midi_buffer_length,
+ std::size_t offset,
+ std::vector<event_t>& events)
+{
+ if(midi_buffer_length != 3)
+ {
+ return;
+ }
+
+ auto key = midi_buffer[1];
+ auto velocity = midi_buffer[2];
+ auto instrument_idx = mmap.lookup(key);
+
+ switch(midi_buffer[0] & NoteMask)
+ {
+ case NoteOff:
+ // Ignore for now
+ break;
+
+ case NoteOn:
+ if(instrument_idx != -1)
+ {
+ events.push_back({EventType::OnSet, (std::size_t)instrument_idx,
+ offset, velocity / 127.0f});
+ }
+ break;
+
+ case NoteAftertouch:
+ if(velocity == 0 && instrument_idx != -1)
+ {
+ events.push_back({EventType::Choke, (std::size_t)instrument_idx,
+ offset, .0f});
+ }
+ break;
+
+ default:
+ break;
+ }
+}
diff --git a/src/audioinputenginemidi.h b/src/audioinputenginemidi.h
index e02ba21..8da7bd2 100644
--- a/src/audioinputenginemidi.h
+++ b/src/audioinputenginemidi.h
@@ -57,6 +57,11 @@ public:
bool isValid() const;
+ void processNote(const std::uint8_t* note_data,
+ std::size_t note_data_size,
+ std::size_t offset,
+ std::vector<event_t>& events);
+
protected:
MidiMapper mmap;