Skip to main content

silence_split/
sample.rs

1//! Input sample types and the stream layout.
2
3mod sealed {
4    pub trait Sealed {}
5    impl Sealed for i16 {}
6    impl Sealed for f32 {}
7}
8
9/// A PCM sample type the analyzer accepts: `i16` or `f32`.
10///
11/// `f32` samples are full scale at ±1.0 and `i16` samples at ±32768. The trait is sealed so other
12/// integer widths can be added later with their own scale.
13pub trait Sample: sealed::Sealed + Copy {
14    /// The sample as a full-scale `f32` value.
15    fn to_f32(self) -> f32;
16}
17
18impl Sample for i16 {
19    fn to_f32(self) -> f32 {
20        f32::from(self) / 32768.0
21    }
22}
23
24impl Sample for f32 {
25    fn to_f32(self) -> f32 {
26        self
27    }
28}
29
30/// Sample rate and channel count of the original audio.
31#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
32#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
33pub struct Layout {
34    /// Samples per second per channel.
35    pub sample_rate: u32,
36    /// Number of interleaved or planar channels.
37    pub channels: u16,
38}