xsynth_core/
audio_stream.rs

1/// Number of audio channels.
2#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
3#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
4pub enum ChannelCount {
5    Mono,
6    Stereo,
7}
8
9impl ChannelCount {
10    pub fn count(&self) -> u16 {
11        match self {
12            ChannelCount::Mono => 1,
13            ChannelCount::Stereo => 2,
14        }
15    }
16
17    pub fn from_count(count: u16) -> Option<Self> {
18        match count {
19            1 => Some(ChannelCount::Mono),
20            2 => Some(ChannelCount::Stereo),
21            _ => None,
22        }
23    }
24}
25
26impl From<u16> for ChannelCount {
27    fn from(count: u16) -> Self {
28        ChannelCount::from_count(count)
29            .expect("Unsupported channel count, only mono and stereo are supported")
30    }
31}
32
33/// Parameters of the output audio.
34#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
35#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
36pub struct AudioStreamParams {
37    pub sample_rate: u32,
38    pub channels: ChannelCount,
39}
40
41impl AudioStreamParams {
42    pub fn new(sample_rate: u32, channels: ChannelCount) -> Self {
43        Self {
44            sample_rate,
45            channels,
46        }
47    }
48}