#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum ChannelLayout {
Mono,
#[default]
Stereo,
Surround51,
Surround71,
AmbisonicFoa,
AmbisonicSoa,
AmbisonicToa,
Custom(usize),
}
impl ChannelLayout {
#[inline]
pub const fn channel_count(&self) -> usize {
match self {
Self::Mono => 1,
Self::Stereo => 2,
Self::Surround51 => 6,
Self::Surround71 => 8,
Self::AmbisonicFoa => 4,
Self::AmbisonicSoa => 9,
Self::AmbisonicToa => 16,
Self::Custom(n) => *n,
}
}
#[inline]
pub const fn is_ambisonic(&self) -> bool {
matches!(self, Self::AmbisonicFoa | Self::AmbisonicSoa | Self::AmbisonicToa)
}
#[inline]
pub const fn ambisonic_order(&self) -> Option<usize> {
match self {
Self::AmbisonicFoa => Some(1),
Self::AmbisonicSoa => Some(2),
Self::AmbisonicToa => Some(3),
_ => None,
}
}
#[inline]
pub const fn from_ambisonic_order(order: usize) -> Option<Self> {
match order {
1 => Some(Self::AmbisonicFoa),
2 => Some(Self::AmbisonicSoa),
3 => Some(Self::AmbisonicToa),
_ => None,
}
}
#[inline]
pub const fn ambisonic_channel_count(order: usize) -> usize {
(order + 1) * (order + 1)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ChannelConfig {
#[default]
Parallel,
Explicit,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn channel_layout_channel_count() {
assert_eq!(ChannelLayout::Mono.channel_count(), 1);
assert_eq!(ChannelLayout::Stereo.channel_count(), 2);
assert_eq!(ChannelLayout::Surround51.channel_count(), 6);
assert_eq!(ChannelLayout::Surround71.channel_count(), 8);
assert_eq!(ChannelLayout::AmbisonicFoa.channel_count(), 4);
assert_eq!(ChannelLayout::AmbisonicSoa.channel_count(), 9);
assert_eq!(ChannelLayout::AmbisonicToa.channel_count(), 16);
assert_eq!(ChannelLayout::Custom(12).channel_count(), 12);
}
#[test]
fn channel_layout_is_ambisonic() {
assert!(!ChannelLayout::Mono.is_ambisonic());
assert!(!ChannelLayout::Stereo.is_ambisonic());
assert!(!ChannelLayout::Surround51.is_ambisonic());
assert!(!ChannelLayout::Surround71.is_ambisonic());
assert!(ChannelLayout::AmbisonicFoa.is_ambisonic());
assert!(ChannelLayout::AmbisonicSoa.is_ambisonic());
assert!(ChannelLayout::AmbisonicToa.is_ambisonic());
assert!(!ChannelLayout::Custom(4).is_ambisonic());
}
#[test]
fn channel_layout_ambisonic_order() {
assert_eq!(ChannelLayout::Mono.ambisonic_order(), None);
assert_eq!(ChannelLayout::Stereo.ambisonic_order(), None);
assert_eq!(ChannelLayout::AmbisonicFoa.ambisonic_order(), Some(1));
assert_eq!(ChannelLayout::AmbisonicSoa.ambisonic_order(), Some(2));
assert_eq!(ChannelLayout::AmbisonicToa.ambisonic_order(), Some(3));
}
#[test]
fn channel_layout_from_ambisonic_order() {
assert_eq!(ChannelLayout::from_ambisonic_order(0), None);
assert_eq!(
ChannelLayout::from_ambisonic_order(1),
Some(ChannelLayout::AmbisonicFoa)
);
assert_eq!(
ChannelLayout::from_ambisonic_order(2),
Some(ChannelLayout::AmbisonicSoa)
);
assert_eq!(
ChannelLayout::from_ambisonic_order(3),
Some(ChannelLayout::AmbisonicToa)
);
assert_eq!(ChannelLayout::from_ambisonic_order(4), None);
}
#[test]
fn channel_layout_ambisonic_channel_count() {
assert_eq!(ChannelLayout::ambisonic_channel_count(1), 4);
assert_eq!(ChannelLayout::ambisonic_channel_count(2), 9);
assert_eq!(ChannelLayout::ambisonic_channel_count(3), 16);
}
#[test]
fn channel_layout_default() {
assert_eq!(ChannelLayout::default(), ChannelLayout::Stereo);
}
#[test]
fn channel_config_default() {
assert_eq!(ChannelConfig::default(), ChannelConfig::Parallel);
}
}