diff --git a/src/effects/effect1.rs b/src/effects/effect1.rs index 2dd336d..978c779 100644 --- a/src/effects/effect1.rs +++ b/src/effects/effect1.rs @@ -1,7 +1,6 @@ -use crate::effects::Effect; use nih_plug::prelude::*; use std::sync::Arc; -use std::any::Any; +use crate::effects::Effect; #[derive(Params)] pub struct Effect1Params { @@ -23,14 +22,6 @@ impl Default for Effect1Params { } } -impl Clone for Effect1Params { - fn clone(&self) -> Self { - Self { - gain: self.gain.clone(), - } - } -} - pub struct Effect1 { params: Arc, } @@ -44,14 +35,10 @@ impl Effect1 { } impl Effect for Effect1 { - fn process(&self, samples: &mut [f32], _sample_rate: f32, params: &dyn Params) { - let gain = if let Some(p) = params.as_any().downcast_ref::() { - p.gain.value() - } else { - 1.0 // Default value if downcast fails - }; - for sample in samples { - *sample *= gain; + fn process(&self, samples: &mut [f32], _sample_rate: f32, _params: &dyn Params) { + // Beispiel: einfacher Gain-Prozess + for sample in samples.iter_mut() { + *sample *= self.params.gain.value(); // Anwendung des Gain-Werts } } } diff --git a/src/effects/effect2.rs b/src/effects/effect2.rs index 222c198..7431516 100644 --- a/src/effects/effect2.rs +++ b/src/effects/effect2.rs @@ -1,21 +1,20 @@ -use crate::effects::Effect; use nih_plug::prelude::*; use std::sync::Arc; -use std::any::Any; +use crate::effects::Effect; #[derive(Params)] pub struct Effect2Params { - #[id = "mix"] - pub mix: FloatParam, + #[id = "gainnnnnn"] + pub gain: FloatParam, } impl Default for Effect2Params { fn default() -> Self { Self { - mix: FloatParam::new( - "Mix", - 0.5, - FloatRange::Linear { min: 0.0, max: 1.0 }, + gain: FloatParam::new( + "Gainnnnnnnn", + 1.0, + FloatRange::Linear { min: 0.0, max: 2.0 }, ) .with_smoother(SmoothingStyle::Linear(50.0)) .with_step_size(0.01), @@ -23,14 +22,6 @@ impl Default for Effect2Params { } } -impl Clone for Effect2Params { - fn clone(&self) -> Self { - Self { - mix: self.mix.clone(), - } - } -} - pub struct Effect2 { params: Arc, } @@ -44,14 +35,10 @@ impl Effect2 { } impl Effect for Effect2 { - fn process(&self, samples: &mut [f32], _sample_rate: f32, params: &dyn Params) { - let mix = if let Some(p) = params.as_any().downcast_ref::() { - p.mix.value() - } else { - 0.5 // Default value if downcast fails - }; - for sample in samples { - *sample = *sample * mix + *sample * (1.0 - mix); + fn process(&self, samples: &mut [f32], _sample_rate: f32, _params: &dyn Params) { + // Beispiel: einfacher Gain-Prozess + for sample in samples.iter_mut() { + *sample *= self.params.gain.value(); // Anwendung des Gain-Werts } } } diff --git a/src/effects/mod.rs b/src/effects/mod.rs index dc7d5b3..dde3c55 100644 --- a/src/effects/mod.rs +++ b/src/effects/mod.rs @@ -1,69 +1,55 @@ use nih_plug::prelude::*; use std::any::Any; +use nih_plug::prelude::Enum; + pub mod effect1; pub mod effect2; -/// Das allgemeine Effekt-Interface, das für alle Effekte implementiert wird. pub trait Effect: Send { fn process(&self, samples: &mut [f32], sample_rate: f32, params: &dyn Params); } -/// Enum zur Auswahl der Effekte -#[derive(Debug, Clone, Copy, PartialEq, Eq, Enum)] +#[derive(Copy, Clone, PartialEq, Eq)] pub enum EffectType { Effect1, Effect2, } -impl std::fmt::Display for EffectType { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - EffectType::Effect1 => write!(f, "Effect 1"), - EffectType::Effect2 => write!(f, "Effect 2"), - } +impl Default for EffectType { + fn default() -> Self { + EffectType::Effect1 } } -pub trait AsAny { - fn as_any(&self) -> &dyn Any; -} - -impl AsAny for T { - fn as_any(&self) -> &dyn Any { self } -} - -impl AsAny for dyn Params {} - - -impl EffectType { +// Implementierung des Enum-Traits für EffectType +impl Enum for EffectType { fn variants() -> &'static [&'static str] { - &["Effect 1", "Effect 2"] + &["Effect1", "Effect2"] + } + + fn ids() -> Option<&'static [&'static str]> { + Some(&["effect1", "effect2"]) } fn to_index(self) -> usize { - match self { - EffectType::Effect1 => 0, - EffectType::Effect2 => 1, - } + self as usize } fn from_index(index: usize) -> Self { match index { 0 => EffectType::Effect1, 1 => EffectType::Effect2, - _ => EffectType::Effect1, + _ => EffectType::Effect1, // Fallback } } +} - fn ids() -> Option<&'static [&'static str]> { - Some(&["effect1", "effect2"]) - } +pub trait AsAny { + fn as_any(&self) -> &dyn Any; } -/// Factory-Funktion, die basierend auf dem Effekt-Typ das entsprechende Effekt-Objekt erzeugt. -pub fn create_effect(effect_type: EffectType) -> Box { - match effect_type { - EffectType::Effect1 => Box::new(effect1::Effect1::new()), - EffectType::Effect2 => Box::new(effect2::Effect2::new()), +impl AsAny for T { + fn as_any(&self) -> &dyn Any { + self } } diff --git a/src/lib.rs b/src/lib.rs index 9e6f701..c9c0b49 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,6 @@ mod params; use effects::Effect; use params::PluginParams; -use crate::effects::EffectType; struct VariableEffects { params: Arc, @@ -56,21 +55,24 @@ impl Plugin for VariableEffects { _aux: &mut AuxiliaryBuffers, context: &mut impl ProcessContext, ) -> ProcessStatus { - let effect_index = self.params.effect_type.value().to_index(); + // Aktuellen Effekt auswählen + let effect_index = self.params.effect_type.value() as usize; let sample_rate = context.transport().sample_rate; - + for mut channel_samples in buffer.iter_samples() { + // Daten vorbereiten let mut samples: Vec = channel_samples.iter_mut().map(|s| *s).collect(); - match self.params.effect_type.value() { - EffectType::Effect1 => { - self.effects[effect_index].process(&mut samples, sample_rate, &self.params.effect1_params); - }, - EffectType::Effect2 => { - self.effects[effect_index].process(&mut samples, sample_rate, &self.params.effect2_params); - }, + // Effekt verarbeiten + if effect_index < self.effects.len() { + self.effects[effect_index].process( + &mut samples, + sample_rate, + self.params.get_params(effect_index), + ); } + // Zurück in den Puffer schreiben for (out, &processed) in channel_samples.iter_mut().zip(samples.iter()) { *out = processed; } diff --git a/src/params.rs b/src/params.rs index 1a9fdcd..773e549 100644 --- a/src/params.rs +++ b/src/params.rs @@ -1,6 +1,5 @@ use nih_plug::prelude::*; -use crate::effects::{EffectType, effect1, effect2, AsAny}; -use std::sync::Arc; +use crate::effects::{EffectType, effect1, effect2}; #[derive(Params)] pub struct PluginParams { @@ -28,12 +27,12 @@ impl Default for PluginParams { } impl PluginParams { - pub fn active_params(&self) -> Arc { - match self.effect_type.value() { - EffectType::Effect1 => Arc::new(self.effect1_params.clone()), - EffectType::Effect2 => Arc::new(self.effect2_params.clone()), + // Diese Methode gibt die entsprechenden Parameter basierend auf dem Effektindex zurück + pub fn get_params(&self, effect_index: usize) -> &dyn Params { + match effect_index { + 0 => &self.effect1_params, + 1 => &self.effect2_params, + _ => &self.effect1_params, // Fallback zu Effect1 bei ungültigem Index } } } - -impl AsAny for PluginParams {} diff --git a/src/traits.rs b/src/traits.rs new file mode 100644 index 0000000..4e9a2de --- /dev/null +++ b/src/traits.rs @@ -0,0 +1,13 @@ +use std::any::Any; + +// Definition des AsAny-Traits +pub trait AsAny { + fn as_any(&self) -> &dyn Any; +} + +// Implementierung des AsAny-Traits für alle Typen, die Any implementieren +impl AsAny for T { + fn as_any(&self) -> &dyn Any { + self + } +}