conformal_vst_wrapper/
lib.rs

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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#![doc = include_str!("../docs_boilerplate.md")]
#![doc = include_str!("../README.md")]

use conformal_component::parameters::UNIQUE_ID_INTERNAL_PREFIX;
pub use conformal_ui::Size as UiSize;
use core::slice;

/// Contains information about the host.
///
/// You can use this to customize the comonent based on the host.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HostInfo {
    /// The name of the host.
    pub name: String,
}

/// A class ID for a VST3 component.
///
/// This must be _globally_ unique for each class
pub type ClassID = [u8; 16];

/// A component factory that can create a component.
///
/// This can return a specialized component based on information
/// about the current host
#[allow(clippy::module_name_repetitions)]
pub trait ComponentFactory: Clone {
    /// The type of component that this factory creates
    type Component;

    /// Create a component
    fn create(&self, host: &HostInfo) -> Self::Component;
}

impl<C, F: Fn(&HostInfo) -> C + Clone> ComponentFactory for F {
    type Component = C;
    fn create(&self, host_info: &HostInfo) -> C {
        (self)(host_info)
    }
}

/// Information about a VST3 component
#[derive(Debug, Clone, Copy)]
pub struct ClassInfo<'a> {
    /// User-visibile name of the component.
    pub name: &'a str,

    /// Class ID for the processor component.  This is used by the host to identify the VST.
    pub cid: ClassID,

    /// Class ID for the so-called "edit controller" component.  This is arbitrary
    /// but must be unique.
    pub edit_controller_cid: ClassID,

    /// Initial size of the UI in logical pixels
    pub ui_initial_size: UiSize,
}

#[doc(hidden)]
pub struct ParameterModel {
    pub parameter_infos: Box<dyn Fn(&HostInfo) -> Vec<conformal_component::parameters::Info>>,
}

#[doc(hidden)]
pub trait ClassCategory {
    fn create_processor(&self, controller_cid: ClassID) -> vst3::ComPtr<IPluginBase>;

    fn info(&self) -> &ClassInfo<'static>;

    fn category_str(&self) -> &'static str;

    fn create_parameter_model(&self) -> ParameterModel;

    fn get_kind(&self) -> edit_controller::Kind;
}

/// Information about a synth component
pub struct SynthClass<CF> {
    /// The actual factory.
    pub factory: CF,

    /// Information about the component
    pub info: ClassInfo<'static>,
}

fn create_parameter_model_internal<CF: ComponentFactory + 'static>(factory: CF) -> ParameterModel
where
    CF::Component: Component,
{
    ParameterModel {
        parameter_infos: Box::new(move |host_info| {
            let component = factory.create(host_info);
            component.parameter_infos()
        }),
    }
}

impl<CF: ComponentFactory + 'static> ClassCategory for SynthClass<CF>
where
    CF::Component: Component<Processor: Synth> + 'static,
{
    fn create_processor(&self, controller_cid: ClassID) -> vst3::ComPtr<IPluginBase> {
        vst3::ComWrapper::new(processor::create_synth(
            self.factory.clone(),
            controller_cid,
        ))
        .to_com_ptr::<IPluginBase>()
        .unwrap()
    }

    fn create_parameter_model(&self) -> ParameterModel {
        create_parameter_model_internal(self.factory.clone())
    }

    fn category_str(&self) -> &'static str {
        "Instrument|Synth"
    }

    fn info(&self) -> &ClassInfo<'static> {
        &self.info
    }

    fn get_kind(&self) -> edit_controller::Kind {
        edit_controller::Kind::Synth()
    }
}

/// Information about an effect component
pub struct EffectClass<CF> {
    /// The actual factory.
    pub factory: CF,

    /// Information about the component
    pub info: ClassInfo<'static>,

    /// The VST3 category for this effect
    /// See [here](https://steinbergmedia.github.io/vst3_doc/vstinterfaces/group__plugType.html)
    /// for a list of possible categories.
    pub category: &'static str,

    /// All effects must have a bypass parameter. This is the unique ID for that parameter.
    pub bypass_id: &'static str,
}

impl<CF: ComponentFactory<Component: Component<Processor: Effect> + 'static> + 'static>
    ClassCategory for EffectClass<CF>
{
    fn create_processor(&self, controller_cid: ClassID) -> vst3::ComPtr<IPluginBase> {
        vst3::ComWrapper::new(processor::create_effect(
            self.factory.clone(),
            controller_cid,
        ))
        .to_com_ptr::<IPluginBase>()
        .unwrap()
    }

    fn category_str(&self) -> &'static str {
        self.category
    }

    fn info(&self) -> &ClassInfo<'static> {
        &self.info
    }

    fn create_parameter_model(&self) -> ParameterModel {
        create_parameter_model_internal(self.factory.clone())
    }

    fn get_kind(&self) -> edit_controller::Kind {
        edit_controller::Kind::Effect {
            bypass_id: self.bypass_id,
        }
    }
}

