An equalized mp4 re-encodes the sound the copy could not carry
Browse files at this commit33f7a153a8784a66e478c17be963e1ab57453a4fParent: 88fbed0- fix
- an equalized mp4 re-encodes the sound the copy could not carry
7 changed files +258 -112
- Cargo.lock +7 -0
- crates/app/src/main.rs +2 -2
- crates/engine/Cargo.toml +6 -0
- crates/engine/src/audio.rs +3 -2
- crates/engine/src/export.rs +147 -57
- crates/engine/src/playback.rs +1 -1
- crates/engine/tests/eq_audio.rs +92 -50
Cargo.lockmodified+7 -0
| @@ -1638,6 +1638,7 @@ dependencies = [ | ||
| 1638 | 1638 | "oxideav-ac3", |
| 1639 | 1639 | "oxideav-core", |
| 1640 | 1640 | "rav1e", |
| 1641 | + "rusty_aac", | |
| 1641 | 1642 | "rusty_h264", |
| 1642 | 1643 | "symphonia", |
| 1643 | 1644 | "symphonia-codec-aac", |
| @@ -4880,6 +4881,12 @@ version = "1.0.23" | ||
| 4880 | 4881 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 4881 | 4882 | checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" |
| 4882 | 4883 | |
| 4884 | +[[package]] | |
| 4885 | +name = "rusty_aac" | |
| 4886 | +version = "0.5.0" | |
| 4887 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 4888 | +checksum = "b7ab980ffb66a559565d09dd6727ff2da7d2a39d2e8b6149e48af6908caa99b5" | |
| 4889 | + | |
| 4883 | 4890 | [[package]] |
| 4884 | 4891 | name = "rusty_h264" |
| 4885 | 4892 | version = "0.8.0" |
crates/app/src/main.rsmodified+2 -2
| @@ -5895,7 +5895,7 @@ impl Quality { | ||
| 5895 | 5895 | /// that opens but never comes back out is exactly the gap a user would other- |
| 5896 | 5896 | /// wise go looking for. |
| 5897 | 5897 | const FORMATS: [(Option<Format>, &str, &str); 9] = [ |
| 5898 | - (Some(Format::Mp4), "MP4", "H.264 picture + copied AAC"), | |
| 5898 | + (Some(Format::Mp4), "MP4", "H.264 picture + AAC sound"), | |
| 5899 | 5899 | ( |
| 5900 | 5900 | Some(Format::Av1), |
| 5901 | 5901 | "AV1", |
| @@ -5905,7 +5905,7 @@ const FORMATS: [(Option<Format>, &str, &str); 9] = [ | ||
| 5905 | 5905 | (Some(Format::Flac), "FLAC", "lossless — audio only"), |
| 5906 | 5906 | (None, "MP3", "encoder is LGPL — licence decision pending"), |
| 5907 | 5907 | (None, "OGG", "no pure-Rust Vorbis or Opus encoder"), |
| 5908 | - (None, "AAC", "no pure-Rust AAC encoder"), | |
| 5908 | + (None, "AAC", "an MP4's sound here, not a file of its own"), | |
| 5909 | 5909 | ( |
| 5910 | 5910 | None, |
| 5911 | 5911 | "HEVC", |
crates/engine/Cargo.tomlmodified+6 -0
| @@ -23,6 +23,12 @@ image = { version = "0.25", default-features = false, features = [ | ||
| 23 | 23 | ] } |
| 24 | 24 | mp4 = "0.14" |
| 25 | 25 | rusty_h264 = { version = "0.8", default-features = false } |
| 26 | +# The AAC-LC *encoder*, so an mp4's sound can be a mix rather than a copy: an | |
| 27 | +# equalized (or otherwise processed) lane has to be decoded, and what is decoded | |
| 28 | +# has to be encoded again. Pure Rust with no C or FFI, Apache-2.0, same family | |
| 29 | +# as `rusty_h264` above. Nothing else in the ecosystem encodes AAC without | |
| 30 | +# linking Fraunhofer's C library, which this project's no-install rule forbids. | |
| 31 | +rusty_aac = "0.5" | |
| 26 | 32 | symphonia-core = "0.6" |
| 27 | 33 | symphonia-codec-aac = "0.6" |
| 28 | 34 | # The umbrella crate for the standalone audio files: one dependency instead of |
crates/engine/src/audio.rsmodified+3 -2
| @@ -647,8 +647,9 @@ impl AudioSession { | ||
| 647 | 647 | /// The raw AAC packets covering `segs` — the same half-open source-second |
| 648 | 648 | /// windows [`open_segments`](Self::open_segments) decodes — copied out |
| 649 | 649 | /// byte for byte. Nothing is decoded and nothing is re-encoded: the bytes go |
| 650 | - /// straight into an mp4 writer, which is the only way to keep audio in an | |
| 651 | - /// export at all (no pure-Rust AAC encoder exists). | |
| 650 | + /// straight into an mp4 writer, which is how an export keeps its sound exact | |
| 651 | + /// wherever the edit lets it (a lane carrying an equalizer cannot be copied | |
| 652 | + /// and is re-encoded instead -- `export::encode_audio`). | |
| 652 | 653 | /// |
| 653 | 654 | /// Cut points do not land on 1024-sample boundaries, so each segment is |
| 654 | 655 | /// rounded to whole packets against an error carried across the joins (see |
crates/engine/src/export.rsmodified+147 -57
| @@ -2,20 +2,23 @@ | ||
| 2 | 2 | //! or, picture left behind, as one WAV or FLAC of the timeline's audio alone. |
| 3 | 3 | //! |
| 4 | 4 | //! Video is fully re-encoded — a cut lands mid-GOP, so stream-copying across it |
| 5 | -//! is impossible — while audio is copied packet for packet, because there is no | |
| 6 | -//! pure-Rust AAC encoder to re-encode with. The audio-only formats have no such | |
| 7 | -//! trouble: `hound` writes PCM and `flacenc` encodes FLAC, both pure Rust, so | |
| 8 | -//! those are *decoded* out of the timeline instead of copied. The worker owns | |
| 9 | -//! everything: the caller gets an [`ExportHandle`] and polls it from its render | |
| 10 | -//! loop. | |
| 5 | +//! is impossible — while audio is copied packet for packet wherever a copy can | |
| 6 | +//! say what the timeline says: a copy is exact, free and never a generation of | |
| 7 | +//! loss. Where it *cannot* — an equalized lane, which is sample math no packet | |
| 8 | +//! copy can reach — that lane is decoded, mixed and encoded again with | |
| 9 | +//! `rusty_aac` ([`encode_audio`]). The audio-only formats have no such split: | |
| 10 | +//! `hound` writes PCM and `flacenc` encodes FLAC, so those are always *decoded* | |
| 11 | +//! out of the timeline. The worker owns everything: the caller gets an | |
| 12 | +//! [`ExportHandle`] and polls it from its render loop. | |
| 11 | 13 | //! |
| 12 | 14 | //! A **speeded** clip splits along the same line, and for the same reason. The |
| 13 | 15 | //! picture is re-encoded, so the frame walk honours a rate: it takes the source |
| 14 | 16 | //! frame each timeline frame shows ([`crate::project::Speed::source_at`], the |
| 15 | 17 | //! very frame preview holds there) and encodes a held one again rather than |
| 16 | -//! decoding it twice. The sound is *copied*, and a packet carries no rate -- so | |
| 17 | -//! a speeded clip on the one audio lane an mp4 copies is refused by name | |
| 18 | -//! ([`copy_audio`]) rather than written out at 1.00x under a re-timed picture. | |
| 18 | +//! decoding it twice. The sound is *copied* unless a filter forces a decode, and | |
| 19 | +//! a packet carries no rate -- so a speeded clip on the one audio lane an mp4 | |
| 20 | +//! copies is refused by name ([`copy_audio`]) rather than written out at 1.00x | |
| 21 | +//! under a re-timed picture. | |
| 19 | 22 | //! AV1 carries no audio at all and never meets that refusal; WAV and FLAC are |
| 20 | 23 | //! decoded and resampled, so they honour a rate like the picture does. |
| 21 | 24 | //! |
| @@ -60,21 +63,26 @@ const AV1_SPEED: u8 = 10; | ||
| 60 | 63 | |
| 61 | 64 | /// What an export writes. Four, not more: H.264-in-mp4 and AV1-in-Matroska are |
| 62 | 65 | /// the only *video* pairs with both an encoder and a decoder under this |
| 63 | -/// project's no-install rule, and WAV and FLAC are the only audio formats with | |
| 64 | -/// a pure-Rust encoder at all. MP3 has one (`shine-rs`) under LGPL-2.0, which is | |
| 65 | -/// a licensing decision this project has not taken; Vorbis, Opus and AAC have | |
| 66 | -/// none. HEVC and VP9 have no encoder here at all (`hevc`/`vp9` import through | |
| 67 | -/// the plugin and stop there). A front-end says so rather than hiding the rows. | |
| 66 | +/// project's no-install rule, and WAV and FLAC are the only *standalone* audio | |
| 67 | +/// formats with a pure-Rust encoder at all. MP3 has one (`shine-rs`) under | |
| 68 | +/// LGPL-2.0, which is a licensing decision this project has not taken; Vorbis | |
| 69 | +/// and Opus have none. (AAC does -- `rusty_aac`, which is what an equalized mp4 | |
| 70 | +/// track is encoded with -- but AAC is an mp4's own audio, never a file of its | |
| 71 | +/// own here.) HEVC and VP9 have no encoder here at all (`hevc`/`vp9` import | |
| 72 | +/// through the plugin and stop there). A front-end says so rather than hiding | |
| 73 | +/// the rows. | |
| 68 | 74 | #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] |
| 69 | 75 | pub enum Format { |
| 70 | - /// Picture and sound: video re-encoded, AAC copied. Copied, so it carries | |
| 71 | - /// **one** audio lane: a timeline whose sound is spread over two is refused | |
| 72 | - /// by name here and mixed by the other two formats, which decode. | |
| 76 | + /// Picture and sound: video re-encoded, AAC copied -- or re-encoded too, | |
| 77 | + /// where a copy could not carry the edit (an equalized lane). Either way it | |
| 78 | + /// carries **one** audio lane: a timeline whose sound is spread over two is | |
| 79 | + /// refused by name here and mixed by the other two formats, which decode. | |
| 73 | 80 | #[default] |
| 74 | 81 | Mp4, |
| 75 | 82 | /// Picture alone, AV1 in Matroska. Alone because Matroska carries no sound |
| 76 | - /// this project can write: there is no AAC or Opus encoder here, and an AAC | |
| 77 | - /// track copied in would be one *this engine's own reader* leaves silent | |
| 83 | + /// this project can write: this engine's Matroska writer has no audio track | |
| 84 | + /// at all, and an AAC track written into one would be one *this engine's own | |
| 85 | + /// reader* leaves silent | |
| 78 | 86 | /// (`audio::AudioSession::open` refuses Matroska audio). The sound of an AV1 |
| 79 | 87 | /// export is a WAV or FLAC beside it, which a front-end says up front. |
| 80 | 88 | Av1, |
| @@ -256,7 +264,9 @@ fn settle(shared: &Shared, result: crate::Result<()>) { | ||
| 256 | 264 | } |
| 257 | 265 | |
| 258 | 266 | /// The timeline's one audio lane, copied packet for packet: the mp4 path's |
| 259 | -/// sound, and the reason that path carries only one lane of it. | |
| 267 | +/// sound, and the reason that path carries only one lane of it. A lane an | |
| 268 | +/// equalizer has been put on cannot be *copied* at all and leaves through | |
| 269 | +/// [`encode_audio`] instead; every other timeline keeps this copy, bit for bit. | |
| 260 | 270 | /// |
| 261 | 271 | /// ponytail: this holds the whole exported AAC track in memory (~3 kB per |
| 262 | 272 | /// 23 ms packet, so ~500 MB for an hour). Upgrade path is a streaming |
| @@ -277,10 +287,15 @@ fn copy_audio( | ||
| 277 | 287 | // of a timeline playing a file's second audio track carries *that* track. |
| 278 | 288 | // |
| 279 | 289 | // One lane, because this path *copies* AAC packets and a mix is not a copy: |
| 280 | - // summing two lanes means decoding both and encoding the result, and there | |
| 281 | - // is no AAC encoder here to encode it with. Refused by name rather than | |
| 282 | - // silently exporting the first lane -- a file missing half its sound is the | |
| 283 | - // one failure a user would not notice. | |
| 290 | + // summing two lanes means decoding both, and only the one thing a copy | |
| 291 | + // cannot express at all -- an equalizer -- is decoded here today. Refused by | |
| 292 | + // name rather than silently exporting the first lane, which is a file | |
| 293 | + // missing half its sound: the one failure a user would not notice. | |
| 294 | + // | |
| 295 | + // ponytail: the ceiling is this routing, not the codec any more. | |
| 296 | + // [`encode_audio`] already mixes *every* audio lane (it is the WAV path's | |
| 297 | + // own opener), so lifting this refusal is routing a second case to it -- | |
| 298 | + // deliberately not done here, where the change under test is the equalizer. | |
| 284 | 299 | // |
| 285 | 300 | // *Which* lane is the same question `audio_segments_from` answers for |
| 286 | 301 | // playback, and it is asked here rather than assumed: the lane that holds |
| @@ -292,32 +307,29 @@ fn copy_audio( | ||
| 292 | 307 | let [segments] = &lanes[..] else { |
| 293 | 308 | return Err(format!( |
| 294 | 309 | "this timeline has {} audio lanes and an mp4 export copies one: \ |
| 295 | - there is no AAC encoder here to mix them with. Export WAV or FLAC, \ | |
| 296 | - which are mixed, or put the sound on one lane", | |
| 310 | + export WAV or FLAC, which are mixed, or put the sound on one lane", | |
| 297 | 311 | lanes.len() |
| 298 | 312 | ) |
| 299 | 313 | .into()); |
| 300 | 314 | }; |
| 301 | - // ...and the same list decides which clips this would be copying, which is | |
| 302 | - // the only way to ask whether any of them carries an equalizer. An EQ is | |
| 303 | - // sample math and a copy never decodes: exporting it here would write the | |
| 304 | - // clip flat, silently -- the same failure a missing audio track is. Named by | |
| 305 | - // where it sits on the timeline, because "some clip" is not a thing a user | |
| 306 | - // can go and fix. | |
| 315 | + // ...and the same list names *which* lane those clips sit on, which is the | |
| 316 | + // only way to ask what has been done to them. | |
| 307 | 317 | let lane = project.audio_lanes()[0]; |
| 308 | 318 | // ...and the same list decides whether any clip this would copy plays at a |
| 309 | 319 | // rate other than the one it was recorded at. A copy hands the mp4 the very |
| 310 | 320 | // AAC packets the source holds and there is no rate inside a packet to |
| 311 | 321 | // change: the picture would come out re-timed (the walk in [`run`] honours |
| 312 | 322 | // it) over sound at 1.00x, which is the drift a person finds only after the |
| 313 | - // file has gone somewhere. Refused by name, exactly as the equalizer below | |
| 314 | - // is -- and only *here*, on the lane being copied: AV1 carries no audio at | |
| 315 | - // all and never reaches this, so it honours a speed like WAV and FLAC do. | |
| 323 | + // file has gone somewhere. Refused by name -- and only *here*, on the lane | |
| 324 | + // being copied: AV1 carries no audio at all and never reaches this, so it | |
| 325 | + // honours a speed like WAV and FLAC do. | |
| 316 | 326 | // |
| 317 | - // ponytail: the ceiling is the packet copy itself, not the rate. Upgrade | |
| 318 | - // path is the same one the equalizer below wants -- an AAC encoder (or a | |
| 319 | - // decode-and-re-encode of this lane) -- at which point the resample the WAV | |
| 320 | - // path already runs feeds it and this refusal goes. | |
| 327 | + // ponytail: the ceiling is the packet copy itself, not the rate, and the way | |
| 328 | + // off it is now next door -- [`encode_audio`] resamples exactly as the WAV | |
| 329 | + // path does, so routing a speeded lane there is the one line the equalizer | |
| 330 | + // below takes. Left refused because this change is the equalizer's, and a | |
| 331 | + // speeded export turning silently from a copy into a re-encode is a change | |
| 332 | + // of its own to make on purpose. | |
| 321 | 333 | if let Some((at, speed)) = project |
| 322 | 334 | .lane(lane) |
| 323 | 335 | .iter() |
| @@ -331,28 +343,106 @@ fn copy_audio( | ||
| 331 | 343 | ) |
| 332 | 344 | .into()); |
| 333 | 345 | } |
| 334 | - if let Some((at, _)) = project | |
| 335 | - .lane(lane) | |
| 336 | - .iter() | |
| 337 | - .enumerate() | |
| 338 | - .find(|(idx, _)| { | |
| 339 | - project | |
| 340 | - .eq_of(lane, *idx) | |
| 341 | - .is_some_and(|eq| !eq.is_identity()) | |
| 342 | - }) | |
| 343 | - .map(|(idx, clip)| (clip.start, idx)) | |
| 344 | - { | |
| 345 | - return Err(format!( | |
| 346 | - "the clip at {} has an equalizer and an mp4 export copies AAC packets, \ | |
| 347 | - which no filter can reach: export WAV or FLAC, which are decoded and \ | |
| 348 | - mixed, or clear the equalizer", | |
| 349 | - timecode(at, meta.frame_rate) | |
| 350 | - ) | |
| 351 | - .into()); | |
| 346 | + // ...and last, whether any clip on it carries an equalizer. An EQ is sample | |
| 347 | + // math and a copy never decodes, so copying such a lane would write the clip | |
| 348 | + // *flat*, silently -- the failure a missing audio track is. This lane is | |
| 349 | + // decoded, filtered, mixed and encoded again instead ([`encode_audio`]), and | |
| 350 | + // only this case is: a timeline nobody has equalized still leaves through | |
| 351 | + // the copy below, packet for packet, so no passthrough quietly becomes a | |
| 352 | + // generation of loss. | |
| 353 | + let equalized = (0..project.lane(lane).len()) | |
| 354 | + .any(|idx| project.eq_of(lane, idx).is_some_and(|eq| !eq.is_identity())); | |
| 355 | + if equalized { | |
| 356 | + return encode_audio(project, meta); | |
| 352 | 357 | } |
| 353 | 358 | AudioSession::copy_multi_streams(&project.audio_sources(), segments) |
| 354 | 359 | } |
| 355 | 360 | |
| 361 | +/// The same lane, *decoded*: what a packet copy cannot carry comes out here as | |
| 362 | +/// AAC written by this project's own encoder. | |
| 363 | +/// | |
| 364 | +/// The samples are the very ones [`run_audio`] would put in a WAV -- the same | |
| 365 | +/// opener with the same equalizers, the same rates and the same resize onto the | |
| 366 | +/// timeline's own length -- so an mp4 and a WAV of one timeline are one mix, | |
| 367 | +/// not two that could disagree. | |
| 368 | +/// | |
| 369 | +/// AAC-LC's encoder delay is one 1024-frame block, which is exactly the priming | |
| 370 | +/// a reader drops when the file carries no edit list (`audio::DEFAULT_PRIMING`, | |
| 371 | +/// and [`crate::mux::Mp4Muxer`] writes none): the first packet is that delay, so | |
| 372 | +/// the sound starts at timeline frame 0 as a copied track's does. | |
| 373 | +/// | |
| 374 | +/// ponytail: the mix sits in memory as f32 before it is encoded (~46 MB a | |
| 375 | +/// minute of 48 kHz stereo), for [`run_audio`]'s reason -- `rusty_aac` buffers | |
| 376 | +/// the whole stream anyway, since it encodes its frames in parallel. Upgrade | |
| 377 | +/// path is a chunked push once that encoder streams. | |
| 378 | +#[allow(clippy::type_complexity)] | |
| 379 | +fn encode_audio( | |
| 380 | + project: &Project, | |
| 381 | + meta: &VideoMeta, | |
| 382 | +) -> crate::Result<Option<(crate::AacTrackParams, Vec<crate::AacPacket>)>> { | |
| 383 | + let sources = project.audio_sources(); | |
| 384 | + let segs = project.audio_segments_from(0, meta.frame_rate); | |
| 385 | + let eqs = project.audio_eqs_from(0, meta.frame_rate); | |
| 386 | + let speeds = project.audio_speeds_from(0, meta.frame_rate); | |
| 387 | + let Some((audio, chunks)) = | |
| 388 | + AudioSession::open_mixed_streams_speed(&sources, &segs, &eqs, &speeds)? | |
| 389 | + else { | |
| 390 | + return Ok(None); // no audio to write, exactly as a copy of nothing is | |
| 391 | + }; | |
| 392 | + let freq_index = rusty_aac::sf_index_for_rate(audio.sample_rate).ok_or_else(|| { | |
| 393 | + format!( | |
| 394 | + "{} Hz is not an AAC sample rate: export WAV or FLAC, which write it as it is", | |
| 395 | + audio.sample_rate | |
| 396 | + ) | |
| 397 | + })?; | |
| 398 | + let channels = usize::from(audio.channels.max(1)); | |
| 399 | + // The timeline's length, not the mix's: a segment resolves its window to | |
| 400 | + // whole samples on its own, so the sum can miss by a sample or two and a | |
| 401 | + // source that ran out early would leave the track short under a picture | |
| 402 | + // that is not. One resize settles both, as [`run_audio`]'s does. | |
| 403 | + let total = (f64::from(project.timeline_frames()) / meta.frame_rate | |
| 404 | + * f64::from(audio.sample_rate)) | |
| 405 | + .round() as usize | |
| 406 | + * channels; | |
| 407 | + let mut samples: Vec<f32> = Vec::with_capacity(total); | |
| 408 | + for chunk in chunks { | |
| 409 | + samples.extend(chunk.samples); | |
| 410 | + } | |
| 411 | + samples.resize(total, 0.0); | |
| 412 | + | |
| 413 | + // 256 kbps, not the encoder's 128 default: this is a *second* generation of | |
| 414 | + // lossy coding over a source that was already AAC, and the export is a | |
| 415 | + // master a person edits from again, not a delivery file. Measured on the | |
| 416 | + // suite's fixtures, an untouched clip re-encoded here moves 0.15 dB at this | |
| 417 | + // rate against 0.46 dB at 128 -- and 32 KB/s beside a 2.76 Mbps picture is | |
| 418 | + // not a size anyone is counting. | |
| 419 | + let mut encoder = rusty_aac::AacEncoder::new(rusty_aac::AacEncoderConfig { | |
| 420 | + bitrate_bps: 256_000, | |
| 421 | + ..Default::default() | |
| 422 | + }); | |
| 423 | + encoder.push_pcm(&samples, audio.channels.max(1), audio.sample_rate)?; | |
| 424 | + encoder.finish(); | |
| 425 | + let mut packets = Vec::new(); | |
| 426 | + // `next_packet` after `finish` yields packets until `Eof` and nothing else, | |
| 427 | + // so the loop ends on the only error it can see. | |
| 428 | + while let Ok(packet) = encoder.next_packet() { | |
| 429 | + packets.push(crate::AacPacket { | |
| 430 | + bytes: packet.data, | |
| 431 | + samples: packet.duration, | |
| 432 | + }); | |
| 433 | + } | |
| 434 | + Ok(Some(( | |
| 435 | + crate::AacTrackParams { | |
| 436 | + freq_index, | |
| 437 | + // 1 and 2 are the ISO channel configurations for mono and stereo, | |
| 438 | + // and the opener refuses anything wider than stereo already. | |
| 439 | + chan_conf: audio.channels.max(1) as u8, | |
| 440 | + sample_rate: audio.sample_rate, | |
| 441 | + }, | |
| 442 | + packets, | |
| 443 | + ))) | |
| 444 | +} | |
| 445 | + | |
| 356 | 446 | fn run( |
| 357 | 447 | project: &Project, |
| 358 | 448 | meta: &VideoMeta, |
crates/engine/src/playback.rsmodified+1 -1
| @@ -1186,7 +1186,7 @@ impl PlaybackSession { | ||
| 1186 | 1186 | /// [`import`](Self::import) refuses a file with. One output device and one |
| 1187 | 1187 | /// copied AAC track mean one set of audio parameters for the whole |
| 1188 | 1188 | /// timeline, so a 22 kHz mono track cannot join a 44.1 kHz stereo one -- |
| 1189 | - /// there is no resampler here, and no AAC encoder to write the join with. | |
| 1189 | + /// there is no resampler here to make one rate of two. | |
| 1190 | 1190 | /// A front-end greys such a row out; this is the backstop that keeps a |
| 1191 | 1191 | /// stale one from making the whole timeline silent. |
| 1192 | 1192 | /// |
crates/engine/tests/eq_audio.rsmodified+92 -50
| @@ -277,72 +277,114 @@ fn a_lane_is_equalized_before_the_mix_and_never_after_it() { | ||
| 277 | 277 | } |
| 278 | 278 | |
| 279 | 279 | /// The export half of the same claim, through `export::start`: what is written |
| 280 | -/// is what is heard, so the WAV carries the boost at the same ratio the feeder | |
| 281 | -/// path measured. | |
| 280 | +/// is what is heard, so both audio-only formats carry the boost at the same | |
| 281 | +/// ratio the feeder path measured. Both, because both are lossless and both are | |
| 282 | +/// one `run_audio` -- a FLAC that disagreed with the WAV beside it would be the | |
| 283 | +/// writer, and this is where that would show. | |
| 282 | 284 | #[test] |
| 283 | -fn a_wav_export_carries_the_same_boost_playback_does() { | |
| 285 | +fn the_audio_only_exports_carry_the_same_boost_playback_does() { | |
| 284 | 286 | let flat = project(vec![clip(0, 0, 30)]); |
| 285 | 287 | let mut eqd = flat.clone(); |
| 286 | 288 | assert!(eqd.set_eq(Lane::A1, 0, Some(boost()))); |
| 287 | 289 | |
| 290 | + for format in [Format::Wav, Format::Flac] { | |
| 291 | + let mut written = Vec::new(); | |
| 292 | + for (name, project) in [("flat", &flat), ("eqd", &eqd)] { | |
| 293 | + let out = out_path(name, format.ext()); | |
| 294 | + let handle = engine::export::start( | |
| 295 | + project.clone(), | |
| 296 | + meta(), | |
| 297 | + &out, | |
| 298 | + &ExportSettings { | |
| 299 | + format, | |
| 300 | + ..Default::default() | |
| 301 | + }, | |
| 302 | + ); | |
| 303 | + wait(&handle, Duration::from_secs(60)).expect("an audio-only export"); | |
| 304 | + written.push(decode(&out)); | |
| 305 | + std::fs::remove_file(&out).unwrap(); | |
| 306 | + } | |
| 307 | + let [plain, boosted] = &written[..] else { | |
| 308 | + unreachable!("two exports") | |
| 309 | + }; | |
| 310 | + assert_boosted( | |
| 311 | + rms(boosted, 0.05, 0.95) / rms(plain, 0.05, 0.95), | |
| 312 | + format.name(), | |
| 313 | + ); | |
| 314 | + // ...and the same number to the ear: the export is not a second, kinder | |
| 315 | + // filter run at a different point in the chain. | |
| 316 | + let played = rms(&play(&eqd), 0.05, 0.95) / rms(&play(&flat), 0.05, 0.95); | |
| 317 | + let exported = rms(boosted, 0.05, 0.95) / rms(plain, 0.05, 0.95); | |
| 318 | + assert!( | |
| 319 | + (played - exported).abs() < 0.05, | |
| 320 | + "playback boosted by {played:.3} and {} by {exported:.3}", | |
| 321 | + format.name() | |
| 322 | + ); | |
| 323 | + } | |
| 324 | +} | |
| 325 | + | |
| 326 | +/// An mp4 export *copies* AAC packets and no filter can reach a copy, so an | |
| 327 | +/// equalized lane is decoded, mixed and encoded again instead | |
| 328 | +/// (`export::encode_audio`). The claim is the same one the WAV makes: the boost | |
| 329 | +/// is in the file, on the clip that was given it and on no other -- and the | |
| 330 | +/// track still starts where the picture does, which is what the encoder's own | |
| 331 | +/// priming packet is for. | |
| 332 | +#[test] | |
| 333 | +fn an_mp4_export_carries_the_boost_a_packet_copy_could_not() { | |
| 334 | + let flat = project(vec![clip(0, 0, 30), clip(30, 0, 30)]); | |
| 335 | + let mut eqd = flat.clone(); | |
| 336 | + assert!(eqd.set_eq(Lane::A1, 1, Some(boost()))); | |
| 337 | + | |
| 288 | 338 | let mut written = Vec::new(); |
| 289 | 339 | for (name, project) in [("flat", &flat), ("eqd", &eqd)] { |
| 290 | - let out = out_path(name, "wav"); | |
| 291 | - let handle = engine::export::start( | |
| 292 | - project.clone(), | |
| 293 | - meta(), | |
| 294 | - &out, | |
| 295 | - &ExportSettings { | |
| 296 | - format: Format::Wav, | |
| 297 | - ..Default::default() | |
| 298 | - }, | |
| 299 | - ); | |
| 300 | - wait(&handle, Duration::from_secs(60)).expect("wav export"); | |
| 340 | + let out = out_path(name, "mp4"); | |
| 341 | + let settings = ExportSettings::default(); | |
| 342 | + let handle = engine::export::start(project.clone(), meta(), &out, &settings); | |
| 343 | + wait(&handle, Duration::from_secs(180)).expect("an equalized timeline exports as mp4"); | |
| 301 | 344 | written.push(decode(&out)); |
| 302 | 345 | std::fs::remove_file(&out).unwrap(); |
| 303 | 346 | } |
| 304 | 347 | let [plain, boosted] = &written[..] else { |
| 305 | 348 | unreachable!("two exports") |
| 306 | 349 | }; |
| 307 | - assert_boosted( | |
| 308 | - rms(boosted, 0.05, 0.95) / rms(plain, 0.05, 0.95), | |
| 309 | - "wav export", | |
| 350 | + | |
| 351 | + // Two seconds of timeline, to within the one 1024-frame packet the length | |
| 352 | + // rounds up to: an encoder delay left uncompensated would show up here as a | |
| 353 | + // whole packet of shift, and in the windows below as a smeared cut. | |
| 354 | + let secs = |s: &[f32]| s.len() as f64 / 2.0 / f64::from(RATE); | |
| 355 | + println!( | |
| 356 | + "mp4 lengths: flat {:.3}s, eqd {:.3}s", | |
| 357 | + secs(plain), | |
| 358 | + secs(boosted) | |
| 310 | 359 | ); |
| 311 | - // ...and the same number to the ear: the export is not a second, kinder | |
| 312 | - // filter run at a different point in the chain. | |
| 313 | - let played = rms(&play(&eqd), 0.05, 0.95) / rms(&play(&flat), 0.05, 0.95); | |
| 314 | - let exported = rms(boosted, 0.05, 0.95) / rms(plain, 0.05, 0.95); | |
| 360 | + for samples in [plain, boosted] { | |
| 361 | + assert!( | |
| 362 | + (secs(samples) - 2.0).abs() < 1024.0 / f64::from(RATE), | |
| 363 | + "the exported track is {:.3}s of a 2.000s timeline", | |
| 364 | + secs(samples) | |
| 365 | + ); | |
| 366 | + } | |
| 367 | + | |
| 368 | + // The boost itself. Not to `assert_boosted`'s fifth of a dB: this file is | |
| 369 | + // AAC at 128 kbps measured against a *copied* one, so a fraction of a dB is | |
| 370 | + // the codec, not the filter. A dB either way is far tighter than the 12 the | |
| 371 | + // band asks for and far wider than any coding noise. | |
| 372 | + let db = 20.0 * (rms(boosted, 1.05, 1.95) / rms(plain, 1.05, 1.95)).log10(); | |
| 373 | + println!("mp4 equalized clip: {db:+.2} dB"); | |
| 315 | 374 | assert!( |
| 316 | - (played - exported).abs() < 0.05, | |
| 317 | - "playback boosted by {played:.3} and the export by {exported:.3}" | |
| 375 | + (db - f64::from(BOOST_DB)).abs() < 1.0, | |
| 376 | + "the equalized clip in the mp4 measured {db:+.2} dB, the band asks for {BOOST_DB:+.1}" | |
| 318 | 377 | ); |
| 319 | -} | |
| 320 | - | |
| 321 | -/// An mp4 export *copies* AAC packets, and no filter can reach a packet copy. | |
| 322 | -/// Refused by name, with the clip's own position in it -- silently writing the | |
| 323 | -/// clip flat is the one failure nobody would notice. | |
| 324 | -#[test] | |
| 325 | -fn an_mp4_export_refuses_an_equalized_clip_and_names_where_it_is() { | |
| 326 | - let mut p = project(vec![clip(0, 0, 30), clip(30, 0, 30)]); | |
| 327 | - assert!(p.set_eq(Lane::A1, 1, Some(boost()))); | |
| 328 | - | |
| 329 | - let out = out_path("refused", "mp4"); | |
| 330 | - let handle = engine::export::start(p.clone(), meta(), &out, &ExportSettings::default()); | |
| 331 | - let err = wait(&handle, Duration::from_secs(60)) | |
| 332 | - .expect_err("an equalized clip cannot leave through the copy path") | |
| 333 | - .to_string(); | |
| 334 | - println!("mp4 refusal: {err}"); | |
| 335 | - assert!(err.contains("00:01"), "the refusal names the clip: {err}"); | |
| 336 | - assert!(err.contains("equalizer"), "{err}"); | |
| 337 | - assert!(err.contains("WAV or FLAC"), "and the way out: {err}"); | |
| 338 | - assert!(!out.exists(), "a refused export leaves no file"); | |
| 339 | - | |
| 340 | - // Taking the curve off is all it takes -- the refusal is about the setting, | |
| 341 | - // never about the clip having once had one. | |
| 342 | - assert!(p.set_eq(Lane::A1, 1, None)); | |
| 343 | - let handle = engine::export::start(p, meta(), &out, &ExportSettings::default()); | |
| 344 | - wait(&handle, Duration::from_secs(120)).expect("a flat timeline exports as mp4"); | |
| 345 | - std::fs::remove_file(&out).unwrap(); | |
| 378 | + // The clip before it is the same sound: not to the bit -- this file is a | |
| 379 | + // re-encode and that one is a copy -- but to a fraction of a dB, which is | |
| 380 | + // what says the filter stopped at the cut rather than lifting the lot. | |
| 381 | + let leak = 20.0 * (rms(boosted, 0.05, 0.95) / rms(plain, 0.05, 0.95)).log10(); | |
| 382 | + println!("mp4 flat clip moved {leak:+.2} dB"); | |
| 383 | + assert!(leak.abs() < 0.5, "the flat clip moved {leak:+.2} dB"); | |
| 384 | + | |
| 385 | + // ...and a timeline nobody equalized never reaches the encoder at all: its | |
| 386 | + // audio track is still the source's own packets, which | |
| 387 | + // `export::exported_packets_are_the_copied_stream` holds to the byte. | |
| 346 | 388 | } |
| 347 | 389 | |
| 348 | 390 | /// A band moved while the timeline is playing: `PlaybackSession::set_eq` writes |