weirdnet-audio-plugin 0.1.0

Audio plugins for Weirdnet: the Plugin trait, typed parameters, a registry, chain processing with a content-hash cache key, and the built-ins gain, levels and frequencies.
Documentation
use weirdnet_media::waveform::WaveformError;

/// Why a plugin could not process a waveform.
#[derive(Debug, Clone, PartialEq)]
pub enum PluginError {
    /// A parameter is unknown, has the wrong type, or is outside its range.
    InvalidParam { name: String, reason: String },
    /// The plugin does not work on this waveform's format, for example pan on a mono waveform.
    UnsupportedFormat,
    /// The result would be longer than the limit.
    TooLong { max_frames: usize },
    /// Anything else.
    Internal(String),
}

impl PluginError {
    pub(crate) fn invalid(name: &str, reason: impl Into<String>) -> Self {
        PluginError::InvalidParam { name: name.to_string(), reason: reason.into() }
    }
}

impl std::fmt::Display for PluginError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            PluginError::InvalidParam { name, reason } => write!(f, "parameter {name}: {reason}"),
            PluginError::UnsupportedFormat => write!(f, "the plugin does not support this waveform's format"),
            PluginError::TooLong { max_frames } => write!(f, "the result would be longer than {max_frames} frames"),
            PluginError::Internal(why) => write!(f, "{why}"),
        }
    }
}

impl std::error::Error for PluginError {}

impl From<WaveformError> for PluginError {
    fn from(e: WaveformError) -> Self {
        match e {
            WaveformError::UnsupportedChannels { .. } => PluginError::UnsupportedFormat,
            WaveformError::TooLong { max_frames } => PluginError::TooLong { max_frames },
            WaveformError::OutOfRange { what, .. } => PluginError::invalid(what, e.to_string()),
            WaveformError::NotFinite(what) => PluginError::invalid(what, e.to_string()),
            other => PluginError::Internal(other.to_string()),
        }
    }
}