/// General global infor about a vst plug-in
#[derive(Debug, Clone, Copy)]
pub struct Info<'a> {
    /// The "vendor" of the plug-in.
    ///
    /// Hosts often present plug-ins grouped by vendor.
    pub vendor: &'a str,

    /// The vendor's URL
    pub url: &'a str,

    /// The vendor's email
    pub email: &'a str,

    /// User-visibile version of components in this factory
    pub version: &'a str,
}

use conformal_component::Component;
use conformal_component::effect::Effect;
use conformal_component::synth::Synth;

use vst3::Steinberg::{IPluginBase, IPluginFactory2, IPluginFactory2Trait};
use vst3::{Class, Steinberg::IPluginFactory};

mod edit_controller;
mod factory;
mod host_info;
mod io;
mod mpe_quirks;
mod parameters;
mod processor;
mod view;

#[cfg(test)]
mod dummy_host;

#[cfg(test)]
mod fake_ibstream;

#[doc(hidden)]
pub fn _wrap_factory(
    classes: &'static [&'static dyn ClassCategory],
    info: Info<'static>,
) -> impl Class<Interfaces = (IPluginFactory, IPluginFactory2)> + 'static + IPluginFactory2Trait {
    factory::Factory::new(classes, info)
}

fn to_utf16(s: &str, buffer: &mut [i16]) {
    for (i, c) in s.encode_utf16().chain([0]).enumerate() {
        buffer[i] = c as i16;
    }
}

fn from_utf16_ptr(buffer: *const i16, max_size: usize) -> Option<String> {
    let mut len = 0;
    unsafe {
        while *buffer.add(len) != 0 {
            if len >= max_size {
                return None;
            }
            len += 1;
        }
    }
    let utf16_slice = unsafe { slice::from_raw_parts(buffer.cast(), len) };
    String::from_utf16(utf16_slice).ok()
}

fn from_utf16_buffer(buffer: &[i16]) -> Option<String> {
    let mut len = 0;
    for c in buffer {
        if *c == 0 {
            break;
        }
        len += 1;
    }
    let utf16_slice = unsafe { slice::from_raw_parts(buffer.as_ptr().cast(), len) };
    String::from_utf16(utf16_slice).ok()
}

fn should_include_parameter_in_snapshot(id: &str) -> bool {
    !id.starts_with(UNIQUE_ID_INTERNAL_PREFIX)
        && !conformal_component::synth::CONTROLLER_PARAMETERS
            .iter()
            .any(|p| id == p.unique_id)
}

