Skip to content

API Enhancement: PerceivedLinear that casts to Decibels #153

@j-ac

Description

@j-ac

In my project, I created an abstraction over Decibels called PerceivedLinear which uses a psychoacoustic principle that an increase of 10 decibels is roughly equivalent to a doubling in perceived volume.

With this abstraction 0.0 corresponds to -60 dB and 1.0 corresponds to 0 dB. 0.5 will sound "half as loud" as 1.0. Working with this unit is very useful because it allows me to consider the user's experience first rather than the physical reality of sound waves. Including something like this in Kira natively might be a good idea.

The equation that relates a PerceivedLinear value to a Decibel value is shown in the screenshot. I can share the derivation if necessary.

Image
#[derive(Copy, Clone)]
pub struct PerceivedLinear(pub f32);

impl From<PerceivedLinear> for Decibels {
    fn from(l: PerceivedLinear) -> Self {
        let db = 10. * ((1. / 64. + l.0 * (1. - 1. / 64.) as f32).log2()); // Equation from the screenshot
        Decibels(db)
    }
}

impl From<PerceivedLinear> for kira::Value<Decibels> {
    fn from(a: PerceivedLinear) -> Self {
        kira::Value::Fixed(a.into())
    }
}

impl Mul<f32> for PerceivedLinear {
    type Output = PerceivedLinear;

    fn mul(self, rhs: f32) -> Self::Output {
        PerceivedLinear(self.0 * rhs)
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions