1
0
Fork 0

Add a matching from_f32_hz_then_khz function

This commit is contained in:
Robbert van der Helm 2022-03-21 14:28:28 +01:00
parent 44aa3dd564
commit e90bfecc0b

View file

@ -39,7 +39,7 @@ pub fn f32_gain_to_db(digits: usize) -> Arc<dyn Fn(f32) -> String + Send + Sync>
pub fn from_f32_gain_to_db() -> Arc<dyn Fn(&str) -> Option<f32> + Send + Sync> {
Arc::new(|string| {
string
.trim_end_matches(&[' ', 'd', 'B', 'F', 'S'])
.trim_end_matches(&[' ', 'd', 'D', 'b', 'B', 'f', 'F', 's', 'S'])
.parse()
.ok()
.map(util::db_to_gain)
@ -61,7 +61,7 @@ pub fn f32_panning() -> Arc<dyn Fn(f32) -> String + Send + Sync> {
pub fn from_f32_panning() -> Arc<dyn Fn(&str) -> Option<f32> + Send + Sync> {
Arc::new(|string| {
let string = string.trim();
let cleaned_string = string.trim_end_matches(&[' ', 'L']).parse().ok();
let cleaned_string = string.trim_end_matches(&[' ', 'l', 'L']).parse().ok();
match string.chars().last()?.to_uppercase().next()? {
'L' => cleaned_string.map(|x: f32| x / -100.0),
'R' => cleaned_string.map(|x: f32| x / 100.0),
@ -82,6 +82,22 @@ pub fn f32_hz_then_khz(digits: usize) -> Arc<dyn Fn(f32) -> String + Send + Sync
})
}
/// Convert an input in the same format at that of [`f32_hz_then_khz`] to a Hertz value.
pub fn from_f32_hz_then_khz() -> Arc<dyn Fn(&str) -> Option<f32> + Send + Sync> {
Arc::new(move |string| {
let string = string.trim();
let cleaned_string = string
.trim_end_matches(&[' ', 'k', 'K', 'h', 'H', 'z', 'Z'])
.parse()
.ok();
match string.get(string.len() - 3..) {
Some(unit) if unit.eq_ignore_ascii_case("khz") => cleaned_string.map(|x| x * 1000.0),
// Even if there's no unit at all, just assume the input is in Hertz
_ => cleaned_string,
}
})
}
/// Format an order/power of two. Useful in conjunction with [`from_power_of_two()`] to limit
/// integer parameter ranges to be only powers of two.
pub fn i32_power_of_two() -> Arc<dyn Fn(i32) -> String + Send + Sync> {