Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
clap_plugin.h
1// SPDX-FileCopyrightText: © 2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include "plugins/plugin.h"
7
8#include <clap/clap.h>
9#include <clap/helpers/host.hh>
10
11namespace zrythm::plugins
12{
13
14using ClapHostBase = clap::helpers::Host<
15 clap::helpers::MisbehaviourHandler::Terminate,
16 clap::helpers::CheckingLevel::Maximal>;
17
23class ClapPlugin : public Plugin, public ClapHostBase
24{
25 Q_OBJECT
26 QML_ELEMENT
27 QML_UNCREATABLE ("")
28
29public:
33 using AudioThreadChecker = std::function<bool ()>;
34
47 StateDirectoryParentPathProvider state_path_provider,
48 AudioThreadChecker audio_thread_checker,
49 PluginHostWindowFactory host_window_factory,
50 QObject * parent = nullptr);
51 Z_DISABLE_COPY_MOVE (ClapPlugin)
52 ~ClapPlugin () override;
53
54 // clap_host
55 void requestRestart () noexcept override;
56 void requestProcess () noexcept override;
57 void requestCallback () noexcept override;
58
59 // clap_host_gui
60 bool implementsGui () const noexcept override { return true; }
61 void guiResizeHintsChanged () noexcept override;
62 bool guiRequestResize (uint32_t width, uint32_t height) noexcept override;
63 bool guiRequestShow () noexcept override;
64 bool guiRequestHide () noexcept override;
65 void guiClosed (bool wasDestroyed) noexcept override;
66
67 // clap_host_timer_support
68 bool implementsTimerSupport () const noexcept override { return true; }
69 bool timerSupportRegisterTimer (uint32_t periodMs, clap_id * timerId) noexcept
70 override;
71 bool timerSupportUnregisterTimer (clap_id timerId) noexcept override;
72
73 // clap_host_log
74 bool implementsLog () const noexcept override { return true; }
75 void logLog (clap_log_severity severity, const char * message)
76 const noexcept override;
77
78 // clap_host_latency
79 bool implementsLatency () const noexcept override { return false; }
80 void latencyChanged () noexcept override;
81
82 // clap_host_thread_check
83 bool threadCheckIsMainThread () const noexcept override;
84 bool threadCheckIsAudioThread () const noexcept override;
85
86 // clap_host_params
87 bool implementsParams () const noexcept override { return true; }
88 void paramsRescan (clap_param_rescan_flags flags) noexcept override;
89 void
90 paramsClear (clap_id paramId, clap_param_clear_flags flags) noexcept override;
91 void paramsRequestFlush () noexcept override;
92
93 // clap_host_posix_fd_support
94 bool implementsPosixFdSupport () const noexcept override { return true; }
95 bool posixFdSupportRegisterFd (int fd, clap_posix_fd_flags_t flags) noexcept
96 override;
97 bool
98 posixFdSupportModifyFd (int fd, clap_posix_fd_flags_t flags) noexcept override;
99 bool posixFdSupportUnregisterFd (int fd) noexcept override;
100
101 // clap_host_thread_pool
102 bool implementsThreadPool () const noexcept override { return true; }
103 bool threadPoolRequestExec (uint32_t numTasks) noexcept override;
104
105 // clap_host_state
106 bool implementsState () const noexcept override { return true; }
107 void stateMarkDirty () noexcept override;
108
109 // ============================================================================
110 // Plugin Interface Implementation
111 // ============================================================================
112
114
115 void save_state (std::optional<fs::path> abs_state_dir) override;
116 void load_state (std::optional<fs::path> abs_state_dir) override;
117
118protected:
119 void prepare_for_processing_impl (
120 units::sample_rate_t sample_rate,
121 nframes_t max_block_length) override;
122
123 void process_impl (EngineProcessTimeInfo time_info) noexcept override;
124
125 void release_resources_impl () override;
126
127Q_SIGNALS:
128 void paramsChanged ();
129 void paramAdjusted (clap_id paramId);
130 void pluginLoadedChanged (bool pluginLoaded);
131
132private Q_SLOTS:
136 void on_configuration_changed ();
137
141 void on_ui_visibility_changed ();
142
143private:
153 bool load_plugin (const fs::path &path, int64_t plugin_unique_id);
154 void unload_current_plugin ();
155
156 void create_ports_from_clap_plugin ();
157 void scanParams ();
158
159 void show_editor ();
160 void hide_editor ();
161
162 static constexpr auto kStateKey = "state"sv;
163 friend void to_json (nlohmann::json &j, const ClapPlugin &p)
164 {
165 to_json (j, static_cast<const Plugin &> (p));
166 }
167 friend void from_json (const nlohmann::json &j, ClapPlugin &p)
168 {
169 from_json (j, static_cast<Plugin &> (p));
170 }
171
172private:
173 class ClapPluginImpl;
174 std::unique_ptr<ClapPluginImpl> pimpl_;
175};
176
177} // namespace zrythm::plugins
CLAP-based plugin host implementation.
Definition clap_plugin.h:24
nframes_t get_single_playback_latency() const override
Returns the latency of only the given processable, without adding the previous/next latencies.
void load_state(std::optional< fs::path > abs_state_dir) override
Load the state from the default directory or from abs_state_dir if given.
ClapPlugin(dsp::ProcessorBase::ProcessorBaseDependencies dependencies, StateDirectoryParentPathProvider state_path_provider, AudioThreadChecker audio_thread_checker, PluginHostWindowFactory host_window_factory, QObject *parent=nullptr)
Constructor for JucePlugin.
std::function< bool()> AudioThreadChecker
Function that returns whether the caller is an audio DSP thread.
Definition clap_plugin.h:33
void save_state(std::optional< fs::path > abs_state_dir) override
Saves the state inside the standard state directory.
DSP processing plugin.
Definition plugin.h:30
Plugin(ProcessorBaseDependencies dependencies, StateDirectoryParentPathProvider state_path_provider, QObject *parent)
Creates/initializes a plugin and its internal plugin (LV2, etc.) using the given setting.
std::function< fs::path()> StateDirectoryParentPathProvider
Returns the parent path where the plugin should save its state directory in (or load it).
Definition plugin.h:48
uint32_t nframes_t
Frame count.
Definition types.h:58
Common struct to pass around during processing to avoid repeating the data in function arguments.
Definition types.h:133