From 2af93436a6c7bebcd1d257e7951c8680a7cf76ca Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Mon, 21 Mar 2022 14:14:24 +0100 Subject: [PATCH] Clean up panning formatters --- src/formatters.rs | 43 ++++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/src/formatters.rs b/src/formatters.rs index 746a5692..0577a993 100644 --- a/src/formatters.rs +++ b/src/formatters.rs @@ -1,5 +1,6 @@ //! Convenience functions for formatting and parsing parameter values in common formats. +use std::cmp::Ordering; use std::sync::Arc; use crate::util; @@ -45,38 +46,26 @@ pub fn from_f32_gain_to_db() -> Arc Option + Send + Sync> { }) } -/// Turn an `f32` value `[-1, 1]` to a panning value `[100L, 100R]` Value of `0.0` becomes `"C"` +/// Turn an `f32` `[-1, 1]` value to a panning value where negative values are represented by +/// `[100L, 1L]`, 0 gets turned into `C`, and positive values become `[1R, 100R]` values. pub fn f32_panning() -> Arc String + Send + Sync> { - Arc::new(move |x| { - if x == 0. { - "C".to_string() - } else if x > 0. { - format!("{:.0}R", x * 100.) - } else { - format!("{:.0}L", x * -100.) - } + Arc::new(move |value| match value.partial_cmp(&0.0) { + Some(Ordering::Less) => format!("{:.0}L", value * -100.0), + Some(Ordering::Equal) => String::from("C"), + Some(Ordering::Greater) => format!("{:.0}R", value * 100.0), + None => String::from("NaN"), }) } -/// Parse a pan value to a linear value, range `[-1, 1]`. Used in -/// conjunction with [`f32_panning`]. + +/// Parse a pan value in the format of [`f32_panning`] to a linear value in the range `[-1, 1]`. pub fn from_f32_panning() -> Arc Option + Send + Sync> { Arc::new(|string| { - if string.contains('L') { - string - .trim_end_matches(&[' ', 'L']) - .parse() - .ok() - .map(|x: f32| x / -100.) - } else if string.contains('R') { - string - .trim_end_matches(&[' ', 'R']) - .parse() - .ok() - .map(|x: f32| x / 100.) - } else if string == "C" { - Some(0.) - } else { - string.trim_end_matches(&[' ']).parse().ok().map(|x: f32| x) + let string = string.trim(); + let cleaned_string = string.trim_end_matches(&[' ', '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), + _ => Some(0.0), } }) }