Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
clap_plugin.h
1// SPDX-FileCopyrightText: © 2025-2026 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include "plugins/plugin.h"
7
8#include <QTimer>
9
10#include <clap/clap.h>
11#include <clap/helpers/host.hh>
12
13namespace zrythm::plugins
14{
15
16using ClapHostBase = clap::helpers::Host<
17 clap::helpers::MisbehaviourHandler::Terminate,
18 clap::helpers::CheckingLevel::Maximal>;
19
25class ClapPlugin : public Plugin, public ClapHostBase
26{
27 Q_OBJECT
28 QML_ELEMENT
29 QML_UNCREATABLE ("")
30
31public:
41 PluginHostWindowFactory host_window_factory,
42 QObject * parent = nullptr);
43 Q_DISABLE_COPY_MOVE (ClapPlugin)
44 ~ClapPlugin () override;
45
46 // clap_host
47 void requestRestart () noexcept override;
48 void requestProcess () noexcept override;
49 void requestCallback () noexcept override;
50
51 // clap_host_gui
52 bool implementsGui () const noexcept override { return true; }
53 void guiResizeHintsChanged () noexcept override;
54 bool guiRequestResize (uint32_t width, uint32_t height) noexcept override;
55 bool guiRequestShow () noexcept override;
56 bool guiRequestHide () noexcept override;
57 void guiClosed (bool wasDestroyed) noexcept override;
58
59 // clap_host_timer_support
60 bool implementsTimerSupport () const noexcept override { return true; }
61 bool timerSupportRegisterTimer (uint32_t periodMs, clap_id * timerId) noexcept
62 override;
63 bool timerSupportUnregisterTimer (clap_id timerId) noexcept override;
64
65 // clap_host_log
66 bool implementsLog () const noexcept override { return true; }
67 void logLog (clap_log_severity severity, const char * message)
68 const noexcept override;
69
70 // clap_host_latency
71 bool implementsLatency () const noexcept override { return false; }
72 void latencyChanged () noexcept override;
73
74 // clap_host_thread_check
75 bool threadCheckIsMainThread () const noexcept override;
76 bool threadCheckIsAudioThread () const noexcept override;
77
78 // clap_host_params
79 bool implementsParams () const noexcept override { return true; }
80 void paramsRescan (clap_param_rescan_flags flags) noexcept override;
81 void
82 paramsClear (clap_id paramId, clap_param_clear_flags flags) noexcept override;
83 void paramsRequestFlush () noexcept override;
84
85 // clap_host_posix_fd_support
86 bool implementsPosixFdSupport () const noexcept override { return true; }
87 bool posixFdSupportRegisterFd (int fd, clap_posix_fd_flags_t flags) noexcept
88 override;
89 bool
90 posixFdSupportModifyFd (int fd, clap_posix_fd_flags_t flags) noexcept override;
91 bool posixFdSupportUnregisterFd (int fd) noexcept override;
92
93 // clap_host_thread_pool
94 bool implementsThreadPool () const noexcept override { return true; }
95 bool threadPoolRequestExec (uint32_t numTasks) noexcept override;
96
97 // clap_host_state
98 bool implementsState () const noexcept override { return true; }
99 void stateMarkDirty () noexcept override;
100
101 // clap_host_preset_load
102 bool implementsPresetLoad () const noexcept override { return true; }
103 void presetLoadLoaded (
104 uint32_t locationKind,
105 const char * location,
106 const char * loadKey) noexcept override;
107 void presetLoadOnError (
108 uint32_t locationKind,
109 const char * location,
110 const char * loadKey,
111 int32_t osError,
112 const char * msg) noexcept override;
113
114 // ============================================================================
115 // Plugin Interface Implementation
116 // ============================================================================
117
118 units::sample_u32_t get_single_playback_latency () const override;
119
120protected:
121 void prepare_for_processing_impl (
122 units::sample_rate_t sample_rate,
123 units::sample_u32_t max_block_length) override;
124
125 void
126 process_impl (dsp::graph::EngineProcessTimeInfo time_info) noexcept override;
127
128 void release_resources_impl () override;
129
130Q_SIGNALS:
131 void paramsChanged ();
132 void paramAdjusted (clap_id paramId);
133 void pluginLoadedChanged (bool pluginLoaded);
134
135private Q_SLOTS:
139 void on_configuration_changed (
140 PluginConfiguration * config,
141 bool generateNewPluginPortsAndParams);
142
146 void on_ui_visibility_changed ();
147
148private:
158 bool load_plugin (
159 const std::filesystem::path &path,
160 int64_t plugin_unique_id,
161 bool generate_new_ports = true);
162 void unload_current_plugin ();
163
164 void create_ports_from_clap_plugin ();
165
166 void show_editor ();
167 void hide_editor ();
168
169 static constexpr auto kStateKey = "state"sv;
170 friend void to_json (nlohmann::json &j, const ClapPlugin &p);
171 friend void from_json (const nlohmann::json &j, ClapPlugin &p);
172
176 QByteArray save_state_to_byte_array () const;
177
178 std::string save_state_impl () const override;
179 void load_state_impl (const std::string &base64_state) override;
180
184 void apply_state_from_byte_array (const QByteArray &data);
185
186private:
187 class ClapPluginImpl;
188 std::unique_ptr<ClapPluginImpl> pimpl_;
189
191 std::optional<QByteArray> state_to_apply_;
192
202 std::unordered_map<dsp::ProcessorParameter *, clap_id> zrythm_to_clap_;
203};
204
205} // namespace zrythm::plugins
units::sample_u32_t get_single_playback_latency() const override
Returns the latency of only the given processable, without adding the previous/next latencies (zero l...
ClapPlugin(dsp::ProcessorBase::ProcessorBaseDependencies dependencies, PluginHostWindowFactory host_window_factory, QObject *parent=nullptr)
Constructor for ClapPlugin.
Configuration for instantiating a plugin descriptor.
Plugin(ProcessorBaseDependencies dependencies, QObject *parent)
Creates/initializes a plugin and its internal plugin (LV2, etc.) using the given setting.
Common struct to pass around during processing to avoid repeating the data in function arguments.
Definition graph_node.h:51