Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow logarithmic amplification #715

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/source/amplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ where
Amplify { input, factor }
}

/// Internal function that converts decibels to linear
pub fn to_linear(decibels: f32) -> f32 {
f32::powf(10f32, decibels * 0.05)
}

/// Filter that modifies each sample by a given value.
#[derive(Clone, Debug)]
pub struct Amplify<I> {
Expand All @@ -26,6 +31,12 @@ impl<I> Amplify<I> {
self.factor = factor;
}

/// Modifies the amplification factor logarithmically.
#[inline]
pub fn set_log_factor(&mut self, factor: f32) {
self.factor = to_linear(factor);
}

/// Returns a reference to the inner source.
#[inline]
pub fn inner(&self) -> &I {
Expand Down Expand Up @@ -93,3 +104,50 @@ where
self.input.try_seek(pos)
}
}

#[cfg(test)]
mod test {
use super::*;

const EPSILON: f32 = 0.3;
/// Based on [Wikipedia's Decibel article].
///
/// [Wikipedia's Decibel article]: https://web.archive.org/web/20230810185300/https://en.wikipedia.org/wiki/Decibel
const DECIBELS_LINEAR_TABLE: [(f32, f32); 27] = [
(100., 100000.),
(90., 31623.),
(80., 10000.),
(70., 3162.),
(60., 1000.),
(50., 316.2),
(40., 100.),
(30., 31.62),
(20., 10.),
(10., 3.162),
(5.998, 1.995),
(3.003, 1.413),
(1.002, 1.122),
(0., 1.),
(-1.002, 0.891),
(-3.003, 0.708),
(-5.998, 0.501),
(-10., 0.3162),
(-20., 0.1),
(-30., 0.03162),
(-40., 0.01),
(-50., 0.003162),
(-60., 0.001),
(-70., 0.0003162),
(-80., 0.0001),
(-90., 0.00003162),
(-100., 0.00001),
];

#[test]
fn convert_decibels_to_linear() {
for (db, linear) in DECIBELS_LINEAR_TABLE {
dbg!(db, linear, f32::abs(to_linear(db) - linear));
assert!(f32::abs(to_linear(db) - linear) < EPSILON,)
}
}
}
10 changes: 10 additions & 0 deletions src/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use core::time::Duration;

use crate::common::{ChannelCount, SampleRate};
use crate::Sample;
use amplify::to_linear;
use dasp_sample::FromSample;

pub use self::agc::AutomaticGainControl;
Expand Down Expand Up @@ -233,6 +234,15 @@ pub trait Source: Iterator<Item = Sample> {
skip::skip_duration(self, duration)
}

/// Amplifies the sound logarithmically by the given value.
#[inline]
fn amplify_decibel(self, value: f32) -> Amplify<Self>
where
Self: Sized,
{
amplify::amplify(self, to_linear(value))
}

/// Amplifies the sound by the given value.
#[inline]
fn amplify(self, value: f32) -> Amplify<Self>
Expand Down