gain_to_db formatter fix
Copy fix from v2s_f32_rounded
This commit is contained in:
parent
5ae23f52f4
commit
ff89e25d2f
1 changed files with 10 additions and 4 deletions
|
@ -79,18 +79,24 @@ pub fn s2v_compression_ratio() -> Arc<dyn Fn(&str) -> Option<f32> + Send + Sync>
|
||||||
|
|
||||||
/// Turn an `f32` value from voltage gain to decibels using the semantics described in
|
/// Turn an `f32` value from voltage gain to decibels using the semantics described in
|
||||||
/// [`util::gain_to_db()]. You should use either `" dB"` or `" dBFS"` for the parameter's unit.
|
/// [`util::gain_to_db()]. You should use either `" dB"` or `" dBFS"` for the parameter's unit.
|
||||||
/// `0.0` will be formatted as `-inf`.
|
/// `0.0` will be formatted as `-inf`. Avoids returning negative zero values to make sure
|
||||||
|
/// string->value->string roundtrips work correctly. Otherwise `-0.001` rounded to two digits
|
||||||
|
/// would result in `-0.00`.
|
||||||
pub fn v2s_f32_gain_to_db(digits: usize) -> Arc<dyn Fn(f32) -> String + Send + Sync> {
|
pub fn v2s_f32_gain_to_db(digits: usize) -> Arc<dyn Fn(f32) -> String + Send + Sync> {
|
||||||
|
let rounding_multiplier = 10u32.pow(digits as u32) as f32;
|
||||||
Arc::new(move |value| {
|
Arc::new(move |value| {
|
||||||
if value < util::MINUS_INFINITY_GAIN {
|
if value < util::MINUS_INFINITY_GAIN {
|
||||||
String::from("-inf")
|
String::from("-inf")
|
||||||
} else {
|
} else {
|
||||||
// Never print -0.0 since that just looks weird and confusing
|
|
||||||
let value_db = util::gain_to_db(value);
|
let value_db = util::gain_to_db(value);
|
||||||
let value_db = if value_db.abs() < 1e-6 { 0.0 } else { value_db };
|
|
||||||
|
|
||||||
|
// See above
|
||||||
|
if (value_db * rounding_multiplier).round() / rounding_multiplier == 0.0 {
|
||||||
|
format!("{:.digits$}", 0.0)
|
||||||
|
} else {
|
||||||
format!("{value_db:.digits$}")
|
format!("{value_db:.digits$}")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue