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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#![feature(libc, catch_panic)]
extern crate libc;
#[macro_use] extern crate bitflags;
extern crate vec_map;
#[doc(hidden)]
pub mod ffi;
#[doc(hidden)]
pub use ffi::ladspa_descriptor;
use ffi::ladspa;
use std::cell::{RefCell, RefMut};
use std::default::Default;
#[allow(improper_ctypes)]
extern {
pub fn get_ladspa_descriptor(index: u64) -> Option<PluginDescriptor>;
}
pub type Data = f32;
pub struct PluginDescriptor {
pub unique_id: u64,
pub label: &'static str,
pub properties: Properties,
pub name: &'static str,
pub maker: &'static str,
pub copyright: &'static str,
pub ports: Vec<Port>,
pub new: fn(desc: &PluginDescriptor, sample_rate: u64) -> Box<Plugin + Send>,
}
#[derive(Copy, Clone, Default)]
pub struct Port {
pub name: &'static str,
pub desc: PortDescriptor,
pub hint: Option<ControlHint>,
pub default: Option<DefaultValue>,
pub lower_bound: Option<Data>,
pub upper_bound: Option<Data>,
}
#[derive(Copy, Clone)]
pub enum PortDescriptor {
Invalid = 0,
AudioInput = (ladspa::PORT_AUDIO | ladspa::PORT_INPUT) as isize,
AudioOutput = (ladspa::PORT_AUDIO | ladspa::PORT_OUTPUT) as isize,
ControlInput = (ladspa::PORT_CONTROL | ladspa::PORT_INPUT) as isize,
ControlOutput = (ladspa::PORT_CONTROL | ladspa::PORT_OUTPUT) as isize,
}
impl Default for PortDescriptor {
fn default() -> PortDescriptor {
PortDescriptor::Invalid
}
}
bitflags!(
#[doc="Represents the special properties a control port may hold. These are merely hints as to the
use of the port and may be completely ignored by the host. For audio ports, use ```CONTROL_HINT_NONE```.
To attach multiple properties, bitwise-or them together.
See documentation for the constants beginning with HINT_ for the more information."]
flags ControlHint: i32 {
#[doc="Indicates that this is a toggled port. Toggled ports may only have default values
of zero or one, although the host may send any value, where <= 0 is false and > 0 is true."]
const HINT_TOGGLED = ladspa::HINT_TOGGLED,
#[doc="Indicates that all values related to the port will be multiplied by the sample rate by
the host before passing them to your plugin. This includes the lower and upper bounds. If you
want an upper bound of 22050 with this property and a sample rate of 44100, set the upper bound
to 0.5"]
const HINT_SAMPLE_RATE = ladspa::HINT_SAMPLE_RATE,
#[doc="Indicates that the data passed through this port would be better represented on a
logarithmic scale"]
const HINT_LOGARITHMIC = ladspa::HINT_LOGARITHMIC,
#[doc="Indicates that the data passed through this port should be represented as integers. Bounds
may be interpreted exclusively depending on the host"]
const HINT_INTEGER = ladspa::HINT_INTEGER,
}
);
#[derive(Copy, Clone)]
pub enum DefaultValue {
Minimum = ladspa::HINT_DEFAULT_MINIMUM as isize,
Low = ladspa::HINT_DEFAULT_LOW as isize,
Middle = ladspa::HINT_DEFAULT_MIDDLE as isize,
High = ladspa::HINT_DEFAULT_HIGH as isize,
Maximum = ladspa::HINT_DEFAULT_MAXIMUM as isize,
Value0 = ladspa::HINT_DEFAULT_0 as isize,
Value1 = ladspa::HINT_DEFAULT_1 as isize,
Value100 = ladspa::HINT_DEFAULT_100 as isize,
Value440 = ladspa::HINT_DEFAULT_440 as isize,
}
pub struct PortConnection<'a> {
pub port: Port,
pub data: PortData<'a>,
}
pub enum PortData<'a> {
AudioInput(&'a [Data]),
AudioOutput(RefCell<&'a mut [Data]>),
ControlInput(&'a Data),
ControlOutput(RefCell<&'a mut Data>),
}
unsafe impl<'a> Sync for PortData<'a> { }
impl<'a> PortConnection<'a> {
pub fn unwrap_audio(&'a self) -> &'a [Data] {
if let PortData::AudioInput(ref data) = self.data {
data
} else {
panic!("PortConnection::unwrap_audio called on a non audio input port!")
}
}
pub fn unwrap_audio_mut(&'a self) -> RefMut<'a, &'a mut [Data]> {
if let PortData::AudioOutput(ref data) = self.data {
data.borrow_mut()
} else {
panic!("PortConnection::unwrap_audio_mut called on a non audio output port!")
}
}
pub fn unwrap_control(&'a self) -> &'a Data {
if let PortData::ControlInput(data) = self.data {
data
} else {
panic!("PortConnection::unwrap_control called on a non control input port!")
}
}
pub fn unwrap_control_mut(&'a self) -> RefMut<'a, &'a mut Data> {
if let PortData::ControlOutput(ref data) = self.data {
data.borrow_mut()
} else {
panic!("PortConnection::unwrap_control called on a non control output port!")
}
}
}
bitflags!(
#[doc="Represents the special properties a LADSPA plugin can have.
To attach multiple properties, bitwise-or them together, for example
```PROP_REALTIME | PROP_INPLACE_BROKEN```.
See documentation for the constants beginning with PROP_ for the more information."]
flags Properties: i32 {
#[doc="No properties."]
const PROP_NONE = 0,
#[doc="Indicates that the plugin has a realtime dependency so it's output may not be cached."]
const PROP_REALTIME = ladspa::PROPERTY_REALTIME,
#[doc="Indicates that the plugin will not function correctly if the input and output audio
data has the same memory location. This could be an issue if you copy input to output
then refer back to previous values of the input as they will be overwritten. It is
recommended that you avoid using this flag if possible as it can decrease the speed of
the plugin."]
const PROP_INPLACE_BROKEN = ladspa::PROPERTY_INPLACE_BROKEN,
#[doc="Indicates that the plugin is capable of running not only in a conventional host but
also in a 'hard real-time' environment. To qualify for this the plugin must
satisfy all of the following:
* The plugin must not use malloc(), free() or other heap memory
management within its run() function. All new
memory used in run() must be managed via the stack. These
restrictions only apply to the run() function.
* The plugin will not attempt to make use of any library
functions with the exceptions of functions in the ANSI standard C
and C maths libraries, which the host is expected to provide.
* The plugin will not access files, devices, pipes, sockets, IPC
or any other mechanism that might result in process or thread
blocking.
* The plugin will take an amount of time to execute a run()
call approximately of form (A+B*SampleCount) where A
and B depend on the machine and host in use. This amount of time
may not depend on input signals or plugin state. The host is left
the responsibility to perform timings to estimate upper bounds for
A and B."]
const PROP_HARD_REALTIME_CAPABLE = ladspa::PROPERTY_HARD_RT_CAPABLE,
}
);
pub trait Plugin {
fn activate(&mut self) { }
fn run<'a>(&mut self, sample_count: usize, ports: &[&'a PortConnection<'a>]);
fn deactivate(&mut self) { }
}