summaryrefslogtreecommitdiff
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
parent4871ba69d6b0e2338f07b9bc8077f8899d7d8842 (diff)
Modernized InputEngine::run()
-rw-r--r--drumgizmo/input/inputdummy.cc4
-rw-r--r--drumgizmo/input/inputdummy.h2
-rw-r--r--drumgizmo/input/jackmidi.cc22
-rw-r--r--drumgizmo/input/jackmidi.h6
-rw-r--r--drumgizmo/input/midifile.cc48
-rw-r--r--drumgizmo/input/midifile.h4
-rw-r--r--include/event.h6
-rw-r--r--plugin/drumgizmo_plugin.cc21
-rw-r--r--plugin/drumgizmo_plugin.h2
-rw-r--r--src/audioinputengine.h3
-rw-r--r--src/audioinputenginemidi.h2
-rw-r--r--src/drumgizmo.cc24
-rw-r--r--src/drumgizmo.h2
13 files changed, 55 insertions, 91 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;
};
diff --git a/include/event.h b/include/event.h
index 30155ce..2e46f8c 100644
--- a/include/event.h
+++ b/include/event.h
@@ -31,9 +31,9 @@
#define TYPE_STOP 1
typedef struct {
- int type;
- int instrument;
- int offset;
+ size_t type;
+ size_t instrument;
+ size_t offset;
float velocity;
} event_t;
diff --git a/plugin/drumgizmo_plugin.cc b/plugin/drumgizmo_plugin.cc
index 150d13e..61c0b17 100644
--- a/plugin/drumgizmo_plugin.cc
+++ b/plugin/drumgizmo_plugin.cc
@@ -205,16 +205,12 @@ void DrumGizmoPlugin::Input::pre()
{
}
-event_t *DrumGizmoPlugin::Input::run(size_t pos, size_t len, size_t* nevents)
+void DrumGizmoPlugin::Input::run(size_t pos, size_t len, std::vector<event_t>& events)
{
-
+ assert(events.empty());
assert(plugin.input_events);
- event_t* list;
- size_t listsize;
-
- list = (event_t*)malloc(sizeof(event_t) * plugin.input_events->size());
- listsize = 0;
+ events.reserve(plugin.input_events->size());
for(auto& event : *plugin.input_events)
{
@@ -226,16 +222,9 @@ event_t *DrumGizmoPlugin::Input::run(size_t pos, size_t len, size_t* nevents)
int i = mmap.lookup(event.key);
if(event.velocity && (i != -1))
{
- list[listsize].type = TYPE_ONSET;
- list[listsize].instrument = i;
- list[listsize].velocity = (float)event.velocity / 127.0f;
- list[listsize].offset = event.getTime();
- ++listsize;
- }
+ events.push_back({TYPE_ONSET, (size_t)i, (size_t)event.getTime(), event.velocity / 127.0f});
+ }
}
-
- *nevents = listsize;
- return list;
}
void DrumGizmoPlugin::Input::post()
diff --git a/plugin/drumgizmo_plugin.h b/plugin/drumgizmo_plugin.h
index acefbf2..a0ce8b9 100644
--- a/plugin/drumgizmo_plugin.h
+++ b/plugin/drumgizmo_plugin.h
@@ -116,7 +116,7 @@ private:
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;
protected:
diff --git a/src/audioinputengine.h b/src/audioinputengine.h
index 22963ee..9dfd8f2 100644
--- a/src/audioinputengine.h
+++ b/src/audioinputengine.h
@@ -28,6 +28,7 @@
#define __DRUMGIZMO_AUDIOINPUTENGINE_H__
#include <string>
+#include <vector>
#include <event.h>
@@ -47,7 +48,7 @@ public:
virtual void stop() = 0;
virtual void pre() = 0;
- virtual event_t *run(size_t pos, size_t len, size_t *nevents) = 0;
+ virtual void run(size_t pos, size_t len, std::vector<event_t>& events) = 0;
virtual void post() = 0;
};
diff --git a/src/audioinputenginemidi.h b/src/audioinputenginemidi.h
index f53343f..68b82bb 100644
--- a/src/audioinputenginemidi.h
+++ b/src/audioinputenginemidi.h
@@ -51,7 +51,7 @@ public:
virtual void stop() = 0;
virtual void pre() = 0;
- virtual event_t *run(size_t pos, size_t len, size_t *nevents) = 0;
+ virtual void run(size_t pos, size_t len, std::vector<event_t>& events) = 0;
virtual void post() = 0;
bool loadMidiMap(std::string file, Instruments &i);
diff --git a/src/drumgizmo.cc b/src/drumgizmo.cc
index 6bcb7bb..1c437d6 100644
--- a/src/drumgizmo.cc
+++ b/src/drumgizmo.cc
@@ -53,9 +53,12 @@ DrumGizmo::DrumGizmo(AudioOutputEngine *o, AudioInputEngine *i)
, ie(i)
, framesize(0)
, freewheel(false)
+ , events{}
{
is_stopping = false;
audioCache.init(10000); // start thread
+
+ events.reserve(1000);
}
DrumGizmo::~DrumGizmo()
@@ -268,15 +271,14 @@ bool DrumGizmo::run(size_t pos, sample_t *samples, size_t nsamples)
//DEBUG(engine, "Number of active events: %d\n", activeevents[0].size());
- size_t nev;
- event_t *evs = ie->run(pos, nsamples, &nev);
+ ie->run(pos, nsamples, events);
- for(size_t e = 0; e < nev; ++e)
+ for(const auto& event: events)
{
- if(evs[e].type == TYPE_ONSET)
+ if(event.type == TYPE_ONSET)
{
Instrument *i = nullptr;
- int d = evs[e].instrument;
+ int d = event.instrument;
/*
Instruments::iterator it = kit.instruments.begin();
while(d-- && it != kit.instruments.end())
@@ -298,7 +300,7 @@ bool DrumGizmo::run(size_t pos, sample_t *samples, size_t nsamples)
if(i == nullptr || !i->isValid())
{
- ERR(drumgizmo, "Missing Instrument %d.\n", evs[e].instrument);
+ ERR(drumgizmo, "Missing Instrument %d.\n", event.instrument);
continue;
}
@@ -330,7 +332,7 @@ bool DrumGizmo::run(size_t pos, sample_t *samples, size_t nsamples)
}
}
- Sample *s = i->sample(evs[e].velocity, evs[e].offset + pos);
+ Sample *s = i->sample(event.velocity, event.offset + pos);
if(s == nullptr)
{
@@ -355,16 +357,16 @@ bool DrumGizmo::run(size_t pos, sample_t *samples, size_t nsamples)
}
else
{
- //DEBUG(drumgizmo, "Adding event %d.\n", evs[e].offset);
+ //DEBUG(drumgizmo, "Adding event %d.\n", event.offset);
Event *evt = new EventSample(ch.num, 1.0, af, i->group(), i);
- evt->offset = (evs[e].offset + pos) * resampler[0].ratio();
+ evt->offset = (event.offset + pos) * resampler[0].ratio();
activeevents[ch.num].push_back(evt);
}
++j;
}
}
- if(evs[e].type == TYPE_STOP)
+ if(event.type == TYPE_STOP)
{
is_stopping = true;
}
@@ -390,7 +392,7 @@ bool DrumGizmo::run(size_t pos, sample_t *samples, size_t nsamples)
}
- free(evs);
+ events.clear();
//
// Write audio
diff --git a/src/drumgizmo.h b/src/drumgizmo.h
index e0aea85..55f4590 100644
--- a/src/drumgizmo.h
+++ b/src/drumgizmo.h
@@ -106,4 +106,6 @@ protected:
size_t framesize;
bool freewheel;
+
+ std::vector<event_t> events;
};