From 604d8d1fb106ca14dcba608013450ac04a5c02b2 Mon Sep 17 00:00:00 2001 From: Micah Johnston Date: Sun, 24 May 2026 17:26:06 -0500 Subject: [PATCH 1/2] remove ParamId and ParamValue type aliases --- coupler-derive/src/enum_.rs | 4 +-- coupler-derive/src/params.rs | 10 ++++---- examples/gain-gui/src/lib.rs | 19 +++++--------- examples/gain/src/lib.rs | 17 +++++------- src/editor.rs | 18 ++++++------- src/events.rs | 4 +-- src/format/clap/gui.rs | 9 +++---- src/format/clap/instance.rs | 7 +++-- src/format/clap/tests.rs | 16 ++++++------ src/format/vst3/component.rs | 3 +-- src/format/vst3/tests.rs | 16 ++++++------ src/format/vst3/view.rs | 7 +++-- src/params.rs | 20 +++++---------- src/params/range.rs | 50 +++++++++++++++++------------------- src/plugin.rs | 15 ++++------- src/process.rs | 3 +-- src/sync/param_gestures.rs | 5 ++-- src/sync/params.rs | 5 ++-- src/util.rs | 6 ++--- 19 files changed, 99 insertions(+), 135 deletions(-) diff --git a/coupler-derive/src/enum_.rs b/coupler-derive/src/enum_.rs index a6e0ced8..aca08ed8 100644 --- a/coupler-derive/src/enum_.rs +++ b/coupler-derive/src/enum_.rs @@ -107,13 +107,13 @@ pub fn expand_enum(input: &DeriveInput) -> Result { Some(#count) } - fn encode(&self) -> ::coupler::params::ParamValue { + fn encode(&self) -> ::std::primitive::f64 { match self { #(#encode_cases)* } } - fn decode(__value: ::coupler::params::ParamValue) -> Self { + fn decode(__value: ::std::primitive::f64) -> Self { match (__value * #count as ::std::primitive::f64) as ::std::primitive::u32 { #(#decode_cases)* _ => #ident::#last_variant, diff --git a/coupler-derive/src/params.rs b/coupler-derive/src/params.rs index 77d0a35a..827e5808 100644 --- a/coupler-derive/src/params.rs +++ b/coupler-derive/src/params.rs @@ -236,21 +236,21 @@ pub fn expand_params(input: &DeriveInput) -> Result { #(.param(#param_info))*; } - fn set_param(&mut self, __id: ::coupler::params::ParamId, __value: ::coupler::params::ParamValue) { + fn set_param(&mut self, __id: ::std::primitive::u32, __value: ::std::primitive::f64) { match __id { #(#set_cases)* _ => {} } } - fn get_param(&self, __id: ::coupler::params::ParamId) -> ::coupler::params::ParamValue { + fn get_param(&self, __id: ::std::primitive::u32) -> ::std::primitive::f64 { match __id { #(#get_cases)* _ => 0.0, } } - fn parse_param(&self, __id: ::coupler::params::ParamId, __text: &::std::primitive::str) -> ::std::option::Option<::coupler::params::ParamValue> { + fn parse_param(&self, __id: ::std::primitive::u32, __text: &::std::primitive::str) -> ::std::option::Option<::std::primitive::f64> { match __id { #(#parse_cases)* _ => ::std::option::Option::None @@ -259,8 +259,8 @@ pub fn expand_params(input: &DeriveInput) -> Result { fn display_param( &self, - __id: ::coupler::params::ParamId, - __value: ::coupler::params::ParamValue, + __id: ::std::primitive::u32, + __value: ::std::primitive::f64, __write: impl fmt::Write, ) -> ::std::result::Result<(), ::std::fmt::Error> { match __id { diff --git a/examples/gain-gui/src/lib.rs b/examples/gain-gui/src/lib.rs index 33e70cf1..094fe1ec 100644 --- a/examples/gain-gui/src/lib.rs +++ b/examples/gain-gui/src/lib.rs @@ -11,7 +11,7 @@ use coupler::events::{Data, Events}; use coupler::format::clap::{BuildClapInfo, ClapInfo, ClapPlugin}; use coupler::format::vst3::{BuildVst3Info, Uuid, Vst3Info, Vst3Plugin}; use coupler::host::Host; -use coupler::params::{BuildParams, ParamId, ParamValue, Params}; +use coupler::params::{BuildParams, Params}; use coupler::plugin::{BuildInfo, Plugin, PluginInfo}; use coupler::process::{Config, Processor}; @@ -79,24 +79,19 @@ impl Plugin for GainGui { self.params.params(build) } - fn set_param(&mut self, id: ParamId, value: ParamValue) { + fn set_param(&mut self, id: u32, value: f64) { self.params.set_param(id, value); } - fn get_param(&self, id: ParamId) -> ParamValue { + fn get_param(&self, id: u32) -> f64 { self.params.get_param(id) } - fn parse_param(&self, id: ParamId, text: &str) -> Option { + fn parse_param(&self, id: u32, text: &str) -> Option { self.params.parse_param(id, text) } - fn display_param( - &self, - id: ParamId, - value: ParamValue, - write: impl fmt::Write, - ) -> Result<(), fmt::Error> { + fn display_param(&self, id: u32, value: f64, write: impl fmt::Write) -> Result<(), fmt::Error> { self.params.display_param(id, value, write) } @@ -157,7 +152,7 @@ pub struct GainGuiProcessor { impl Processor for GainGuiProcessor { fn reset(&mut self) {} - fn set_param(&mut self, id: ParamId, value: ParamValue) { + fn set_param(&mut self, id: u32, value: f64) { self.params.set_param(id, value); } @@ -368,7 +363,7 @@ impl Editor for GainGuiEditor { } } - fn param_changed(&mut self, id: ParamId, value: ParamValue) { + fn param_changed(&mut self, id: u32, value: f64) { self.params.borrow_mut().set_param(id, value); } } diff --git a/examples/gain/src/lib.rs b/examples/gain/src/lib.rs index 8a92a560..307ce423 100644 --- a/examples/gain/src/lib.rs +++ b/examples/gain/src/lib.rs @@ -9,7 +9,7 @@ use coupler::events::{Data, Events}; use coupler::format::clap::{BuildClapInfo, ClapInfo, ClapPlugin}; use coupler::format::vst3::{BuildVst3Info, Uuid, Vst3Info, Vst3Plugin}; use coupler::host::Host; -use coupler::params::{BuildParams, ParamId, ParamValue, Params}; +use coupler::params::{BuildParams, Params}; use coupler::plugin::{BuildInfo, Plugin, PluginInfo}; use coupler::process::{Config, Processor}; @@ -70,24 +70,19 @@ impl Plugin for Gain { self.params.params(build) } - fn set_param(&mut self, id: ParamId, value: ParamValue) { + fn set_param(&mut self, id: u32, value: f64) { self.params.set_param(id, value); } - fn get_param(&self, id: ParamId) -> ParamValue { + fn get_param(&self, id: u32) -> f64 { self.params.get_param(id) } - fn parse_param(&self, id: ParamId, text: &str) -> Option { + fn parse_param(&self, id: u32, text: &str) -> Option { self.params.parse_param(id, text) } - fn display_param( - &self, - id: ParamId, - value: ParamValue, - write: impl fmt::Write, - ) -> Result<(), fmt::Error> { + fn display_param(&self, id: u32, value: f64, write: impl fmt::Write) -> Result<(), fmt::Error> { self.params.display_param(id, value, write) } @@ -148,7 +143,7 @@ pub struct GainProcessor { impl Processor for GainProcessor { fn reset(&mut self) {} - fn set_param(&mut self, id: ParamId, value: ParamValue) { + fn set_param(&mut self, id: u32, value: f64) { self.params.set_param(id, value); } diff --git a/src/editor.rs b/src/editor.rs index c4a366e9..29ae79f2 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -2,12 +2,10 @@ use std::ffi::{c_ulong, c_void}; use std::marker::PhantomData; use std::rc::Rc; -use crate::params::{ParamId, ParamValue}; - pub trait EditorHostInner { - 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: u32); + fn end_gesture(&self, id: u32); + fn set_param(&self, id: u32, value: f64); } #[derive(Clone)] @@ -25,15 +23,15 @@ impl EditorHost { } } - pub fn begin_gesture(&self, id: ParamId) { + pub fn begin_gesture(&self, id: u32) { self.inner.begin_gesture(id); } - pub fn end_gesture(&self, id: ParamId) { + pub fn end_gesture(&self, id: u32) { self.inner.end_gesture(id); } - pub fn set_param(&self, id: ParamId, value: ParamValue) { + pub fn set_param(&self, id: u32, value: f64) { self.inner.set_param(id, value); } } @@ -66,7 +64,7 @@ pub struct Size { pub trait Editor: Sized + 'static { fn size(&self) -> Size; - fn param_changed(&mut self, id: ParamId, value: ParamValue); + fn param_changed(&mut self, id: u32, value: f64); } pub struct NoEditor; @@ -79,5 +77,5 @@ impl Editor for NoEditor { } } - fn param_changed(&mut self, _id: ParamId, _value: ParamValue) {} + fn param_changed(&mut self, _id: u32, _value: f64) {} } diff --git a/src/events.rs b/src/events.rs index 8ca8113c..e8892ce5 100644 --- a/src/events.rs +++ b/src/events.rs @@ -1,8 +1,6 @@ use std::ops::{Index, RangeBounds}; use std::slice; -use crate::params::{ParamId, ParamValue}; - #[derive(Copy, Clone, Debug)] pub struct Event { pub time: i64, @@ -12,7 +10,7 @@ pub struct Event { #[derive(Copy, Clone, Debug)] #[non_exhaustive] pub enum Data { - ParamChange { id: ParamId, value: ParamValue }, + ParamChange { id: u32, value: f64 }, } #[derive(Copy, Clone)] diff --git a/src/format/clap/gui.rs b/src/format/clap/gui.rs index 676066b2..10c7733c 100644 --- a/src/format/clap/gui.rs +++ b/src/format/clap/gui.rs @@ -8,7 +8,6 @@ use clap_sys::plugin::*; use super::instance::{Extensions, HostPtr, Instance}; use crate::editor::{Editor, EditorHost, EditorHostInner, ParentWindow, RawParent}; -use crate::params::{ParamId, ParamValue}; use crate::plugin::Plugin; use crate::sync::param_gestures::ParamGestures; use crate::sync::thread_cell::ThreadCell; @@ -16,12 +15,12 @@ use crate::sync::thread_cell::ThreadCell; struct ClapEditorHost { host: HostPtr, extensions: Extensions, - param_map: Arc>, + param_map: Arc>, param_gestures: Arc, } impl EditorHostInner for ClapEditorHost { - fn begin_gesture(&self, id: ParamId) { + fn begin_gesture(&self, id: u32) { self.param_gestures.begin_gesture(self.param_map[&id]); if let Some(host_params) = self.extensions.host_params { @@ -29,7 +28,7 @@ impl EditorHostInner for ClapEditorHost { } } - fn end_gesture(&self, id: ParamId) { + fn end_gesture(&self, id: u32) { self.param_gestures.end_gesture(self.param_map[&id]); if let Some(host_params) = self.extensions.host_params { @@ -37,7 +36,7 @@ impl EditorHostInner for ClapEditorHost { } } - fn set_param(&self, id: ParamId, value: ParamValue) { + fn set_param(&self, id: u32, value: f64) { self.param_gestures.set_value(self.param_map[&id], value); if let Some(host_params) = self.extensions.host_params { diff --git a/src/format/clap/instance.rs b/src/format/clap/instance.rs index 0786edf4..a6000afa 100644 --- a/src/format/clap/instance.rs +++ b/src/format/clap/instance.rs @@ -14,7 +14,6 @@ use crate::bus::{BusDir, Layout}; use crate::editor::Editor; use crate::events::{Data, Event, Events}; use crate::host::Host; -use crate::params::{ParamId, ParamValue}; use crate::plugin::Plugin; use crate::process::{Config, Processor}; use crate::sync::param_gestures::{GestureStates, GestureUpdate, ParamGestures}; @@ -32,7 +31,7 @@ fn port_type_from_layout(layout: &Layout) -> &'static CStr { } } -fn map_param_in(param: &OwnedParamInfo, value: f64) -> ParamValue { +fn map_param_in(param: &OwnedParamInfo, value: f64) -> f64 { if let Some(steps) = param.steps { (value + 0.5) / steps as f64 } else { @@ -40,7 +39,7 @@ fn map_param_in(param: &OwnedParamInfo, value: f64) -> ParamValue { } } -fn map_param_out(param: &OwnedParamInfo, value: ParamValue) -> f64 { +fn map_param_out(param: &OwnedParamInfo, value: f64) -> f64 { if let Some(steps) = param.steps { (value * steps as f64).floor() } else { @@ -94,7 +93,7 @@ pub struct Instance { pub input_bus_map: Vec, pub output_bus_map: Vec, pub params: Vec, - pub param_map: Arc>, + pub param_map: Arc>, // Processor -> plugin parameter changes pub plugin_params: ParamValues, // Plugin -> processor parameter changes diff --git a/src/format/clap/tests.rs b/src/format/clap/tests.rs index 6d13bd60..16e3bb6f 100644 --- a/src/format/clap/tests.rs +++ b/src/format/clap/tests.rs @@ -10,7 +10,7 @@ use crate::bus::{BuildBusConfigs, BuildBuses}; use crate::editor::{Editor, EditorHost, ParentWindow, Size}; use crate::events::Events; use crate::host::Host; -use crate::params::{BuildParams, ParamId, ParamValue}; +use crate::params::BuildParams; use crate::plugin::{BuildInfo, Plugin, PluginInfo}; use crate::process::{Config, Processor}; @@ -42,17 +42,17 @@ impl Plugin for TestPlugin { fn buses(&self, _build: impl BuildBuses) {} fn bus_configs(&self, _build: impl BuildBusConfigs) {} fn params(&self, _build: impl BuildParams) {} - fn set_param(&mut self, _id: ParamId, _value: ParamValue) {} - fn get_param(&self, _id: ParamId) -> ParamValue { + fn set_param(&mut self, _id: u32, _value: f64) {} + fn get_param(&self, _id: u32) -> f64 { 0.0 } - fn parse_param(&self, _id: ParamId, _text: &str) -> Option { + fn parse_param(&self, _id: u32, _text: &str) -> Option { None } fn display_param( &self, - _id: ParamId, - _value: ParamValue, + _id: u32, + _value: f64, _write: impl fmt::Write, ) -> Result<(), fmt::Error> { Ok(()) @@ -95,7 +95,7 @@ struct TestProcessor; impl Processor for TestProcessor { fn reset(&mut self) {} - fn set_param(&mut self, _id: ParamId, _value: ParamValue) {} + fn set_param(&mut self, _id: u32, _value: f64) {} fn process(&mut self, _buffers: Buffers, _events: Events) {} } @@ -108,7 +108,7 @@ impl Editor for TestEditor { height: 0.0, } } - fn param_changed(&mut self, _id: ParamId, _value: ParamValue) {} + fn param_changed(&mut self, _id: u32, _value: f64) {} } unsafe fn str_from_ptr<'a>(ptr: *const c_char) -> Result<&'a str, std::str::Utf8Error> { diff --git a/src/format/vst3/component.rs b/src/format/vst3/component.rs index 11997c93..86fa9e83 100644 --- a/src/format/vst3/component.rs +++ b/src/format/vst3/component.rs @@ -13,7 +13,6 @@ use crate::bus::{BusDir, Layout}; use crate::editor::Editor; use crate::events::{Data, Event, Events}; use crate::host::Host; -use crate::params::ParamId; use crate::plugin::Plugin; use crate::process::{Config, Processor}; use crate::sync::params::ParamValues; @@ -62,7 +61,7 @@ pub struct Component { output_bus_map: Vec, bus_config_set: HashSet>, params: Vec, - param_map: HashMap, + param_map: HashMap, plugin_params: ParamValues, processor_params: ParamValues, _host: Arc, diff --git a/src/format/vst3/tests.rs b/src/format/vst3/tests.rs index 95e06c9b..5dca0080 100644 --- a/src/format/vst3/tests.rs +++ b/src/format/vst3/tests.rs @@ -17,7 +17,7 @@ use crate::bus::{BuildBusConfigs, BuildBuses}; use crate::editor::{Editor, EditorHost, ParentWindow, Size}; use crate::events::Events; use crate::host::Host; -use crate::params::{BuildParams, ParamId, ParamValue}; +use crate::params::BuildParams; use crate::plugin::{BuildInfo, Plugin, PluginInfo}; use crate::process::{Config, Processor}; @@ -49,17 +49,17 @@ impl Plugin for TestPlugin { fn buses(&self, _build: impl BuildBuses) {} fn bus_configs(&self, _build: impl BuildBusConfigs) {} fn params(&self, _build: impl BuildParams) {} - fn set_param(&mut self, _id: ParamId, _value: ParamValue) {} - fn get_param(&self, _id: ParamId) -> ParamValue { + fn set_param(&mut self, _id: u32, _value: f64) {} + fn get_param(&self, _id: u32) -> f64 { 0.0 } - fn parse_param(&self, _id: ParamId, _text: &str) -> Option { + fn parse_param(&self, _id: u32, _text: &str) -> Option { None } fn display_param( &self, - _id: ParamId, - _value: ParamValue, + _id: u32, + _value: f64, _write: impl fmt::Write, ) -> Result<(), fmt::Error> { Ok(()) @@ -104,7 +104,7 @@ struct TestProcessor; impl Processor for TestProcessor { fn reset(&mut self) {} - fn set_param(&mut self, _id: ParamId, _value: ParamValue) {} + fn set_param(&mut self, _id: u32, _value: f64) {} fn process(&mut self, _buffers: Buffers, _events: Events) {} } @@ -117,7 +117,7 @@ impl Editor for TestEditor { height: 0.0, } } - fn param_changed(&mut self, _id: ParamId, _value: ParamValue) {} + fn param_changed(&mut self, _id: u32, _value: f64) {} } fn str_from_chars(chars: &[char8]) -> Result<&str, Box> { diff --git a/src/format/vst3/view.rs b/src/format/vst3/view.rs index d107c57e..c2809464 100644 --- a/src/format/vst3/view.rs +++ b/src/format/vst3/view.rs @@ -7,7 +7,6 @@ use vst3::{Class, ComPtr, ComRef, Steinberg::*}; use super::component::MainThreadState; use crate::editor::{Editor, EditorHost, EditorHostInner, ParentWindow, RawParent}; -use crate::params::{ParamId, ParamValue}; use crate::plugin::Plugin; use crate::sync::{sync_cell::SyncCell, thread_cell::ThreadCell}; use crate::util::RequireSendSync; @@ -17,7 +16,7 @@ struct Vst3EditorHost { } impl EditorHostInner for Vst3EditorHost { - fn begin_gesture(&self, id: ParamId) { + fn begin_gesture(&self, id: u32) { if let Some(handler) = &self.handler { unsafe { handler.beginEdit(id); @@ -25,7 +24,7 @@ impl EditorHostInner for Vst3EditorHost { } } - fn end_gesture(&self, id: ParamId) { + fn end_gesture(&self, id: u32) { if let Some(handler) = &self.handler { unsafe { handler.endEdit(id); @@ -33,7 +32,7 @@ impl EditorHostInner for Vst3EditorHost { } } - fn set_param(&self, id: ParamId, value: ParamValue) { + fn set_param(&self, id: u32, value: f64) { if let Some(handler) = &self.handler { unsafe { handler.performEdit(id, value); diff --git a/src/params.rs b/src/params.rs index 49b55eaa..cc906c15 100644 --- a/src/params.rs +++ b/src/params.rs @@ -10,13 +10,10 @@ mod range; pub use format::{DefaultFormat, Format}; pub use range::{DefaultRange, Encode, Log, Range}; -pub type ParamId = u32; -pub type ParamValue = f64; - pub struct ParamInfo<'a> { - pub id: ParamId, + pub id: u32, pub name: &'a str, - pub default: ParamValue, + pub default: f64, pub steps: Option, } @@ -26,15 +23,10 @@ pub trait BuildParams { pub trait Params { fn params(&self, build: impl BuildParams); - fn set_param(&mut self, id: ParamId, value: ParamValue); - fn get_param(&self, id: ParamId) -> ParamValue; - fn parse_param(&self, id: ParamId, text: &str) -> Option; - fn display_param( - &self, - id: ParamId, - value: ParamValue, - write: impl fmt::Write, - ) -> Result<(), fmt::Error>; + fn set_param(&mut self, id: u32, value: f64); + fn get_param(&self, id: u32) -> f64; + fn parse_param(&self, id: u32, text: &str) -> Option; + fn display_param(&self, id: u32, value: f64, write: impl fmt::Write) -> Result<(), fmt::Error>; } pub trait Enum: Encode + FromStr + Display {} diff --git a/src/params/range.rs b/src/params/range.rs index b1ad3e98..6f57637f 100644 --- a/src/params/range.rs +++ b/src/params/range.rs @@ -1,15 +1,13 @@ -use super::ParamValue; - pub trait Range { fn steps(&self) -> Option; - fn encode(&self, value: &T) -> ParamValue; - fn decode(&self, value: ParamValue) -> T; + fn encode(&self, value: &T) -> f64; + fn decode(&self, value: f64) -> T; } pub trait Encode { fn steps() -> Option; - fn encode(&self) -> ParamValue; - fn decode(value: ParamValue) -> Self; + fn encode(&self) -> f64; + fn decode(value: f64) -> Self; } pub struct DefaultRange; @@ -19,11 +17,11 @@ impl Range for DefaultRange { T::steps() } - fn encode(&self, value: &T) -> ParamValue { + fn encode(&self, value: &T) -> f64 { T::encode(value) } - fn decode(&self, value: ParamValue) -> T { + fn decode(&self, value: f64) -> T { T::decode(value) } } @@ -40,12 +38,12 @@ macro_rules! float_range { } #[inline] - fn encode(&self, value: &$float) -> ParamValue { + fn encode(&self, value: &$float) -> f64 { ((value - self.start) / (self.end - self.start)) as f64 } #[inline] - fn decode(&self, value: ParamValue) -> $float { + fn decode(&self, value: f64) -> $float { (1.0 - value as $float) * self.start + value as $float * self.end } } @@ -57,12 +55,12 @@ macro_rules! float_range { } #[inline] - fn encode(&self, value: &$float) -> ParamValue { + fn encode(&self, value: &$float) -> f64 { ((value - self.start()) / (self.end() - self.start())) as f64 } #[inline] - fn decode(&self, value: ParamValue) -> $float { + fn decode(&self, value: f64) -> $float { (1.0 - value as $float) * self.start() + value as $float * self.end() } } @@ -74,12 +72,12 @@ macro_rules! float_range { } #[inline] - fn encode(&self, value: &$float) -> ParamValue { + fn encode(&self, value: &$float) -> f64 { (value / self.0.start).log(self.0.end / self.0.start) as f64 } #[inline] - fn decode(&self, value: ParamValue) -> $float { + fn decode(&self, value: f64) -> $float { self.0.start * (self.0.end / self.0.start).powf(value as $float) } } @@ -91,12 +89,12 @@ macro_rules! float_range { } #[inline] - fn encode(&self, value: &$float) -> ParamValue { + fn encode(&self, value: &$float) -> f64 { (value / self.0.start()).log(self.0.end() / self.0.start()) as f64 } #[inline] - fn decode(&self, value: ParamValue) -> $float { + fn decode(&self, value: f64) -> $float { self.0.start() * (self.0.end() / self.0.start()).powf(value as $float) } } @@ -106,11 +104,11 @@ macro_rules! float_range { (0.0..1.0).steps() } - fn encode(&self) -> ParamValue { + fn encode(&self) -> f64 { (0.0..1.0).encode(self) } - fn decode(value: ParamValue) -> Self { + fn decode(value: f64) -> Self { (0.0..1.0).decode(value) } } @@ -129,13 +127,13 @@ macro_rules! int_range { } #[inline] - fn encode(&self, value: &$int) -> ParamValue { + fn encode(&self, value: &$int) -> f64 { let steps_recip = 1.0 / (self.end as f64 - self.start as f64); (*value as f64 - self.start as f64 + 0.5) * steps_recip } #[inline] - fn decode(&self, value: ParamValue) -> $int { + fn decode(&self, value: f64) -> $int { let steps = self.end as f64 - self.start as f64; (self.start as f64 + value * steps) as $int } @@ -148,13 +146,13 @@ macro_rules! int_range { } #[inline] - fn encode(&self, value: &$int) -> ParamValue { + fn encode(&self, value: &$int) -> f64 { let steps_recip = 1.0 / (*self.end() as f64 + 1.0 - *self.start() as f64); (*value as f64 - *self.start() as f64 + 0.5) * steps_recip } #[inline] - fn decode(&self, value: ParamValue) -> $int { + fn decode(&self, value: f64) -> $int { let steps = *self.end() as f64 + 1.0 - *self.start() as f64; (*self.start() as f64 + value * steps) as $int } @@ -165,11 +163,11 @@ macro_rules! int_range { (0..2).steps() } - fn encode(&self) -> ParamValue { + fn encode(&self) -> f64 { (0..2).encode(self) } - fn decode(value: ParamValue) -> Self { + fn decode(value: f64) -> Self { (0..2).decode(value) } } @@ -191,14 +189,14 @@ impl Encode for bool { Some(2) } - fn encode(&self) -> ParamValue { + fn encode(&self) -> f64 { match self { false => 0.25, true => 0.75, } } - fn decode(value: ParamValue) -> Self { + fn decode(value: f64) -> Self { value >= 0.5 } } diff --git a/src/plugin.rs b/src/plugin.rs index 228ff5f5..6436b1d5 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -3,7 +3,7 @@ use std::{fmt, io}; use crate::bus::{BuildBusConfigs, BuildBuses}; use crate::editor::{Editor, EditorHost, ParentWindow, Size}; use crate::host::Host; -use crate::params::{BuildParams, ParamId, ParamValue}; +use crate::params::BuildParams; use crate::process::{Config, Processor}; #[derive(Default)] @@ -30,15 +30,10 @@ pub trait Plugin: Send + Sized + 'static { fn bus_configs(&self, build: impl BuildBusConfigs); fn params(&self, build: impl BuildParams); - fn set_param(&mut self, id: ParamId, value: ParamValue); - fn get_param(&self, id: ParamId) -> ParamValue; - fn parse_param(&self, id: ParamId, text: &str) -> Option; - fn display_param( - &self, - id: ParamId, - value: ParamValue, - write: impl fmt::Write, - ) -> Result<(), fmt::Error>; + fn set_param(&mut self, id: u32, value: f64); + fn get_param(&self, id: u32) -> f64; + fn parse_param(&self, id: u32, text: &str) -> Option; + fn display_param(&self, id: u32, value: f64, write: impl fmt::Write) -> Result<(), fmt::Error>; fn save(&self, output: impl io::Write) -> io::Result<()>; fn load(&mut self, input: impl io::Read) -> io::Result<()>; diff --git a/src/process.rs b/src/process.rs index 5f6de7d5..478e8c3e 100644 --- a/src/process.rs +++ b/src/process.rs @@ -1,7 +1,6 @@ use crate::buffers::Buffers; use crate::bus::Layout; use crate::events::Events; -use crate::params::{ParamId, ParamValue}; #[derive(Clone)] pub struct Config<'a> { @@ -12,6 +11,6 @@ pub struct Config<'a> { pub trait Processor: Send + Sized + 'static { fn reset(&mut self); - fn set_param(&mut self, id: ParamId, value: ParamValue); + fn set_param(&mut self, id: u32, value: f64); fn process(&mut self, buffers: Buffers, events: Events); } diff --git a/src/sync/param_gestures.rs b/src/sync/param_gestures.rs index e64357a7..a7c3ed11 100644 --- a/src/sync/param_gestures.rs +++ b/src/sync/param_gestures.rs @@ -2,7 +2,6 @@ use std::sync::atomic::Ordering; use super::bitset::{self, AtomicBitset, Bitset}; use super::float::AtomicF64; -use crate::params::ParamValue; pub struct ParamGestures { values: Vec, @@ -31,7 +30,7 @@ impl ParamGestures { self.dirty.set(index, true, Ordering::Release); } - pub fn set_value(&self, index: usize, value: ParamValue) { + pub fn set_value(&self, index: usize, value: f64) { self.values[index].store(value, Ordering::Relaxed); self.values_dirty.set(index, true, Ordering::Release); self.dirty.set(index, true, Ordering::Release); @@ -64,7 +63,7 @@ impl GestureStates { pub struct GestureUpdate { pub index: usize, pub begin_gesture: bool, - pub set_value: Option, + pub set_value: Option, pub end_gesture: bool, } diff --git a/src/sync/params.rs b/src/sync/params.rs index f1fe9488..f8cfa1f9 100644 --- a/src/sync/params.rs +++ b/src/sync/params.rs @@ -2,7 +2,6 @@ use std::sync::atomic::Ordering; use super::bitset::{self, AtomicBitset}; use super::float::AtomicF64; -use crate::params::ParamValue; pub struct ParamValues { values: Vec, @@ -17,7 +16,7 @@ impl ParamValues { } } - pub fn set(&self, index: usize, value: ParamValue) { + pub fn set(&self, index: usize, value: f64) { self.values[index].store(value, Ordering::Relaxed); self.dirty.set(index, true, Ordering::Release); } @@ -36,7 +35,7 @@ pub struct Poll<'a> { } impl<'a> Iterator for Poll<'a> { - type Item = (usize, ParamValue); + type Item = (usize, f64); fn next(&mut self) -> Option { if let Some(index) = self.iter.next() { diff --git a/src/util.rs b/src/util.rs index c689cd65..68dc2205 100644 --- a/src/util.rs +++ b/src/util.rs @@ -3,7 +3,7 @@ use std::os::raw::c_char; use std::slice; use crate::bus::{BuildBusConfigs, BuildBuses, BusConfig, BusDir, BusInfo, Layout}; -use crate::params::{BuildParams, ParamId, ParamInfo, ParamValue}; +use crate::params::{BuildParams, ParamInfo}; use crate::plugin::{BuildInfo, Plugin, PluginInfo}; pub fn copy_cstring(src: &str, dst: &mut [c_char]) { @@ -100,9 +100,9 @@ pub fn collect_bus_configs(plugin: &P) -> Vec { } pub struct OwnedParamInfo { - pub id: ParamId, + pub id: u32, pub name: String, - pub default: ParamValue, + pub default: f64, pub steps: Option, } From 318f53c9d2549d46ce2c65ca888f98eb1a913f5d Mon Sep 17 00:00:00 2001 From: Micah Johnston Date: Sun, 24 May 2026 17:51:44 -0500 Subject: [PATCH 2/2] use indices instead of u32 identifiers to address parameters --- coupler-derive/src/params.rs | 16 ++++++------- examples/gain-gui/src/lib.rs | 33 +++++++++++++++------------ examples/gain/src/lib.rs | 29 ++++++++++++++---------- src/editor.rs | 22 +++++++++--------- src/events.rs | 2 +- src/format/clap/gui.rs | 15 +++++------- src/format/clap/instance.rs | 44 +++++++++++++++--------------------- src/format/clap/tests.rs | 12 +++++----- src/format/vst3/component.rs | 43 ++++++++++++++++++----------------- src/format/vst3/tests.rs | 12 +++++----- src/format/vst3/view.rs | 23 ++++++++++++------- src/params.rs | 13 +++++++---- src/plugin.rs | 13 +++++++---- src/process.rs | 2 +- 14 files changed, 148 insertions(+), 131 deletions(-) diff --git a/coupler-derive/src/params.rs b/coupler-derive/src/params.rs index 827e5808..88278773 100644 --- a/coupler-derive/src/params.rs +++ b/coupler-derive/src/params.rs @@ -236,22 +236,22 @@ pub fn expand_params(input: &DeriveInput) -> Result { #(.param(#param_info))*; } - fn set_param(&mut self, __id: ::std::primitive::u32, __value: ::std::primitive::f64) { - match __id { + fn set_param(&mut self, __index: ::std::primitive::usize, __value: ::std::primitive::f64) { + match __index { #(#set_cases)* _ => {} } } - fn get_param(&self, __id: ::std::primitive::u32) -> ::std::primitive::f64 { - match __id { + fn get_param(&self, __index: ::std::primitive::usize) -> ::std::primitive::f64 { + match __index { #(#get_cases)* _ => 0.0, } } - fn parse_param(&self, __id: ::std::primitive::u32, __text: &::std::primitive::str) -> ::std::option::Option<::std::primitive::f64> { - match __id { + fn parse_param(&self, __index: ::std::primitive::usize, __text: &::std::primitive::str) -> ::std::option::Option<::std::primitive::f64> { + match __index { #(#parse_cases)* _ => ::std::option::Option::None } @@ -259,11 +259,11 @@ pub fn expand_params(input: &DeriveInput) -> Result { fn display_param( &self, - __id: ::std::primitive::u32, + __index: ::std::primitive::usize, __value: ::std::primitive::f64, __write: impl fmt::Write, ) -> ::std::result::Result<(), ::std::fmt::Error> { - match __id { + match __index { #(#display_cases)* _ => Ok(()) } diff --git a/examples/gain-gui/src/lib.rs b/examples/gain-gui/src/lib.rs index 094fe1ec..11496425 100644 --- a/examples/gain-gui/src/lib.rs +++ b/examples/gain-gui/src/lib.rs @@ -79,20 +79,25 @@ impl Plugin for GainGui { self.params.params(build) } - fn set_param(&mut self, id: u32, value: f64) { - self.params.set_param(id, value); + fn set_param(&mut self, index: usize, value: f64) { + self.params.set_param(index, value); } - fn get_param(&self, id: u32) -> f64 { - self.params.get_param(id) + fn get_param(&self, index: usize) -> f64 { + self.params.get_param(index) } - fn parse_param(&self, id: u32, text: &str) -> Option { - self.params.parse_param(id, text) + fn parse_param(&self, index: usize, text: &str) -> Option { + self.params.parse_param(index, text) } - fn display_param(&self, id: u32, value: f64, write: impl fmt::Write) -> Result<(), fmt::Error> { - self.params.display_param(id, value, write) + fn display_param( + &self, + index: usize, + value: f64, + write: impl fmt::Write, + ) -> Result<(), fmt::Error> { + self.params.display_param(index, value, write) } fn save(&self, output: impl io::Write) -> io::Result<()> { @@ -152,16 +157,16 @@ pub struct GainGuiProcessor { impl Processor for GainGuiProcessor { fn reset(&mut self) {} - fn set_param(&mut self, id: u32, value: f64) { - self.params.set_param(id, value); + fn set_param(&mut self, index: usize, value: f64) { + self.params.set_param(index, value); } fn process(&mut self, buffers: Buffers, events: Events) { let mut buffers: (BufferMut,) = buffers.try_into().unwrap(); for (mut buffer, events) in buffers.0.split_at_events(events) { for event in events { - if let Data::ParamChange { id, value } = event.data { - self.set_param(id, value); + if let Data::ParamChange { index, value } = event.data { + self.set_param(index, value); } } @@ -363,7 +368,7 @@ impl Editor for GainGuiEditor { } } - fn param_changed(&mut self, id: u32, value: f64) { - self.params.borrow_mut().set_param(id, value); + fn param_changed(&mut self, index: usize, value: f64) { + self.params.borrow_mut().set_param(index, value); } } diff --git a/examples/gain/src/lib.rs b/examples/gain/src/lib.rs index 307ce423..ef364ed0 100644 --- a/examples/gain/src/lib.rs +++ b/examples/gain/src/lib.rs @@ -70,20 +70,25 @@ impl Plugin for Gain { self.params.params(build) } - fn set_param(&mut self, id: u32, value: f64) { - self.params.set_param(id, value); + fn set_param(&mut self, index: usize, value: f64) { + self.params.set_param(index, value); } - fn get_param(&self, id: u32) -> f64 { - self.params.get_param(id) + fn get_param(&self, index: usize) -> f64 { + self.params.get_param(index) } - fn parse_param(&self, id: u32, text: &str) -> Option { - self.params.parse_param(id, text) + fn parse_param(&self, index: usize, text: &str) -> Option { + self.params.parse_param(index, text) } - fn display_param(&self, id: u32, value: f64, write: impl fmt::Write) -> Result<(), fmt::Error> { - self.params.display_param(id, value, write) + fn display_param( + &self, + index: usize, + value: f64, + write: impl fmt::Write, + ) -> Result<(), fmt::Error> { + self.params.display_param(index, value, write) } fn save(&self, output: impl io::Write) -> io::Result<()> { @@ -143,16 +148,16 @@ pub struct GainProcessor { impl Processor for GainProcessor { fn reset(&mut self) {} - fn set_param(&mut self, id: u32, value: f64) { - self.params.set_param(id, value); + fn set_param(&mut self, index: usize, value: f64) { + self.params.set_param(index, value); } fn process(&mut self, buffers: Buffers, events: Events) { let mut buffers: (BufferMut,) = buffers.try_into().unwrap(); for (mut buffer, events) in buffers.0.split_at_events(events) { for event in events { - if let Data::ParamChange { id, value } = event.data { - self.set_param(id, value); + if let Data::ParamChange { index, value } = event.data { + self.set_param(index, value); } } diff --git a/src/editor.rs b/src/editor.rs index 29ae79f2..58517716 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -3,9 +3,9 @@ use std::marker::PhantomData; use std::rc::Rc; pub trait EditorHostInner { - fn begin_gesture(&self, id: u32); - fn end_gesture(&self, id: u32); - fn set_param(&self, id: u32, value: f64); + fn begin_gesture(&self, index: usize); + fn end_gesture(&self, index: usize); + fn set_param(&self, index: usize, value: f64); } #[derive(Clone)] @@ -23,16 +23,16 @@ impl EditorHost { } } - pub fn begin_gesture(&self, id: u32) { - self.inner.begin_gesture(id); + pub fn begin_gesture(&self, index: usize) { + self.inner.begin_gesture(index); } - pub fn end_gesture(&self, id: u32) { - self.inner.end_gesture(id); + pub fn end_gesture(&self, index: usize) { + self.inner.end_gesture(index); } - pub fn set_param(&self, id: u32, value: f64) { - self.inner.set_param(id, value); + pub fn set_param(&self, index: usize, value: f64) { + self.inner.set_param(index, value); } } @@ -64,7 +64,7 @@ pub struct Size { pub trait Editor: Sized + 'static { fn size(&self) -> Size; - fn param_changed(&mut self, id: u32, value: f64); + fn param_changed(&mut self, index: usize, value: f64); } pub struct NoEditor; @@ -77,5 +77,5 @@ impl Editor for NoEditor { } } - fn param_changed(&mut self, _id: u32, _value: f64) {} + fn param_changed(&mut self, _index: usize, _value: f64) {} } diff --git a/src/events.rs b/src/events.rs index e8892ce5..1f9e6851 100644 --- a/src/events.rs +++ b/src/events.rs @@ -10,7 +10,7 @@ pub struct Event { #[derive(Copy, Clone, Debug)] #[non_exhaustive] pub enum Data { - ParamChange { id: u32, value: f64 }, + ParamChange { index: usize, value: f64 }, } #[derive(Copy, Clone)] diff --git a/src/format/clap/gui.rs b/src/format/clap/gui.rs index 10c7733c..2d541e2d 100644 --- a/src/format/clap/gui.rs +++ b/src/format/clap/gui.rs @@ -1,4 +1,3 @@ -use std::collections::HashMap; use std::ffi::{CStr, c_char}; use std::rc::Rc; use std::sync::Arc; @@ -15,29 +14,28 @@ use crate::sync::thread_cell::ThreadCell; struct ClapEditorHost { host: HostPtr, extensions: Extensions, - param_map: Arc>, param_gestures: Arc, } impl EditorHostInner for ClapEditorHost { - fn begin_gesture(&self, id: u32) { - self.param_gestures.begin_gesture(self.param_map[&id]); + fn begin_gesture(&self, index: usize) { + self.param_gestures.begin_gesture(index); if let Some(host_params) = self.extensions.host_params { unsafe { host_params.as_ref().request_flush.unwrap()(self.host.0) }; } } - fn end_gesture(&self, id: u32) { - self.param_gestures.end_gesture(self.param_map[&id]); + fn end_gesture(&self, index: usize) { + self.param_gestures.end_gesture(index); if let Some(host_params) = self.extensions.host_params { unsafe { host_params.as_ref().request_flush.unwrap()(self.host.0) }; } } - fn set_param(&self, id: u32, value: f64) { - self.param_gestures.set_value(self.param_map[&id], value); + fn set_param(&self, index: usize, value: f64) { + self.param_gestures.set_value(index, value); if let Some(host_params) = self.extensions.host_params { unsafe { host_params.as_ref().request_flush.unwrap()(self.host.0) }; @@ -198,7 +196,6 @@ impl Instance

{ let host = EditorHost::from_inner(Rc::new(ClapEditorHost { host: instance.host, extensions: main_thread_state.extensions, - param_map: Arc::clone(&instance.param_map), param_gestures: Arc::clone(&instance.param_gestures), })); let parent = unsafe { ParentWindow::from_raw(raw_parent) }; diff --git a/src/format/clap/instance.rs b/src/format/clap/instance.rs index a6000afa..4c0082f5 100644 --- a/src/format/clap/instance.rs +++ b/src/format/clap/instance.rs @@ -93,7 +93,7 @@ pub struct Instance { pub input_bus_map: Vec, pub output_bus_map: Vec, pub params: Vec, - pub param_map: Arc>, + pub param_map: HashMap, // Processor -> plugin parameter changes pub plugin_params: ParamValues, // Plugin -> processor parameter changes @@ -157,7 +157,7 @@ impl Instance

{ input_bus_map, output_bus_map, params, - param_map: Arc::new(param_map), + param_map, plugin_params: ParamValues::with_count(param_count), processor_params: ParamValues::with_count(param_count), param_gestures: Arc::new(ParamGestures::with_count(param_count)), @@ -182,19 +182,17 @@ impl Instance

{ fn sync_plugin(&self, main_thread_state: &mut MainThreadState

) { for (index, value) in self.plugin_params.poll() { - let id = self.params[index].id; - main_thread_state.plugin.set_param(id, value); + main_thread_state.plugin.set_param(index, value); if let Some(editor) = &mut main_thread_state.editor { - editor.param_changed(id, value); + editor.param_changed(index, value); } } } fn sync_processor(&self, processor: &mut P::Processor) { for (index, value) in self.processor_params.poll() { - let id = self.params[index].id; - processor.set_param(id, value); + processor.set_param(index, value); } } @@ -207,8 +205,7 @@ impl Instance

{ ) { for update in self.param_gestures.poll(gesture_states) { if let Some(value) = update.set_value { - let id = self.params[update.index].id; - processor.set_param(id, value); + processor.set_param(update.index, value); self.plugin_params.set(update.index, value); } @@ -478,10 +475,7 @@ impl Instance

{ process_state.events.push(Event { time: event.header.time as i64, - data: Data::ParamChange { - id: event.param_id, - value, - }, + data: Data::ParamChange { index, value }, }); instance.plugin_params.set(index, value); @@ -762,7 +756,7 @@ impl Instance

{ let param = &instance.params[index]; let value = unsafe { &mut *value }; - *value = map_param_out(param, main_thread_state.plugin.get_param(param_id)); + *value = map_param_out(param, main_thread_state.plugin.get_param(index)); return true; } @@ -784,7 +778,7 @@ impl Instance

{ let mut text = String::new(); let _ = main_thread_state.plugin.display_param( - param_id, + index, map_param_in(param, value), &mut text, ); @@ -810,7 +804,7 @@ impl Instance

{ if let Some(&index) = instance.param_map.get(¶m_id) { if let Ok(text) = unsafe { CStr::from_ptr(display) }.to_str() { let param = &instance.params[index]; - if let Some(out) = main_thread_state.plugin.parse_param(param_id, text) { + if let Some(out) = main_thread_state.plugin.parse_param(index, text) { let value = unsafe { &mut *value }; *value = map_param_out(param, out); return true; @@ -850,7 +844,7 @@ impl Instance

{ if let Some(&index) = instance.param_map.get(&event.param_id) { let value = map_param_in(&instance.params[index], event.value); - processor.set_param(event.param_id, value); + processor.set_param(index, value); instance.plugin_params.set(index, value); @@ -882,23 +876,21 @@ impl Instance

{ if let Some(&index) = instance.param_map.get(&event.param_id) { let value = map_param_in(&instance.params[index], event.value); - main_thread_state.plugin.set_param(event.param_id, value); + main_thread_state.plugin.set_param(index, value); if let Some(editor) = &mut main_thread_state.editor { - editor.param_changed(event.param_id, value); + editor.param_changed(index, value); } } } } for update in instance.param_gestures.poll(&mut process_state.gesture_states) { - let param = &instance.params[update.index]; - if let Some(value) = update.set_value { - main_thread_state.plugin.set_param(param.id, value); + main_thread_state.plugin.set_param(update.index, value); if let Some(editor) = &mut main_thread_state.editor { - editor.param_changed(param.id, value); + editor.param_changed(update.index, value); } } @@ -979,12 +971,12 @@ impl Instance

{ instance.sync_plugin(&mut *main_thread_state); if main_thread_state.plugin.load(&mut StreamReader(stream)).is_ok() { - for (index, param) in instance.params.iter().enumerate() { - let value = main_thread_state.plugin.get_param(param.id); + for (index, _param) in instance.params.iter().enumerate() { + let value = main_thread_state.plugin.get_param(index); instance.processor_params.set(index, value); if let Some(editor) = &mut main_thread_state.editor { - editor.param_changed(param.id, value); + editor.param_changed(index, value); } } diff --git a/src/format/clap/tests.rs b/src/format/clap/tests.rs index 16e3bb6f..a7a51b54 100644 --- a/src/format/clap/tests.rs +++ b/src/format/clap/tests.rs @@ -42,16 +42,16 @@ impl Plugin for TestPlugin { fn buses(&self, _build: impl BuildBuses) {} fn bus_configs(&self, _build: impl BuildBusConfigs) {} fn params(&self, _build: impl BuildParams) {} - fn set_param(&mut self, _id: u32, _value: f64) {} - fn get_param(&self, _id: u32) -> f64 { + fn set_param(&mut self, _index: usize, _value: f64) {} + fn get_param(&self, _index: usize) -> f64 { 0.0 } - fn parse_param(&self, _id: u32, _text: &str) -> Option { + fn parse_param(&self, _index: usize, _text: &str) -> Option { None } fn display_param( &self, - _id: u32, + _index: usize, _value: f64, _write: impl fmt::Write, ) -> Result<(), fmt::Error> { @@ -95,7 +95,7 @@ struct TestProcessor; impl Processor for TestProcessor { fn reset(&mut self) {} - fn set_param(&mut self, _id: u32, _value: f64) {} + fn set_param(&mut self, _index: usize, _value: f64) {} fn process(&mut self, _buffers: Buffers, _events: Events) {} } @@ -108,7 +108,7 @@ impl Editor for TestEditor { height: 0.0, } } - fn param_changed(&mut self, _id: u32, _value: f64) {} + fn param_changed(&mut self, _index: usize, _value: f64) {} } unsafe fn str_from_ptr<'a>(ptr: *const c_char) -> Result<&'a str, std::str::Utf8Error> { diff --git a/src/format/vst3/component.rs b/src/format/vst3/component.rs index 86fa9e83..3d2d53b4 100644 --- a/src/format/vst3/component.rs +++ b/src/format/vst3/component.rs @@ -60,7 +60,7 @@ pub struct Component { input_bus_map: Vec, output_bus_map: Vec, bus_config_set: HashSet>, - params: Vec, + params: Arc>, param_map: HashMap, plugin_params: ParamValues, processor_params: ParamValues, @@ -122,7 +122,7 @@ impl Component

{ input_bus_map, output_bus_map, bus_config_set, - params, + params: Arc::new(params), param_map, plugin_params: ParamValues::with_count(param_count), processor_params: ParamValues::with_count(param_count), @@ -149,15 +149,13 @@ impl Component

{ fn sync_plugin(&self, plugin: &mut P) { for (index, value) in self.plugin_params.poll() { - let id = self.params[index].id; - plugin.set_param(id, value); + plugin.set_param(index, value); } } fn sync_processor(&self, processor: &mut P::Processor) { for (index, value) in self.processor_params.poll() { - let id = self.params[index].id; - processor.set_param(id, value); + processor.set_param(index, value); } } } @@ -352,12 +350,12 @@ impl IComponentTrait for Component

{ self.sync_plugin(&mut main_thread_state.plugin); if main_thread_state.plugin.load(&mut StreamReader(state)).is_ok() { - for (index, param) in self.params.iter().enumerate() { - let value = main_thread_state.plugin.get_param(param.id); + for (index, _param) in self.params.iter().enumerate() { + let value = main_thread_state.plugin.get_param(index); self.processor_params.set(index, value); if let Some(editor) = &mut main_thread_state.editor { - editor.param_changed(param.id, value); + editor.param_changed(index, value); } } @@ -581,7 +579,10 @@ impl IAudioProcessorTrait for Component

{ process_state.events.push(Event { time: offset as i64, - data: Data::ParamChange { id, value }, + data: Data::ParamChange { + index: param_index, + value, + }, }); self.plugin_params.set(param_index, value); @@ -617,7 +618,7 @@ impl IAudioProcessorTrait for Component

{ continue; } - processor.set_param(id, value); + processor.set_param(param_index, value); self.plugin_params.set(param_index, value); } @@ -687,9 +688,9 @@ impl IEditControllerTrait for Component

{ ) -> tresult { let main_thread_state = self.main_thread_state.borrow(); - if self.param_map.contains_key(&id) { + if let Some(&index) = self.param_map.get(&id) { let mut text = String::new(); - let _ = main_thread_state.plugin.display_param(id, valueNormalized, &mut text); + let _ = main_thread_state.plugin.display_param(index, valueNormalized, &mut text); copy_wstring(&text, unsafe { &mut *string }); return kResultOk; @@ -706,9 +707,9 @@ impl IEditControllerTrait for Component

{ ) -> tresult { let main_thread_state = self.main_thread_state.borrow(); - if self.param_map.contains_key(&id) { + if let Some(&index) = self.param_map.get(&id) { if let Ok(display) = String::from_utf16(unsafe { utf16_from_ptr(string) }) { - if let Some(value) = main_thread_state.plugin.parse_param(id, &display) { + if let Some(value) = main_thread_state.plugin.parse_param(index, &display) { unsafe { *valueNormalized = value }; return kResultOk; } @@ -733,8 +734,8 @@ impl IEditControllerTrait for Component

{ unsafe fn getParamNormalized(&self, id: ParamID) -> ParamValue { let main_thread_state = self.main_thread_state.borrow(); - if self.param_map.contains_key(&id) { - return main_thread_state.plugin.get_param(id); + if let Some(&index) = self.param_map.get(&id) { + return main_thread_state.plugin.get_param(index); } 0.0 @@ -743,11 +744,11 @@ impl IEditControllerTrait for Component

{ unsafe fn setParamNormalized(&self, id: ParamID, value: ParamValue) -> tresult { let mut main_thread_state = self.main_thread_state.borrow(); - if self.param_map.contains_key(&id) { - main_thread_state.plugin.set_param(id, value); + if let Some(&index) = self.param_map.get(&id) { + main_thread_state.plugin.set_param(index, value); if let Some(editor) = &mut main_thread_state.editor { - editor.param_changed(id, value); + editor.param_changed(index, value); } return kResultOk; @@ -777,7 +778,7 @@ impl IEditControllerTrait for Component

{ return ptr::null_mut(); } - let view = ComWrapper::new(PlugView::new(&self.main_thread_state)); + let view = ComWrapper::new(PlugView::new(&self.params, &self.main_thread_state)); view.to_com_ptr::().unwrap().into_raw() } } diff --git a/src/format/vst3/tests.rs b/src/format/vst3/tests.rs index 5dca0080..97de8491 100644 --- a/src/format/vst3/tests.rs +++ b/src/format/vst3/tests.rs @@ -49,16 +49,16 @@ impl Plugin for TestPlugin { fn buses(&self, _build: impl BuildBuses) {} fn bus_configs(&self, _build: impl BuildBusConfigs) {} fn params(&self, _build: impl BuildParams) {} - fn set_param(&mut self, _id: u32, _value: f64) {} - fn get_param(&self, _id: u32) -> f64 { + fn set_param(&mut self, _index: usize, _value: f64) {} + fn get_param(&self, _index: usize) -> f64 { 0.0 } - fn parse_param(&self, _id: u32, _text: &str) -> Option { + fn parse_param(&self, _index: usize, _text: &str) -> Option { None } fn display_param( &self, - _id: u32, + _index: usize, _value: f64, _write: impl fmt::Write, ) -> Result<(), fmt::Error> { @@ -104,7 +104,7 @@ struct TestProcessor; impl Processor for TestProcessor { fn reset(&mut self) {} - fn set_param(&mut self, _id: u32, _value: f64) {} + fn set_param(&mut self, _index: usize, _value: f64) {} fn process(&mut self, _buffers: Buffers, _events: Events) {} } @@ -117,7 +117,7 @@ impl Editor for TestEditor { height: 0.0, } } - fn param_changed(&mut self, _id: u32, _value: f64) {} + fn param_changed(&mut self, _index: usize, _value: f64) {} } fn str_from_chars(chars: &[char8]) -> Result<&str, Box> { diff --git a/src/format/vst3/view.rs b/src/format/vst3/view.rs index c2809464..87c45f7c 100644 --- a/src/format/vst3/view.rs +++ b/src/format/vst3/view.rs @@ -9,47 +9,53 @@ use super::component::MainThreadState; use crate::editor::{Editor, EditorHost, EditorHostInner, ParentWindow, RawParent}; use crate::plugin::Plugin; use crate::sync::{sync_cell::SyncCell, thread_cell::ThreadCell}; -use crate::util::RequireSendSync; +use crate::util::{OwnedParamInfo, RequireSendSync}; struct Vst3EditorHost { handler: Option>, + params: Arc>, } impl EditorHostInner for Vst3EditorHost { - fn begin_gesture(&self, id: u32) { + fn begin_gesture(&self, index: usize) { if let Some(handler) = &self.handler { unsafe { - handler.beginEdit(id); + handler.beginEdit(self.params[index].id); } } } - fn end_gesture(&self, id: u32) { + fn end_gesture(&self, index: usize) { if let Some(handler) = &self.handler { unsafe { - handler.endEdit(id); + handler.endEdit(self.params[index].id); } } } - fn set_param(&self, id: u32, value: f64) { + fn set_param(&self, index: usize, value: f64) { if let Some(handler) = &self.handler { unsafe { - handler.performEdit(id, value); + handler.performEdit(self.params[index].id, value); } } } } pub struct PlugView { + params: Arc>, main_thread_state: Arc>>, } impl RequireSendSync for PlugView

{} impl PlugView

{ - pub fn new(main_thread_state: &Arc>>) -> PlugView

{ + pub fn new( + params: &Arc>, + main_thread_state: &Arc>>, + ) -> PlugView

{ PlugView { + params: params.clone(), main_thread_state: main_thread_state.clone(), } } @@ -99,6 +105,7 @@ impl IPlugViewTrait for PlugView

{ let host = EditorHost::from_inner(Rc::new(Vst3EditorHost { handler: main_thread_state.handler.clone(), + params: self.params.clone(), })); let parent = unsafe { ParentWindow::from_raw(raw_parent) }; let editor = main_thread_state.plugin.editor(host, &parent); diff --git a/src/params.rs b/src/params.rs index cc906c15..681df9bc 100644 --- a/src/params.rs +++ b/src/params.rs @@ -23,10 +23,15 @@ pub trait BuildParams { pub trait Params { fn params(&self, build: impl BuildParams); - fn set_param(&mut self, id: u32, value: f64); - fn get_param(&self, id: u32) -> f64; - fn parse_param(&self, id: u32, text: &str) -> Option; - fn display_param(&self, id: u32, value: f64, write: impl fmt::Write) -> Result<(), fmt::Error>; + fn set_param(&mut self, index: usize, value: f64); + fn get_param(&self, index: usize) -> f64; + fn parse_param(&self, index: usize, text: &str) -> Option; + fn display_param( + &self, + index: usize, + value: f64, + write: impl fmt::Write, + ) -> Result<(), fmt::Error>; } pub trait Enum: Encode + FromStr + Display {} diff --git a/src/plugin.rs b/src/plugin.rs index 6436b1d5..0c073810 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -30,10 +30,15 @@ pub trait Plugin: Send + Sized + 'static { fn bus_configs(&self, build: impl BuildBusConfigs); fn params(&self, build: impl BuildParams); - fn set_param(&mut self, id: u32, value: f64); - fn get_param(&self, id: u32) -> f64; - fn parse_param(&self, id: u32, text: &str) -> Option; - fn display_param(&self, id: u32, value: f64, write: impl fmt::Write) -> Result<(), fmt::Error>; + fn set_param(&mut self, index: usize, value: f64); + fn get_param(&self, index: usize) -> f64; + fn parse_param(&self, index: usize, text: &str) -> Option; + fn display_param( + &self, + index: usize, + value: f64, + write: impl fmt::Write, + ) -> Result<(), fmt::Error>; fn save(&self, output: impl io::Write) -> io::Result<()>; fn load(&mut self, input: impl io::Read) -> io::Result<()>; diff --git a/src/process.rs b/src/process.rs index 478e8c3e..9af51420 100644 --- a/src/process.rs +++ b/src/process.rs @@ -11,6 +11,6 @@ pub struct Config<'a> { pub trait Processor: Send + Sized + 'static { fn reset(&mut self); - fn set_param(&mut self, id: u32, value: f64); + fn set_param(&mut self, index: usize, value: f64); fn process(&mut self, buffers: Buffers, events: Events); }