summaryrefslogtreecommitdiff
path: root/drumgizmo/input/midifile.cc
diff options
context:
space:
mode:
authorChristian Glöckner <cgloeckner@freenet.de>2016-03-22 00:22:14 +0100
committerChristian Glöckner <cgloeckner@freenet.de>2016-03-22 00:22:14 +0100
commitbc26b2ffd80890003948c2f1c7f50acb5a9dfc87 (patch)
tree78cbc082d8144f4270a400b33028e6e3bef35b61 /drumgizmo/input/midifile.cc
parent4871ba69d6b0e2338f07b9bc8077f8899d7d8842 (diff)
Modernized InputEngine::run()
Diffstat (limited to 'drumgizmo/input/midifile.cc')
-rw-r--r--drumgizmo/input/midifile.cc48
1 files changed, 14 insertions, 34 deletions
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()