Skip to main content

device/
audio.rs

1//! Audio character-device control contract (`device::kind::AUDIO`).
2//!
3//! Configuration is fixed for an open stream. [`GET_WRITABLE`] returns free
4//! bytes. Volume is `0..=100` percent, applied when rendering. [`STOP`] pauses
5//! on the next callback and retains queued PCM.
6//! [`GET_UNDERRUNS`] returns the number of output underruns since boot.
7//! The count saturates at `u32::MAX`.
8
9use file_system::{ControlCommand, IoErrorKind, Result, define_command};
10
11use crate::kind;
12
13/// The little-endian representation of one PCM sample in the device byte stream.
14#[repr(u8)]
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub enum SampleFormat {
17    /// Unsigned 8-bit, with silence at 128.
18    U8 = 0,
19    I16 = 1,
20    I24 = 2,
21    I32 = 3,
22    /// IEEE 754, with full scale at -1.0 and 1.0.
23    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    /// Fixed-width device payload. Frames contain one sample per interleaved channel.
55    ///
56    /// The format is stored as a byte so malformed command data cannot create an
57    /// invalid Rust enum. Reserved bytes must be zero; there is no implicit padding.
58    #[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);