Skip to content

Commit

Permalink
Merge pull request #34 from Serdnad/add-atan-shape
Browse files Browse the repository at this point in the history
Add atan shape
  • Loading branch information
SamiPerttu authored Oct 6, 2023
2 parents ade89cc + 0ca823f commit 56f7054
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,7 @@ These are arguments to the `shape` opcode.
- `Shape::Clip`: Clip signal to -1...1.
- `Shape::ClipTo(minimum, maximum)`: Clip signal between the two arguments.
- `Shape::Tanh(hardness)`: Apply `tanh` distortion with configurable hardness. Argument to `tanh` is multiplied by the hardness value.
- `Shape::Atan(hardness)`: Apply `atan` distortion with configurable hardness. Argument to `atan` is multiplied by the hardness value.
- `Shape::Softsign(hardness)`: Apply `softsign` distortion with configurable hardness. Argument to `softsign` is multiplied by the hardness value.
- `Shape::Crush(levels)`: Apply a staircase function with configurable number of levels per unit.
- `Shape::SoftCrush(levels)`: Apply a smooth staircase function with configurable number of levels per unit.
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ pub trait Real: Num + Float {
fn cos(self) -> Self;
fn tan(self) -> Self;
fn tanh(self) -> Self;
fn atan(self) -> Self;
}

macro_rules! impl_real {
Expand All @@ -242,6 +243,7 @@ macro_rules! impl_real {
#[inline] fn cos(self) -> Self { self.cos() }
#[inline] fn tan(self) -> Self { <$t>::tan(self) }
#[inline] fn tanh(self) -> Self { <$t>::tanh(self) }
#[inline] fn atan(self) -> Self { <$t>::atan(self) }
}) *
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,16 @@ pub fn cos<T: Real>(x: T) -> T {
pub fn tan<T: Real>(x: T) -> T {
x.tan()
}
/// Hyperbolic tangent function. Squashes `x` to -1...1.
/// Hyperbolic tangent function. Squashes `x` to (-1, 1).
#[inline]
pub fn tanh<T: Real>(x: T) -> T {
x.tanh()
}
/// Inverse tangent function. Squashes `x` to (-π/2, π/2).
#[inline]
pub fn atan<T: Real>(x: T) -> T {
x.atan()
}

/// sqrt(2)
pub const SQRT_2: f64 = std::f64::consts::SQRT_2;
Expand Down
9 changes: 9 additions & 0 deletions src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ pub enum Shape<T: Real> {
/// Apply `tanh` distortion with configurable hardness.
/// Argument to `tanh` is multiplied by the hardness value.
Tanh(T),
/// Apply `atan` distortion with configurable hardness.
/// Argument to `atan` is multiplied by the hardness value.
ATan(T),
/// Apply `softsign` distortion with configurable hardness.
/// Argument to `softsign` is multiplied by the hardness value.
Softsign(T),
Expand Down Expand Up @@ -138,6 +141,7 @@ impl<T: Real> AudioNode for Shaper<T> {
Shape::Clip => [clamp11(input)].into(),
Shape::ClipTo(min, max) => [clamp(min, max, input)].into(),
Shape::Tanh(hardness) => [tanh(input * hardness)].into(),
Shape::ATan(hardness) => [atan(input * hardness)].into(),
Shape::Softsign(hardness) => [softsign(input * hardness)].into(),
Shape::Crush(levels) => [round(input * levels) / levels].into(),
Shape::SoftCrush(levels) => {
Expand Down Expand Up @@ -177,6 +181,11 @@ impl<T: Real> AudioNode for Shaper<T> {
*x = tanh(*y * hardness);
}
}
Shape::ATan(hardness) => {
for (x, y) in output[0..size].iter_mut().zip(input[0..size].iter()) {
*x = atan(*y * hardness);
}
}
Shape::Softsign(hardness) => {
for (x, y) in output[0..size].iter_mut().zip(input[0..size].iter()) {
*x = softsign(*y * hardness);
Expand Down

0 comments on commit 56f7054

Please sign in to comment.