1use file_system::{ControlCommand, IoErrorKind, Result, define_command};
10
11use crate::kind;
12
13#[repr(u8)]
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub enum SampleFormat {
17 U8 = 0,
19 I16 = 1,
20 I24 = 2,
21 I32 = 3,
22 F32 = 4,
24}
25
26impl SampleFormat {
27 pub const fn sample_size(self) -> usize {
28 match self {
29 Self::U8 => 1,
30 Self::I16 => 2,
31 Self::I24 => 3,
32 Self::I32 | Self::F32 => 4,
33 }
34 }
35}
36
37impl TryFrom<u8> for SampleFormat {
38 type Error = IoErrorKind;
39
40 fn try_from(value: u8) -> Result<Self> {
41 match value {
42 0 => Ok(Self::U8),
43 1 => Ok(Self::I16),
44 2 => Ok(Self::I24),
45 3 => Ok(Self::I32),
46 4 => Ok(Self::F32),
47 _ => Err(IoErrorKind::InvalidArgument),
48 }
49 }
50}
51
52file_system::plain_data! {
53 #[repr(C)]
54 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
59 pub struct Configuration {
60 pub sample_rate: u32,
61 pub channels: u8,
62 format: u8,
63 reserved: [u8; 2],
64 }
65}
66
67impl Configuration {
68 pub const fn new(sample_rate: u32, channels: u8, format: SampleFormat) -> Self {
69 Self {
70 sample_rate,
71 channels,
72 format: format as u8,
73 reserved: [0; 2],
74 }
75 }
76
77 pub fn sample_format(&self) -> Result<SampleFormat> {
78 SampleFormat::try_from(self.format)
79 }
80
81 pub fn validate(&self) -> Result<()> {
82 if self.sample_rate == 0 || !(1..=8).contains(&self.channels) || self.reserved != [0; 2] {
83 return Err(IoErrorKind::InvalidArgument);
84 }
85 self.sample_format()?;
86 Ok(())
87 }
88
89 pub fn frame_size(&self) -> Result<usize> {
90 self.validate()?;
91 Ok(self.channels as usize * self.sample_format()?.sample_size())
92 }
93}
94
95define_command!(
96 SET_CONFIGURATION,
97 Write,
98 kind::AUDIO,
99 0x01,
100 Configuration,
101 ()
102);
103define_command!(
104 GET_CONFIGURATION,
105 Read,
106 kind::AUDIO,
107 0x02,
108 (),
109 Configuration
110);
111define_command!(GET_WRITABLE, Read, kind::AUDIO, 0x03, (), u32);
112define_command!(SET_VOLUME, Write, kind::AUDIO, 0x04, u8, ());
113define_command!(GET_VOLUME, Read, kind::AUDIO, 0x05, (), u8);
114define_command!(START, Write, kind::AUDIO, 0x06, (), ());
115define_command!(STOP, Write, kind::AUDIO, 0x07, (), ());
116define_command!(GET_UNDERRUNS, Read, kind::AUDIO, 0x08, (), u32);