1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use crate::{math::Float, Frame, Seek, Signal};

/// Smoothly maps a signal of any range into (-1, 1)
///
/// For each input sample `x`, outputs `x.tanh()`. Similar to [`Reinhard`](crate::Reinhard), but
/// distorts quiet sounds less, and loud sounds more.
pub struct Tanh<T>(T);

impl<T> Tanh<T> {
    /// Apply the hypberbolic tangent operator to `signal`
    pub fn new(signal: T) -> Self {
        Self(signal)
    }
}

impl<T: Signal> Signal for Tanh<T>
where
    T::Frame: Frame,
{
    type Frame = T::Frame;

    fn sample(&mut self, interval: f32, out: &mut [T::Frame]) {
        self.0.sample(interval, out);
        for x in out {
            for channel in x.channels_mut() {
                *channel = channel.tanh();
            }
        }
    }

    fn is_finished(&self) -> bool {
        self.0.is_finished()
    }
}

impl<T> Seek for Tanh<T>
where
    T: Signal + Seek,
    T::Frame: Frame,
{
    fn seek(&mut self, seconds: f32) {
        self.0.seek(seconds);
    }
}