Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

fix(audio): an equaliser, high-pass or low-pass works on a WAV whose header names no channels

Jareere353fe57d775
diff --git a/src/crates/concat-media/src/audio.rs b/src/crates/concat-media/src/audio.rs
index 2858952d..75aa9ff5 100644
--- a/src/crates/concat-media/src/audio.rs
+++ b/src/crates/concat-media/src/audio.rs
@@ -359,7 +359,10 @@ impl MixInput {
}
let read = || crate::feed::read_packet(&mut self.input, self.stream);
match crate::feed::next_frame(&mut self.decoder, &mut self.held, read) {
- Ok(Some(frame)) => Ok(Some(frame)),
+ Ok(Some(mut frame)) => {
+ ffi::name_channels(&mut frame);
+ Ok(Some(frame))
+ }
Ok(None) => {
self.done = true;
Ok(None)
diff --git a/src/crates/concat-media/src/ffi.rs b/src/crates/concat-media/src/ffi.rs
index 6b1ed854..c755228f 100644
--- a/src/crates/concat-media/src/ffi.rs
+++ b/src/crates/concat-media/src/ffi.rs
@@ -15,6 +15,23 @@ use libc::{c_char, c_int, c_void};
use crate::error::Error;
+/// Names the channels of a decoded frame whose file did not. A WAV
+/// without a channel mask decodes as "2 channels" in no order, and every
+/// filter that works on named channels - each equaliser, high-pass and
+/// low-pass - passes channels it cannot name through untouched, so a
+/// telephone or an underwater on such a file did nothing at all. FFmpeg's
+/// own command line guesses the usual layout for the count; so does this.
+pub fn name_channels(frame: &mut ffmpeg::util::frame::audio::Audio) {
+ use ffmpeg::util::channel_layout::{ChannelLayout, ChannelOrder};
+ let layout = frame.ch_layout();
+ if layout.order() == ChannelOrder::Unspecified && layout.channels() > 0 {
+ let named = ChannelLayout::default_for_channels(layout.channels());
+ if named.order() != ChannelOrder::Unspecified {
+ frame.set_ch_layout(named);
+ }
+ }
+}
+
/// Makes sure the libraries are initialised and quiet. Cheap after the
/// first call; every entry point calls it.
pub fn init() {
diff --git a/src/crates/concat-media/src/samples.rs b/src/crates/concat-media/src/samples.rs
index 95117f20..efd2e3e4 100644
--- a/src/crates/concat-media/src/samples.rs
+++ b/src/crates/concat-media/src/samples.rs
@@ -291,8 +291,12 @@ impl AudioDecoder {
/// One decoded frame out of the codec, or `None` when the file is over.
fn next_source(&mut self) -> Result<Option<Audio>> {
let read = || crate::feed::read_packet(&mut self.input, self.stream);
- crate::feed::next_frame(&mut self.decoder, &mut self.held, read)
- .map_err(|stall| crate::feed::fail(stall, &self.path))
+ let mut frame = crate::feed::next_frame(&mut self.decoder, &mut self.held, read)
+ .map_err(|stall| crate::feed::fail(stall, &self.path))?;
+ if let Some(frame) = frame.as_mut() {
+ crate::ffi::name_channels(frame);
+ }
+ Ok(frame)
}
/// The next frame out of the graph: interleaved samples at the requested
@@ -563,6 +567,67 @@ mod tests {
}
}
+ /// A WAV whose header names no channel layout - the plain PCM header,
+ /// which many recorders write - is still filtered: a low-pass under a
+ /// 440 Hz tone all but takes it away. It used to pass the tone through
+ /// untouched, the filter unable to name the channels it was given.
+ #[test]
+ fn a_wav_that_names_no_channels_is_still_filtered() {
+ let rate: u32 = 48_000;
+ let mut data = Vec::new();
+ for index in 0..rate {
+ let t = f64::from(index) / f64::from(rate);
+ let value = (0.5 * (2.0 * std::f64::consts::PI * 440.0 * t).sin() * 32767.0) as i16;
+ data.extend_from_slice(&value.to_le_bytes());
+ data.extend_from_slice(&value.to_le_bytes());
+ }
+ let mut bytes = Vec::new();
+ bytes.extend_from_slice(b"RIFF");
+ bytes.extend_from_slice(&(36 + data.len() as u32).to_le_bytes());
+ bytes.extend_from_slice(b"WAVEfmt ");
+ bytes.extend_from_slice(&16u32.to_le_bytes());
+ bytes.extend_from_slice(&1u16.to_le_bytes());
+ bytes.extend_from_slice(&2u16.to_le_bytes());
+ bytes.extend_from_slice(&rate.to_le_bytes());
+ bytes.extend_from_slice(&(rate * 4).to_le_bytes());
+ bytes.extend_from_slice(&4u16.to_le_bytes());
+ bytes.extend_from_slice(&16u16.to_le_bytes());
+ bytes.extend_from_slice(b"data");
+ bytes.extend_from_slice(&(data.len() as u32).to_le_bytes());
+ bytes.extend_from_slice(&data);
+ let path = std::env::temp_dir().join(format!("concat-unnamed-{}.wav", std::process::id()));
+ std::fs::write(&path, bytes).expect("writes the wav");
+
+ let loudest = |filters: Vec<String>| {
+ let options = AudioOptions {
+ filters,
+ ..AudioOptions::default()
+ };
+ let mut decoder = AudioDecoder::open(&path, &options).expect("opens");
+ let mut samples: Vec<i16> = Vec::new();
+ while let Some(frame) = decoder.next_frame().expect("decodes") {
+ let bytes = decoder.bytes_of(&frame);
+ samples.extend(
+ bytes
+ .chunks_exact(2)
+ .map(|b| i16::from_le_bytes([b[0], b[1]])),
+ );
+ }
+ // Past the filter's first tenth of a second, where it settles.
+ samples
+ .iter()
+ .skip(2 * 4_800)
+ .map(|sample| i32::from(*sample).abs())
+ .max()
+ .unwrap_or(0)
+ };
+ let plain = loudest(Vec::new());
+ let filtered = loudest(vec!["lowpass=f=60,lowpass=f=60".to_owned()]);
+ let _ = std::fs::remove_file(&path);
+ assert!(plain > 15_000, "the tone is there, at {plain}");
+ assert!(filtered < 1_000, "the tone came through at {filtered}");
+ }
+
#[test]
fn a_missing_file_is_an_error_not_a_panic() {
assert!(AudioDecoder::open("does-not-exist.mp3", &AudioOptions::default()).is_err());