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