Skip to main content

sva_samples/
label.rs

1// Concern: states how exact one sampled value is and what it cost | Non-concern: deciding that (collapse.rs), printing it | IO: none
2
3#[derive(Clone, Copy, Debug, PartialEq, Eq)]
4pub enum Source {
5    Exact,
6    Measured,
7}
8
9/// A line the grid could not hold, levelled against amplitude 1.0.
10#[derive(Clone, Copy, Debug, PartialEq)]
11pub struct Dropped {
12    pub hz: f64,
13    pub db: f64,
14}
15
16/// The rows declared once: enum, printed name, name read back.
17macro_rules! rules {
18    ($($variant:ident => $text:literal,)+) => {
19        #[derive(Clone, Copy, Debug, PartialEq, Eq)]
20        pub enum Rule {
21            $($variant,)+
22        }
23
24        impl Rule {
25            /// The row of FORMAT 9.1 that ran, as the label prints it.
26            pub fn as_str(self) -> &'static str {
27                match self {
28                    $(Rule::$variant => $text,)+
29                }
30            }
31
32            pub fn named(text: &str) -> Option<Rule> {
33                match text {
34                    $($text => Some(Rule::$variant),)+
35                    _ => None,
36                }
37            }
38        }
39    };
40}
41
42rules! {
43    LineSpectrumExact => "line spectrum, no crop",
44    LineSpectrumSummed => "line spectrum, summed directly",
45    LineSpectrumMixed => "line spectrum, placed and summed",
46    BandLimited => "band-limited projection",
47    CroppedPair => "cropped pair, tail truncated",
48    PointSampled => "point sampling",
49    InverseSpectrum => "inverse spectrum",
50    Istft => "inverse short-time transform",
51    Reading => "reading",
52    Added => "sum, addend by addend",
53}
54
55#[derive(Clone, Debug, PartialEq)]
56pub enum Detail {
57    Lines {
58        rule: Rule,
59        /// Lines the transform placed; the rest were summed directly.
60        placed: usize,
61        summed: usize,
62        dropped: Vec<Dropped>,
63        dropped_more: usize,
64        terms: Option<usize>,
65        tail_db: Option<f64>,
66    },
67    Continuous {
68        rule: Rule,
69    },
70    Cropped {
71        rule: Rule,
72        tail_db: Option<f64>,
73    },
74    Point {
75        rule: Rule,
76        alias_db: Option<f64>,
77    },
78    Spectrum {
79        rule: Rule,
80        wrap_db: f64,
81    },
82    Roundtrip {
83        rule: Rule,
84        edited: bool,
85    },
86    /// A reading off a buffer that exists; no collapse ran under it.
87    Reading {
88        rule: Rule,
89    },
90    /// A sum whose addends named different rows; the planes added.
91    Added {
92        parts: Vec<Detail>,
93    },
94}
95
96#[derive(Clone, Copy, Debug, PartialEq, Eq)]
97pub struct Cost {
98    pub flops: u128,
99    pub budget: u128,
100}
101
102#[derive(Clone, Debug, PartialEq)]
103pub struct Label {
104    pub source: Source,
105    pub profile: &'static str,
106    pub rate: u32,
107    pub detail: Detail,
108    pub cost: Option<Cost>,
109}
110
111impl Label {
112    pub fn measured(profile: &'static str, rate: u32) -> Label {
113        Label {
114            source: Source::Measured,
115            profile,
116            rate,
117            detail: Detail::Reading {
118                rule: Rule::Reading,
119            },
120            cost: None,
121        }
122    }
123
124    pub fn new(source: Source, profile: &'static str, rate: u32, detail: Detail) -> Label {
125        Label {
126            source,
127            profile,
128            rate,
129            detail,
130            cost: None,
131        }
132    }
133
134    pub fn costing(self, flops: u128, budget: u128) -> Label {
135        Label {
136            cost: Some(Cost { flops, budget }),
137            ..self
138        }
139    }
140
141    pub fn rule(&self) -> Rule {
142        self.detail.rule()
143    }
144}
145
146impl Detail {
147    pub fn rule(&self) -> Rule {
148        match *self {
149            Detail::Lines { rule, .. }
150            | Detail::Continuous { rule }
151            | Detail::Cropped { rule, .. }
152            | Detail::Point { rule, .. }
153            | Detail::Spectrum { rule, .. }
154            | Detail::Roundtrip { rule, .. }
155            | Detail::Reading { rule } => rule,
156            Detail::Added { .. } => Rule::Added,
157        }
158    }
159}