#amplitude #sample #dsp

decibel

Quick conversion utilities for decibel values

3 releases

Uses old Rust 2015

0.1.2 Mar 18, 2016
0.1.1 Mar 18, 2016
0.1.0 Mar 16, 2016

#9 in #amplitude

36 downloads per month
Used in riff-wave

Apache-2.0

9KB
93 lines

decibel

Quick conversion utilities for decibel values.


lib.rs:

Conversion utilities to convert between amplitudes and decibels.

See also: [Decibel][1] and [dBFS][2]. [1]: https://en.wikipedia.org/wiki/Decibel [2]: https://en.wikipedia.org/wiki/DBFS

Converting amplitude values into decibel values

To convert from an amplitude into a decibel value, call into() on the AmplitudeRatio.

When used for normalized amplitudes in the range of 0 to 1, this will give the value in dBFS (decibels relative to full scale).

Example

extern crate decibel;

use decibel::{AmplitudeRatio, DecibelRatio};

fn main() {
    // An amplitude halfway between 1 and zero should be close to -6 dBFS.
    let result: DecibelRatio<_> = AmplitudeRatio(0.5).into();
    let expected_decibels = -6.02059991327962;
    assert!(result.decibel_value() >= expected_decibels - 0.001
         && result.decibel_value() <= expected_decibels + 0.001);
}

Converting decibel values into amplitude values

To convert from a decibel value into an amplitude, call into() on the DecibelRatio.

Example

Let's say we want to scale our audio by 10dB. To figure out how much we need to scale each sample by, let's convert this into an amplitude ratio:

extern crate decibel;

use decibel::{AmplitudeRatio, DecibelRatio};

fn main() {
    // A +10dB gain should require us to scale each sample by around
    // 3.1622776601683795.
    let result: AmplitudeRatio<_> = DecibelRatio(10.0).into();
    let expected_amplitude = 3.1622776601683795;
    assert!(result.amplitude_value() >= expected_amplitude - 0.001
         && result.amplitude_value() <= expected_amplitude + 0.001);
}

To scale our audio by 10dB, we need to scale each sample by approximately 3.162 times.

No runtime deps