diff --git a/crates/au-host/src/ffi.rs b/crates/au-host/src/ffi.rs index a1152449..f7b801e3 100644 --- a/crates/au-host/src/ffi.rs +++ b/crates/au-host/src/ffi.rs @@ -369,6 +369,7 @@ pub struct Instance { max_frames: usize, sample_rate: f64, sample_time: f64, + link: std::sync::Arc, } // SAFETY: an Audio Unit instance may be used from any thread as long as calls are not @@ -405,6 +406,7 @@ impl Instance { outputs: Vec::new(), abl: AudioBufferList { number_buffers: 0, buffers: [empty; MAX_AU_CHANNELS] }, has_input, + link: std::sync::Arc::new(StateLink(std::sync::Mutex::new(LinkedUnit(Some(unit))))), initialized: false, channels: 0, max_frames: 0, @@ -678,35 +680,12 @@ impl Instance { out } - /// The unit's `ClassInfo` as a binary property list. - pub fn save_state(&self) -> Option> { - let mut plist: CFTypeRef = ptr::null(); - let mut size = std::mem::size_of::() as u32; - // SAFETY: the property is a `CFPropertyListRef` returned +1; `plist` is a local pointer. - let st = unsafe { AudioUnitGetProperty(self.unit, PROP_CLASS_INFO, SCOPE_GLOBAL, 0, (&mut plist as *mut CFTypeRef).cast(), &mut size) }; - if st != 0 || plist.is_null() { - release(plist); - return None; - } - let mut err: CFTypeRef = ptr::null(); - // SAFETY: `plist` is a live property list; `err` is a local out pointer. - let data = unsafe { CFPropertyListCreateData(ptr::null(), plist, CF_PROPERTY_LIST_BINARY_FORMAT, 0, &mut err) }; - release(err); - release(plist); - if data.is_null() { - return None; - } - // SAFETY: `data` is a live CFData we own; its byte pointer is valid for its length. - let out = unsafe { - let len = CFDataGetLength(data); - let p = CFDataGetBytePtr(data); - if len <= 0 || len as usize > MAX_STATE || p.is_null() { None } else { Some(std::slice::from_raw_parts(p, len as usize).to_vec()) } - }; - release(data); - out + /// The link for state handles. + pub fn state_link(&self) -> std::sync::Arc { + std::sync::Arc::clone(&self.link) } - /// Restores a blob from [`Instance::save_state`] (hostile input: parsed by CoreFoundation, + /// Restores a blob from [`StateLink::save_state`] (hostile input: parsed by CoreFoundation, /// must be a dictionary, and the unit validates it). pub fn load_state(&self, bytes: &[u8]) -> bool { if bytes.is_empty() || bytes.len() > MAX_STATE { @@ -733,8 +712,61 @@ impl Instance { } } +struct LinkedUnit(Option); + +// SAFETY: the pointer is only used under the link's mutex, while the instance keeps the unit alive. +unsafe impl Send for LinkedUnit {} + +/// The link between an instance and its state handles: the unit's state is read on the main +/// thread while the instance renders on the audio thread (hosts make property calls from other +/// threads during rendering). The instance clears the link under its lock before it disposes of +/// the unit, so a handle never reads a dead unit. +pub(crate) struct StateLink(std::sync::Mutex); + +impl StateLink { + fn lock(&self) -> std::sync::MutexGuard<'_, LinkedUnit> { + self.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner) + } + + /// The unit's `ClassInfo` as a binary property list (`None` once the instance is gone). + pub fn save_state(&self) -> Option> { + let g = self.lock(); + let unit = g.0?; + let mut plist: CFTypeRef = ptr::null(); + let mut size = std::mem::size_of::() as u32; + // SAFETY: the unit is live while the link holds it (see the type); the property is a + // `CFPropertyListRef` returned +1; `plist` is a local pointer. + let st = unsafe { AudioUnitGetProperty(unit, PROP_CLASS_INFO, SCOPE_GLOBAL, 0, (&mut plist as *mut CFTypeRef).cast(), &mut size) }; + if st != 0 || plist.is_null() { + release(plist); + return None; + } + let mut err: CFTypeRef = ptr::null(); + // SAFETY: `plist` is a live property list; `err` is a local out pointer. + let data = unsafe { CFPropertyListCreateData(ptr::null(), plist, CF_PROPERTY_LIST_BINARY_FORMAT, 0, &mut err) }; + release(err); + release(plist); + if data.is_null() { + return None; + } + // SAFETY: `data` is a live CFData we own; its byte pointer is valid for its length. + let out = unsafe { + let len = CFDataGetLength(data); + let p = CFDataGetBytePtr(data); + if len <= 0 || len as usize > MAX_STATE || p.is_null() { None } else { Some(std::slice::from_raw_parts(p, len as usize).to_vec()) } + }; + release(data); + out + } + + fn kill(&self) { + self.lock().0 = None; + } +} + impl Drop for Instance { fn drop(&mut self) { + self.link.kill(); // SAFETY: the unit is live (disposed only here); after disposal nothing can call the // input callback, so the leaked input state can be freed. unsafe { diff --git a/crates/au-host/src/plugin.rs b/crates/au-host/src/plugin.rs index c76aab00..eed7fc21 100644 --- a/crates/au-host/src/plugin.rs +++ b/crates/au-host/src/plugin.rs @@ -237,10 +237,6 @@ impl Plugin for AuPlugin { self.push_event(MidiEvent { time: 0, status: 0xB0, data1: 123, data2: 0 }); } - fn save_state(&mut self) -> Option> { - self.inst.save_state() - } - fn load_state(&mut self, data: &[u8]) -> bool { let ok = self.inst.load_state(data); if ok { @@ -248,4 +244,9 @@ impl Plugin for AuPlugin { } ok } + + fn state_reader(&mut self) -> Option> { + let link = self.inst.state_link(); + Some(Box::new(move || link.save_state())) + } } diff --git a/crates/au-host/tests/system_units.rs b/crates/au-host/tests/system_units.rs index 2e246758..85c4f080 100644 --- a/crates/au-host/tests/system_units.rs +++ b/crates/au-host/tests/system_units.rs @@ -100,6 +100,35 @@ fn lowpass_attenuates_highs() { assert!(run_tone(p.as_mut(), 100.0).is_finite()); } +#[test] +fn state_reader_reads_while_another_thread_renders() { + if !available(LOWPASS) { + return; + } + let info = soundcraft_au_host::plugin_info(LOWPASS).unwrap(); + let cutoff = info.params.iter().find(|q| q.unit == soundcraft_dsp::Unit::Hz).unwrap(); + let mut p = soundcraft_au_host::create(LOWPASS).unwrap(); + p.prepare(48_000.0, 512, 2); + assert!(p.set_param(cutoff.id, 500.0)); + let mut reader = p.state_reader().expect("every unit has a ClassInfo"); + let audio = std::thread::spawn(move || { + for _ in 0..4 { + run_tone(p.as_mut(), 100.0); + } + p + }); + while !audio.is_finished() { + assert!(reader.save_state().is_some()); + } + let p = audio.join().unwrap(); + let state = reader.save_state().expect("ClassInfo"); + drop(p); + let mut q = soundcraft_au_host::create(LOWPASS).unwrap(); + assert_eq!(reader.save_state(), None); + assert!(q.load_state(&state)); + assert!((q.param(cutoff.id).unwrap() - 500.0).abs() < 1.0, "{:?}", q.param(cutoff.id)); +} + #[test] fn delay_runs_mono_stereo_and_odd_blocks() { if !available(DELAY) { diff --git a/crates/clap-host/src/ffi.rs b/crates/clap-host/src/ffi.rs index 16aaf33f..69cdeac6 100644 --- a/crates/clap-host/src/ffi.rs +++ b/crates/clap-host/src/ffi.rs @@ -275,7 +275,7 @@ impl Bundle { inst.state_ext = inst.extension::(CLAP_EXT_STATE); let gui = inst.extension::(CLAP_EXT_GUI); inst.gui = Arc::new(GuiLink { - inner: Mutex::new(GuiInner { plugin, gui, alive: !gui.is_null(), created: false, visible: false }), + inner: Mutex::new(GuiInner { plugin, gui, state: inst.state_ext, alive: true, created: false, visible: false }), host: Arc::clone(&inst.state), }); Ok(inst) @@ -696,17 +696,19 @@ fn window_api() -> Option<&'static CStr> { struct GuiInner { plugin: *const clap_plugin, gui: *const clap_plugin_gui, + state: *const clap_plugin_state, /// The plugin instance exists (cleared by `Instance::drop` before it destroys the plugin). alive: bool, created: bool, visible: bool, } -/// The link between an instance and its editor handles. The instance may process on the audio -/// thread while the editor is driven from the main thread: CLAP allows `clap.gui` (main-thread) -/// calls concurrently with audio-thread calls. Every GUI call happens under this lock, and the -/// instance clears `alive` (destroying any GUI) under the same lock before it destroys the -/// plugin, so a handle can never call into a destroyed plugin. +/// The link between an instance and its editor and state handles. The instance may process on +/// the audio thread while the editor is driven and the state saved from the main thread: CLAP +/// allows `clap.gui` and `clap.state` (main-thread) calls concurrently with audio-thread calls. +/// Every such call happens under this lock, and the instance clears `alive` (destroying any GUI) +/// under the same lock before it destroys the plugin, so a handle can never call into a destroyed +/// plugin. pub(crate) struct GuiLink { inner: Mutex, host: Arc, @@ -715,7 +717,14 @@ pub(crate) struct GuiLink { impl Default for GuiLink { fn default() -> Self { GuiLink { - inner: Mutex::new(GuiInner { plugin: std::ptr::null(), gui: std::ptr::null(), alive: false, created: false, visible: false }), + inner: Mutex::new(GuiInner { + plugin: std::ptr::null(), + gui: std::ptr::null(), + state: std::ptr::null(), + alive: false, + created: false, + visible: false, + }), host: Arc::new(HostState::default()), } } @@ -731,7 +740,8 @@ impl GuiLink { } pub fn available(&self) -> bool { - self.lock().alive + let g = self.lock(); + g.alive && !g.gui.is_null() } pub fn host(&self) -> &HostState { @@ -805,6 +815,22 @@ impl GuiLink { g.visible = false; } + /// `clap_plugin_state.save` into memory (`None` without the extension, on failure, or once the + /// plugin is gone). + pub fn save_state(&self) -> Option> { + let g = self.lock(); + if !g.alive { + return None; + } + // SAFETY: `state` is null or the plugin's static extension vtable, valid while `alive`. + let save = unsafe { g.state.as_ref() }?.save?; + let mut out: Vec = Vec::new(); + let stream = clap_ostream { ctx: (&mut out as *mut Vec).cast::(), write: Some(ostream_write) }; + // SAFETY: live plugin (`alive`, under the lock); the stream and its context live on this + // frame for the call. + unsafe { save(g.plugin, &stream) }.then_some(out) + } + /// Closes (destroys) the editor. pub fn close(&self) { if !on_main_thread() { @@ -1012,19 +1038,14 @@ impl Instance { self.gui.available().then(|| Arc::clone(&self.gui)) } - /// `clap_plugin_state.save` into memory (`None` without the extension or on failure). - pub fn save_state(&self) -> Option> { - // SAFETY: the extension pointer, when non-null, points to a static vtable in the plugin. - let save = unsafe { self.state_ext.as_ref() }?.save?; - let mut out: Vec = Vec::new(); - let stream = clap_ostream { ctx: (&mut out as *mut Vec).cast::(), write: Some(ostream_write) }; - // SAFETY: valid instance; the stream and its context live on this frame for the call. - unsafe { save(self.plugin, &stream) }.then_some(out) + /// The same link, for state handles (`None` when the plugin has no `clap.state`). + pub fn state_link(&self) -> Option> { + (!self.state_ext.is_null()).then(|| Arc::clone(&self.gui)) } /// `clap_plugin_state.load` from memory. `false` without the extension or when refused. pub fn load_state(&self, data: &[u8]) -> bool { - // SAFETY: see `save_state`. + // SAFETY: the extension pointer, when non-null, points to a static vtable in the plugin. let Some(load) = (unsafe { self.state_ext.as_ref() }).and_then(|s| s.load) else { return false }; let mut cursor = ReadCursor { data, pos: 0 }; let stream = clap_istream { ctx: (&mut cursor as *mut ReadCursor<'_>).cast::(), read: Some(istream_read) }; diff --git a/crates/clap-host/src/plugin.rs b/crates/clap-host/src/plugin.rs index 6fe1abcf..dff5d429 100644 --- a/crates/clap-host/src/plugin.rs +++ b/crates/clap-host/src/plugin.rs @@ -256,10 +256,6 @@ impl Plugin for ClapPlugin { } } - fn save_state(&mut self) -> Option> { - self.inst.save_state() - } - fn load_state(&mut self, data: &[u8]) -> bool { let ok = self.inst.load_state(data); if ok { @@ -283,6 +279,11 @@ impl Plugin for ClapPlugin { let link = self.inst.gui()?; Some(Box::new(ClapEditor { link, info: self.info, ids: self.ids.clone() })) } + + fn state_reader(&mut self) -> Option> { + let link = self.inst.state_link()?; + Some(Box::new(move || link.save_state())) + } } /// A CLAP plugin's floating editor, driven from the main thread (see [`GuiLink`]). diff --git a/crates/clap-host/tests/fixture_e2e.rs b/crates/clap-host/tests/fixture_e2e.rs index 22164875..10cb04b9 100644 --- a/crates/clap-host/tests/fixture_e2e.rs +++ b/crates/clap-host/tests/fixture_e2e.rs @@ -146,11 +146,36 @@ fn state_saves_and_restores_into_a_new_instance() { // Plugins without clap.state. let mut s = soundcraft_clap_host::create(SYNTH).unwrap(); assert!(s.save_state().is_none()); + assert!(s.state_reader().is_none()); assert!(!s.load_state(&blob)); assert!(s.editor().is_none(), "the synth has no GUI"); assert!(s.open_editor().is_err()); } +#[test] +fn state_reader_reads_while_another_thread_processes() { + fixture_dir(); + let mut p = soundcraft_clap_host::create(GAIN).unwrap(); + p.prepare(48_000.0, 64, 2); + assert!(p.set_param("7", 1.5)); + p.process(&mut vec![vec![0.5f32; 64]; 2], 64); + let mut reader = p.state_reader().expect("the gain fixture has clap.state"); + let blob = p.save_state().unwrap(); + let audio = std::thread::spawn(move || { + for _ in 0..2_000 { + p.process(&mut vec![vec![0.5f32; 64]; 2], 64); + } + p + }); + while !audio.is_finished() { + assert_eq!(reader.save_state().as_ref(), Some(&blob)); + } + let p = audio.join().unwrap(); + assert_eq!(reader.save_state(), Some(blob)); + drop(p); + assert_eq!(reader.save_state(), None); +} + #[test] fn editors_refuse_to_open_off_the_main_thread() { fixture_dir(); diff --git a/crates/dsp/src/lib.rs b/crates/dsp/src/lib.rs index 3d5bd331..f0ca05ad 100644 --- a/crates/dsp/src/lib.rs +++ b/crates/dsp/src/lib.rs @@ -206,11 +206,12 @@ pub trait Plugin: Send { fn set_sample(&mut self, _sample: Option>) -> bool { false } - /// The plugin's complete internal state as an opaque blob (third-party plugins), for saving - /// in the session. `None` when the plugin has no state beyond its parameters (built-ins) or - /// the plugin refused. May allocate: never call it in steady-state audio processing. + /// The plugin's complete internal state as an opaque blob (third-party plugins), as its + /// [`Plugin::state_reader`] reads it. `None` when the plugin has no state beyond its + /// parameters (built-ins) or the plugin refused. May allocate: never call it in steady-state + /// audio processing. fn save_state(&mut self) -> Option> { - None + self.state_reader()?.save_state() } /// Restores a blob from [`Plugin::save_state`]. `false` if unsupported or the plugin rejected /// it (the plugin keeps working with its previous state). The data is hostile input. @@ -229,6 +230,10 @@ pub trait Plugin: Send { fn editor(&mut self) -> Option> { None } + /// Reads this instance's state from the UI thread (`None` when it has no state of its own). + fn state_reader(&mut self) -> Option> { + None + } } /// A plugin's editor (GUI), driven from the main (UI) thread. Obtained from @@ -249,6 +254,19 @@ pub trait PluginEditor: Send { fn set_param(&mut self, _id: &str, _value: f32) {} } +/// Reads a plugin's state on the main (UI) thread while the instance processes on the audio +/// thread, as plugin formats allow. Obtained from [`Plugin::state_reader`]; closures are readers. +pub trait PluginStateReader: Send { + /// What [`Plugin::save_state`] would return; `None` once the instance is gone. + fn save_state(&mut self) -> Option>; +} + +impl Option> + Send> PluginStateReader for F { + fn save_state(&mut self) -> Option> { + self() + } +} + /// Recorded audio a sample-playing instrument reads: planar `f32` at its own sample rate (the /// instrument converts rates as it plays). Implementations must be cheap to read from the audio /// thread: no locks, no I/O. diff --git a/crates/engine/src/cmd/plugin_state.rs b/crates/engine/src/cmd/plugin_state.rs index b3ca0d3d..e999c863 100644 --- a/crates/engine/src/cmd/plugin_state.rs +++ b/crates/engine/src/cmd/plugin_state.rs @@ -1,6 +1,6 @@ //! Third-party plugin state in the session (`mix.insert_state`): the UI reads each live plugin's -//! state from the audio engine right before saving and stores it on its insert (base64), from -//! where the mixer restores it into new instances. +//! state right before saving and stores it on its insert (base64), from where the mixer restores +//! it into new instances. use super::*; use serde_json::json; diff --git a/crates/engine/tests/clap_e2e.rs b/crates/engine/tests/clap_e2e.rs index 9a559f18..404252c0 100644 --- a/crates/engine/tests/clap_e2e.rs +++ b/crates/engine/tests/clap_e2e.rs @@ -92,13 +92,11 @@ fn clap_state_is_captured_stored_and_restored_before_the_first_block() { p.set_param("7", 1.5); let mut io = vec![vec![0.0f32; 64]]; p.process(&mut io, 64); + let mut reader = p.state_reader().unwrap(); eng.adopt(vec![soundcraft_mix::PreparedInstance { track: t, slot: 0, id: gain.into(), ch: 1, plugin: p }]); eng.ensure_synced(e.session()); - let states = eng.capture_states(); - assert_eq!(states.len(), 1); - let (tt, slot, blob) = &states[0]; - assert_eq!((*tt, *slot), (t, 0)); - e.execute("mix.insert_state", &json!({"track": t.0, "slot": slot, "state": soundcraft_model::b64::encode(blob)})).unwrap(); + let blob = reader.save_state().unwrap(); + e.execute("mix.insert_state", &json!({"track": t.0, "slot": 0, "state": soundcraft_model::b64::encode(&blob)})).unwrap(); assert_eq!(eng.take_retired().len(), 0); // Offline render (synchronous creation): the state is restored before the first block. diff --git a/crates/mix/src/lib.rs b/crates/mix/src/lib.rs index 82690ab3..87a35643 100644 --- a/crates/mix/src/lib.rs +++ b/crates/mix/src/lib.rs @@ -39,9 +39,8 @@ //! //! An instance starts from its stored state; the insert's parameter values are then applied on //! top before the first block (the session's values are kept in step with the plugin, including -//! edits made in its own editor, so both agree; the state carries everything else). -//! [`MixEngine::capture_states`] reads every live instance's state back (for saving the session); -//! it calls into the plugins and allocates, so a realtime host runs it only on request. +//! edits made in its own editor, so both agree; the state carries everything else). A host reads +//! states back for saving through each instance's [`Plugin::state_reader`], on its UI thread. //! //! A built-in instrument's parameter values are applied the same way. A sample-playing //! instrument (the Sampler) is handed the decoded audio of its insert's @@ -71,8 +70,8 @@ pub fn main_channels(s: &Session) -> usize { s.main_format().channels().clamp(MAIN_CHANNELS, MAX_CHANNELS) } -/// Slot index used for a track's instrument plugin in [`InstanceSpec`], [`PreparedInstance`] and -/// [`MixEngine::capture_states`] (insert slots use their index). +/// Slot index used for a track's instrument plugin in [`InstanceSpec`] and [`PreparedInstance`] +/// (insert slots use their index). pub const INSTRUMENT_SLOT: usize = usize::MAX; /// Hosted third-party plugin ids (CLAP or VST3): created off the audio thread in realtime use. @@ -431,24 +430,6 @@ impl MixEngine { out } - /// The state of every live plugin that has one (third-party plugins), as - /// `(track, slot or INSTRUMENT_SLOT, state)`. Calls into the plugins and allocates. - pub fn capture_states(&mut self) -> Vec<(TrackId, usize, Vec)> { - let mut out = Vec::new(); - for (tid, st) in self.strips.iter_mut() { - for (i, slot) in st.plugins.iter_mut().enumerate() { - if let Some(data) = slot.as_mut().and_then(|s| s.plugin.save_state()) { - out.push((*tid, i, data)); - } - } - if let Some(data) = st.instrument.as_mut().and_then(|s| s.plugin.save_state()) { - out.push((*tid, INSTRUMENT_SLOT, data)); - } - } - out.sort_by_key(|(t, s, _)| (*t, *s)); - out - } - pub fn max_block(&self) -> usize { self.max_block } @@ -2136,10 +2117,9 @@ mod tests { let _ = render_range(&s, Range::new(0, 4800), 512); } - /// A built-in gain behind a third-party id, with a state blob and a log of parameter sets. + /// A built-in gain behind a third-party id, with a log of parameter sets. struct Stateful { inner: Box, - blob: Vec, sets: Arc, } @@ -2163,9 +2143,6 @@ mod tests { fn process(&mut self, io: &mut [Vec], frames: usize) { self.inner.process(io, frames); } - fn save_state(&mut self) -> Option> { - Some(self.blob.clone()) - } } fn render_block(eng: &mut MixEngine, s: &Session, pos: i64) -> f32 { @@ -2175,7 +2152,7 @@ mod tests { } #[test] - fn external_instances_are_adopted_retired_and_captured() { + fn external_instances_are_adopted_and_retired() { let (mut s, t) = session_with_dc(0.5, 48_000); let mut ins = Insert::new("clap:test.fake"); ins.params.insert("gain".into(), -6.0); @@ -2191,9 +2168,9 @@ mod tests { let sets = Arc::new(std::sync::atomic::AtomicUsize::new(0)); let mut gain = soundcraft_dsp::create("gain").unwrap(); gain.prepare(48_000.0, 512, 1); - let fake = Stateful { inner: gain, blob: b"live state".to_vec(), sets: Arc::clone(&sets) }; + let fake = Stateful { inner: gain, sets: Arc::clone(&sets) }; // An instance for the wrong channel count is retired, not used. - let wrong = Stateful { inner: soundcraft_dsp::create("gain").unwrap(), blob: Vec::new(), sets: Arc::clone(&sets) }; + let wrong = Stateful { inner: soundcraft_dsp::create("gain").unwrap(), sets: Arc::clone(&sets) }; eng.adopt(vec![PreparedInstance { track: t, slot: 0, id: "clap:test.fake".into(), ch: 2, plugin: Box::new(wrong) }]); assert!(render_block(&mut eng, &s, 512) > 0.3); assert_eq!(eng.take_retired().len(), 1); @@ -2204,13 +2181,11 @@ mod tests { } assert!((wet / dry - 0.501).abs() < 0.01, "{wet} vs {dry}"); assert!(sets.load(std::sync::atomic::Ordering::Relaxed) > 0, "no stored state: session params are pushed"); - assert_eq!(eng.capture_states(), vec![(t, 0, b"live state".to_vec())]); assert!(eng.take_retired().is_empty()); // Removing the insert retires the instance instead of dropping it here. s.track_mut(t).unwrap().mixer.inserts[0] = None; render_block(&mut eng, &s, 20 * 512); assert_eq!(eng.take_retired().len(), 1); - assert!(eng.capture_states().is_empty()); // Instances nobody claims are retired too; drain_plugins empties the engine. eng.adopt(vec![PreparedInstance { track: t, slot: 3, id: "clap:x".into(), ch: 1, plugin: soundcraft_dsp::create("gain").unwrap() }]); render_block(&mut eng, &s, 21 * 512); @@ -2229,7 +2204,7 @@ mod tests { let mut eng = MixEngine::new(48_000.0, 512); eng.set_external_instances(true); let sets = Arc::new(std::sync::atomic::AtomicUsize::new(0)); - let fake = Stateful { inner: soundcraft_dsp::create("gain").unwrap(), blob: Vec::new(), sets: Arc::clone(&sets) }; + let fake = Stateful { inner: soundcraft_dsp::create("gain").unwrap(), sets: Arc::clone(&sets) }; eng.adopt(vec![PreparedInstance { track: t, slot: 0, id: "vst3:00000000000000000000000000000001".into(), ch: 1, plugin: Box::new(fake) }]); render_block(&mut eng, &s, 0); assert_eq!(sets.load(std::sync::atomic::Ordering::Relaxed), 1, "session params apply once"); diff --git a/crates/playback/src/lib.rs b/crates/playback/src/lib.rs index bd63c382..c079eced 100644 --- a/crates/playback/src/lib.rs +++ b/crates/playback/src/lib.rs @@ -16,15 +16,14 @@ //! state and prepared on the thread that calls [`Player::update_session`] (the UI thread), and //! travel to the audio thread together with the session snapshot that needs them; the mix //! engine adopts them (see `soundcraft_mix`'s crate docs). Instances the engine retires come back -//! on a return channel and are dropped on the UI thread ([`Player::idle`]). Plugin states for -//! saving are read on request ([`Player::capture_states`]: a command with a reply; the audio -//! thread answers between two blocks, never blocking). Plugin editors are driven on the UI thread +//! on a return channel and are dropped on the UI thread ([`Player::idle`]). Plugin editors are +//! driven, and plugin states for saving read ([`Player::capture_states`]), on the UI thread //! through handles taken from each instance before it leaves ([`Player::open_editor`]). #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] pub mod record; -use soundcraft_dsp::{Plugin, PluginEditor}; +use soundcraft_dsp::{Plugin, PluginEditor, PluginStateReader}; use soundcraft_mix::{MAX_CHANNELS, MixEngine, PreparedInstance, StripMeter, itu_stereo_fold, main_channels}; use soundcraft_model::{Session, TrackId}; use soundcraft_time::{Range, Samples}; @@ -55,8 +54,6 @@ pub type EditorEdit = (TrackId, usize, String, f32); enum Cmd { /// A new snapshot plus the third-party instances it needs (created on the UI thread). Session(Arc, Vec), - /// Read every plugin state and reply. - CaptureStates(Sender), Input(Arc), Recording(bool), Play { @@ -136,10 +133,6 @@ impl AudioState { match c { Cmd::Input(r) => self.input = Some(r), Cmd::Recording(r) => self.recording = r, - Cmd::CaptureStates(reply) => { - self.mix.ensure_synced(&self.session); - let _ = reply.send(self.mix.capture_states()); - } Cmd::Session(s, instances) => { changed = true; if (s.sample_rate.as_f64() - self.session.sample_rate.as_f64()).abs() > 0.5 { @@ -180,8 +173,8 @@ impl AudioState { } } if changed { - // Place adopted instances now (not only once playback starts), so state captures and - // editors see them; `sync` creates no third-party plugins here. + // Place adopted instances now (not only once playback starts), so editors see them; + // `sync` creates no third-party plugins here. self.mix.ensure_synced(&self.session); } self.send_garbage(); @@ -325,10 +318,16 @@ struct Side { rate: f32, /// Editor handles of the live instances. editors: HashMap<(TrackId, usize), Box>, + /// State readers of the live instances. + states: HashMap<(TrackId, usize), Box>, garbage: Receiver>>, } impl Side { + fn new(rate: f32, garbage: Receiver>>) -> Side { + Side { shadow: HashMap::new(), rate, editors: HashMap::new(), states: HashMap::new(), garbage } + } + /// Drops retired instances (closing their editors first, inside the plugin's drop). fn collect_garbage(&mut self) { while let Ok(g) = self.garbage.try_recv() { @@ -338,6 +337,7 @@ impl Side { fn forget(&mut self, key: (TrackId, usize)) { self.shadow.remove(&key); + self.states.remove(&key); if let Some(mut e) = self.editors.remove(&key) { e.close(); } @@ -376,7 +376,7 @@ impl Player { fn open(session: Arc) -> Player { let (gtx, grx) = channel(); - let side = Mutex::new(Side { shadow: HashMap::new(), rate: session.sample_rate.as_f64() as f32, editors: HashMap::new(), garbage: grx }); + let side = Mutex::new(Side::new(session.sample_rate.as_f64() as f32, grx)); let shared = Arc::new(Shared { position: AtomicI64::new(0), playing: AtomicBool::new(false), @@ -525,22 +525,9 @@ impl Player { let _ = self.tx.send(Cmd::Session(s, instances)); } - /// Reads the state of every live third-party plugin (for saving the session). Waits briefly - /// for the audio thread to answer between two blocks; empty when it does not answer. + /// Reads the state of every live third-party plugin, for saving the session. pub fn capture_states(&self) -> PluginStates { - self.side().collect_garbage(); - #[cfg(not(target_arch = "wasm32"))] - { - let (tx, rx) = channel(); - if self.tx.send(Cmd::CaptureStates(tx)).is_err() { - return Vec::new(); - } - rx.recv_timeout(std::time::Duration::from_secs(2)).unwrap_or_default() - } - #[cfg(target_arch = "wasm32")] - { - Vec::new() - } + self.side().capture_states() } /// Per-UI-frame housekeeping: drops retired instances and services plugin editors. Returns @@ -656,6 +643,12 @@ fn copy_meter_snapshot(source: &MeterSnapshot, out: &mut MeterSnapshot) { } impl Side { + fn capture_states(&mut self) -> PluginStates { + let mut out: PluginStates = self.states.iter_mut().filter_map(|(&(t, slot), r)| Some((t, slot, r.save_state()?))).collect(); + out.sort_by_key(|(t, slot, _)| (*t, *slot)); + out + } + /// Creates the third-party instances `s` needs that the audio thread does not have yet. fn prepare(&mut self, s: &Session) -> Vec { self.collect_garbage(); @@ -686,6 +679,9 @@ impl Side { if let Some(e) = plugin.editor() { self.editors.insert(key, e); } + if let Some(r) = plugin.state_reader() { + self.states.insert(key, r); + } out.push(PreparedInstance { track: spec.track, slot: spec.slot, id: spec.id, ch: spec.ch, plugin }); } } @@ -881,12 +877,8 @@ mod tests { let mut st = AudioState::new(rx, Arc::clone(&shared), Arc::new(soundcraft_model::Session::default()), 48_000.0, gtx); let plugin: Box = Box::new(Stateful(soundcraft_dsp::create("gain").unwrap())); tx.send(Cmd::Session(Arc::clone(&s), vec![PreparedInstance { track: t, slot: 1, id: "clap:test.fake".into(), ch: 2, plugin }])).unwrap(); - let (rtx, rrx) = channel(); - tx.send(Cmd::CaptureStates(rtx)).unwrap(); - // Stopped: the callback only drains commands (adopting the instance) and answers. let mut out = vec![0.0f32; 256]; st.fill(&mut out, 2); - assert_eq!(rrx.try_recv().unwrap(), vec![(t, 1, b"blob".to_vec())]); assert!(grx.try_recv().is_err(), "nothing retired yet"); // A session without the insert retires the instance to the return channel. tx.send(Cmd::Session(Arc::new(soundcraft_model::Session::default()), Vec::new())).unwrap(); @@ -912,11 +904,32 @@ mod tests { assert_eq!(grx.try_recv().unwrap().len(), 1); } + struct Reads(Option<&'static [u8]>); + + impl PluginStateReader for Reads { + fn save_state(&mut self) -> Option> { + self.0.map(<[u8]>::to_vec) + } + } + + #[test] + fn plugin_states_are_read_without_the_audio_thread() { + let mut side = Side::new(48_000.0, channel().1); + let (a, b) = (TrackId(1), TrackId(2)); + side.states.insert((b, 0), Box::new(Reads(Some(b"b0")))); + side.states.insert((a, soundcraft_mix::INSTRUMENT_SLOT), Box::new(Reads(Some(b"synth")))); + side.states.insert((a, 3), Box::new(Reads(Some(b"a3")))); + side.states.insert((a, 4), Box::new(Reads(None))); + let want = vec![(a, 3, b"a3".to_vec()), (a, soundcraft_mix::INSTRUMENT_SLOT, b"synth".to_vec()), (b, 0, b"b0".to_vec())]; + assert_eq!(side.capture_states(), want); + side.forget((a, 3)); + assert_eq!(side.capture_states(), want[1..]); + } + #[test] fn side_creates_each_missing_instance_once() { use soundcraft_model::{ChannelFormat, Insert, TrackKind}; - let (_gtx, grx) = channel(); - let mut side = Side { shadow: HashMap::new(), rate: 48_000.0, editors: HashMap::new(), garbage: grx }; + let mut side = Side::new(48_000.0, channel().1); let mut s = soundcraft_model::Session::default(); let t = s.add_track(TrackKind::Audio, ChannelFormat::Mono, None); s.track_mut(t).unwrap().mixer.inserts[0] = Some(Insert::new("clap:does.not.exist")); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 5c4a7514..096f143b 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -485,8 +485,8 @@ impl SoundApp { r.arm(plan) } - /// Reads every live third-party plugin's state from the audio engine and stores it on its - /// insert (`mix.insert_state`), so saves and bounces include it. + /// Reads every live third-party plugin's state and stores it on its insert + /// (`mix.insert_state`), so saves and bounces include it. pub fn capture_plugin_states(&mut self) { let Some(p) = &self.player else { return }; for (t, slot, data) in p.capture_states() { diff --git a/crates/vst3-host/src/ffi.rs b/crates/vst3-host/src/ffi.rs index 341ad534..d73d8c9f 100644 --- a/crates/vst3-host/src/ffi.rs +++ b/crates/vst3-host/src/ffi.rs @@ -571,8 +571,10 @@ impl Bundle { // SAFETY: valid controller; the handler (host context) outlives it. unsafe { c.setComponentHandler(h.as_ptr()) }; } - inst.editor = Arc::new(EditorLink { inner: Mutex::new(EditorInner { controller: Some(c.clone()), ..EditorInner::default() }) }); } + inst.editor = Arc::new(EditorLink { + inner: Mutex::new(EditorInner { component: Some(inst.component.clone()), controller: inst.controller.clone(), ..EditorInner::default() }), + }); Ok(inst) } } @@ -1123,6 +1125,11 @@ impl Instance { self.editor.has_controller().then(|| Arc::clone(&self.editor)) } + /// The same link, for state handles. + pub fn state_link(&self) -> Arc { + Arc::clone(&self.editor) + } + /// Writes one state into a fresh memory stream. fn read_state(f: impl FnOnce(*mut sb::IBStream) -> sb::tresult) -> Option> { let stream = MemoryStream::new(Vec::new()); @@ -1130,26 +1137,7 @@ impl Instance { ok(f(s.as_ptr())).then(|| stream.take()) } - /// The component state plus the controller state, in SoundCraft's container (see - /// `STATE_MAGIC`). `None` when the component refuses `getState`. - pub fn save_state(&self) -> Option> { - // SAFETY: valid component; the stream lives in `read_state` for the whole call. - let comp = Instance::read_state(|s| unsafe { self.component.getState(s) })?; - let ctrl = match &self.controller { - // SAFETY: valid controller; as above. - Some(c) => Instance::read_state(|s| unsafe { c.getState(s) }).unwrap_or_default(), - None => Vec::new(), - }; - let mut out = Vec::with_capacity(12 + comp.len() + ctrl.len()); - out.extend_from_slice(STATE_MAGIC); - out.extend_from_slice(&u32::try_from(comp.len()).ok()?.to_le_bytes()); - out.extend_from_slice(&comp); - out.extend_from_slice(&u32::try_from(ctrl.len()).ok()?.to_le_bytes()); - out.extend_from_slice(&ctrl); - Some(out) - } - - /// Restores a [`Instance::save_state`] blob (or a bare component state): component + /// Restores a [`EditorLink::save_state`] blob (or a bare component state): component /// `setState`, then the controller's `setComponentState` and `setState`. `false` when the /// blob is malformed or the component refuses it. pub fn load_state(&self, data: &[u8]) -> bool { @@ -1545,17 +1533,20 @@ fn rect_size(r: &sb::ViewRect) -> (f64, f64) { #[derive(Default)] struct EditorInner { /// `None` once the instance is gone. + component: Option>, + /// `None` once the instance is gone, or without an edit controller. controller: Option>, view: Option>, frame: Option>, window: Option, } -/// The link between an instance and its editor handles. The instance may process on the audio -/// thread while the editor (the edit controller's `IPlugView`) is driven from the main thread, -/// which is VST3's own threading model (controller and views on the UI thread). All view calls -/// happen under this lock, and the instance closes the view and drops the controller reference -/// under the same lock before it terminates, so a handle never calls into a dead plugin. +/// The link between an instance and its editor and state handles. The instance may process on +/// the audio thread while the editor (the edit controller's `IPlugView`) is driven and the state +/// read from the main thread, which is VST3's own threading model (controller, views and +/// `getState` on the UI thread). All such calls happen under this lock, and the instance closes +/// the view and drops the component and controller references under the same lock before it +/// terminates, so a handle never calls into a dead plugin. #[derive(Default)] pub(crate) struct EditorLink { inner: Mutex, @@ -1657,6 +1648,28 @@ impl EditorLink { } } + /// The component state plus the controller state, in SoundCraft's container (see + /// `STATE_MAGIC`). `None` when the component refuses `getState` or the instance is gone. + pub fn save_state(&self) -> Option> { + let g = self.lock(); + let component = g.component.as_ref()?; + // SAFETY: valid component (the instance clears it under this lock before it terminates); + // the stream lives in `read_state` for the whole call. + let comp = Instance::read_state(|s| unsafe { component.getState(s) })?; + let ctrl = match &g.controller { + // SAFETY: valid controller; as above. + Some(c) => Instance::read_state(|s| unsafe { c.getState(s) }).unwrap_or_default(), + None => Vec::new(), + }; + let mut out = Vec::with_capacity(12 + comp.len() + ctrl.len()); + out.extend_from_slice(STATE_MAGIC); + out.extend_from_slice(&u32::try_from(comp.len()).ok()?.to_le_bytes()); + out.extend_from_slice(&comp); + out.extend_from_slice(&u32::try_from(ctrl.len()).ok()?.to_le_bytes()); + out.extend_from_slice(&ctrl); + Some(out) + } + /// Closes the editor. Main thread only (no-op elsewhere). pub fn close(&self) { if window::on_main_thread() { @@ -1739,6 +1752,7 @@ impl EditorLink { } } } + g.component = None; g.controller = None; leaked } diff --git a/crates/vst3-host/src/plugin.rs b/crates/vst3-host/src/plugin.rs index 254671f5..05a3e0ea 100644 --- a/crates/vst3-host/src/plugin.rs +++ b/crates/vst3-host/src/plugin.rs @@ -318,10 +318,6 @@ impl Plugin for Vst3Plugin { } } - fn save_state(&mut self) -> Option> { - self.inst.save_state() - } - fn load_state(&mut self, data: &[u8]) -> bool { let ok = self.inst.load_state(data); if ok { @@ -344,6 +340,11 @@ impl Plugin for Vst3Plugin { let link = self.inst.editor()?; Some(Box::new(Vst3Editor { link, host: self.inst.host_ref(), info: self.info, ids: self.ids.clone(), maps: self.maps.clone() })) } + + fn state_reader(&mut self) -> Option> { + let link = self.inst.state_link(); + Some(Box::new(move || link.save_state())) + } } /// A VST3 plugin's editor (`IPlugView` in a host window), driven from the main thread. diff --git a/crates/vst3-host/tests/fixture_e2e.rs b/crates/vst3-host/tests/fixture_e2e.rs index e3603b7a..f98b37fc 100644 --- a/crates/vst3-host/tests/fixture_e2e.rs +++ b/crates/vst3-host/tests/fixture_e2e.rs @@ -208,6 +208,30 @@ fn state_saves_component_and_controller_and_restores_them() { assert_eq!(q.param("7"), Some(1.0)); } +#[test] +fn state_reader_reads_while_another_thread_processes() { + let id = id_of("Fixture Gain"); + let mut p = soundcraft_vst3_host::create(&id).unwrap(); + p.prepare(48_000.0, 64, 2); + assert!(p.set_param("7", 1.5)); + p.process(&mut vec![vec![0.5f32; 64]; 2], 64); + let mut reader = p.state_reader().expect("every VST3 component has a state"); + let blob = p.save_state().unwrap(); + let audio = std::thread::spawn(move || { + for _ in 0..2_000 { + p.process(&mut vec![vec![0.5f32; 64]; 2], 64); + } + p + }); + while !audio.is_finished() { + assert_eq!(reader.save_state().as_ref(), Some(&blob)); + } + let p = audio.join().unwrap(); + assert_eq!(reader.save_state(), Some(blob)); + drop(p); + assert_eq!(reader.save_state(), None); +} + #[test] fn editor_view_is_probed_and_edits_come_back_as_plain_values() { let id = id_of("Fixture Gain");