Skip to main content

ebur128_stream/
channel.rs

1//! Channel layout and per-channel weighting.
2//!
3//! Per ITU-R BS.1770-4 §4.1, the loudness sum applies these per-channel
4//! weights:
5//!
6//! | Channel | Weight (linear) | dB |
7//! |---|---|---|
8//! | Left, Right, Center | 1.0 | 0 |
9//! | LeftSurround, RightSurround | 1.41 | +1.5 |
10//! | Lfe | 0.0 | (excluded) |
11//!
12//! [`Channel::Other`] is treated as unweighted (1.0); document the choice
13//! at the call site if it matters for your layout.
14
15/// One channel of a multi-channel programme.
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18#[non_exhaustive]
19pub enum Channel {
20    /// Front left.
21    Left,
22    /// Front right.
23    Right,
24    /// Front center.
25    Center,
26    /// Left surround.
27    LeftSurround,
28    /// Right surround.
29    RightSurround,
30    /// Low-frequency effects channel; excluded from loudness sum per
31    /// BS.1770-4.
32    Lfe,
33    /// Any other channel; treated as unweighted (1.0).
34    Other,
35}
36
37impl Channel {
38    /// The channel's contribution weight in the K-weighted sum, per
39    /// BS.1770-4 §4.1. Returns `0.0` for [`Channel::Lfe`].
40    ///
41    /// # Example
42    ///
43    /// ```
44    /// use ebur128_stream::Channel;
45    /// assert_eq!(Channel::Center.weight(), 1.0);
46    /// assert_eq!(Channel::LeftSurround.weight(), 1.41);
47    /// assert_eq!(Channel::Lfe.weight(), 0.0);  // excluded
48    /// ```
49    #[inline]
50    #[must_use]
51    pub const fn weight(self) -> f32 {
52        match self {
53            Channel::Left | Channel::Right | Channel::Center | Channel::Other => 1.0,
54            Channel::LeftSurround | Channel::RightSurround => 1.41,
55            Channel::Lfe => 0.0,
56        }
57    }
58}