use std::time::Duration;
use crate::{
Decibels, Mix, Value,
effect::{Effect, EffectBuilder},
};
use super::{Compressor, CompressorHandle, command_writers_and_readers};
pub struct CompressorBuilder {
pub threshold: Value<f64>,
pub ratio: Value<f64>,
pub attack_duration: Value<Duration>,
pub release_duration: Value<Duration>,
pub makeup_gain: Value<Decibels>,
pub mix: Value<Mix>,
}
impl CompressorBuilder {
pub(crate) const DEFAULT_THRESHOLD: f64 = 0.0;
pub(crate) const DEFAULT_RATIO: f64 = 1.0;
pub(crate) const DEFAULT_ATTACK_DURATION: Duration = Duration::from_millis(10);
pub(crate) const DEFAULT_RELEASE_DURATION: Duration = Duration::from_millis(100);
pub(crate) const DEFAULT_MAKEUP_GAIN: Decibels = Decibels(0.0);
pub(crate) const DEFAULT_MIX: Mix = Mix::WET;
#[must_use]
pub fn new() -> Self {
Self {
threshold: Value::Fixed(Self::DEFAULT_THRESHOLD),
ratio: Value::Fixed(Self::DEFAULT_RATIO),
attack_duration: Value::Fixed(Self::DEFAULT_ATTACK_DURATION),
release_duration: Value::Fixed(Self::DEFAULT_RELEASE_DURATION),
makeup_gain: Value::Fixed(Self::DEFAULT_MAKEUP_GAIN),
mix: Value::Fixed(Self::DEFAULT_MIX),
}
}
#[must_use = "This method consumes self and returns a modified CompressorBuilder, so the return value should be used"]
pub fn threshold(self, threshold: impl Into<Value<f64>>) -> Self {
Self {
threshold: threshold.into(),
..self
}
}
#[must_use = "This method consumes self and returns a modified CompressorBuilder, so the return value should be used"]
pub fn ratio(self, ratio: impl Into<Value<f64>>) -> Self {
Self {
ratio: ratio.into(),
..self
}
}
#[must_use = "This method consumes self and returns a modified CompressorBuilder, so the return value should be used"]
pub fn attack_duration(self, attack_duration: impl Into<Value<Duration>>) -> Self {
Self {
attack_duration: attack_duration.into(),
..self
}
}
#[must_use = "This method consumes self and returns a modified CompressorBuilder, so the return value should be used"]
pub fn release_duration(self, release_duration: impl Into<Value<Duration>>) -> Self {
Self {
release_duration: release_duration.into(),
..self
}
}
#[must_use = "This method consumes self and returns a modified CompressorBuilder, so the return value should be used"]
pub fn makeup_gain(self, makeup_gain: impl Into<Value<Decibels>>) -> Self {
Self {
makeup_gain: makeup_gain.into(),
..self
}
}
#[must_use = "This method consumes self and returns a modified CompressorBuilder, so the return value should be used"]
pub fn mix(self, mix: impl Into<Value<Mix>>) -> Self {
Self {
mix: mix.into(),
..self
}
}
}
impl Default for CompressorBuilder {
fn default() -> Self {
Self::new()
}
}
impl EffectBuilder for CompressorBuilder {
type Handle = CompressorHandle;
fn build(self) -> (Box<dyn Effect>, Self::Handle) {
let (command_writers, command_readers) = command_writers_and_readers();
(
Box::new(Compressor::new(self, command_readers)),
CompressorHandle { command_writers },
)
}
}