Skip to main content

truce_loader/
lib.rs

1//! Hot-reloadable plugin logic for truce.
2//!
3//! Split your plugin into a static shell (loaded by the DAW) and a
4//! hot-reloadable logic dylib (reloads on recompile). The developer
5//! implements [`PluginLogic`] — a safe Rust trait — and exports it
6//! via `#[no_mangle]` functions. The shell loads the dylib, verifies
7//! ABI compatibility, and delegates audio processing + GUI rendering
8//! to the trait object.
9//!
10//! # For the logic dylib
11//!
12//! ```ignore
13//! use truce_loader::prelude::*;
14//!
15//! struct MyPlugin { /* ... */ }
16//! impl PluginLogic for MyPlugin { /* ... */ }
17//!
18//! #[no_mangle]
19//! pub fn truce_create() -> Box<dyn PluginLogic> { Box::new(MyPlugin::new()) }
20//!
21//! #[no_mangle]
22//! pub fn truce_abi_canary() -> AbiCanary { AbiCanary::current() }
23//!
24//! #[no_mangle]
25//! pub fn truce_vtable_probe() -> Box<dyn PluginLogic> { Box::new(ProbePlugin) }
26//! ```
27
28mod safe_types;
29mod traits;
30mod canary;
31
32#[cfg(feature = "shell")]
33mod loader;
34#[cfg(feature = "shell")]
35pub mod shell;
36pub mod static_shell;
37
38pub use safe_types::*;
39pub use traits::*;
40pub use canary::{AbiCanary, ProbePlugin, verify_probe};
41
42#[cfg(feature = "shell")]
43pub use loader::NativeLoader;
44
45/// Export the three `#[no_mangle]` functions required by the shell.
46///
47/// ```ignore
48/// struct MyPlugin { /* ... */ }
49/// impl PluginLogic for MyPlugin { /* ... */ }
50///
51/// export_plugin!(MyPlugin);
52/// ```
53#[macro_export]
54macro_rules! export_plugin {
55    ($ty:ty) => {
56        #[no_mangle]
57        pub fn truce_create() -> Box<dyn $crate::PluginLogic> {
58            Box::new(<$ty as $crate::PluginLogic>::new())
59        }
60
61        #[no_mangle]
62        pub fn truce_abi_canary() -> $crate::AbiCanary {
63            $crate::AbiCanary::current()
64        }
65
66        #[no_mangle]
67        pub fn truce_vtable_probe() -> Box<dyn $crate::PluginLogic> {
68            Box::new($crate::ProbePlugin)
69        }
70    };
71}
72
73/// Convenience prelude for logic dylib authors.
74pub mod prelude {
75    pub use crate::safe_types::*;
76    pub use crate::traits::*;
77    pub use crate::canary::{AbiCanary, ProbePlugin};
78
79    // Layout types from truce-gui
80    pub use truce_gui::layout::{PluginLayout, KnobRow, KnobDef, WidgetKind};
81
82    // Re-export param types so the developer can own params in their struct.
83    pub use truce_params::{Params, FloatParam, BoolParam, IntParam, EnumParam, ParamEnum};
84    pub use truce_params::{Smoother, SmoothingStyle};
85
86    // Re-export utility functions.
87    pub use truce_core::util::{db_to_linear, linear_to_db, midi_note_to_freq};
88}