Skip to main content

midas_audio/
types.rs

1use std::fmt;
2
3use crate::codecs::adpcm::SampleType;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum Channels {
7    Mono = 1,
8    Stereo = 2,
9}
10
11impl Channels {
12    #[must_use]
13    pub fn count(&self) -> usize {
14        *self as usize
15    }
16
17    #[must_use]
18    pub fn from_u8(v: u8) -> Option<Self> {
19        match v {
20            1 => Some(Channels::Mono),
21            2 => Some(Channels::Stereo),
22            _ => None,
23        }
24    }
25}
26
27impl fmt::Display for Channels {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        match self {
30            Channels::Mono => write!(f, "Mono"),
31            Channels::Stereo => write!(f, "Stereo"),
32        }
33    }
34}
35
36#[derive(Debug, Clone, Copy, PartialEq, Eq)]
37pub enum SampleRate {
38    Hz8000 = 8000,
39    Hz11025 = 11025,
40    Hz22050 = 22050,
41    Hz32000 = 32000,
42    Hz44100 = 44100,
43    Hz48000 = 48000,
44}
45
46impl SampleRate {
47    #[must_use]
48    pub fn value(&self) -> u32 {
49        *self as u32
50    }
51
52    #[must_use]
53    pub fn from_u32(v: u32) -> Option<Self> {
54        match v {
55            8000 => Some(SampleRate::Hz8000),
56            11025 => Some(SampleRate::Hz11025),
57            22050 => Some(SampleRate::Hz22050),
58            32000 => Some(SampleRate::Hz32000),
59            44100 => Some(SampleRate::Hz44100),
60            48000 => Some(SampleRate::Hz48000),
61            _ => None,
62        }
63    }
64}
65
66impl fmt::Display for SampleRate {
67    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68        write!(f, "{} Hz", *self as u32)
69    }
70}
71
72/// Represents the bit depth (resolution) of PCM audio data.
73#[derive(Debug, Clone, Copy, PartialEq, Eq)]
74pub enum BitDepth {
75    /// 8-bit unsigned PCM.
76    EightBit = 8,
77    /// 16-bit signed PCM.
78    SixteenBit = 16,
79}
80
81impl BitDepth {
82    #[must_use]
83    pub fn bits(&self) -> u16 {
84        *self as u16
85    }
86
87    #[must_use]
88    pub fn from_u16(v: u16) -> Option<Self> {
89        match v {
90            8 => Some(BitDepth::EightBit),
91            16 => Some(BitDepth::SixteenBit),
92            _ => None,
93        }
94    }
95}
96
97/// Metadata information extracted from an audio file.
98#[derive(Debug, Clone, Copy, PartialEq, Eq)]
99pub struct AudioMetadata {
100    pub channels: Channels,
101    pub sample_type: SampleType,
102    pub frame_length: usize,
103}