#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct ParameterFlags {
pub discrete: bool,
pub automatable: bool,
pub read_only: bool,
pub hidden: bool,
pub bypass: bool,
pub list: bool,
pub program_change: bool,
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct ParameterInfo {
pub id: u64,
pub title: String,
pub short_title: String,
pub units: String,
pub step_count: u32,
pub default_value: Option<f64>,
pub flags: ParameterFlags,
}
impl ParameterInfo {
#[must_use]
pub fn new(id: u64, title: impl Into<String>) -> ParameterInfo {
ParameterInfo {
id,
title: title.into(),
short_title: String::new(),
units: String::new(),
step_count: 0,
default_value: None,
flags: ParameterFlags::default(),
}
}
pub fn automation_value(&self, value: f64) -> Result<f64, crate::InputError> {
self.validate_edit(value)?;
if !self.flags.automatable {
return Err(crate::InputError::NotAutomatable { id: self.id });
}
Ok(if self.step_count == 0 {
value
} else {
(value * f64::from(self.step_count)).round() / f64::from(self.step_count)
})
}
#[doc(hidden)]
pub fn validate_edit(&self, value: f64) -> Result<(), crate::InputError> {
if !value.is_finite() || !(0.0..=1.0).contains(&value) {
return Err(crate::InputError::ParameterValue);
}
if self.flags.read_only {
return Err(crate::InputError::ReadOnlyParameter { id: self.id });
}
Ok(())
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct ParameterChange {
pub id: u64,
pub offset: usize,
pub value: f64,
}
impl ParameterChange {
#[must_use]
pub const fn new(id: u64, offset: usize, value: f64) -> ParameterChange {
ParameterChange { id, offset, value }
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct AutomationEvent {
pub slot: usize,
pub change: ParameterChange,
}
impl AutomationEvent {
#[must_use]
pub const fn new(slot: usize, change: ParameterChange) -> AutomationEvent {
AutomationEvent { slot, change }
}
}
pub fn changes_fit(changes: &[ParameterChange], frames: usize) -> bool {
changes
.iter()
.all(|c| c.offset < frames && c.value.is_finite() && (0.0..=1.0).contains(&c.value))
&& changes
.windows(2)
.all(|pair| pair[0].offset <= pair[1].offset)
}
pub const PARAMETER_TEXT_BYTES: usize = 4096;
pub fn validate_parameter_text(text: &str) -> Result<(), crate::InputError> {
if text.len() > PARAMETER_TEXT_BYTES || text.contains('\0') {
Err(crate::InputError::ParameterText)
} else {
Ok(())
}
}
pub fn validate_normalized(value: f64) -> Result<(), crate::InputError> {
if value.is_finite() && (0.0..=1.0).contains(&value) {
Ok(())
} else {
Err(crate::InputError::ParameterValue)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ParameterGroup {
pub id: String,
pub name: String,
pub program_list_id: Option<i32>,
}
impl ParameterGroup {
#[doc(hidden)]
#[must_use]
pub fn new(id: String, name: String, program_list_id: Option<i32>) -> ParameterGroup {
ParameterGroup {
id,
name,
program_list_id,
}
}
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct ParameterDetails {
pub info: ParameterInfo,
pub native_unit: Option<u32>,
pub plain_at_zero: f64,
pub plain_at_one: f64,
pub groups: Option<Vec<ParameterGroup>>,
}
impl ParameterDetails {
#[must_use]
pub fn new(info: ParameterInfo, plain_at_zero: f64, plain_at_one: f64) -> ParameterDetails {
ParameterDetails {
info,
native_unit: None,
plain_at_zero,
plain_at_one,
groups: None,
}
}
}
pub const PARAMETER_CHOICE_PAGE_SIZE: u32 = 256;
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct ParameterChoice {
pub index: u64,
pub normalized: f64,
pub plain: f64,
pub text: String,
}
impl ParameterChoice {
#[doc(hidden)]
#[must_use]
pub fn new(index: u64, normalized: f64, plain: f64, text: String) -> ParameterChoice {
ParameterChoice {
index,
normalized,
plain,
text,
}
}
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct ParameterChoicePage {
pub total: u64,
pub start: u64,
pub choices: Vec<ParameterChoice>,
}
impl ParameterChoicePage {
#[doc(hidden)]
#[must_use]
pub fn new(total: u64, start: u64, choices: Vec<ParameterChoice>) -> ParameterChoicePage {
ParameterChoicePage {
total,
start,
choices,
}
}
}
pub fn validate_choice_page(start: u64, count: u32) -> Result<(), crate::InputError> {
if count == 0 || count > PARAMETER_CHOICE_PAGE_SIZE || start > u64::from(u32::MAX) + 1 {
Err(crate::InputError::ParameterChoicePage)
} else {
Ok(())
}
}