From f0ee2739f13c2a501db647a2d0d7f8d1810b0bea Mon Sep 17 00:00:00 2001 From: Fredemus Date: Sun, 20 Mar 2022 16:07:35 +0100 Subject: [PATCH] a few more formatters --- src/formatters.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/formatters.rs b/src/formatters.rs index 2d0b9073..ab6c3083 100644 --- a/src/formatters.rs +++ b/src/formatters.rs @@ -7,6 +7,28 @@ pub fn f32_rounded(digits: usize) -> Arc String + Send + Sync> { Arc::new(move |x| format!("{:.digits$}", x)) } +/// Turn an `f32` value from linear to dBFS (reference value 1) +pub fn f32_lin_to_db(digits: usize) -> Arc String + Send + Sync> { + Arc::new(move |x| format!("{:.digits$}", x.log10() * 20.0)) +} + +/// Turn an `f32` value from dBFS (reference value 1) to linear +pub fn f32_db_to_lin(digits: usize) -> Arc String + Send + Sync> { + Arc::new(move |x| format!("{:.digits$}", 10f32.powf(x / 20.0))) +} + +/// Round an `f32` value and divide it by 1000 when it gets over 1000 +pub fn f32_hertz_then_khz(digits: usize) -> Arc String + Send + Sync> { + Arc::new(move |x| { + if x < 1000.0 { + format!("{:.digits$}", x) + } else { + let digits = digits + 1; + format!("{:.digits$}", x / 1000.0) + } + }) +} + /// Format a `[0, 1]` number as a percentage. Does not include the percent sign, you should specify /// this as the parameter's unit. pub fn f32_percentage(digits: usize) -> Arc String + Send + Sync> {