From 9e1ddadfd090a3c1ae510ca8387da2e54a9cdbaa Mon Sep 17 00:00:00 2001 From: trinitou Date: Thu, 12 Oct 2023 23:59:48 +0200 Subject: [PATCH] Make Plugin derive from clap::helpers::Host --- src/clap_proxy.cpp | 202 ++++++--------------------------------------- src/clap_proxy.h | 113 ++++++++++++------------- 2 files changed, 81 insertions(+), 234 deletions(-) diff --git a/src/clap_proxy.cpp b/src/clap_proxy.cpp index b48cddc0..d1584952 100644 --- a/src/clap_proxy.cpp +++ b/src/clap_proxy.cpp @@ -13,117 +13,11 @@ namespace Clap { -namespace HostExt -{ -static Plugin* self(const clap_host_t* host) -{ - return static_cast(host->host_data); -} - -void host_log(const clap_host_t* host, clap_log_severity severity, const char* msg) -{ - self(host)->log(severity, msg); -} - -clap_host_log_t log = {host_log}; - -void rescan(const clap_host_t* host, clap_param_rescan_flags flags) -{ - self(host)->param_rescan(flags); -} - -// Clears references to a parameter. -// [main-thread] -void clear(const clap_host_t* host, clap_id param_id, clap_param_clear_flags flags) -{ -} - -// Request a parameter flush. -// -// If the plugin is processing, this will result in no action. The process call -// will run normally. If plugin isn't processing, the host will make a subsequent -// call to clap_plugin_params->flush(). As a result, this function is always -// safe to call from a non-audio thread (typically the UI thread on a gesture) -// whether processing is active or not. -// -// This must not be called on the [audio-thread]. -// [thread-safe,!audio-thread] -void request_flush(const clap_host_t* host) -{ - self(host)->param_request_flush(); -} -clap_host_params_t params = {rescan, clear, request_flush}; - -bool is_main_thread(const clap_host_t* host) -{ - return self(host)->is_main_thread(); -} - -// Returns true if "this" thread is one of the audio threads. -// [thread-safe] -bool is_audio_thread(const clap_host_t* host) -{ - return self(host)->is_audio_thread(); -} - -clap_host_thread_check_t threadcheck = {is_main_thread, is_audio_thread}; - -static void resize_hints_changed(const clap_host_t* host) -{ - self(host)->resize_hints_changed(); -} -static bool request_resize(const clap_host_t* host, uint32_t width, uint32_t height) -{ - return self(host)->request_resize(width, height); -} -static bool request_show(const clap_host_t* host) -{ - return self(host)->request_show(); -} -static bool request_hide(const clap_host_t* host) -{ - return self(host)->request_hide(); -} -static void closed(const clap_host_t* host, bool was_destroyed) -{ - self(host)->closed(was_destroyed); -} - -const clap_host_gui hostgui = {resize_hints_changed, request_resize, request_show, request_hide, closed}; - -const clap_host_timer_support hosttimer = { - /* register_timer */ [](const clap_host_t* host, uint32_t period_ms, clap_id* timer_id) -> bool - { return self(host)->register_timer(period_ms, timer_id); }, - /* unregister_timer */ - [](const clap_host_t* host, clap_id timer_id) -> bool - { return self(host)->unregister_timer(timer_id); }}; - -#if LIN -const clap_host_posix_fd_support hostposixfd = { - [](const clap_host_t* host, int fd, clap_posix_fd_flags_t flags) -> bool - { return self(host)->register_fd(fd, flags); }, - [](const clap_host_t* host, int fd, clap_posix_fd_flags_t flags) -> bool - { return self(host)->modify_fd(fd, flags); }, - [](const clap_host_t* host, int fd) -> bool { return self(host)->unregister_fd(fd); }}; -#endif - -const clap_host_latency latency = {[](const clap_host_t* host) -> void - { self(host)->latency_changed(); }}; - -static void tail_changed(const clap_host_t* host) -{ - self(host)->tail_changed(); -} - -const clap_host_tail tail = {tail_changed}; - -} // namespace HostExt - std::shared_ptr Plugin::createInstance(const clap_plugin_factory* fac, const std::string& id, Clap::IHost* host) { auto plug = std::shared_ptr(new Plugin(host)); - auto instance = fac->create_plugin(fac, plug->getClapHostInterface(), id.c_str()); + auto instance = fac->create_plugin(fac, plug->clapHost(), id.c_str()); plug->connectClap(instance); return plug; @@ -144,7 +38,7 @@ std::shared_ptr Plugin::createInstance(Clap::Library& library, size_t in { auto plug = std::shared_ptr(new Plugin(host)); auto instance = library._pluginFactory->create_plugin( - library._pluginFactory, plug->getClapHostInterface(), library.plugins[index]->id); + library._pluginFactory, plug->clapHost(), library.plugins[index]->id); plug->connectClap(instance); return plug; @@ -153,16 +47,10 @@ std::shared_ptr Plugin::createInstance(Clap::Library& library, size_t in } Plugin::Plugin(IHost* host) - : _host{CLAP_VERSION, - this, - "Clap-As-VST3-Wrapper", - "defiant nerd", - "https://www.defiantnerd.com", - "0.0.1", - Plugin::clapExtension, - Plugin::clapRequestRestart, - Plugin::clapRequestProcess, - Plugin::clapRequestCallback} + : clap::helpers::Host{"Clap-As-VST3-Wrapper", + "defiant nerd", + "https://www.defiantnerd.com", + "0.0.1"} , _parentHost(host) { } @@ -329,17 +217,17 @@ const clap_plugin_gui_t* Plugin::getUI() const return nullptr; } -void Plugin::latency_changed() +void Plugin::latencyChanged() noexcept { _parentHost->latency_changed(); } -void Plugin::tail_changed() +void Plugin::tailChanged() noexcept { _parentHost->tail_changed(); } -void Plugin::log(clap_log_severity severity, const char* msg) +void Plugin::logLog(clap_log_severity severity, const char* msg) noexcept { std::string n; switch (severity) @@ -383,12 +271,12 @@ void Plugin::log(clap_log_severity severity, const char* msg) #endif } -bool Plugin::is_main_thread() const +bool Plugin::threadCheckIsMainThread() noexcept { return _main_thread_id == std::this_thread::get_id(); } -bool Plugin::is_audio_thread() const +bool Plugin::threadCheckIsAudioThread() noexcept { if (this->_audio_thread_override > 0) { @@ -402,94 +290,56 @@ CLAP_NODISCARD Raise Plugin::AlwaysAudioThread() return Raise(this->_audio_thread_override); } -void Plugin::param_rescan(clap_param_rescan_flags flags) +void Plugin::paramsRescan(clap_param_rescan_flags flags) noexcept { _parentHost->param_rescan(flags); } -void Plugin::param_clear(clap_id param, clap_param_clear_flags flags) +void Plugin::paramsClear(clap_id param, clap_param_clear_flags flags) noexcept { _parentHost->param_clear(param, flags); } -void Plugin::param_request_flush() +void Plugin::paramsRequestFlush() noexcept { _parentHost->param_request_flush(); } -// Query an extension. -// [thread-safe] -const void* Plugin::clapExtension(const clap_host* /*host*/, const char* extension) -{ - if (!strcmp(extension, CLAP_EXT_LOG)) return &HostExt::log; - if (!strcmp(extension, CLAP_EXT_PARAMS)) return &HostExt::params; - if (!strcmp(extension, CLAP_EXT_THREAD_CHECK)) return &HostExt::threadcheck; - if (!strcmp(extension, CLAP_EXT_GUI)) return &HostExt::hostgui; - if (!strcmp(extension, CLAP_EXT_TIMER_SUPPORT)) return &HostExt::hosttimer; -#if LIN - if (!strcmp(extension, CLAP_EXT_POSIX_FD_SUPPORT)) return &HostExt::hostposixfd; -#endif - if (!strcmp(extension, CLAP_EXT_LATENCY)) return &HostExt::latency; - if (!strcmp(extension, CLAP_EXT_TAIL)) - { - return &HostExt::tail; - } - if (!strcmp(extension, CLAP_EXT_RENDER)) - { - // TODO: implement CLAP_EXT_RENDER - } - - return nullptr; -} - -// Request the host to schedule a call to plugin->on_main_thread(plugin) on the main thread. -// [thread-safe] -void Plugin::clapRequestCallback(const clap_host* host) -{ - auto self = static_cast(host->host_data); - self->_parentHost->request_callback(); -} - -// Request the host to deactivate and then reactivate the plugin. -// The operation may be delayed by the host. -// [thread-safe] -void Plugin::clapRequestRestart(const clap_host* host) -{ - auto self = static_cast(host->host_data); - self->_parentHost->restartPlugin(); +void Plugin::requestRestart() noexcept override { + _parentHost->restartPlugin(); } -// Request the host to activate and start processing the plugin. -// This is useful if you have external IO and need to wake up the plugin from "sleep". -// [thread-safe] -void Plugin::clapRequestProcess(const clap_host* host) -{ +void Plugin::requestProcess() noexcept override { // right now, I don't know how to communicate this to the host // in VST3 you can't force processing... } +void Plugin::requestCallback() noexcept override { + _parentHost->request_callback(); +} + // Registers a periodic timer. // The host may adjust the period if it is under a certain threshold. // 30 Hz should be allowed. // [main-thread] -bool Plugin::register_timer(uint32_t period_ms, clap_id* timer_id) +bool Plugin::timerSupportRegisterTimer(uint32_t period_ms, clap_id* timer_id) noexcept { return _parentHost->register_timer(period_ms, timer_id); } -bool Plugin::unregister_timer(clap_id timer_id) +bool Plugin::timerSupportUnregisterTimer(clap_id timer_id) noexcept { return _parentHost->unregister_timer(timer_id); } #if LIN -bool Plugin::register_fd(int fd, clap_posix_fd_flags_t flags) +bool Plugin::posixFdSupportRegisterFd(int fd, clap_posix_fd_flags_t flags) noexcept { return _parentHost->register_fd(fd, flags); } -bool Plugin::modify_fd(int fd, clap_posix_fd_flags_t flags) +bool Plugin::posixFdSupportModifyFd(int fd, clap_posix_fd_flags_t flags) noexcept { return _parentHost->modify_fd(fd, flags); } -bool Plugin::unregister_fd(int fd) +bool Plugin::posixFdSupportUnregisterFd(int fd) noxcept { return _parentHost->unregister_fd(fd); } diff --git a/src/clap_proxy.h b/src/clap_proxy.h index 7d53b1a9..320c3c5e 100644 --- a/src/clap_proxy.h +++ b/src/clap_proxy.h @@ -14,6 +14,7 @@ */ #include +#include #include #include #include @@ -121,7 +122,7 @@ class Raise /// Plugin is the `host` for the CLAP plugin instance /// and the interface for the VST3 plugin wrapper /// -class Plugin +class Plugin : clap::helpers::Host { public: static std::shared_ptr createInstance(const clap_plugin_factory*, const std::string& id, @@ -132,10 +133,6 @@ class Plugin protected: // only the Clap::Library is allowed to create instances Plugin(IHost* host); - const clap_host_t* getClapHostInterface() - { - return &_host; - } void connectClap(const clap_plugin_t* clap); public: @@ -162,70 +159,70 @@ class Plugin ClapPluginExtensions _ext; const clap_plugin_t* _plugin = nullptr; - void log(clap_log_severity severity, const char* msg); - // threadcheck - bool is_main_thread() const; - bool is_audio_thread() const; + CLAP_NODISCARD Raise AlwaysAudioThread(); - // param - void param_rescan(clap_param_rescan_flags flags); - void param_clear(clap_id param, clap_param_clear_flags flags); - void param_request_flush(); + protected: + /////////////////////////////////// + // clap::helpers::Host overrides // + /////////////////////////////////// + + // clap_host + virtual void requestRestart() noexcept override; + virtual void requestProcess() noexcept override; + virtual void requestCallback() noexcept override; + + // clap_host_gui + virtual bool implementsGui() const noexcept override { return true; } + virtual void guiResizeHintsChanged() noexcept override {} + virtual bool guiRequestResize(uint32_t width, uint32_t height) noexcept override { + if (!_parentHost->gui_can_resize()) + return false; + + _parentHost->gui_request_resize(width, height); + return true; + } + virtual bool guiRequestShow() noexcept override { return _parentHost->gui_request_show(); } + virtual bool guiRequestHide() noexcept override { return false; } + virtual void guiClosed(bool /*wasDestroyed*/) noexcept override {} - // latency - void latency_changed(); + // clap_host_latency + virtual bool implementsLatency() const noexcept override { return true; } + virtual void latencyChanged() noexcept override; - // tail - void tail_changed(); + // clap_host_log + virtual bool implementsLog() const noexcept override { return true; } + virtual void logLog(clap_log_severity severity, const char *message) const noexcept override; - // hostgui - void resize_hints_changed() - { - } - bool request_resize(uint32_t width, uint32_t height) - { - if (_parentHost->gui_can_resize()) - { - _parentHost->gui_request_resize(width, height); - return true; - } - return false; - } - bool request_show() - { - return _parentHost->gui_request_show(); - } - bool request_hide() - { - return false; - } - void closed(bool was_destroyed) - { - } - - // clap_timer support - bool register_timer(uint32_t period_ms, clap_id* timer_id); - bool unregister_timer(clap_id timer_id); + // clap_host_params + virtual bool implementsParams() const noexcept override { return true; } + virtual void paramsRescan(clap_param_rescan_flags flags) noexcept override; + virtual void paramsClear(clap_id paramId, clap_param_clear_flags flags) noexcept override; + virtual void paramsRequestFlush() noexcept override; #if LIN - bool register_fd(int fd, clap_posix_fd_flags_t flags); - bool modify_fd(int fd, clap_posix_fd_flags_t flags); - bool unregister_fd(int fd); - + // clap_host_posix_fd_support + virtual bool implementsPosixFdSupport() const noexcept override { return true; } + virtual bool posixFdSupportRegisterFd(int fd, clap_posix_fd_flags_t flags) noexcept override; + virtual bool posixFdSupportModifyFd(int fd, clap_posix_fd_flags_t flags) noexcept override; + virtual bool posixFdSupportUnregisterFd(int fd) noexcept override; #endif - CLAP_NODISCARD Raise AlwaysAudioThread(); - private: - static const void* clapExtension(const clap_host* host, const char* extension); - static void clapRequestCallback(const clap_host* host); - static void clapRequestRestart(const clap_host* host); - static void clapRequestProcess(const clap_host* host); + // clap_host_tail + virtual bool implementsTail() const noexcept override { return true; } + virtual void tailChanged() noexcept override; - //static bool clapIsMainThread(const clap_host* host); - //static bool clapIsAudioThread(const clap_host* host); + // clap_host_timer_support + virtual bool implementsTimerSupport() const noexcept override { return true; } + virtual bool timerSupportRegisterTimer(uint32_t periodMs, clap_id *timerId) noexcept override; + virtual bool timerSupportUnregisterTimer(clap_id timerId) noexcept override; - clap_host_t _host; // the host_t structure for the proxy + // clap_host_thread_check + virtual bool implementsThreadCheck() const noexcept override { return true; } + virtual bool threadCheckIsMainThread() noexcept override; + virtual bool threadCheckIsAudioThread() noexcept override; + + private: IHost* _parentHost = nullptr; const std::thread::id _main_thread_id = std::this_thread::get_id(); std::atomic _audio_thread_override = 0;