use std::sync::Arc;
use tracing::debug;
use crate::{
model::{InputConstraints, InputType},
params::{
Parameter, ParameterData, PresetParameter, SelectOption,
macros::select_non_default_custom_value,
},
visitors::{CommandBuilder, VisitorContext},
};
pub(crate) struct AudioPitch;
impl AudioPitch {
pub(crate) const ID: &'static str = "pitch";
pub(crate) const NAME: &'static str = "Audio Pitch";
const DEFAULT: &'static str = "1";
const VARIANTS: [&str; 8] = ["0.6", "0.8", "0.9", "1", "1.15", "1.25", "1.5", "2"];
pub fn new_parameter() -> Parameter {
Parameter::new(
Self::ID,
Self::NAME,
ParameterData::CustomSelect {
options: SelectOption::from_slice(&Self::VARIANTS),
selected_index: 3,
value: Self::DEFAULT.to_owned(),
constraints: InputConstraints {
length: 4,
input_type: InputType::PositiveDecimal,
},
validator: Arc::new(Self::validate),
formatter: None,
},
)
}
fn validate(value: &str) -> Result<String, &str> {
if let Ok(num) = value.parse::<f64>()
&& (0.01..=100.0).contains(&num)
{
Ok(num.to_string())
} else {
Err("Invalid value. Expected a number in range 0.01..100")
}
}
pub fn build_command(cb: &mut CommandBuilder, data: &ParameterData) {
if !cb.discard_audio
&& let Some(value) = select_non_default_custom_value!(data)
{
debug!(value, "build_command");
cb.audio_filters
.push(format!("rubberband=pitchq=quality:pitch={}", value));
}
}
}
impl<'a> PresetParameter<'a> for AudioPitch {
fn apply_preset(_ctx: &VisitorContext, data: &mut ParameterData, preset_value: &str) {
if Self::validate(preset_value).is_ok() {
Self::set_parameter_value(data, preset_value);
}
}
fn save_preset(_ctx: &VisitorContext, data: &'a ParameterData) -> Option<String> {
select_non_default_custom_value!(data).cloned()
}
}