1
0
Fork 0

a few more formatters

This commit is contained in:
Fredemus 2022-03-20 16:07:35 +01:00
parent d5d54f0c06
commit f0ee2739f1

View file

@ -7,6 +7,28 @@ pub fn f32_rounded(digits: usize) -> Arc<dyn Fn(f32) -> 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<dyn Fn(f32) -> 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<dyn Fn(f32) -> 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<dyn Fn(f32) -> 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<dyn Fn(f32) -> String + Send + Sync> {