sva-samples 0.8.1

Buffers, frames, the f64 machine, the collapse of a law onto a grid, and the measured observations over one
Documentation
// Concern: states how exact one sampled value is | Non-concern: deciding that (collapse/blocks.rs), printing it | IO: none

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Source {
    Exact,
    Measured,
}

/// A line the grid could not hold, levelled against amplitude 1.0.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Dropped {
    pub hz: f64,
    pub db: f64,
}

macro_rules! rules {
    ($($variant:ident => $text:literal,)+) => {
        #[derive(Clone, Copy, Debug, PartialEq, Eq)]
        pub enum Rule {
            $($variant,)+
        }

        impl Rule {
            /// The row of FORMAT 9.1 that ran, as the label prints it.
            pub fn as_str(self) -> &'static str {
                match self {
                    $(Rule::$variant => $text,)+
                }
            }

            pub fn named(text: &str) -> Option<Rule> {
                match text {
                    $($text => Some(Rule::$variant),)+
                    _ => None,
                }
            }
        }
    };
}

rules! {
    LineSpectrumSummed => "line spectrum, summed directly",
    BandLimited => "band-limited projection",
    CroppedPair => "cropped pair, tail truncated",
    PointSampled => "point sampling",
    Istft => "inverse short-time transform",
    Reading => "reading",
    Added => "sum, addend by addend",
}

#[derive(Clone, Debug, PartialEq)]
pub enum Detail {
    Lines {
        rule: Rule,
        summed: usize,
        dropped: Vec<Dropped>,
        dropped_more: usize,
        tail_db: Option<f64>,
    },
    Continuous {
        rule: Rule,
    },
    Cropped {
        rule: Rule,
        tail_db: Option<f64>,
    },
    Point {
        rule: Rule,
        alias_db: Option<f64>,
        tail_db: Option<f64>,
    },
    Roundtrip {
        rule: Rule,
        edited: bool,
    },
    /// A reading off a buffer that exists; no collapse ran under it.
    Reading {
        rule: Rule,
    },
    /// A sum whose addends named different rows; the planes added.
    Added {
        parts: Vec<Detail>,
    },
}

#[derive(Clone, Debug, PartialEq)]
pub struct CuttingBelowSilenceThreshold {
    pub silence_threshold_dbfs: f64,
    pub treated_as_silent_from_sample: Vec<(String, i64)>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct Label {
    pub source: Source,
    pub profile: &'static str,
    pub rate: u32,
    pub detail: Detail,
    /// The most seconds a read was moved to land on a whole sample.
    pub moved: Option<f64>,
    pub cutting_below_silence_threshold: Option<CuttingBelowSilenceThreshold>,
}

impl Label {
    pub fn measured(profile: &'static str, rate: u32) -> Label {
        Label {
            source: Source::Measured,
            profile,
            rate,
            detail: Detail::Reading {
                rule: Rule::Reading,
            },
            moved: None,
            cutting_below_silence_threshold: None,
        }
    }

    pub fn new(source: Source, profile: &'static str, rate: u32, detail: Detail) -> Label {
        Label {
            source,
            profile,
            rate,
            detail,
            moved: None,
            cutting_below_silence_threshold: None,
        }
    }

    pub fn rule(&self) -> Rule {
        self.detail.rule()
    }
}

impl Detail {
    pub fn rule(&self) -> Rule {
        match *self {
            Detail::Lines { rule, .. }
            | Detail::Continuous { rule }
            | Detail::Cropped { rule, .. }
            | Detail::Point { rule, .. }
            | Detail::Roundtrip { rule, .. }
            | Detail::Reading { rule } => rule,
            Detail::Added { .. } => Rule::Added,
        }
    }
}