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
#[macro_use]
mod util;
mod context;
mod factory;
mod inner;
mod note_expressions;
mod param_units;
pub mod subcategories;
mod view;
mod wrapper;
/// Re-export for the wrapper.
pub use factory::PluginInfo;
use nice_plug_core::plugin::Plugin;
pub use vst3;
pub use wrapper::Wrapper;
use crate::wrapper::vst3::subcategories::Vst3SubCategory;
/// Provides auxiliary metadata needed for a VST3 plugin.
pub trait Vst3Plugin: Plugin {
/// The unique class ID that identifies this particular plugin. You can use the
/// `*b"fooofooofooofooo"` syntax for this.
///
/// This will be shuffled into a different byte order on Windows for project-compatibility.
const VST3_CLASS_ID: [u8; 16];
/// One or more subcategories. The host may use these to categorize the plugin. Internally this
/// slice will be converted to a string where each character is separated by a pipe character
/// (`|`). This string has a limit of 127 characters, and anything longer than that will be
/// truncated.
const VST3_SUBCATEGORIES: &'static [Vst3SubCategory];
/// [`VST3_CLASS_ID`][Self::VST3_CLASS_ID`] in the correct order for the current platform so
/// projects and presets can be shared between platforms. This should not be overridden.
const PLATFORM_VST3_CLASS_ID: [u8; 16] = swap_vst3_uid_byte_order(Self::VST3_CLASS_ID);
}
#[cfg(not(target_os = "windows"))]
const fn swap_vst3_uid_byte_order(uid: [u8; 16]) -> [u8; 16] {
uid
}
#[cfg(target_os = "windows")]
const fn swap_vst3_uid_byte_order(mut uid: [u8; 16]) -> [u8; 16] {
// No mutable references in const functions, so we can't use `uid.swap()`
let original_uid = uid;
uid[0] = original_uid[3];
uid[1] = original_uid[2];
uid[2] = original_uid[1];
uid[3] = original_uid[0];
uid[4] = original_uid[5];
uid[5] = original_uid[4];
uid[6] = original_uid[7];
uid[7] = original_uid[6];
uid
}
/// Export one or more VST3 plugins from this library using the provided plugin types. The first
/// plugin's vendor information is used for the factory's information.
#[macro_export]
macro_rules! nice_export_vst3 {
($($plugin_ty:ty),+) => {
// Earlier versions used a simple generic struct for this, but because we don't have
// variadic generics (yet) we can't generate the struct for multiple plugin types without
// macros. So instead we'll generate the implementation ad-hoc inside of this macro.
#[doc(hidden)]
mod vst3 {
use ::std::collections::HashSet;
use ::std::ffi::c_void;
// `vst3` is imported from the VST3 wrapper module
use $crate::wrapper::vst3::{PluginInfo, Wrapper};
use $crate::wrapper::vst3::vst3::Steinberg::{kInvalidArgument, kResultOk, tresult, int32, FIDString, TUID};
use $crate::wrapper::vst3::vst3::Steinberg::{
PFactoryInfo_::FactoryFlags_, IPluginFactory, IPluginFactory2, IPluginFactory3, FUnknown,
PClassInfo, PClassInfo2, PClassInfoW, PFactoryInfo, IPluginFactoryTrait, IPluginFactory2Trait, IPluginFactory3Trait,
};
use $crate::wrapper::vst3::vst3::{Class, ComWrapper};
// Because the `$plugin_ty`s are likely defined in the enclosing scope. This works even
// if the types are not public because this is a child module.
use super::*;
// Sneaky way to get the number of expanded elements
const PLUGIN_COUNT: usize = [$(stringify!($plugin_ty)),+].len();
#[doc(hidden)]
pub struct Factory {
// This is a type erased version of the information stored on the plugin types
plugin_infos: [PluginInfo; PLUGIN_COUNT],
}
impl Class for Factory {
type Interfaces = (IPluginFactory, IPluginFactory2, IPluginFactory3);
}
impl Factory {
pub fn new() -> Self {
let plugin_infos = [$(PluginInfo::for_plugin::<$plugin_ty>()),+];
if cfg!(debug_assertions) {
let unique_cids: HashSet<[u8; 16]> = plugin_infos.iter().map(|d| *d.cid).collect();
$crate::nice_debug_assert_eq!(
unique_cids.len(),
plugin_infos.len(),
"Duplicate VST3 class IDs found in `nice_export_vst3!()` call"
);
}
Factory { plugin_infos }
}
}
impl IPluginFactoryTrait for Factory {
unsafe fn getFactoryInfo(&self, info: *mut PFactoryInfo) -> tresult {
if info.is_null() {
return kInvalidArgument;
}
// We'll use the first plugin's info for this
unsafe { *info = self.plugin_infos[0].create_factory_info(); }
kResultOk
}
unsafe fn countClasses(&self) -> int32 {
self.plugin_infos.len() as i32
}
unsafe fn getClassInfo(&self, index: int32, info: *mut PClassInfo) -> tresult {
if index < 0 || index >= self.plugin_infos.len() as i32 {
return kInvalidArgument;
}
unsafe { *info = self.plugin_infos[index as usize].create_class_info(); }
kResultOk
}
unsafe fn createInstance(
&self,
cid: FIDString,
iid: FIDString,
obj: *mut *mut c_void,
) -> tresult {
// Can't use `check_null_ptr!()` here without polluting nice-plug's general
// exports
if cid.is_null() || obj.is_null() {
return kInvalidArgument;
}
let cid = &*(cid as *const [u8; 16]);
// This is a poor man's way of treating `$plugin_ty` like an indexable array.
// Assuming `self.plugin_infos` is in the same order, we can simply check all of
// the registered plugin CIDs for matches using an unrolled loop.
let mut plugin_idx = 0;
$({
let plugin_info = &self.plugin_infos[plugin_idx];
if cid == plugin_info.cid {
let wrapper = ComWrapper::new(Wrapper::<$plugin_ty>::new());
let unknown = wrapper.as_com_ref::<FUnknown>().unwrap();
let ptr = unknown.as_ptr();
return ((*(*ptr).vtbl).queryInterface)(ptr, iid as *const TUID, obj);
}
plugin_idx += 1;
})+
kInvalidArgument
}
}
impl IPluginFactory2Trait for Factory {
unsafe fn getClassInfo2(&self, index: int32, info: *mut PClassInfo2) -> tresult {
if index < 0 || index >= self.plugin_infos.len() as i32 {
return kInvalidArgument;
}
unsafe { *info = self.plugin_infos[index as usize].create_class_info_2(); }
kResultOk
}
}
impl IPluginFactory3Trait for Factory {
unsafe fn getClassInfoUnicode(
&self,
index: int32,
info: *mut PClassInfoW,
) -> tresult {
if index < 0 || index >= self.plugin_infos.len() as i32 {
return kInvalidArgument;
}
unsafe { *info = self.plugin_infos[index as usize].create_class_info_unicode(); }
kResultOk
}
unsafe fn setHostContext(&self, _context: *mut FUnknown) -> tresult {
// We don't need to do anything with this
kResultOk
}
}
}
/// The VST3 plugin factory entry point.
#[unsafe(no_mangle)]
pub extern "system" fn GetPluginFactory() -> *mut ::std::ffi::c_void {
use $crate::wrapper::vst3::vst3::{ComWrapper, Steinberg::IPluginFactory};
ComWrapper::new(self::vst3::Factory::new())
.to_com_ptr::<IPluginFactory>()
.unwrap()
.into_raw() as *mut ::std::ffi::c_void
}
// These two entry points are used on Linux, and they would theoretically also be used on
// the BSDs:
// https://github.com/steinbergmedia/vst3_public_sdk/blob/c3948deb407bdbff89de8fb6ab8500ea4df9d6d9/source/main/linuxmain.cpp#L47-L52
#[allow(missing_docs)]
#[unsafe(no_mangle)]
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
pub extern "C" fn ModuleEntry(_lib_handle: *mut ::std::ffi::c_void) -> bool {
$crate::wrapper::setup_logger();
true
}
#[allow(missing_docs)]
#[unsafe(no_mangle)]
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
pub extern "C" fn ModuleExit() -> bool {
true
}
// These two entry points are used on macOS:
// https://github.com/steinbergmedia/vst3_public_sdk/blob/bc459feee68803346737901471441fd4829ec3f9/source/main/macmain.cpp#L60-L61
#[allow(missing_docs)]
#[unsafe(no_mangle)]
#[cfg(target_os = "macos")]
pub extern "C" fn bundleEntry(_lib_handle: *mut ::std::ffi::c_void) -> bool {
$crate::wrapper::setup_logger();
true
}
#[allow(missing_docs)]
#[unsafe(no_mangle)]
#[cfg(target_os = "macos")]
pub extern "C" fn bundleExit() -> bool {
true
}
// And these two entry points are used on Windows:
// https://github.com/steinbergmedia/vst3_public_sdk/blob/bc459feee68803346737901471441fd4829ec3f9/source/main/dllmain.cpp#L59-L60
#[allow(missing_docs)]
#[unsafe(no_mangle)]
#[cfg(target_os = "windows")]
pub extern "system" fn InitDll() -> bool {
$crate::wrapper::setup_logger();
true
}
#[allow(missing_docs)]
#[unsafe(no_mangle)]
#[cfg(target_os = "windows")]
pub extern "system" fn ExitDll() -> bool {
true
}
};
}