2022-03-27 12:43:39 +11:00
|
|
|
//! Convenience functions for formatting and parsing parameter values in various common formats.
|
|
|
|
//!
|
|
|
|
//! Functions prefixed with `v2s_` are meant to be used with the `.value_to_string()` parameter
|
|
|
|
//! fucntions, while the `s2v_` functions are meant to be used wit the `.string_to_value()`.
|
|
|
|
//! functions. Most of these formatters come as a pair. Check each formatter's documentation for any
|
|
|
|
//! additional usage information.
|
2022-01-29 00:33:29 +11:00
|
|
|
|
2022-03-22 00:14:24 +11:00
|
|
|
use std::cmp::Ordering;
|
2022-02-01 06:44:10 +11:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-03-21 23:59:31 +11:00
|
|
|
use crate::util;
|
|
|
|
|
2022-03-27 12:43:39 +11:00
|
|
|
// TODO: The v2s and s2v naming convention isn't ideal, but at least it's unambiguous. Is there a
|
|
|
|
// better way to name these functions? Should we just split this up into two modules?
|
|
|
|
|
2022-01-29 00:33:29 +11:00
|
|
|
/// Round an `f32` value to always have a specific number of decimal digits.
|
2022-03-27 12:43:39 +11:00
|
|
|
pub fn v2s_f32_rounded(digits: usize) -> Arc<dyn Fn(f32) -> String + Send + Sync> {
|
2022-03-21 23:59:31 +11:00
|
|
|
Arc::new(move |value| format!("{:.digits$}", value))
|
2022-01-29 00:33:29 +11:00
|
|
|
}
|
2022-03-21 23:59:31 +11:00
|
|
|
|
2022-03-21 07:15:17 +11:00
|
|
|
/// Format a `[0, 1]` number as a percentage. Does not include the percent sign, you should specify
|
|
|
|
/// this as the parameter's unit.
|
2022-03-27 12:43:39 +11:00
|
|
|
pub fn v2s_f32_percentage(digits: usize) -> Arc<dyn Fn(f32) -> String + Send + Sync> {
|
2022-03-21 07:15:17 +11:00
|
|
|
Arc::new(move |value| format!("{:.digits$}", value * 100.0))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse a `[0, 100]` percentage to a `[0, 1]` number. Handles the percentage unit for you. Used in
|
2022-03-27 12:43:39 +11:00
|
|
|
/// conjunction with [`v2s_f32_percentage()`].
|
|
|
|
pub fn s2v_f32_percentage() -> Arc<dyn Fn(&str) -> Option<f32> + Send + Sync> {
|
2022-03-21 07:15:17 +11:00
|
|
|
Arc::new(|string| {
|
|
|
|
string
|
|
|
|
.trim_end_matches(&[' ', '%'])
|
|
|
|
.parse()
|
|
|
|
.ok()
|
|
|
|
.map(|x: f32| x / 100.0)
|
|
|
|
})
|
|
|
|
}
|
2022-03-09 04:30:06 +11:00
|
|
|
|
2022-07-21 04:11:24 +10:00
|
|
|
/// Format a positive number as a compression ratio. A value of 4 will be formatted as `4.0:1` while
|
|
|
|
/// 0.25 is formatted as `1:4.0`.
|
|
|
|
pub fn v2s_compression_ratio(digits: usize) -> Arc<dyn Fn(f32) -> String + Send + Sync> {
|
|
|
|
Arc::new(move |value| {
|
|
|
|
if value >= 1.0 {
|
|
|
|
format!("{:.digits$}:1", value)
|
|
|
|
} else {
|
|
|
|
format!("1:{:.digits$}", value.recip())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse a `x:y` compression ratio back to a floating point number. Used in conjunction with
|
|
|
|
/// [`v2s_compression_ratio()`].
|
|
|
|
pub fn s2v_compression_ratio() -> Arc<dyn Fn(&str) -> Option<f32> + Send + Sync> {
|
|
|
|
Arc::new(|string| {
|
|
|
|
let (numerator, denominator) = string.trim().split_once(':')?;
|
|
|
|
let numerator: f32 = numerator.trim().parse().ok()?;
|
|
|
|
let denominator: f32 = denominator.trim().parse().ok()?;
|
|
|
|
|
|
|
|
Some(numerator / denominator)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-21 23:59:31 +11:00
|
|
|
/// Turn an `f32` value from voltage gain to decibels using the semantics described in
|
2022-03-27 12:43:39 +11:00
|
|
|
/// [`util::gain_to_db()]. You should use either `" dB"` or `" dBFS"` for the parameter's unit.
|
|
|
|
pub fn v2s_f32_gain_to_db(digits: usize) -> Arc<dyn Fn(f32) -> String + Send + Sync> {
|
2022-07-25 05:20:53 +10:00
|
|
|
Arc::new(move |value| {
|
|
|
|
// Never print -0.0 since that just looks weird and confusing
|
|
|
|
let value_db = util::gain_to_db(value);
|
|
|
|
let value_db = if value_db.abs() < 1e-6 { 0.0 } else { value_db };
|
|
|
|
|
|
|
|
format!("{:.digits$}", value_db)
|
|
|
|
})
|
2022-03-21 02:07:35 +11:00
|
|
|
}
|
2022-03-21 23:59:31 +11:00
|
|
|
|
|
|
|
/// Parse a decibel value to a linear voltage gain ratio. Handles the `dB` or `dBFS` units for you.
|
2022-03-27 12:43:39 +11:00
|
|
|
/// Used in conjunction with [`v2s_f32_gain_to_db()`].
|
|
|
|
pub fn s2v_f32_gain_to_db() -> Arc<dyn Fn(&str) -> Option<f32> + Send + Sync> {
|
2022-03-21 07:15:17 +11:00
|
|
|
Arc::new(|string| {
|
|
|
|
string
|
2022-03-22 00:28:28 +11:00
|
|
|
.trim_end_matches(&[' ', 'd', 'D', 'b', 'B', 'f', 'F', 's', 'S'])
|
2022-03-21 07:15:17 +11:00
|
|
|
.parse()
|
|
|
|
.ok()
|
2022-03-21 23:59:31 +11:00
|
|
|
.map(util::db_to_gain)
|
2022-03-21 07:15:17 +11:00
|
|
|
})
|
|
|
|
}
|
2022-03-21 23:59:31 +11:00
|
|
|
|
2022-03-22 00:14:24 +11:00
|
|
|
/// 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.
|
2022-03-27 12:43:39 +11:00
|
|
|
pub fn v2s_f32_panning() -> Arc<dyn Fn(f32) -> String + Send + Sync> {
|
2022-03-22 00:14:24 +11:00
|
|
|
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"),
|
2022-03-21 07:37:50 +11:00
|
|
|
})
|
|
|
|
}
|
2022-03-22 00:14:24 +11:00
|
|
|
|
2022-03-27 12:43:39 +11:00
|
|
|
/// Parse a pan value in the format of [`v2s_f32_panning()] to a linear value in the range `[-1,
|
|
|
|
/// 1]`.
|
|
|
|
pub fn s2v_f32_panning() -> Arc<dyn Fn(&str) -> Option<f32> + Send + Sync> {
|
2022-03-21 07:37:50 +11:00
|
|
|
Arc::new(|string| {
|
2022-03-22 00:14:24 +11:00
|
|
|
let string = string.trim();
|
2022-03-22 00:28:28 +11:00
|
|
|
let cleaned_string = string.trim_end_matches(&[' ', 'l', 'L']).parse().ok();
|
2022-03-22 00:14:24 +11:00
|
|
|
match string.chars().last()?.to_uppercase().next()? {
|
|
|
|
'L' => cleaned_string.map(|x: f32| x / -100.0),
|
2022-03-22 00:56:50 +11:00
|
|
|
'C' => Some(0.0),
|
2022-03-22 00:14:24 +11:00
|
|
|
'R' => cleaned_string.map(|x: f32| x / 100.0),
|
2022-03-22 00:56:50 +11:00
|
|
|
_ => None,
|
2022-03-21 07:37:50 +11:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-22 00:17:16 +11:00
|
|
|
/// Format a `f32` Hertz value as a rounded `Hz` below 1000 Hz, and as a rounded `kHz` value above
|
|
|
|
/// 1000 Hz. This already includes the unit.
|
2022-03-27 12:43:39 +11:00
|
|
|
pub fn v2s_f32_hz_then_khz(digits: usize) -> Arc<dyn Fn(f32) -> String + Send + Sync> {
|
2022-03-22 00:17:16 +11:00
|
|
|
Arc::new(move |value| {
|
|
|
|
if value < 1000.0 {
|
|
|
|
format!("{:.digits$} Hz", value)
|
2022-03-21 02:07:35 +11:00
|
|
|
} else {
|
2022-03-22 00:47:30 +11:00
|
|
|
format!("{:.digits$} kHz", value / 1000.0, digits = digits.max(1))
|
2022-03-21 02:07:35 +11:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-23 02:41:50 +10:00
|
|
|
/// Convert an input in the same format at that of [`v2s_f32_hz_then_khz()] to a Hertz value. This
|
|
|
|
/// additionally also accepts note names in the same format as [`s2v_i32_note_formatter()`].
|
2022-03-27 12:43:39 +11:00
|
|
|
pub fn s2v_f32_hz_then_khz() -> Arc<dyn Fn(&str) -> Option<f32> + Send + Sync> {
|
2022-06-23 02:41:50 +10:00
|
|
|
// FIXME: This is a very crude way to reuse the note value formatter. There's no real runtime
|
|
|
|
// penalty for doing it this way, but it does look less pretty.
|
|
|
|
let note_formatter = s2v_i32_note_formatter();
|
|
|
|
|
2022-03-22 00:28:28 +11:00
|
|
|
Arc::new(move |string| {
|
2022-06-23 02:41:50 +10:00
|
|
|
// If the user inputs a note representation, then we'll use that
|
|
|
|
if let Some(midi_note_number) = note_formatter(string) {
|
2022-06-23 03:40:25 +10:00
|
|
|
return Some(util::midi_note_to_freq(midi_note_number.clamp(0, 127) as u8) as f32);
|
2022-06-23 02:41:50 +10:00
|
|
|
}
|
|
|
|
|
2022-03-22 00:28:28 +11:00
|
|
|
let string = string.trim();
|
|
|
|
let cleaned_string = string
|
|
|
|
.trim_end_matches(&[' ', 'k', 'K', 'h', 'H', 'z', 'Z'])
|
|
|
|
.parse()
|
|
|
|
.ok();
|
2022-03-22 00:59:20 +11:00
|
|
|
match string.get(string.len().saturating_sub(3)..) {
|
2022-03-22 00:28:28 +11:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-27 12:43:39 +11:00
|
|
|
/// Format an order/power of two. Useful in conjunction with [`s2v_i32_power_of_two()`] to limit
|
2022-03-09 04:30:06 +11:00
|
|
|
/// integer parameter ranges to be only powers of two.
|
2022-03-27 12:43:39 +11:00
|
|
|
pub fn v2s_i32_power_of_two() -> Arc<dyn Fn(i32) -> String + Send + Sync> {
|
2022-03-09 04:30:06 +11:00
|
|
|
Arc::new(|value| format!("{}", 1 << value))
|
|
|
|
}
|
|
|
|
|
2022-03-27 12:32:45 +11:00
|
|
|
/// Parse a parameter input string to a power of two. Useful in conjunction with
|
2022-03-27 12:43:39 +11:00
|
|
|
/// [`v2s_i32_power_of_two()`] to limit integer parameter ranges to be only powers of two.
|
|
|
|
pub fn s2v_i32_power_of_two() -> Arc<dyn Fn(&str) -> Option<i32> + Send + Sync> {
|
2022-03-09 04:30:06 +11:00
|
|
|
Arc::new(|string| string.parse().ok().map(|n: i32| (n as f32).log2() as i32))
|
|
|
|
}
|
2022-03-21 07:15:17 +11:00
|
|
|
|
2022-03-22 00:40:17 +11:00
|
|
|
/// Turns an integer MIDI note number (usually in the range [0, 127]) into a note name, where 60 is
|
|
|
|
/// C4 and 69 is A4 (nice).
|
2022-03-27 12:43:39 +11:00
|
|
|
pub fn v2s_i32_note_formatter() -> Arc<dyn Fn(i32) -> String + Send + Sync> {
|
2022-03-22 00:40:17 +11:00
|
|
|
Arc::new(move |value| {
|
|
|
|
let note_name = util::NOTES[value as usize % 12];
|
|
|
|
let octave = (value / 12) - 1;
|
2022-03-21 07:15:17 +11:00
|
|
|
format!("{note_name}{octave}")
|
|
|
|
})
|
|
|
|
}
|
2022-03-22 00:40:17 +11:00
|
|
|
|
2022-03-27 12:43:39 +11:00
|
|
|
/// Parse a note name to a MIDI number using the inverse mapping from [`v2s_i32_note_formatter()].
|
|
|
|
pub fn s2v_i32_note_formatter() -> Arc<dyn Fn(&str) -> Option<i32> + Send + Sync> {
|
2022-03-21 07:15:17 +11:00
|
|
|
Arc::new(|string| {
|
2022-06-23 03:34:46 +10:00
|
|
|
let string = string.trim();
|
|
|
|
if string.len() < 2 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A valid trimmed string will either be be two characters (we already checked the length),
|
|
|
|
// or two characters separated by spaces
|
2022-03-22 00:40:17 +11:00
|
|
|
let (note_name, octave) = string
|
2022-06-23 03:34:46 +10:00
|
|
|
.split_once(|c: char| c.is_whitespace())
|
|
|
|
.unwrap_or_else(|| (&string[..1], &string[1..]));
|
2022-03-22 00:40:17 +11:00
|
|
|
|
|
|
|
let note_id = util::NOTES
|
|
|
|
.iter()
|
|
|
|
.position(|&candidate| note_name.eq_ignore_ascii_case(candidate))?
|
|
|
|
as i32;
|
|
|
|
let octave: i32 = octave.trim().parse().ok()?;
|
|
|
|
|
2022-06-23 03:34:46 +10:00
|
|
|
// 0 = C-1, 12 = C0, 24 = C1
|
|
|
|
Some(note_id + (12 * (octave + 1)))
|
2022-03-21 07:15:17 +11:00
|
|
|
})
|
|
|
|
}
|
2022-03-24 03:37:40 +11:00
|
|
|
|
|
|
|
/// Display 'Bypassed' or 'Not Bypassed' depending on whether the parameter is true or false.
|
|
|
|
/// 'Enabled' would have also been a possibilty here, but that could be a bit confusing.
|
2022-03-27 12:43:39 +11:00
|
|
|
pub fn v2s_bool_bypass() -> Arc<dyn Fn(bool) -> String + Send + Sync> {
|
2022-03-24 03:37:40 +11:00
|
|
|
Arc::new(move |value| {
|
|
|
|
if value {
|
|
|
|
String::from("Bypassed")
|
|
|
|
} else {
|
|
|
|
String::from("Not Bypassed")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-27 12:43:39 +11:00
|
|
|
/// Parse a string in the same format as [`v2s_bool_bypass()].
|
|
|
|
pub fn s2v_bool_bypass() -> Arc<dyn Fn(&str) -> Option<bool> + Send + Sync> {
|
2022-03-24 03:37:40 +11:00
|
|
|
Arc::new(|string| {
|
|
|
|
let string = string.trim();
|
|
|
|
if string.eq_ignore_ascii_case("bypass") {
|
|
|
|
Some(true)
|
|
|
|
} else if string.eq_ignore_ascii_case("not bypassed") {
|
|
|
|
Some(false)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|