commit 8a7f6e6b7d38c3324ec80cdbaa238941a596d3da
parent e0ab1758b1fe8305f0e13b23f9733836307f9795
Author: Alexandre BIQUE <bique.alexandre@gmail.com>
Date: Tue, 31 Aug 2021 10:49:20 +0200
Improve the process loop
Diffstat:
3 files changed, 60 insertions(+), 41 deletions(-)
diff --git a/examples/plugins/core-plugin.cc b/examples/plugins/core-plugin.cc
@@ -1,6 +1,8 @@
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
+#include <sstream>
+
#include "core-plugin.hh"
#include "stream-helper.hh"
@@ -193,4 +195,59 @@ namespace clap {
process->out_events->push_back(process->out_events, &ev);
});
}
+
+ uint32_t CorePlugin::processEvents(const clap_process *process,
+ uint32_t &index,
+ uint32_t count,
+ uint32_t time) {
+ for (; index < count; ++index) {
+ auto ev = process->in_events->get(process->in_events, index);
+
+ if (ev->time < time) {
+ hostMisbehaving("Events must be ordered by time");
+ std::terminate();
+ }
+
+ if (ev->time > time) {
+ // This event is in the future
+ return std::min(ev->time, process->frames_count);
+ }
+
+ switch (ev->type) {
+ case CLAP_EVENT_PARAM_VALUE: {
+ auto p = reinterpret_cast<Parameter *>(ev->param_value.cookie);
+ if (p) {
+ if (p->info().id != ev->param_value.param_id) {
+ std::ostringstream os;
+ os << "Host provided invalid cookie for param id: " << ev->param_value.param_id;
+ hostMisbehaving(os.str());
+ std::terminate();
+ }
+
+ p->setValue(ev->param_value.value);
+ pluginToGuiQueue_.set(p->info().id, {ev->param_value.value, p->modulation()});
+ }
+ break;
+ }
+
+ case CLAP_EVENT_PARAM_MOD: {
+ auto p = reinterpret_cast<Parameter *>(ev->param_mod.cookie);
+ if (p) {
+ if (p->info().id != ev->param_mod.param_id) {
+ std::ostringstream os;
+ os << "Host provided invalid cookie for param id: " << ev->param_mod.param_id;
+ hostMisbehaving(os.str());
+ std::terminate();
+ }
+
+ p->setModulation(ev->param_mod.amount);
+ pluginToGuiQueue_.set(p->info().id, {p->value(), ev->param_mod.amount});
+ }
+ break;
+ }
+ }
+ }
+
+ return process->frames_count;
+ }
} // namespace clap
\ No newline at end of file
diff --git a/examples/plugins/core-plugin.hh b/examples/plugins/core-plugin.hh
@@ -150,6 +150,8 @@ namespace clap {
void guiAdjust(clap_id paramId, double value, clap_event_param_flags flags);
void processGuiEvents(const clap_process *process);
+ uint32_t
+ processEvents(const clap_process *process, uint32_t& index, uint32_t count, uint32_t time);
struct GuiToPluginValue {
double value;
diff --git a/examples/plugins/dc-offset/dc-offset.cc b/examples/plugins/dc-offset/dc-offset.cc
@@ -77,7 +77,6 @@ namespace clap {
float **out = process->audio_outputs[0].data32;
uint32_t evCount = process->in_events->size(process->in_events);
uint32_t nextEvIndex = 0;
- const clap_event *ev = nullptr;
uint32_t N = process->frames_count;
processGuiEvents(process);
@@ -85,46 +84,7 @@ namespace clap {
/* foreach frames */
for (uint32_t i = 0; i < process->frames_count;) {
- /* check if there are events to process */
- for (; nextEvIndex < evCount; ++nextEvIndex) {
- ev = process->in_events->get(process->in_events, nextEvIndex);
-
- if (ev->time < i) {
- hostMisbehaving("Events must be ordered by time");
- std::terminate();
- }
-
- if (ev->time > i) {
- // This event is in the future
- N = std::min(ev->time, process->frames_count);
- break;
- }
-
- switch (ev->type) {
- case CLAP_EVENT_PARAM_VALUE: {
- auto id = ev->param_value.param_id;
- auto p = parameters_.getById(id);
- if (p) {
- p->setValue(ev->param_value.value);
- pluginToGuiQueue_.set(id, {ev->param_value.value, p->modulation()});
- }
- break;
- }
-
- case CLAP_EVENT_PARAM_MOD: {
- auto id = ev->param_mod.param_id;
- auto p = parameters_.getById(id);
- if (p) {
- p->setModulation(ev->param_mod.amount);
- pluginToGuiQueue_.set(id, {p->value(), ev->param_mod.amount});
- }
- break;
- }
- }
- }
-
- if (nextEvIndex == evCount)
- N = process->frames_count;
+ N = processEvents(process, nextEvIndex, evCount, i);
/* Process as many samples as possible until the next event */
for (; i < N; ++i) {