Skip to main content

sva_samples/
profile.rs

1// Concern: the named tolerance set every label cites, and the ceiling and floor it puts on a rate | Non-concern: what a collapse does with either (collapse/) | IO: (name) -> Profile
2
3#[derive(Clone, Copy, Debug, PartialEq)]
4pub struct Profile {
5    pub name: &'static str,
6    pub level_db: f64,
7    pub floor_db: f64,
8    pub floor_db_above_5k: f64,
9    pub ceiling_hz: f64,
10    pub band_db: f64,
11    /// The operation count a render pays without the caller saying so.
12    pub flop_budget: u128,
13    pub precision_bits: i32,
14}
15
16pub const PSYCHOACOUSTIC_V1: Profile = Profile {
17    name: "psychoacoustic-v1",
18    level_db: 0.5,
19    floor_db: -20.0,
20    floor_db_above_5k: -25.0,
21    ceiling_hz: 20_000.0,
22    band_db: 1.0,
23    flop_budget: 10_000_000_000,
24    precision_bits: 24,
25};
26
27pub fn named(name: &str) -> Option<Profile> {
28    (name == PSYCHOACOUSTIC_V1.name).then_some(PSYCHOACOUSTIC_V1)
29}
30
31impl Profile {
32    pub fn ceiling(&self, rate: u32) -> f64 {
33        self.ceiling_hz.min(f64::from(rate) / 2.0)
34    }
35
36    pub fn half_lsb(&self) -> f64 {
37        2f64.powi(-self.precision_bits)
38    }
39
40    pub fn floor(&self, hz: f64) -> f64 {
41        if hz > 5_000.0 {
42            self.floor_db_above_5k
43        } else {
44            self.floor_db
45        }
46    }
47}