diff --git a/crates/ec-ac3/src/decoder.rs b/crates/ec-ac3/src/decoder.rs index 3c69d80c..f30751a7 100644 --- a/crates/ec-ac3/src/decoder.rs +++ b/crates/ec-ac3/src/decoder.rs @@ -413,20 +413,10 @@ impl Ac3Decoder { } } - /// Apply the requested downmix, if any (§7.8.2). - fn fold(&self, samples: Vec, info: &FrameInfo, positions: &[ChannelPosition]) -> Vec { - if self.options.downmix == Downmix::Native { - return samples; - } - let channels = positions.len(); - if channels == 0 { - return samples; - } + /// Per-channel gains into the left and right downmix sums (§7.8.2). + fn downmix_gains(info: &FrameInfo, positions: &[ChannelPosition]) -> (Vec, Vec) { let clev = info.center_mix_level.unwrap_or(0.707); let slev = info.surround_mix_level.unwrap_or(0.707); - // §7.8.2 with the standard's own coefficients, then one scaling so a - // full-scale input cannot overflow the downmix — the standard's fixed - // -7.65 dB would quieten every stereo listener instead. let (mut left, mut right) = (Vec::new(), Vec::new()); for &p in positions { let (l, r) = match p { @@ -447,6 +437,22 @@ impl Ac3Decoder { left.push(l); right.push(r); } + (left, right) + } + + /// Apply the requested downmix, if any (§7.8.2). + fn fold(&self, samples: Vec, info: &FrameInfo, positions: &[ChannelPosition]) -> Vec { + if self.options.downmix == Downmix::Native { + return samples; + } + let channels = positions.len(); + if channels == 0 { + return samples; + } + // The standard's own coefficients, then one scaling so a full-scale + // input cannot overflow the downmix — the standard's fixed -7.65 dB + // would quieten every stereo listener instead. + let (left, right) = Self::downmix_gains(info, positions); let peak = left .iter() .sum::()