From f4f54029eb4c5f54870f7e90dcac7374c7ef9ad9 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Tue, 24 May 2022 13:03:30 +0200 Subject: [PATCH] Add a deactivation callback This is the equivalent of initialize() --- plugins/examples/gain/src/lib.rs | 4 ++++ src/lib.rs | 3 +++ src/plugin.rs | 10 ++++++++++ src/wrapper/clap/wrapper.rs | 7 +++++-- src/wrapper/standalone/wrapper.rs | 4 ++++ src/wrapper/vst3/wrapper.rs | 6 +++++- 6 files changed, 31 insertions(+), 3 deletions(-) diff --git a/plugins/examples/gain/src/lib.rs b/plugins/examples/gain/src/lib.rs index aa344063..dabb4f27 100644 --- a/plugins/examples/gain/src/lib.rs +++ b/plugins/examples/gain/src/lib.rs @@ -145,6 +145,10 @@ impl Plugin for Gain { ProcessStatus::Normal } + + // This can be used for cleaning up special resources like socket connections whenever the + // plugin is deactivated. Most plugins won't need to do anything here. + fn deactivate(&mut self) {} } impl ClapPlugin for Gain { diff --git a/src/lib.rs b/src/lib.rs index c0d54bb7..c5b84f83 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,6 +58,9 @@ //! `Buffer` object for all of the ways you can use this API. You can access note events, //! transport data, and more through the [`ProcessContext`][prelude::ProcessContext] that's //! also passed to the process function. +//! 6. [`Plugin::deactivate()`][prelude::Plugin::deactivate()] is called from the when the plugin +//! gets deactivated. You probably don't need to do anything here, but you could deallocate or +//! clean up resources here. //! //! - Plugin parameters are managed automatically by creating a struct deriving the //! [`Params`][prelude::Params] trait and returning a handle to it from the diff --git a/src/plugin.rs b/src/plugin.rs index 296639cf..bf8d4ccf 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -145,6 +145,16 @@ pub trait Plugin: Default + Send + Sync + 'static { /// TODO: Create an example plugin that uses block-based processing fn process(&mut self, buffer: &mut Buffer, context: &mut impl ProcessContext) -> ProcessStatus; + /// Called when the plugin is deactivated. The host will call + /// [`initialize()`][Self::initialize()] again before the plugin resumes processing audio. These + /// two functions will not be called when the host only temporarily stops processing audio. You + /// can clean up or deallocate resources here. In most cases you can safely ignore this. + /// + /// There is no one-to-one relationship between calls to `initialize()` and `deactivate()`. + /// `initialize()` may be called more than once before `deactivate()` is called, for instance + /// when restoring state while the plugin is still activate. + fn deactivate(&mut self) {} + /// Convenience function provided to allocate memory for block-based smoothing for this plugin. /// Since this allocates memory, this should be called in [`initialize()`][Self::initialize()]. /// If you are going to use [`Buffer::iter_blocks()`] and want to use parameter smoothing in diff --git a/src/wrapper/clap/wrapper.rs b/src/wrapper/clap/wrapper.rs index d9c8318f..8bcecd33 100644 --- a/src/wrapper/clap/wrapper.rs +++ b/src/wrapper/clap/wrapper.rs @@ -1575,8 +1575,11 @@ impl Wrapper

{ } } - unsafe extern "C" fn deactivate(_plugin: *const clap_plugin) { - // We currently don't do anything here + unsafe extern "C" fn deactivate(plugin: *const clap_plugin) { + check_null_ptr!((), plugin); + let wrapper = &*(plugin as *const Self); + + wrapper.plugin.write().deactivate(); } unsafe extern "C" fn start_processing(plugin: *const clap_plugin) -> bool { diff --git a/src/wrapper/standalone/wrapper.rs b/src/wrapper/standalone/wrapper.rs index 5bea7a8b..94b58aaf 100644 --- a/src/wrapper/standalone/wrapper.rs +++ b/src/wrapper/standalone/wrapper.rs @@ -309,6 +309,10 @@ impl Wrapper { terminate_audio_thread.store(true, Ordering::SeqCst); audio_thread.join().unwrap(); + // Some plugins may use this to clean up resources. Should not be needed for the standalone + // application, but it seems like a good idea to stay consistent. + self.plugin.write().deactivate(); + Ok(()) } diff --git a/src/wrapper/vst3/wrapper.rs b/src/wrapper/vst3/wrapper.rs index 6160f3e7..f58ec407 100644 --- a/src/wrapper/vst3/wrapper.rs +++ b/src/wrapper/vst3/wrapper.rs @@ -373,7 +373,11 @@ impl IComponent for Wrapper

{ } } (true, None) => kResultFalse, - (false, _) => kResultOk, + (false, _) => { + self.inner.plugin.write().deactivate(); + + kResultOk + } } }