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/), choosing the rate | IO: none -> Profile
2
3#[derive(Clone, Copy, Debug, PartialEq)]
4pub struct Profile {
5    pub name: &'static str,
6    pub floor_db: f64,
7    pub floor_db_above_5k: f64,
8    pub ceiling_hz: f64,
9    /// The operation count a render pays without the caller saying so.
10    pub flop_budget: u128,
11    pub precision_bits: i32,
12    pub prune_db: f64,
13}
14
15pub const PSYCHOACOUSTIC_V1: Profile = Profile {
16    name: "psychoacoustic-v1",
17    floor_db: -20.0,
18    floor_db_above_5k: -25.0,
19    ceiling_hz: 20_000.0,
20    flop_budget: 10_000_000_000,
21    precision_bits: 24,
22    prune_db: -120.0,
23};
24
25impl Profile {
26    pub fn ceiling(&self, rate: u32) -> f64 {
27        self.ceiling_hz.min(f64::from(rate) / 2.0)
28    }
29
30    pub fn half_lsb(&self) -> f64 {
31        2f64.powi(-self.precision_bits)
32    }
33
34    pub fn prune_level(&self) -> f64 {
35        10f64.powf(self.prune_db / 20.0)
36    }
37
38    /// Every setting a value or its label depends on.
39    pub fn deciding(&self) -> [u64; 6] {
40        let Profile {
41            name,
42            floor_db,
43            floor_db_above_5k,
44            ceiling_hz,
45            flop_budget: _,
46            precision_bits,
47            prune_db,
48        } = *self;
49        let named = name.bytes().fold(0xcbf2_9ce4_8422_2325u64, |held, b| {
50            (held ^ u64::from(b)).wrapping_mul(0x0100_0000_01b3)
51        });
52        [
53            named,
54            floor_db.to_bits(),
55            floor_db_above_5k.to_bits(),
56            ceiling_hz.to_bits(),
57            precision_bits as u64,
58            prune_db.to_bits(),
59        ]
60    }
61
62    pub fn floor(&self, hz: f64) -> f64 {
63        if hz > 5_000.0 {
64            self.floor_db_above_5k
65        } else {
66            self.floor_db
67        }
68    }
69}