2022-01-26 22:37:45 +11:00
|
|
|
// nih-plug: plugins, but rewritten in Rust
|
|
|
|
// Copyright (C) 2022 Robbert van der Helm
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2022-02-07 04:46:16 +11:00
|
|
|
use std::ffi::c_void;
|
2022-01-27 04:14:54 +11:00
|
|
|
use std::marker::PhantomData;
|
2022-02-09 08:28:18 +11:00
|
|
|
use std::mem;
|
|
|
|
use vst3_sys::base::{kInvalidArgument, kNoInterface, kResultOk, tresult};
|
|
|
|
use vst3_sys::base::{IPluginFactory, IPluginFactory2, IPluginFactory3};
|
2022-01-30 06:54:52 +11:00
|
|
|
use vst3_sys::VST3;
|
2022-01-27 04:14:54 +11:00
|
|
|
|
2022-02-07 04:51:46 +11:00
|
|
|
mod context;
|
2022-02-07 03:50:15 +11:00
|
|
|
mod inner;
|
2022-02-07 03:40:35 +11:00
|
|
|
#[macro_use]
|
|
|
|
mod util;
|
2022-02-07 04:46:16 +11:00
|
|
|
mod view;
|
2022-02-09 08:28:18 +11:00
|
|
|
mod wrapper;
|
2022-02-07 03:40:35 +11:00
|
|
|
|
2022-02-09 08:28:18 +11:00
|
|
|
use self::wrapper::Wrapper;
|
|
|
|
use crate::plugin::Vst3Plugin;
|
|
|
|
use crate::wrapper::util::{strlcpy, u16strlcpy};
|
2022-01-27 10:19:50 +11:00
|
|
|
|
|
|
|
// Alias needed for the VST3 attribute macro
|
|
|
|
use vst3_sys as vst3_com;
|
2022-01-26 22:37:45 +11:00
|
|
|
|
2022-01-27 05:19:20 +11:00
|
|
|
/// Re-export for the wrapper.
|
|
|
|
pub use vst3_sys::sys::GUID;
|
|
|
|
|
2022-01-27 08:23:44 +11:00
|
|
|
/// The VST3 SDK version this is roughtly based on.
|
2022-01-27 10:19:50 +11:00
|
|
|
const VST3_SDK_VERSION: &str = "VST 3.6.14";
|
2022-02-06 04:41:41 +11:00
|
|
|
|
2022-01-30 00:20:14 +11:00
|
|
|
#[doc(hidden)]
|
2022-01-27 08:23:44 +11:00
|
|
|
#[VST3(implements(IPluginFactory, IPluginFactory2, IPluginFactory3))]
|
2022-01-28 08:13:13 +11:00
|
|
|
pub struct Factory<P: Vst3Plugin> {
|
2022-01-27 04:14:54 +11:00
|
|
|
/// The type will be used for constructing plugin instances later.
|
|
|
|
_phantom: PhantomData<P>,
|
|
|
|
}
|
|
|
|
|
2022-01-28 08:13:13 +11:00
|
|
|
impl<P: Vst3Plugin> Factory<P> {
|
|
|
|
pub fn new() -> Box<Self> {
|
2022-02-07 10:19:05 +11:00
|
|
|
Self::allocate(PhantomData::default())
|
2022-01-27 04:14:54 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-28 08:13:13 +11:00
|
|
|
impl<P: Vst3Plugin> IPluginFactory for Factory<P> {
|
2022-01-27 04:14:54 +11:00
|
|
|
unsafe fn get_factory_info(&self, info: *mut vst3_sys::base::PFactoryInfo) -> tresult {
|
2022-01-27 05:57:56 +11:00
|
|
|
*info = mem::zeroed();
|
|
|
|
|
|
|
|
let info = &mut *info;
|
2022-01-29 00:20:16 +11:00
|
|
|
strlcpy(&mut info.vendor, P::VENDOR);
|
2022-01-27 05:57:56 +11:00
|
|
|
strlcpy(&mut info.url, P::URL);
|
|
|
|
strlcpy(&mut info.email, P::EMAIL);
|
|
|
|
info.flags = vst3_sys::base::FactoryFlags::kUnicode as i32;
|
|
|
|
|
|
|
|
kResultOk
|
2022-01-27 04:14:54 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn count_classes(&self) -> i32 {
|
2022-01-27 05:57:56 +11:00
|
|
|
// We don't do shell plugins, and good of an idea having separated components and edit
|
|
|
|
// controllers in theory is, few software can use it, and doing that would make our simple
|
|
|
|
// microframework a lot less simple
|
|
|
|
1
|
2022-01-27 04:14:54 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn get_class_info(&self, index: i32, info: *mut vst3_sys::base::PClassInfo) -> tresult {
|
2022-01-27 05:57:56 +11:00
|
|
|
if index != 0 {
|
|
|
|
return kInvalidArgument;
|
|
|
|
}
|
|
|
|
|
|
|
|
*info = mem::zeroed();
|
|
|
|
|
|
|
|
let info = &mut *info;
|
2022-02-07 10:19:05 +11:00
|
|
|
info.cid.data = P::VST3_CLASS_ID;
|
2022-01-27 05:57:56 +11:00
|
|
|
info.cardinality = vst3_sys::base::ClassCardinality::kManyInstances as i32;
|
|
|
|
strlcpy(&mut info.category, "Audio Module Class");
|
|
|
|
strlcpy(&mut info.name, P::NAME);
|
|
|
|
|
|
|
|
kResultOk
|
2022-01-27 04:14:54 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn create_instance(
|
|
|
|
&self,
|
2022-01-27 07:12:02 +11:00
|
|
|
cid: *const vst3_sys::IID,
|
|
|
|
_iid: *const vst3_sys::IID,
|
|
|
|
obj: *mut *mut vst3_sys::c_void,
|
2022-01-27 04:14:54 +11:00
|
|
|
) -> tresult {
|
2022-01-28 23:45:17 +11:00
|
|
|
check_null_ptr!(cid, obj);
|
2022-01-28 08:51:29 +11:00
|
|
|
|
2022-02-07 10:19:05 +11:00
|
|
|
if (*cid).data != P::VST3_CLASS_ID {
|
2022-01-27 07:12:02 +11:00
|
|
|
return kNoInterface;
|
|
|
|
}
|
|
|
|
|
2022-02-01 06:18:12 +11:00
|
|
|
*obj = Box::into_raw(Wrapper::<P>::new()) as *mut vst3_sys::c_void;
|
2022-01-27 07:12:02 +11:00
|
|
|
|
|
|
|
kResultOk
|
2022-01-27 04:14:54 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-28 08:13:13 +11:00
|
|
|
impl<P: Vst3Plugin> IPluginFactory2 for Factory<P> {
|
2022-01-27 08:23:44 +11:00
|
|
|
unsafe fn get_class_info2(
|
|
|
|
&self,
|
|
|
|
index: i32,
|
|
|
|
info: *mut vst3_sys::base::PClassInfo2,
|
|
|
|
) -> tresult {
|
|
|
|
if index != 0 {
|
|
|
|
return kInvalidArgument;
|
|
|
|
}
|
|
|
|
|
|
|
|
*info = mem::zeroed();
|
|
|
|
|
|
|
|
let info = &mut *info;
|
2022-02-07 10:19:05 +11:00
|
|
|
info.cid.data = P::VST3_CLASS_ID;
|
2022-01-27 08:23:44 +11:00
|
|
|
info.cardinality = vst3_sys::base::ClassCardinality::kManyInstances as i32;
|
|
|
|
strlcpy(&mut info.category, "Audio Module Class");
|
|
|
|
strlcpy(&mut info.name, P::NAME);
|
|
|
|
info.class_flags = 1 << 1; // kSimpleModeSupported
|
|
|
|
strlcpy(&mut info.subcategories, P::VST3_CATEGORIES);
|
|
|
|
strlcpy(&mut info.vendor, P::VENDOR);
|
|
|
|
strlcpy(&mut info.version, P::VERSION);
|
|
|
|
strlcpy(&mut info.sdk_version, VST3_SDK_VERSION);
|
|
|
|
|
|
|
|
kResultOk
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-28 08:13:13 +11:00
|
|
|
impl<P: Vst3Plugin> IPluginFactory3 for Factory<P> {
|
2022-01-27 08:23:44 +11:00
|
|
|
unsafe fn get_class_info_unicode(
|
|
|
|
&self,
|
|
|
|
index: i32,
|
|
|
|
info: *mut vst3_sys::base::PClassInfoW,
|
|
|
|
) -> tresult {
|
|
|
|
if index != 0 {
|
|
|
|
return kInvalidArgument;
|
|
|
|
}
|
|
|
|
|
|
|
|
*info = mem::zeroed();
|
|
|
|
|
|
|
|
let info = &mut *info;
|
2022-02-07 10:19:05 +11:00
|
|
|
info.cid.data = P::VST3_CLASS_ID;
|
2022-01-27 08:23:44 +11:00
|
|
|
info.cardinality = vst3_sys::base::ClassCardinality::kManyInstances as i32;
|
|
|
|
strlcpy(&mut info.category, "Audio Module Class");
|
|
|
|
u16strlcpy(&mut info.name, P::NAME);
|
|
|
|
info.class_flags = 1 << 1; // kSimpleModeSupported
|
|
|
|
strlcpy(&mut info.subcategories, P::VST3_CATEGORIES);
|
|
|
|
u16strlcpy(&mut info.vendor, P::VENDOR);
|
|
|
|
u16strlcpy(&mut info.version, P::VERSION);
|
|
|
|
u16strlcpy(&mut info.sdk_version, VST3_SDK_VERSION);
|
|
|
|
|
|
|
|
kResultOk
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn set_host_context(&self, _context: *mut c_void) -> tresult {
|
|
|
|
// We don't need to do anything with this
|
|
|
|
kResultOk
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-28 08:13:13 +11:00
|
|
|
/// Export a VST3 plugin from this library using the provided plugin type.
|
2022-01-26 22:37:45 +11:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! nih_export_vst3 {
|
2022-01-28 08:13:13 +11:00
|
|
|
($plugin_ty:ty) => {
|
2022-01-27 04:14:54 +11:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "system" fn GetPluginFactory() -> *mut ::std::ffi::c_void {
|
2022-01-28 08:13:13 +11:00
|
|
|
let factory = ::nih_plug::wrapper::vst3::Factory::<$plugin_ty>::new();
|
2022-01-27 04:14:54 +11:00
|
|
|
|
|
|
|
Box::into_raw(factory) as *mut ::std::ffi::c_void
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't need any special initialization logic, so all of these module entry point
|
|
|
|
// functions just return true all the time
|
|
|
|
|
2022-01-26 22:37:45 +11:00
|
|
|
// 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
|
|
|
|
#[no_mangle]
|
|
|
|
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
|
2022-01-27 04:14:54 +11:00
|
|
|
pub extern "C" fn ModuleEntry(_lib_handle: *mut ::std::ffi::c_void) -> bool {
|
2022-01-26 22:37:45 +11:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
|
|
|
#[no_mangle]
|
|
|
|
#[cfg(target_os = "macos")]
|
2022-01-27 04:14:54 +11:00
|
|
|
pub extern "C" fn bundleEntry(_lib_handle: *mut ::std::ffi::c_void) -> bool {
|
2022-01-26 22:37:45 +11:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
|
|
|
#[no_mangle]
|
|
|
|
#[cfg(target_os = "windows")]
|
2022-02-07 09:37:40 +11:00
|
|
|
pub extern "system" fn InitDll() -> bool {
|
2022-01-26 22:37:45 +11:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
#[cfg(target_os = "windows")]
|
2022-02-07 09:37:40 +11:00
|
|
|
pub extern "system" fn ExitDll() -> bool {
|
2022-01-26 22:37:45 +11:00
|
|
|
true
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|