Skip to main content

moq_audio/
layout.rs

1use crate::Error;
2
3/// Speaker meaning and interleaving order for PCM channels.
4#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
5#[non_exhaustive]
6pub enum Layout {
7	/// One center channel.
8	Mono,
9	/// Left then right channels.
10	#[default]
11	Stereo,
12	/// Channels with no declared speaker positions, in source order.
13	Discrete(u32),
14}
15
16impl Layout {
17	/// Infer today's conventional layout from a channel count.
18	pub fn from_channels(channels: u32) -> Result<Self, Error> {
19		match channels {
20			0 => Err(Error::Unsupported(
21				"audio layout must contain at least one channel".into(),
22			)),
23			1 => Ok(Self::Mono),
24			2 => Ok(Self::Stereo),
25			channels => Ok(Self::Discrete(channels)),
26		}
27	}
28
29	/// Number of interleaved channels in this layout.
30	pub fn channels(self) -> u32 {
31		match self {
32			Self::Mono => 1,
33			Self::Stereo => 2,
34			Self::Discrete(channels) => channels,
35		}
36	}
37
38	pub(crate) fn validate(self) -> Result<(), Error> {
39		if self.channels() == 0 {
40			return Err(Error::Unsupported(
41				"audio layout must contain at least one channel".into(),
42			));
43		}
44		Ok(())
45	}
46}