use std::sync::Arc;
use crate::{
Value,
modulator::{Modulator, ModulatorBuilder, ModulatorId},
};
use super::{Lfo, LfoShared, Waveform, command_writers_and_readers, handle::LfoHandle};
pub struct LfoBuilder {
pub waveform: Waveform,
pub frequency: Value<f64>,
pub amplitude: Value<f64>,
pub offset: Value<f64>,
pub starting_phase: f64,
}
impl LfoBuilder {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use = "This method consumes self and returns a modified LfoBuilder, so the return value should be used"]
pub fn waveform(self, waveform: Waveform) -> Self {
Self { waveform, ..self }
}
#[must_use = "This method consumes self and returns a modified LfoBuilder, so the return value should be used"]
pub fn frequency(self, frequency: impl Into<Value<f64>>) -> Self {
Self {
frequency: frequency.into(),
..self
}
}
#[must_use = "This method consumes self and returns a modified LfoBuilder, so the return value should be used"]
pub fn amplitude(self, amplitude: impl Into<Value<f64>>) -> Self {
Self {
amplitude: amplitude.into(),
..self
}
}
#[must_use = "This method consumes self and returns a modified LfoBuilder, so the return value should be used"]
pub fn offset(self, offset: impl Into<Value<f64>>) -> Self {
Self {
offset: offset.into(),
..self
}
}
#[must_use = "This method consumes self and returns a modified LfoBuilder, so the return value should be used"]
pub fn starting_phase(self, starting_phase: f64) -> Self {
Self {
starting_phase,
..self
}
}
}
impl Default for LfoBuilder {
fn default() -> Self {
Self {
waveform: Waveform::Sine,
frequency: Value::Fixed(2.0),
amplitude: Value::Fixed(1.0),
offset: Value::Fixed(0.0),
starting_phase: 0.0,
}
}
}
impl ModulatorBuilder for LfoBuilder {
type Handle = LfoHandle;
fn build(self, id: ModulatorId) -> (Box<dyn Modulator>, Self::Handle) {
let (command_writers, command_readers) = command_writers_and_readers();
let shared = Arc::new(LfoShared::new());
(
Box::new(Lfo::new(&self, command_readers, shared.clone())),
LfoHandle {
id,
command_writers,
shared,
},
)
}
}