/// Create a VST3-compatible plug-in entry point.
///
/// This is the main entry point for the VST3 Conformal Wrapper, and must
/// be invoked exactly once in each VST3 plug-in binary.
///
/// Note that each VST3 plug-in binary can contain _multiple_ components,
/// so this takes a slice of `EffectClass` and `SynthClass` instances.
///
/// Note that to create a loadable plug-in, you must add this to your
/// `Cargo.toml`:
///
/// ```toml
/// [lib]
/// crate-type = ["cdylib"]
/// ```
///
/// Conformal provides a template project that you can use to get started,
/// using `bun create conformal` script. This will provide a working example
/// of using the VST3 wrapper.
///
/// # Example
///
/// ```
/// use conformal_vst_wrapper::{ClassID, ClassInfo, EffectClass, HostInfo, Info};
/// use conformal_component::audio::{channels, channels_mut, Buffer, BufferMut};
/// use conformal_component::effect::Effect as EffectTrait;
/// use conformal_component::parameters::{self, BufferStates, Flags, InfoRef, TypeSpecificInfoRef};
/// use conformal_component::pzip;
/// use conformal_component::{Component as ComponentTrait, ProcessingEnvironment, Processor};
///
/// const PARAMETERS: [InfoRef<'static, &'static str>; 2] = [
///     InfoRef {
///         title: "Bypass",
///         short_title: "Bypass",
///         unique_id: "bypass",
///         flags: Flags { automatable: true },
///         type_specific: TypeSpecificInfoRef::Switch { default: false },
///     },
///     InfoRef {
///         title: "Gain",
///         short_title: "Gain",
///         unique_id: "gain",
///         flags: Flags { automatable: true },
///         type_specific: TypeSpecificInfoRef::Numeric {
///             default: 100.,
///             valid_range: 0f32..=100.,
///             units: Some("%"),
///         },
///     },
/// ];
///
/// #[derive(Clone, Debug, Default)]
/// pub struct Component {}
///
/// #[derive(Clone, Debug, Default)]
/// pub struct Effect {}
///
/// impl Processor for Effect {
///     fn set_processing(&mut self, _processing: bool) {}
/// }
///
/// impl EffectTrait for Effect {
///     fn handle_parameters<P: parameters::States>(&mut self, _: P) {}
///     fn process<P: BufferStates, I: Buffer, O: BufferMut>(
///         &mut self,
///         parameters: P,
///         input: &I,
///         output: &mut O,
///     ) {
///         for (input_channel, output_channel) in channels(input).zip(channels_mut(output)) {
///             for ((input_sample, output_sample), (gain, bypass)) in input_channel
///                 .iter()
///                 .zip(output_channel.iter_mut())
///                 .zip(pzip!(parameters[numeric "gain", switch "bypass"]))
///             {
///                 *output_sample = *input_sample * (if bypass { 1.0 } else { gain / 100.0 });
///             }
///         }
///     }
/// }
///
/// impl ComponentTrait for Component {
///     type Processor = Effect;
///
///     fn parameter_infos(&self) -> Vec<parameters::Info> {
///         parameters::to_infos(&PARAMETERS)
///     }
///
///     fn create_processor(&self, _env: &ProcessingEnvironment) -> Self::Processor {
///         Default::default()
///     }
/// }
///
/// // DO NOT USE this class ID, rather generate your own globally unique one.
/// const CID: ClassID = [
///   0x1d, 0x33, 0x78, 0xb8, 0xbd, 0xc9, 0x40, 0x8d, 0x86, 0x1f, 0xaf, 0xa4, 0xb5, 0x42, 0x5b, 0x74
/// ];
///
/// // DO NOT USE this class ID, rather generate your own globally unique one.
/// const EDIT_CONTROLLER_CID: ClassID = [
///   0x96, 0xa6, 0xd4, 0x7d, 0xb2, 0x73, 0x46, 0x7c, 0xb0, 0xd6, 0xea, 0x6a, 0xd0, 0x27, 0xb2, 0x6f
/// ];
///
/// conformal_vst_wrapper::wrap_factory!(
///     &const {
///         [&EffectClass {
///             info: ClassInfo {
///                 name: "My effect",
///                 cid: CID,
///                 edit_controller_cid: EDIT_CONTROLLER_CID,
///                 ui_initial_size: conformal_vst_wrapper::UiSize {
///                     width: 400,
///                     height: 400,
///                 },
///             },
///             factory: |_: &HostInfo| -> Component { Default::default() },
///             category: "Fx",
///             bypass_id: "bypass",
///         }]
///     },
///     Info {
///         vendor: "My vendor name",
///         url: "www.example.com",
///         email: "test@example.com",
///         version: "1.0.0",
///     }
/// );
/// ```
#[macro_export]
macro_rules! wrap_factory {
    ($CLASSES:expr, $INFO:expr) => {
        #[unsafe(no_mangle)]
        #[allow(non_snake_case, clippy::missing_safety_doc, clippy::missing_panics_doc)]
        pub unsafe extern "system" fn GetPluginFactory() -> *mut core::ffi::c_void {
            let factory = conformal_vst_wrapper::_wrap_factory($CLASSES, $INFO);
            vst3::ComWrapper::new(factory)
                .to_com_ptr::<vst3::Steinberg::IPluginFactory>()
                .unwrap()
                .into_raw()
                .cast()
        }

        /// This is required by the API [see here](https://steinbergmedia.github.io/vst3_dev_portal/pages/Technical+Documentation/VST+Module+Architecture/Index.html?highlight=GetPluginFactory#module-factory)
        #[cfg(target_os = "macos")]
        #[unsafe(no_mangle)]
        #[allow(non_snake_case)]
        pub extern "system" fn bundleEntry(_: *mut core::ffi::c_void) -> bool {
            true
        }

        /// This is required by the API [see here](https://steinbergmedia.github.io/vst3_dev_portal/pages/Technical+Documentation/VST+Module+Architecture/Index.html?highlight=GetPluginFactory#module-factory)
        #[cfg(target_os = "macos")]
        #[unsafe(no_mangle)]
        #[allow(non_snake_case)]
        pub extern "system" fn bundleExit() -> bool {
            true
        }
    };
}