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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use std::{fmt, num::NonZeroU32};
mod sealed {
use std::num::NonZeroU32;
use derive_more::Display;
#[derive(Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Display)]
#[derive_const(Clone)]
#[repr(transparent)]
pub struct SampleRate(NonZeroU32);
impl SampleRate {
pub const fn new(n: NonZeroU32) -> Self {
Self(n)
}
pub const fn get(&self) -> NonZeroU32 {
self.0
}
}
}
pub use self::sealed::SampleRate;
impl fmt::Debug for SampleRate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let hz = self.get().get();
if hz > 1_000 {
let khz = hz as f32 / 1_000.;
write!(f, "{khz:.1} kHz")
} else {
write!(f, "{hz} Hz")
}
}
}
impl const From<NonZeroU32> for SampleRate {
fn from(value: NonZeroU32) -> Self {
Self::new(value)
}
}
impl const From<SampleRate> for NonZeroU32 {
fn from(value: SampleRate) -> Self {
value.get()
}
}
#[macro_export]
macro_rules! sample_rate {
($hz:literal) => {
::audio_time::SampleRate::new(::std::num::NonZeroU32::new($hz).unwrap())
};
}