diff options
author | Christian Glöckner <cgloeckner@freenet.de> | 2016-03-22 00:22:14 +0100 |
---|---|---|
committer | Christian Glöckner <cgloeckner@freenet.de> | 2016-03-22 00:22:14 +0100 |
commit | bc26b2ffd80890003948c2f1c7f50acb5a9dfc87 (patch) | |
tree | 78cbc082d8144f4270a400b33028e6e3bef35b61 /drumgizmo/input | |
parent | 4871ba69d6b0e2338f07b9bc8077f8899d7d8842 (diff) |
Modernized InputEngine::run()
Diffstat (limited to 'drumgizmo/input')
-rw-r--r-- | drumgizmo/input/inputdummy.cc | 4 | ||||
-rw-r--r-- | drumgizmo/input/inputdummy.h | 2 | ||||
-rw-r--r-- | drumgizmo/input/jackmidi.cc | 22 | ||||
-rw-r--r-- | drumgizmo/input/jackmidi.h | 6 | ||||
-rw-r--r-- | drumgizmo/input/midifile.cc | 48 | ||||
-rw-r--r-- | drumgizmo/input/midifile.h | 4 |
6 files changed, 28 insertions, 58 deletions
diff --git a/drumgizmo/input/inputdummy.cc b/drumgizmo/input/inputdummy.cc index bd2e55e..08f903d 100644 --- a/drumgizmo/input/inputdummy.cc +++ b/drumgizmo/input/inputdummy.cc @@ -56,10 +56,8 @@ void DummyInputEngine::pre() { } -event_t* DummyInputEngine::run(size_t pos, size_t len, size_t* nevents) +void DummyInputEngine::run(size_t pos, size_t len, std::vector<event_t>& events) { - nevents = 0u; - return nullptr; } void DummyInputEngine::post() diff --git a/drumgizmo/input/inputdummy.h b/drumgizmo/input/inputdummy.h index e52e047..5827cb3 100644 --- a/drumgizmo/input/inputdummy.h +++ b/drumgizmo/input/inputdummy.h @@ -40,6 +40,6 @@ public: bool start() override; void stop() override; void pre() override; - event_t* run(size_t pos, size_t len, size_t* nevents) override; + void run(size_t pos, size_t len, std::vector<event_t>& events) override; void post() override; }; diff --git a/drumgizmo/input/jackmidi.cc b/drumgizmo/input/jackmidi.cc index 797d57d..266fb51 100644 --- a/drumgizmo/input/jackmidi.cc +++ b/drumgizmo/input/jackmidi.cc @@ -38,8 +38,7 @@ JackMidiInputEngine::JackMidiInputEngine(JackClient& client) , client(client) , port{nullptr} , pos{0u} - , list{nullptr} - , listsize{0u} + , events{} { client.add(*this); } @@ -91,14 +90,11 @@ void JackMidiInputEngine::pre() { } -event_t* JackMidiInputEngine::run(size_t pos, size_t len, size_t* nevents) +void JackMidiInputEngine::run(size_t pos, size_t len, + std::vector<event_t>& events) { - *nevents = listsize; - event_t* l = list; - // todo: get rid of malloc - list = (event_t*)malloc(sizeof(event_t) * 1000); - listsize = 0; - return l; + assert(events.empty()); + std::swap(events, this->events); } void JackMidiInputEngine::post() @@ -111,6 +107,8 @@ void JackMidiInputEngine::process(jack_nframes_t num_frames) void* buffer = jack_port_get_buffer(port->port, num_frames); jack_nframes_t num_events = jack_midi_get_event_count(buffer); + assert(events.empty()); + events.reserve(num_events); for(jack_nframes_t i = 0; i < num_events; ++i) { jack_midi_event_t event; @@ -129,11 +127,7 @@ void JackMidiInputEngine::process(jack_nframes_t num_frames) int k = mmap.lookup(key); if(k != -1 && velocity) { - list[listsize].type = TYPE_ONSET; - list[listsize].instrument = k; - list[listsize].velocity = velocity / 127.0; - list[listsize].offset = event.time; - ++listsize; + events.push_back({TYPE_ONSET, (size_t)k, event.time, velocity / 127.f}); } } jack_midi_clear_buffer(buffer); diff --git a/drumgizmo/input/jackmidi.h b/drumgizmo/input/jackmidi.h index 5adec4e..56d622d 100644 --- a/drumgizmo/input/jackmidi.h +++ b/drumgizmo/input/jackmidi.h @@ -48,7 +48,7 @@ public: bool start() override; void stop() override; void pre() override; - event_t* run(size_t pos, size_t len, size_t* nevents) override; + void run(size_t pos, size_t len, std::vector<event_t>& events) override; void post() override; // based on JackProcess @@ -59,7 +59,5 @@ private: std::unique_ptr<JackPort> port; std::size_t pos; - - event_t* list; - size_t listsize; + std::vector<event_t> events; }; diff --git a/drumgizmo/input/midifile.cc b/drumgizmo/input/midifile.cc index 4aae18f..c5fec5c 100644 --- a/drumgizmo/input/midifile.cc +++ b/drumgizmo/input/midifile.cc @@ -25,6 +25,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ #include <iostream> +#include <cassert> #include "midifile.h" @@ -39,6 +40,7 @@ MidifileInputEngine::MidifileInputEngine() , track{-1} // all tracks , loop{false} , offset{0.0} + , samplerate{44100.0} // todo: via ctor arg { } @@ -128,14 +130,12 @@ void MidifileInputEngine::pre() { } -event_t* MidifileInputEngine::run(size_t pos, size_t len, size_t* nevents) +void MidifileInputEngine::run(size_t pos, size_t len, std::vector<event_t>& events) { - event_t* evs{nullptr}; - size_t num_events{0u}; + assert(events.empty()); - double current_max_time = (1.0 + pos + len) / (44100.0 / speed); + double current_max_time = (1.0 + pos + len) / (samplerate / speed); current_max_time -= offset; - // double cur_min_time = (double)(pos) / (44100.0 / speed); if(!current_event) { @@ -151,32 +151,20 @@ event_t* MidifileInputEngine::run(size_t pos, size_t len, size_t* nevents) (track == -1 || current_event->track_number == track) && current_event->midi_buffer[2] > 0) { - - if(evs == nullptr) - { - // todo: get rid of malloc - evs = (event_t*)malloc(sizeof(event_t) * 1000); - } - int key = current_event->midi_buffer[1]; int velocity = current_event->midi_buffer[2]; - evs[num_events].type = TYPE_ONSET; - size_t evpos = current_event->time_seconds * (44100.0 / speed); - evs[num_events].offset = evpos - pos; + events.emplace_back(); + auto& event = events.back(); + event.type = TYPE_ONSET; + size_t evpos = current_event->time_seconds * (samplerate / speed); + event.offset = evpos - pos; int i = mmap.lookup(key); if(i != -1) { - evs[num_events].instrument = i; - evs[num_events].velocity = velocity / 127.0; - - ++num_events; - if(num_events > 999) - { - fprintf(stderr, "PANIC!\n"); - break; - } + event.instrument = i; + event.velocity = velocity / 127.0; } } } @@ -193,18 +181,10 @@ event_t* MidifileInputEngine::run(size_t pos, size_t len, size_t* nevents) } else { - if(evs == nullptr) - { - // TODO: get rid of malloc - evs = (event_t*)malloc(sizeof(event_t) * 1000); - } - evs[num_events].type = TYPE_STOP; - evs[num_events].offset = len - 1; - ++num_events; + assert(len >= 1); + events.push_back({TYPE_STOP, 0, len-1, 0.f}); } } - *nevents = num_events; - return evs; } void MidifileInputEngine::post() diff --git a/drumgizmo/input/midifile.h b/drumgizmo/input/midifile.h index ca00359..9920a3b 100644 --- a/drumgizmo/input/midifile.h +++ b/drumgizmo/input/midifile.h @@ -47,7 +47,7 @@ public: bool start() override; void stop() override; void pre() override; - event_t* run(size_t pos, size_t len, size_t* nevents) override; + void run(size_t pos, size_t len, std::vector<event_t>& events) override; void post() override; private: @@ -58,5 +58,5 @@ private: float speed; int track; bool loop; - double offset; + double offset, samplerate; }; |