Skip to content

Commit

Permalink
clap: request main thread callback when params are changed
Browse files Browse the repository at this point in the history
  • Loading branch information
micahrj committed Mar 18, 2024
1 parent 2831bab commit 8ce9b1b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/format/clap/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ impl<P: Plugin + ClapPlugin> Factory<P> {

unsafe extern "C" fn create_plugin(
factory: *const clap_plugin_factory,
_host: *const clap_host,
host: *const clap_host,
plugin_id: *const c_char,
) -> *const clap_plugin {
let factory = &*(factory as *const Self);

if let Some(state) = &*factory.state.get() {
if CStr::from_ptr(plugin_id) == CStr::from_ptr(state.descriptor.id) {
let instance = Box::new(Instance::<P>::new(&state.descriptor, &state.info));
let instance = Box::new(Instance::<P>::new(&state.descriptor, &state.info, host));
return Box::into_raw(instance) as *const clap_plugin;
}
}
Expand Down
33 changes: 30 additions & 3 deletions src/format/clap/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::sync::Arc;
use std::{io, ptr, slice};

use clap_sys::ext::{audio_ports::*, audio_ports_config::*, gui::*, params::*, state::*};
use clap_sys::{events::*, id::*, plugin::*, process::*, stream::*};
use clap_sys::{events::*, host::*, id::*, plugin::*, process::*, stream::*};

use crate::buffers::{BufferData, BufferType, Buffers};
use crate::bus::{BusDir, Format};
Expand Down Expand Up @@ -59,6 +59,7 @@ pub struct ProcessState<P: Plugin> {
pub struct Instance<P: Plugin> {
#[allow(unused)]
pub clap_plugin: clap_plugin,
pub host: *const clap_host,
pub info: Arc<PluginInfo>,
pub input_bus_map: Vec<usize>,
pub output_bus_map: Vec<usize>,
Expand All @@ -74,7 +75,11 @@ pub struct Instance<P: Plugin> {
unsafe impl<P: Plugin> Sync for Instance<P> {}

impl<P: Plugin> Instance<P> {
pub fn new(desc: *const clap_plugin_descriptor, info: &Arc<PluginInfo>) -> Self {
pub fn new(
desc: *const clap_plugin_descriptor,
info: &Arc<PluginInfo>,
host: *const clap_host,
) -> Self {
let mut input_bus_map = Vec::new();
let mut output_bus_map = Vec::new();
for (index, bus) in info.buses.iter().enumerate() {
Expand Down Expand Up @@ -108,6 +113,7 @@ impl<P: Plugin> Instance<P> {
get_extension: Some(Self::get_extension),
on_main_thread: Some(Self::on_main_thread),
},
host,
info: info.clone(),
input_bus_map,
output_bus_map,
Expand Down Expand Up @@ -329,6 +335,8 @@ impl<P: Plugin> Instance<P> {
});
}

let mut params_changed = false;

let in_events = process.in_events;
let size = (*in_events).size.unwrap()(in_events);
for i in 0..size {
Expand All @@ -351,10 +359,16 @@ impl<P: Plugin> Instance<P> {
});

instance.plugin_params.set(index, value);

params_changed = true;
}
}
}

if params_changed {
(*instance.host).request_callback.unwrap()(instance.host);
}

processor.process(
Buffers::from_raw_parts(
&process_state.buffer_data,
Expand Down Expand Up @@ -400,7 +414,12 @@ impl<P: Plugin> Instance<P> {
ptr::null()
}

unsafe extern "C" fn on_main_thread(_plugin: *const clap_plugin) {}
unsafe extern "C" fn on_main_thread(plugin: *const clap_plugin) {
let instance = &*(plugin as *const Self);
let main_thread_state = &mut *instance.main_thread_state.get();

instance.sync_plugin(main_thread_state);
}
}

impl<P: Plugin> Instance<P> {
Expand Down Expand Up @@ -683,6 +702,8 @@ impl<P: Plugin> Instance<P> {
});
}

let mut params_changed = false;

let size = (*in_).size.unwrap()(in_);
for i in 0..size {
let event = (*in_).get.unwrap()(in_, i);
Expand All @@ -704,10 +725,16 @@ impl<P: Plugin> Instance<P> {
});

instance.plugin_params.set(index, value);

params_changed = true;
}
}
}

if params_changed {
(*instance.host).request_callback.unwrap()(instance.host);
}

processor.process(
Buffers::from_raw_parts(
&process_state.buffer_data,
Expand Down

0 comments on commit 8ce9b1b

Please sign in to comment.