Skip to main content

wedeo_core/
sample_format.rs

1use std::fmt;
2
3/// Audio sample format, matching FFmpeg's AVSampleFormat.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[repr(i32)]
6pub enum SampleFormat {
7    None = -1,
8    /// Unsigned 8 bits.
9    U8 = 0,
10    /// Signed 16 bits.
11    S16 = 1,
12    /// Signed 32 bits.
13    S32 = 2,
14    /// Float.
15    Flt = 3,
16    /// Double.
17    Dbl = 4,
18    /// Unsigned 8 bits, planar.
19    U8p = 5,
20    /// Signed 16 bits, planar.
21    S16p = 6,
22    /// Signed 32 bits, planar.
23    S32p = 7,
24    /// Float, planar.
25    Fltp = 8,
26    /// Double, planar.
27    Dblp = 9,
28    /// Signed 64 bits.
29    S64 = 10,
30    /// Signed 64 bits, planar.
31    S64p = 11,
32}
33
34impl SampleFormat {
35    /// Return the number of bytes per sample.
36    pub fn bytes_per_sample(self) -> usize {
37        match self {
38            SampleFormat::None => 0,
39            SampleFormat::U8 | SampleFormat::U8p => 1,
40            SampleFormat::S16 | SampleFormat::S16p => 2,
41            SampleFormat::S32 | SampleFormat::S32p | SampleFormat::Flt | SampleFormat::Fltp => 4,
42            SampleFormat::Dbl | SampleFormat::Dblp | SampleFormat::S64 | SampleFormat::S64p => 8,
43        }
44    }
45
46    /// Check if the sample format is planar.
47    pub fn is_planar(self) -> bool {
48        matches!(
49            self,
50            SampleFormat::U8p
51                | SampleFormat::S16p
52                | SampleFormat::S32p
53                | SampleFormat::Fltp
54                | SampleFormat::Dblp
55                | SampleFormat::S64p
56        )
57    }
58
59    /// Get the packed equivalent of a planar format (or return self if already packed).
60    pub fn packed(self) -> Self {
61        match self {
62            SampleFormat::U8p => SampleFormat::U8,
63            SampleFormat::S16p => SampleFormat::S16,
64            SampleFormat::S32p => SampleFormat::S32,
65            SampleFormat::Fltp => SampleFormat::Flt,
66            SampleFormat::Dblp => SampleFormat::Dbl,
67            SampleFormat::S64p => SampleFormat::S64,
68            other => other,
69        }
70    }
71
72    /// Get the planar equivalent of a packed format (or return self if already planar).
73    pub fn planar(self) -> Self {
74        match self {
75            SampleFormat::U8 => SampleFormat::U8p,
76            SampleFormat::S16 => SampleFormat::S16p,
77            SampleFormat::S32 => SampleFormat::S32p,
78            SampleFormat::Flt => SampleFormat::Fltp,
79            SampleFormat::Dbl => SampleFormat::Dblp,
80            SampleFormat::S64 => SampleFormat::S64p,
81            other => other,
82        }
83    }
84
85    pub fn name(self) -> &'static str {
86        match self {
87            SampleFormat::None => "none",
88            SampleFormat::U8 => "u8",
89            SampleFormat::S16 => "s16",
90            SampleFormat::S32 => "s32",
91            SampleFormat::Flt => "flt",
92            SampleFormat::Dbl => "dbl",
93            SampleFormat::U8p => "u8p",
94            SampleFormat::S16p => "s16p",
95            SampleFormat::S32p => "s32p",
96            SampleFormat::Fltp => "fltp",
97            SampleFormat::Dblp => "dblp",
98            SampleFormat::S64 => "s64",
99            SampleFormat::S64p => "s64p",
100        }
101    }
102}
103
104impl fmt::Display for SampleFormat {
105    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106        write!(f, "{}", self.name())
107    }
108}