Skip to content

Commit

Permalink
vst3: implement Host param edit API
Browse files Browse the repository at this point in the history
  • Loading branch information
micahrj committed May 1, 2024
1 parent e51e4be commit b95c022
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
15 changes: 13 additions & 2 deletions src/format/vst3/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub struct Component<P: Plugin> {
param_map: HashMap<ParamId, usize>,
plugin_params: ParamValues,
processor_params: ParamValues,
host: Arc<Vst3Host>,
main_thread_state: Arc<UnsafeCell<MainThreadState<P>>>,
// When the audio processor is *not* active, references to ProcessState may only be formed from
// the main thread. When the audio processor *is* active, references to ProcessState may only
Expand Down Expand Up @@ -96,6 +97,8 @@ impl<P: Plugin> Component<P> {

let scratch_buffers = ScratchBuffers::new(input_bus_map.len(), output_bus_map.len());

let host = Arc::new(Vst3Host::new());

Component {
info: info.clone(),
input_bus_map,
Expand All @@ -104,9 +107,10 @@ impl<P: Plugin> Component<P> {
param_map,
plugin_params: ParamValues::new(&info.params),
processor_params: ParamValues::new(&info.params),
host: host.clone(),
main_thread_state: Arc::new(UnsafeCell::new(MainThreadState {
config: config.clone(),
plugin: P::new(Host::from_inner(Arc::new(Vst3Host {}))),
plugin: P::new(Host::from_inner(host)),
editor_params,
editor: None,
})),
Expand Down Expand Up @@ -688,7 +692,14 @@ impl<P: Plugin> IEditControllerTrait for Component<P> {
kInvalidArgument
}

unsafe fn setComponentHandler(&self, _handler: *mut IComponentHandler) -> tresult {
unsafe fn setComponentHandler(&self, handler: *mut IComponentHandler) -> tresult {
let mut current_handler = self.host.handler.write().unwrap();
if let Some(handler) = ComRef::from_raw(handler) {
*current_handler = Some(handler.to_com_ptr());
} else {
*current_handler = None;
}

kResultOk
}

Expand Down
46 changes: 42 additions & 4 deletions src/format/vst3/host.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,48 @@
use std::sync::RwLock;

use vst3::ComPtr;
use vst3::Steinberg::Vst::{IComponentHandler, IComponentHandlerTrait};

use crate::host::HostInner;
use crate::params::{ParamId, ParamValue};

pub struct Vst3Host {}
pub struct Vst3Host {
pub handler: RwLock<Option<ComPtr<IComponentHandler>>>,
}

impl Vst3Host {
pub fn new() -> Vst3Host {
Vst3Host {
handler: RwLock::new(None),
}
}
}

impl HostInner for Vst3Host {
fn begin_gesture(&self, _id: ParamId) {}
fn end_gesture(&self, _id: ParamId) {}
fn set_param(&self, _id: ParamId, _value: ParamValue) {}
fn begin_gesture(&self, id: ParamId) {
let handler = self.handler.read().unwrap();
if let Some(handler) = &*handler {
unsafe {
handler.beginEdit(id);
}
}
}

fn end_gesture(&self, id: ParamId) {
let handler = self.handler.read().unwrap();
if let Some(handler) = &*handler {
unsafe {
handler.endEdit(id);
}
}
}

fn set_param(&self, id: ParamId, value: ParamValue) {
let handler = self.handler.read().unwrap();
if let Some(handler) = &*handler {
unsafe {
handler.performEdit(id, value);
}
}
}
}

0 comments on commit b95c022

Please sign in to comment.