clap

CLAP Audio Plugin API
Log | Files | Refs | README | LICENSE

commit c3b21b3dc67ba0c4907d7c747c44abb7c99f57a3
parent 73ca1b6144ccb1628ed8573fe8041e3adda5f519
Author: Alexandre BIQUE <bique.alexandre@gmail.com>
Date:   Tue, 29 Jun 2021 13:17:30 +0200

Better DcOffset process loop

Diffstat:
Mexamples/plugins/dc-offset/dc-offset.cc | 35++++++++++++++++++++++++-----------
Mexamples/plugins/dc-offset/dc-offset.hh | 9+++------
Minclude/clap/clap.h | 10+++++-----
3 files changed, 32 insertions(+), 22 deletions(-)

diff --git a/examples/plugins/dc-offset/dc-offset.cc b/examples/plugins/dc-offset/dc-offset.cc @@ -68,22 +68,32 @@ namespace clap { audioOutputs_.push_back(info); } - bool DcOffset::activate(int sample_rate) noexcept { return true; } - - void DcOffset::deactivate() noexcept { channelCount_ = 0; } - clap_process_status DcOffset::process(const clap_process *process) noexcept { float **in = process->audio_inputs[0].data32; 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; - for (int i = 0; i < process->frames_count; ++i) { - if (nextEvIndex < evCount) { + /* foreach frames */ + for (uint32_t i = 0; i < process->frames_count; ++i) { + + /* check if there are events to process */ + for (; nextEvIndex < evCount; ++nextEvIndex) { ev = process->in_events->get(process->in_events, nextEvIndex); - if (ev->time != i) + + 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_SET: @@ -98,10 +108,13 @@ namespace clap { } } - float offset = offsetParam_->value(); - for (int c = 0; c < channelCount_; ++c) - out[c][i] = in[c][i] + offset; - offsetParam_->step(1); + /* Process as many samples as possible until the next event */ + for (; i < N; ++i) { + float offset = offsetParam_->value(); + for (int c = 0; c < channelCount_; ++c) + out[c][i] = in[c][i] + offset; + offsetParam_->step(1); + } } return CLAP_PROCESS_CONTINUE_IF_NOT_QUIET; diff --git a/examples/plugins/dc-offset/dc-offset.hh b/examples/plugins/dc-offset/dc-offset.hh @@ -13,15 +13,12 @@ namespace clap { static const clap_plugin_descriptor *descriptor(); protected: - // clap_plugin - bool init() noexcept override; - void defineAudioPorts() noexcept; - bool activate(int sample_rate) noexcept override; - void deactivate() noexcept override; + bool init() noexcept override; + void defineAudioPorts() noexcept; clap_process_status process(const clap_process *process) noexcept override; private: - int channelCount_ = 1; + int channelCount_ = 2; Parameter *offsetParam_ = nullptr; }; } // namespace clap \ No newline at end of file diff --git a/include/clap/clap.h b/include/clap/clap.h @@ -63,14 +63,14 @@ typedef struct clap_audio_buffer { // Either data32 or data64 pointer will be set. float ** data32; double **data64; - int32_t channel_count; + uint32_t channel_count; uint32_t latency; // latency from/to the audio interface uint64_t constant_mask; // mask & (1 << N) to test if channel N is constant } clap_audio_buffer; typedef struct clap_process { - int64_t steady_time; // a steady sample time counter, requiered - int32_t frames_count; // number of frame to process + uint64_t steady_time; // a steady sample time counter, requiered + uint32_t frames_count; // number of frame to process // time info at sample 0 // If null, then this is a free running host, no transport events will be provided @@ -84,8 +84,8 @@ typedef struct clap_process { // then it gets a default stereo input and output. const clap_audio_buffer *audio_inputs; const clap_audio_buffer *audio_outputs; - int32_t audio_inputs_count; - int32_t audio_outputs_count; + uint32_t audio_inputs_count; + uint32_t audio_outputs_count; /* events */ const clap_event_list *in_events;