ezk_audio/
channels.rs

1use bitflags::bitflags;
2use ezk::ValueRange;
3
4#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
5pub enum Channels {
6    NotPositioned(u32),
7    Positioned(Vec<ChannelPosition>),
8}
9
10impl Channels {
11    #[must_use]
12    pub fn channel_count(&self) -> usize {
13        match self {
14            Self::NotPositioned(count) => {
15                usize::try_from(*count).expect("not supporting more than usize::MAX channels")
16            }
17            Self::Positioned(positions) => positions.len(),
18        }
19    }
20
21    #[must_use]
22    pub fn any() -> ValueRange<Self> {
23        ValueRange::range(
24            Self::NotPositioned(1),
25            Self::NotPositioned(u32::from(u16::MAX)),
26        )
27    }
28
29    #[must_use]
30    pub fn is_positioned(&self) -> bool {
31        matches!(self, Self::Positioned(..))
32    }
33
34    #[must_use]
35    pub fn is_mono(&self) -> bool {
36        match self {
37            Self::NotPositioned(1) => true,
38            Self::NotPositioned(_) => false,
39            Self::Positioned(positions) => {
40                matches!(&positions[..], [ChannelPosition::MONO])
41            }
42        }
43    }
44
45    #[must_use]
46    pub fn is_stereo(&self) -> bool {
47        match self {
48            Self::NotPositioned(2) => true,
49            Self::NotPositioned(_) => false,
50            Self::Positioned(positions) => {
51                matches!(
52                    &positions[..],
53                    [ChannelPosition::FRONT_LEFT, ChannelPosition::FRONT_RIGHT]
54                )
55            }
56        }
57    }
58}
59
60bitflags! {
61    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
62    pub struct ChannelPosition: u32 {
63        const MONO = 0x00000000;
64        const FRONT_LEFT = 0x00000001;
65        const FRONT_RIGHT = 0x00000002;
66        const FRONT_CENTER = 0x00000004;
67        const LOW_FREQUENCY = 0x00000008;
68        const BACK_LEFT = 0x00000010;
69        const BACK_RIGHT = 0x00000020;
70        const FRONT_LEFT_OF_CENTER = 0x00000040;
71        const FRONT_RIGHT_OF_CENTER = 0x00000080;
72        const BACK_CENTER = 0x00000100;
73        const SIDE_LEFT = 0x00000200;
74        const SIDE_RIGHT = 0x00000400;
75        const TOP_CENTER = 0x00000800;
76        const TOP_FRONT_LEFT = 0x00001000;
77        const TOP_FRONT_CENTER = 0x00002000;
78        const TOP_FRONT_RIGHT = 0x00004000;
79        const TOP_BACK_LEFT = 0x00008000;
80        const TOP_BACK_CENTER = 0x00010000;
81        const TOP_BACK_RIGHT = 0x00020000;
82    }
83}