clap

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

commit 9033a64e86adb7b56e8cb1b8ca003ff0837d187f
parent 91f9a918c1e57e92f1e102ab25b4f324e785ee6f
Author: Alexandre BIQUE <bique.alexandre@gmail.com>
Date:   Mon, 24 May 2021 09:08:10 +0200

Add logic to the gain plugin to adjust its port definition according to the track info.

Diffstat:
Mexamples/plugins/gain/gain.cc | 51++++++++++++++++++++++++++++++++++++---------------
Mexamples/plugins/gain/gain.hh | 10++++++++--
Mexamples/plugins/plugin.hh | 3+++
Minclude/clap/ext/draft/track-info.h | 8++++----
4 files changed, 51 insertions(+), 21 deletions(-)

diff --git a/examples/plugins/gain/gain.cc b/examples/plugins/gain/gain.cc @@ -22,30 +22,52 @@ const clap_plugin_descriptor *Gain::descriptor() { Gain::Gain(clap_host *host) : Plugin(descriptor(), host) {} bool Gain::init() { - if (canUseTrackInfo()) { - clap_track_info info; - if (hostTrackInfo_->get_track_info(host_, &info)) - { - channelCount_ = info.channel_count; - channelMap_ = info.channel_map; - } - } - + updateChannelCount(false); return true; } -clap_process_status Gain::process(const clap_process *process) -{ +void Gain::updateChannelCount(bool shouldNotifyHost) { + if (!canUseTrackInfo()) + return; + + clap_track_info info; + if (!hostTrackInfo_->get(host_, &info)) + return; + + if (channelCount_ == info.channel_count) + return; + + if (isActive_) + // we can't change our ports now, delay it + schedulePortUpdate_ = true; + else + channelCount_ = info.channel_count; + + if (canChangeAudioPorts()) + hostAudioPorts_->changed(host_); +} + +bool Gain::activate(int sample_rate) { return true; } + +void Gain::deactivate() { + if (schedulePortUpdate_) { + schedulePortUpdate_ = false; + updateChannelCount(true); + } +} + +void Gain::trackInfoChanged() { updateChannelCount(true); } + +clap_process_status Gain::process(const clap_process *process) { auto **in = process->audio_inputs[0].data32; auto **out = process->audio_outputs[0].data32; float k = 1; - for (int i = 0; i < process->frames_count; ++i) - { + for (int i = 0; i < process->frames_count; ++i) { for (int j = 0; j < channelCount_; ++j) out[j][i] = k * in[j][i]; } return CLAP_PROCESS_CONTINUE_IF_NOT_QUIET; -} -\ No newline at end of file +} diff --git a/examples/plugins/gain/gain.hh b/examples/plugins/gain/gain.hh @@ -10,10 +10,16 @@ public: static const clap_plugin_descriptor *descriptor(); - bool init() override; +protected: + bool init() override; + bool activate(int sample_rate) override; + void deactivate() override; clap_process_status process(const clap_process *process) override; + void trackInfoChanged() override; + void updateChannelCount(bool shouldNotifyHost); + private: int channelCount_ = 2; - int channelMap_ = CLAP_CHMAP_STEREO; + bool schedulePortUpdate_ = false; }; \ No newline at end of file diff --git a/examples/plugins/plugin.hh b/examples/plugins/plugin.hh @@ -26,6 +26,8 @@ protected: virtual clap_process_status process(const clap_process *process) { return CLAP_PROCESS_SLEEP; } virtual const void * extension(const char *id) { return nullptr; } + virtual void trackInfoChanged() {} + ///////////////////// // CLAP Interfaces // ///////////////////// @@ -54,6 +56,7 @@ protected: bool canUseHostLog() const noexcept; bool canUseThreadCheck() const noexcept; bool canUseTrackInfo() const noexcept; + bool canChangeAudioPorts() const noexcept; ///////////////////// // Thread Checking // diff --git a/include/clap/ext/draft/track-info.h b/include/clap/ext/draft/track-info.h @@ -14,7 +14,7 @@ typedef struct clap_track_info { clap_id id; int32_t index; char name[CLAP_NAME_SIZE]; - char path[512]; // Like "/group1/group2/drum-machine/drum-pad" + char path[512]; // Like "/group1/group2/drum-machine/drum-pad-13" int32_t channel_count; clap_chmap channel_map; clap_color color; @@ -23,13 +23,13 @@ typedef struct clap_track_info { typedef struct clap_plugin_track_info { // [main-thread] - void (*track_info_changed)(clap_plugin *plugin); + void (*changed)(clap_plugin *plugin); } clap_plugin_track_info; typedef struct clap_host_track_info { - // Informs the host that the note names has changed. + // Get info about the track the plugin belongs to. // [main-thread] - bool (*get_track_info)(clap_host *host, clap_track_info *info); + bool (*get)(clap_host *host, clap_track_info *info); } clap_host_track_info; #ifdef __cplusplus