1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//! Core types and traits to implement a Clack plugin.
//!
//! The [`PluginAudioProcessor`] trait is the main one required to be implemented for a Clack plugin. It
//! can also be associated to two more types, implementing [`PluginMainThread`] and [`PluginShared`],
//! following the CLAP thread model, as described below.
//!
//! # Thread model
//!
//! CLAP's thread model for plugins is split into three classes of operations: those happening in an
//! audio processing thread, those happening in the main thread, and thread-safe operations:
//!
//! * The *audio thread* (`[audio-thread]` in the CLAP specification): this is represented by a type
//! implementing the main [`PluginAudioProcessor`] trait (also named the audio processor), which is [`Send`] but
//! [`!Sync`](Sync), and is the only one required to implement a Clack plugin.
//!
//! This type handles all DSP in one of the host's audio threads, of which there may be
//! multiple, if the host uses a thread pool for example.
//!
//! The host is free to [`Send`] the [`PluginAudioProcessor`] type between any of its audio threads, but any
//! operation of this class is guaranteed to be exclusive (`&mut`) to a single audio thread.
//!
//! One exception is for CLAP plugins' activation and deactivation (represented in Clack by the
//! plugin type's construction and destruction), which is guaranteed to happen in the Main Thread
//! instead. This allows the plugin's [`activate`](PluginAudioProcessor::activate) and
//! [`deactivate`](PluginAudioProcessor::deactivate) methods to receive temporary exclusive references to the
//! main thread type during its construction and destruction.
//!
//! * The *main thread* (`[main-thread]` in the CLAP specification): this is represented by a type
//! implementing the [`PluginMainThread`] trait, which is neither [`Send`] nor [`Sync`]. If
//! main-thread operations are not needed by a plugin implementation, `()` can be used instead.
//!
//! This type can handle all non-thread-safe operations, such as those related to GUI handling,
//! however extensions can extend its use to more kinds of operations.
//!
//! The host cannot [`Send`] this type to any other threads, and this type has to be constructed
//! before any other operation is done on the plugin.
//!
//! * *Thread-safe operations* (`[thread-safe]` in the CLAP specification) are represented by a type
//! implementing the [`PluginShared`] trait, which is both [`Send`] and [`Sync`], and will be
//! shared between the main thread and the audio thread. If it isn't needed , `()` can be used
//! instead.
//!
//! It can be used to hold read-only data (such as all the detected host extensions), or to
//! hold any other kind of synchronized state.
//!
//! However, it should be noted that this type *can* be used by the host simultaneously from
//! threads that are neither the main thread nor the audio thread.
use cratePluginExtensions;
use crateHostAudioProcessorHandle;
use crate;
pub
pub use PluginError;
pub use *;
pub use *;
/// The part of the data and operations of a plugin that are thread-safe.
///
/// The associated lifetime `'a` represents the lifetime of the plugin itself, as well as the
/// lifetime of the data exposed by the host.
///
/// This type requires to be both [`Send`] and [`Sync`]: it can be used simultaneously by multiple
/// threads, including (but not limited to) the main thread and the audio thread.
///
/// See the [module documentation](crate::plugin) for more information on the thread model.
/// The part of the data and operation of a plugin that must be on the main thread.
///
/// The associated lifetime `'a` represents the lifetime of the plugin itself, as well as the
/// lifetime of the data exposed by the host.
///
/// This type requires neither [`Send`] nor [`Sync`]: it is guaranteed to stay on the main thread
/// at all times.
///
/// See the [module documentation](crate::plugin) for more information on the thread model.
/// The main trait required to implement a CLAP plugin.
///
/// Types implementing this trait are never instantiated and are not meant to contain any data.
///
/// Their purpose is to tie together the various subtypes that will be instantiated and live across
/// different threads.
///
/// # Plugin instantiation
///
/// Plugin instantiation and initialization is not covered by this trait. The [`Shared`] and
/// [`MainThread`] components are created from a [`PluginFactory`] implementation
/// (in [`PluginFactory::create_plugin`]) using the [`PluginInstance::new`] method.
///
/// Alternatively, if custom [`PluginFactory`] and [`Entry`] implementations are not needed, one
/// can use a [`SinglePluginEntry`] and implement its companion trait [`DefaultPluginFactory`]
/// to implement the instantiation instead.
///
/// [`Shared`]: Self::Shared
/// [`MainThread`]: Self::MainThread
/// [`PluginFactory`]: crate::factory::plugin::PluginFactoryImpl
/// [`PluginFactory::create_plugin`]: crate::factory::plugin::PluginFactoryImpl::create_plugin
/// [`Entry`]: crate::entry::Entry
/// [`SinglePluginEntry`]: crate::entry::SinglePluginEntry
/// [`DefaultPluginFactory`]: crate::entry::DefaultPluginFactory
/// The audio processor and main part of a plugin.
///
/// This type implements all DSP-related operations, most notably [`process`](PluginAudioProcessor::process),
/// which processes all input and output audio and events.
///
/// The associated lifetime `'a` represents the lifetime of the plugin itself, as well as the
/// lifetime of the data exposed by the host.
///
/// This type requires to be [`Send`] but not [`Sync`]: it can be sent between any of the host's
/// threads, but none of its operations will be performed on multiple threads simultaneously.
///
/// The audio processor can also define two associated types, [`Shared`](Plugin::Shared) and
/// [`MainThread`](Plugin::MainThread), allowing to execute operations in and hold data belonging
/// to other threads. If they are not needed, the empty `()` type can be used instead, for convenience.
///
/// See the [module documentation](crate::plugin) for more information on the thread model.