firefly_rust/audio/
time.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use super::*;

pub struct Time(pub(super) u32);

impl Time {
    pub const ZERO: Self = Self(0);

    #[must_use]
    pub fn samples(s: u32) -> Self {
        Self(s)
    }

    #[must_use]
    pub fn seconds(s: u32) -> Self {
        Self(s * SAMPLE_RATE)
    }

    #[must_use]
    pub fn ms(s: u32) -> Self {
        Self(s * SAMPLE_RATE / 1000)
    }

    #[must_use]
    pub fn duration(s: core::time::Duration) -> Self {
        let s = s.as_secs_f32() * 44_100.;
        #[expect(clippy::cast_sign_loss)]
        let s = s as u32;
        Self(s)
    }
}