Loading...
Searching...
No Matches
score-plugin-clap/Clap/EffectModel.hpp
1#pragma once
2#include <Process/GenericProcessFactory.hpp>
3#include <Process/Process.hpp>
4#include <Process/ProcessMetadata.hpp>
5
6#include <Control/DefaultEffectItem.hpp>
7#include <Effect/EffectFactory.hpp>
8
10#include <score/serialization/JSONVisitor.hpp>
11
12#include <ossia/detail/flat_map.hpp>
13#include <ossia/detail/hash_map.hpp>
14
15#include <QSocketNotifier>
16
17#include <clap/all.h>
18
19#include <memory>
20#include <verdigris>
21
22namespace Clap
23{
24class Model;
25}
26
27PROCESS_METADATA(
28 , Clap::Model, "10e8d26d-1d82-4bfc-a9fb-d85ffdf04e5f", "CLAP", "CLAP",
29 Process::ProcessCategory::Other, "Plugins", "CLever Audio Plug-in", "ossia score",
30 {}, {}, {},
31 QUrl(
32 "https://ossia.io/score-docs/processes/"
33 "audio-plugins.html#common-formats-vst-vst3-lv2-jsfx"),
34 Process::ProcessFlags::ExternalEffect)
35DESCRIPTION_METADATA(, Clap::Model, "Clap")
36
37namespace Clap
38{
39class Window;
40class Model;
41struct PluginHandle
42{
43 PluginHandle();
44 PluginHandle(const PluginHandle&) = delete;
45 PluginHandle(PluginHandle&&) = delete;
46 PluginHandle& operator=(const PluginHandle&) = delete;
47 PluginHandle& operator=(PluginHandle&&) = delete;
48 ~PluginHandle();
49
50 void load(Model& context, QByteArray path, QByteArray id);
51 void* library{};
52 const clap_plugin_entry_t* entry{};
53 const clap_plugin_factory_t* factory{};
54 const clap_plugin_t* plugin{};
55 const clap_plugin_descriptor_t* desc{};
56
57 clap_host_t host{};
58
59 // Common plugin extensions
60 const clap_plugin_params_t* ext_params{};
61 const clap_plugin_timer_support_t* ext_timer_support{};
62
63 // Metadata we parsed
64 std::vector<clap_param_info_t> m_parameters_ins;
65 std::vector<clap_param_info_t> m_parameters_outs;
66 std::vector<clap_audio_port_info_t> m_audio_ins;
67 std::vector<clap_audio_port_info_t> m_audio_outs;
68 std::vector<clap_note_port_info_t> m_midi_ins;
69 std::vector<clap_note_port_info_t> m_midi_outs;
70
71 ossia::flat_map<clap_id, std::uint32_t> param_outs_by_id;
72
73 QPointer<Model> model;
74
75 bool activated{};
76};
77
78class Model final : public Process::ProcessModel
79{
80 W_OBJECT(Model)
81 SCORE_SERIALIZE_FRIENDS
82
83public:
84 MODEL_METADATA_IMPL(Model)
85
86 Model(
87 const TimeVal& duration, const QString& pluginPath,
88 const Id<Process::ProcessModel>& id, QObject* parent);
89
90 Model(DataStream::Deserializer& vis, QObject* parent);
91 Model(JSONObject::Deserializer& vis, QObject* parent);
92 Model(DataStream::Deserializer&& vis, QObject* parent);
93 Model(JSONObject::Deserializer&& vis, QObject* parent);
94
95 ~Model() override;
96
97 QString prettyShortName() const noexcept override
98 {
100 }
101 QString category() const noexcept override
102 {
104 }
105 QStringList tags() const noexcept override { return Metadata<Tags_k, Model>::get(); }
106 Process::ProcessFlags flags() const noexcept override;
107 bool hasExternalUI() const noexcept;
108 const std::shared_ptr<PluginHandle>& handle() const noexcept { return m_plugin; }
109
110 void closeUI() const;
111
112 const QString& pluginId() const noexcept { return m_pluginId; }
113 bool supports64() const noexcept { return m_supports64; }
114
115 const std::vector<clap_param_info_t>& parameterInputs() const noexcept
116 {
117 return m_plugin->m_parameters_ins;
118 }
119 const std::vector<clap_param_info_t>& parameterOutputs() const noexcept
120 {
121 return m_plugin->m_parameters_outs;
122 }
123 const std::vector<clap_note_port_info_t>& midiInputs() const noexcept
124 {
125 return m_plugin->m_midi_ins;
126 }
127 const std::vector<clap_note_port_info_t>& midiOutputs() const noexcept
128 {
129 return m_plugin->m_midi_outs;
130 }
131 const std::vector<clap_audio_port_info_t>& audioInputs() const noexcept
132 {
133 return m_plugin->m_audio_ins;
134 }
135 const std::vector<clap_audio_port_info_t>& audioOutputs() const noexcept
136 {
137 return m_plugin->m_audio_outs;
138 }
139
140 // Preset functionality
141 void loadPreset(const Process::Preset& preset) override;
142 Process::Preset savePreset() const noexcept override;
143 std::vector<Process::Preset> builtinPresets() const noexcept override;
144
145 void restartPlugin() W_SIGNAL(restartPlugin);
146 Clap::Window* window{};
147 std::vector<std::pair<clap_id, QTimer*>> timers;
148
149 struct FdNotifiers
150 {
151 std::unique_ptr<QSocketNotifier> read;
152 std::unique_ptr<QSocketNotifier> write;
153 std::unique_ptr<QSocketNotifier> error;
154 };
155 ossia::hash_map<int, std::unique_ptr<FdNotifiers>> fd_notifiers;
156
157 void requestFlush() W_SIGNAL(requestFlush);
158
159 void flushFromPluginToHost();
160
161 bool currentlyReadingValues{};
162
163 struct GestureState
164 {
165 QTimer* endTimer{};
166 bool active{};
167 void* cookie{};
168 };
169 ossia::hash_map<clap_id, GestureState> gestures;
170
171 void markStateDirty();
172 void stateSnapshotChanged(const QByteArray& blob)
173 W_SIGNAL(stateSnapshotChanged, blob);
174
175 void onParamRescan(clap_param_rescan_flags flags);
176
177private:
178 void loadPlugin();
179 void createControls(bool loading);
180
181 void refreshParamValues();
182 // Returns false on structural mismatch — caller should treat as RESCAN_ALL.
183 bool refreshParamInfoIfStable();
184 void setupControlInlet(
185 const clap_plugin_params_t&, const clap_param_info_t& info, int index,
187 void setupControlOutlet(
188 const clap_plugin_params_t&, const clap_param_info_t& info, int index,
190
191 void sendParamChange(const clap_param_info_t& info, double value);
192 void endGesture(clap_id param_id, void* cookie);
193
194 QTimer* m_stateDirtyTimer{};
195
196 QString m_pluginPath;
197 QString m_pluginId;
198
199 std::shared_ptr<PluginHandle> m_plugin;
200 QByteArray m_loadedState;
201
202 bool m_supports64{};
203
204 clap_param_rescan_flags m_pendingRescanFlags{};
205};
206}
207
208namespace Process
209{
210template <>
211QString EffectProcessFactory_T<Clap::Model>::customConstructionData() const noexcept;
212
213template <>
214Process::Descriptor
215EffectProcessFactory_T<Clap::Model>::descriptor(QString d) const noexcept;
216}
217
218namespace Clap
219{
220class Window;
223}
Definition DataStreamVisitor.hpp:202
Definition JSONVisitor.hpp:423
Definition Port.hpp:206
Definition Port.hpp:427
Definition EffectFactory.hpp:86
Definition EffectFactory.hpp:14
The Process class.
Definition score-lib-process/Process/Process.hpp:62
The id_base_t class.
Definition Identifier.hpp:59
Base classes and tools to implement processes and layers.
Definition JSONVisitor.hpp:1115
ProcessFlags
Various settings for processes.
Definition ProcessFlags.hpp:17
STL namespace.
Static metadata implementation.
Definition lib/score/tools/Metadata.hpp:36
Definition Preset.hpp:32
Definition TimeValue.hpp:21