ebur128_stream/sample.rs
1//! The [`Sample`] trait — sealed over `f32` (and `f64` under the `f64`
2//! feature).
3//!
4//! Internally the analyzer always works in `f32` (per `SPEC.md` §5: the
5//! K-weighting filter's accuracy delta between `f32` and `f64` is below 0.001
6//! LU, well under the 0.1 LU EBU R128 spec tolerance). The `f64` feature
7//! lets callers push `f64` buffers without manually casting; values are
8//! converted to `f32` on ingest.
9
10mod sealed {
11 pub trait Sealed {}
12 impl Sealed for f32 {}
13 #[cfg(feature = "f64")]
14 impl Sealed for f64 {}
15}
16
17/// A sample type accepted by [`Analyzer::push_planar`] and
18/// [`Analyzer::push_interleaved`].
19///
20/// Sealed: only `f32` (and `f64` under the `f64` feature) implement this
21/// trait. Adding more sample types is a deliberate API change.
22///
23/// [`Analyzer::push_planar`]: crate::Analyzer::push_planar
24/// [`Analyzer::push_interleaved`]: crate::Analyzer::push_interleaved
25pub trait Sample: sealed::Sealed + Copy {
26 /// Convert the sample to the analyzer's internal `f32` representation.
27 fn to_f32(self) -> f32;
28}
29
30impl Sample for f32 {
31 #[inline]
32 fn to_f32(self) -> f32 {
33 self
34 }
35}
36
37#[cfg(feature = "f64")]
38impl Sample for f64 {
39 #[inline]
40 fn to_f32(self) -> f32 {
41 self as f32
42 }
43}