Skip to main content

xsynth_core/
audio_stream.rs

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