oddio/
tanh.rs

1use crate::{math::Float, Frame, Seek, Signal};
2
3/// Smoothly maps a signal of any range into (-1, 1)
4///
5/// For each input sample `x`, outputs `x.tanh()`. Similar to [`Reinhard`](crate::Reinhard), but
6/// distorts quiet sounds less, and loud sounds more.
7pub struct Tanh<T>(T);
8
9impl<T> Tanh<T> {
10    /// Apply the hypberbolic tangent operator to `signal`
11    pub fn new(signal: T) -> Self {
12        Self(signal)
13    }
14}
15
16impl<T: Signal> Signal for Tanh<T>
17where
18    T::Frame: Frame,
19{
20    type Frame = T::Frame;
21
22    fn sample(&mut self, interval: f32, out: &mut [T::Frame]) {
23        self.0.sample(interval, out);
24        for x in out {
25            for channel in x.channels_mut() {
26                *channel = channel.tanh();
27            }
28        }
29    }
30
31    fn is_finished(&self) -> bool {
32        self.0.is_finished()
33    }
34}
35
36impl<T> Seek for Tanh<T>
37where
38    T: Signal + Seek,
39    T::Frame: Frame,
40{
41    fn seek(&mut self, seconds: f32) {
42        self.0.seek(seconds);
43    }
44}