Group ids only where the lanes agree, priming for a leading gap
Browse files at this commit07cd3d32486731be146f9b4ae77aaad89014f8e1Parent: 85c5140- fix
- group ids only where the lanes agree, priming for a leading gap
3 changed files +186 -16
- crates/engine/src/audio.rs +13 -3
- crates/engine/src/project.rs +125 -13
- crates/engine/tests/export.rs +48 -0
crates/engine/src/audio.rsmodified+13 -3
| @@ -272,7 +272,8 @@ impl AudioSession { | |||
| 272 | /// A segment naming **no** source is a gap, and is copied as that many | 272 | /// A segment naming **no** source is a gap, and is copied as that many |
| 273 | /// packets of [`silent_packet`] silence — the rounding debt carried through | 273 | /// packets of [`silent_packet`] silence — the rounding debt carried through |
| 274 | /// it like any other, so the hole occupies its exact duration and the audio | 274 | /// it like any other, so the hole occupies its exact duration and the audio |
| 275 | - /// after it stays in sync with the picture. | 275 | + /// after it stays in sync with the picture. A gap the track *opens* on gets |
| 276 | + /// one silent packet more, which is the priming a reader drops. | ||
| 276 | pub fn copy_multi_segments( | 277 | pub fn copy_multi_segments( |
| 277 | sources: &[PathBuf], | 278 | sources: &[PathBuf], |
| 278 | segs: &[(Option<usize>, f64, f64)], | 279 | segs: &[(Option<usize>, f64, f64)], |
| @@ -297,7 +298,14 @@ impl AudioSession { | |||
| 297 | let Some(source) = source else { | 298 | let Some(source) = source else { |
| 298 | let ideal = ((end_secs - start_secs).max(0.0) * f64::from(sample_rate)) as i64; | 299 | let ideal = ((end_secs - start_secs).max(0.0) * f64::from(sample_rate)) as i64; |
| 299 | let bytes = silent_packet(params.chan_conf)?; | 300 | let bytes = silent_packet(params.chan_conf)?; |
| 300 | - for _ in 0..packet_run(&mut err, ideal, u32::MAX) { | 301 | + // A reader drops the first packet of an AAC track as the |
| 302 | + // encoder's priming. When the track opens on a hole, that has to | ||
| 303 | + // come out of one *extra* packet of silence: dropped out of the | ||
| 304 | + // hole itself it would shorten it, and everything after the hole | ||
| 305 | + // would play a packet early. It is not part of the run, so it | ||
| 306 | + // owes the rounding debt nothing. | ||
| 307 | + let priming = u32::from(packets.is_empty()); | ||
| 308 | + for _ in 0..packet_run(&mut err, ideal, u32::MAX) + priming { | ||
| 301 | packets.push(AacPacket { | 309 | packets.push(AacPacket { |
| 302 | bytes: bytes.clone(), | 310 | bytes: bytes.clone(), |
| 303 | samples: SAMPLES_PER_PACKET, | 311 | samples: SAMPLES_PER_PACKET, |
| @@ -318,7 +326,9 @@ impl AudioSession { | |||
| 318 | let ideal = ((end_secs - start_secs).max(0.0) * f64::from(track.sample_rate)) as i64; | 326 | let ideal = ((end_secs - start_secs).max(0.0) * f64::from(track.sample_rate)) as i64; |
| 319 | let available = track.sample_count.saturating_sub(start_id - 1); | 327 | let available = track.sample_count.saturating_sub(start_id - 1); |
| 320 | // The head packet is the priming of the first segment only: a reader | 328 | // The head packet is the priming of the first segment only: a reader |
| 321 | - // drops exactly one, so an interior join must not add another. | 329 | + // drops exactly one, so an interior join must not add another -- |
| 330 | + // and neither must a segment behind a leading gap, which already | ||
| 331 | + // paid the priming in silence. | ||
| 322 | let head = (packets.is_empty() && start_id > 1).then(|| start_id - 1); | 332 | let head = (packets.is_empty() && start_id > 1).then(|| start_id - 1); |
| 323 | let ids = head | 333 | let ids = head |
| 324 | .into_iter() | 334 | .into_iter() |
crates/engine/src/project.rsmodified+125 -13
| @@ -376,18 +376,43 @@ impl Project { | |||
| 376 | /// placement's own start, in a gap, and past the end -- all of which would | 376 | /// placement's own start, in a gap, and past the end -- all of which would |
| 377 | /// produce an empty clip or nothing at all. | 377 | /// produce an empty clip or nothing at all. |
| 378 | /// | 378 | /// |
| 379 | + /// The two lanes' halves share an id only where they are the *same* span: | ||
| 380 | + /// the lanes are edited apart, so the clip being cut here may start (or | ||
| 381 | + /// end) somewhere else in the other lane, and a group id whose two clips | ||
| 382 | + /// disagree about their span is not one take (see [`links_are_consistent`], | ||
| 383 | + /// which refuses to load one). | ||
| 384 | + /// | ||
| 379 | /// Metadata only: no mapping changes, in any lane. | 385 | /// Metadata only: no mapping changes, in any lane. |
| 380 | pub fn split(&mut self, timeline_frame: u32) -> bool { | 386 | pub fn split(&mut self, timeline_frame: u32) -> bool { |
| 381 | - if !self | 387 | + let cut = Lane::ALL |
| 382 | - .lanes | 388 | + .map(|l| splittable(self.lane(l), timeline_frame).map(|idx| self.lane(l)[idx])); |
| 383 | - .iter() | 389 | + if cut.iter().all(Option::is_none) { |
| 384 | - .any(|lane| splittable(lane, timeline_frame).is_some()) | ||
| 385 | - { | ||
| 386 | return false; | 390 | return false; |
| 387 | } | 391 | } |
| 388 | self.snapshot(); | 392 | self.snapshot(); |
| 393 | + let same = |f: fn(&Clip) -> u32| match cut { | ||
| 394 | + [Some(a), Some(b)] => f(&a) == f(&b), | ||
| 395 | + _ => false, | ||
| 396 | + }; | ||
| 389 | let (left, right) = (self.new_link(), self.new_link()); | 397 | let (left, right) = (self.new_link(), self.new_link()); |
| 390 | - for lane in &mut self.lanes { | 398 | + // Each side is its own question: two clips may start together and end |
| 399 | + // apart, and the halves that do line up stay one take. | ||
| 400 | + let ids = [ | ||
| 401 | + (left, right), | ||
| 402 | + ( | ||
| 403 | + if same(|c| c.start) { | ||
| 404 | + left | ||
| 405 | + } else { | ||
| 406 | + self.new_link() | ||
| 407 | + }, | ||
| 408 | + if same(Clip::end) { | ||
| 409 | + right | ||
| 410 | + } else { | ||
| 411 | + self.new_link() | ||
| 412 | + }, | ||
| 413 | + ), | ||
| 414 | + ]; | ||
| 415 | + for (lane, (left, right)) in self.lanes.iter_mut().zip(ids) { | ||
| 391 | let Some(idx) = splittable(lane, timeline_frame) else { | 416 | let Some(idx) = splittable(lane, timeline_frame) else { |
| 392 | continue; | 417 | continue; |
| 393 | }; | 418 | }; |
| @@ -407,18 +432,28 @@ impl Project { | |||
| 407 | /// Only what a split could have produced is rejoined -- the two sides must | 432 | /// Only what a split could have produced is rejoined -- the two sides must |
| 408 | /// touch on the timeline *and* be consecutive frames of the same source -- | 433 | /// touch on the timeline *and* be consecutive frames of the same source -- |
| 409 | /// so the clip list comes back exactly as it was and traversal with it. | 434 | /// so the clip list comes back exactly as it was and traversal with it. |
| 410 | - /// `false` when no lane has such a pair. | 435 | + /// `false` when no lane has such a pair. The rejoined clips share one id |
| 436 | + /// only when they rejoin into the same span, for [`split`](Project::split)'s | ||
| 437 | + /// reason. | ||
| 411 | pub fn regroup(&mut self, timeline_frame: u32) -> bool { | 438 | pub fn regroup(&mut self, timeline_frame: u32) -> bool { |
| 412 | - if !self | 439 | + // What each lane would end up covering, if it joins here at all. |
| 413 | - .lanes | 440 | + let joined = Lane::ALL.map(|l| { |
| 414 | - .iter() | 441 | + joinable(self.lane(l), timeline_frame) |
| 415 | - .any(|lane| joinable(lane, timeline_frame).is_some()) | 442 | + .map(|idx| (self.lane(l)[idx].start, self.lane(l)[idx + 1].end())) |
| 416 | - { | 443 | + }); |
| 444 | + if joined.iter().all(Option::is_none) { | ||
| 417 | return false; | 445 | return false; |
| 418 | } | 446 | } |
| 419 | self.snapshot(); | 447 | self.snapshot(); |
| 420 | let link = self.new_link(); | 448 | let link = self.new_link(); |
| 421 | - for lane in &mut self.lanes { | 449 | + let ids = [ |
| 450 | + link, | ||
| 451 | + match joined { | ||
| 452 | + [Some(a), Some(b)] if a == b => link, | ||
| 453 | + _ => self.new_link(), | ||
| 454 | + }, | ||
| 455 | + ]; | ||
| 456 | + for (lane, link) in self.lanes.iter_mut().zip(ids) { | ||
| 422 | let Some(idx) = joinable(lane, timeline_frame) else { | 457 | let Some(idx) = joinable(lane, timeline_frame) else { |
| 423 | continue; | 458 | continue; |
| 424 | }; | 459 | }; |
| @@ -1456,4 +1491,81 @@ mod tests { | |||
| 1456 | assert!(p.undo()); | 1491 | assert!(p.undo()); |
| 1457 | consistent(&p, "undo"); | 1492 | consistent(&p, "undo"); |
| 1458 | } | 1493 | } |
| 1494 | + | ||
| 1495 | + /// What an edit produces is what a save writes, so *every* state the public | ||
| 1496 | + /// API can reach has to load again. The two the verifier panel found where | ||
| 1497 | + /// it did not: a split and a regroup handed one group id to two clips that | ||
| 1498 | + /// were not the same span, because the lanes had been edited apart. | ||
| 1499 | + #[test] | ||
| 1500 | + fn every_reachable_state_reloads() { | ||
| 1501 | + let mut p = Project::single(FILE, 150); | ||
| 1502 | + assert!(p.place(Lane::Audio, 3, clip(0, 0, 50, 0))); | ||
| 1503 | + assert!(p.split(10), "the lanes are cut apart at 10"); | ||
| 1504 | + reloads(&p, "place on one lane, then split"); | ||
| 1505 | + | ||
| 1506 | + let mut p = Project::single(FILE, 150); | ||
| 1507 | + assert!(p.split(30)); | ||
| 1508 | + assert!(p.lift(Lane::Video, 0), "the picture goes, the sound stays"); | ||
| 1509 | + assert!(p.regroup(30), "only the audio lane can rejoin"); | ||
| 1510 | + assert!(p.split(60)); | ||
| 1511 | + reloads(&p, "lift, regroup one lane, split"); | ||
| 1512 | + } | ||
| 1513 | + | ||
| 1514 | + /// The same claim, swept: random op sequences off the public surface, the | ||
| 1515 | + /// project reloaded after every one of them. A failure prints its seed, and | ||
| 1516 | + /// the seed replays the whole sequence. | ||
| 1517 | + #[test] | ||
| 1518 | + fn random_edit_sequences_reload() { | ||
| 1519 | + for seed in 0..200u64 { | ||
| 1520 | + let mut rng = seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1); | ||
| 1521 | + let mut next = move |n: u32| { | ||
| 1522 | + // xorshift64*: no dependency, and the seed replays it exactly. | ||
| 1523 | + rng ^= rng << 13; | ||
| 1524 | + rng ^= rng >> 7; | ||
| 1525 | + rng ^= rng << 17; | ||
| 1526 | + (rng >> 33) as u32 % n | ||
| 1527 | + }; | ||
| 1528 | + let mut p = Project::single(FILE, 40); | ||
| 1529 | + for step in 0..24 { | ||
| 1530 | + let frame = next(44); | ||
| 1531 | + // A clipboard copy carries the group id of the take it came from | ||
| 1532 | + // -- the input that made a placement duplicate an id. | ||
| 1533 | + let copied = *p | ||
| 1534 | + .lane(if next(2) == 0 { | ||
| 1535 | + Lane::Video | ||
| 1536 | + } else { | ||
| 1537 | + Lane::Audio | ||
| 1538 | + }) | ||
| 1539 | + .get(next(4) as usize) | ||
| 1540 | + .unwrap_or(&clip(0, 0, 5, 0)); | ||
| 1541 | + let lane = if next(2) == 0 { | ||
| 1542 | + Lane::Video | ||
| 1543 | + } else { | ||
| 1544 | + Lane::Audio | ||
| 1545 | + }; | ||
| 1546 | + let idx = next(4) as usize; | ||
| 1547 | + let _ = match next(9) { | ||
| 1548 | + 0 => p.split(frame), | ||
| 1549 | + 1 => p.regroup(frame), | ||
| 1550 | + 2 => p.lift(lane, idx), | ||
| 1551 | + 3 => p.place(lane, frame, copied), | ||
| 1552 | + 4 => p.paste(frame, copied), | ||
| 1553 | + 5 => p.ripple_delete(frame, next(9)), | ||
| 1554 | + 6 => p.delete(idx), | ||
| 1555 | + 7 => p.undo(), | ||
| 1556 | + _ => p.append_clip(0, 1 + next(9)), | ||
| 1557 | + }; | ||
| 1558 | + reloads(&p, &format!("seed {seed}, step {step}")); | ||
| 1559 | + } | ||
| 1560 | + } | ||
| 1561 | + } | ||
| 1562 | + | ||
| 1563 | + /// The save/load round trip a project must survive: the parts a save writes, | ||
| 1564 | + /// handed back to the constructor a load goes through. | ||
| 1565 | + fn reloads(p: &Project, what: &str) { | ||
| 1566 | + let (sources, video, audio) = p.clone().without_orphan_sources(); | ||
| 1567 | + if let Err(e) = Project::from_parts(sources, video, audio) { | ||
| 1568 | + panic!("{what}: saved but would not load: {e}\n{:?}", p.lanes); | ||
| 1569 | + } | ||
| 1570 | + } | ||
| 1459 | } | 1571 | } |
crates/engine/tests/export.rsmodified+48 -0
| @@ -552,6 +552,54 @@ fn synthetic(index: u32, width: usize, height: usize) -> (Vec<u8>, Vec<u8>, Vec< | |||
| 552 | ) | 552 | ) |
| 553 | } | 553 | } |
| 554 | 554 | ||
| 555 | +/// A timeline whose *first* second of sound was lifted. A reader drops the | ||
| 556 | +/// first packet of an AAC track as encoder priming, so a track that opens on a | ||
| 557 | +/// hole has to carry one extra packet of silence for it to drop -- without it | ||
| 558 | +/// the drop comes out of the hole and every sound after the hole plays 23 ms | ||
| 559 | +/// early, which is a lip sync error for the whole export. | ||
| 560 | +#[test] | ||
| 561 | +fn a_leading_audio_gap_keeps_the_sound_after_it_in_place() { | ||
| 562 | + pin_software(); | ||
| 563 | + let source = asset("test_av.mp4"); | ||
| 564 | + let mut session = PlaybackSession::open(&source).expect("open source"); | ||
| 565 | + assert!(session.cut_at(1.0), "cut at 1 s"); | ||
| 566 | + assert!(session.lift_clip(Lane::Audio, 0), "lift the first second"); | ||
| 567 | + session.pause(); | ||
| 568 | + let out = out_path("leading_gap"); | ||
| 569 | + let handle = session.export_to(&out); | ||
| 570 | + wait(&handle, Duration::from_secs(180)).expect("export"); | ||
| 571 | + | ||
| 572 | + let (_, chunks) = engine::AudioSession::open(&out) | ||
| 573 | + .expect("reopen export audio") | ||
| 574 | + .expect("export has an audio track"); | ||
| 575 | + let mut pcm: Vec<f32> = Vec::new(); | ||
| 576 | + while let Ok(chunk) = chunks.recv_timeout(Duration::from_secs(10)) { | ||
| 577 | + pcm.resize(chunk.start_sample as usize * 2, 0.0); | ||
| 578 | + pcm.extend_from_slice(&chunk.samples); | ||
| 579 | + } | ||
| 580 | + // Where the sound starts, measured in whole packets so the MDCT leak either | ||
| 581 | + // side of the splice cannot be mistaken for the onset: the first block | ||
| 582 | + // carrying a quarter of the export's own peak. | ||
| 583 | + let block = 1024 * 2; | ||
| 584 | + let peak = |b: &[f32]| b.iter().fold(0.0f32, |m, s| m.max(s.abs())); | ||
| 585 | + let loud = peak(&pcm) / 4.0; | ||
| 586 | + let onset = pcm | ||
| 587 | + .chunks(block) | ||
| 588 | + .position(|b| peak(b) > loud) | ||
| 589 | + .expect("the export has no sound at all") as f64 | ||
| 590 | + * 1024.0; | ||
| 591 | + println!("leading gap: sound starts at sample {onset} of an expected 44100"); | ||
| 592 | + assert!( | ||
| 593 | + (onset - 44_100.0).abs() <= 1024.0, | ||
| 594 | + "the sound after the gap is at {onset}, not at the 1 s hole's end" | ||
| 595 | + ); | ||
| 596 | + // ...and the hole really is a hole, not the first second played quietly. | ||
| 597 | + let quiet = peak(&pcm[..(0.9 * 44_100.0) as usize * 2]); | ||
| 598 | + assert!(quiet < 1e-3, "the lifted second is not silent: {quiet}"); | ||
| 599 | + | ||
| 600 | + std::fs::remove_file(&out).unwrap(); | ||
| 601 | +} | ||
| 602 | + | ||
| 555 | /// A timeline with a hole in each lane, exported. The video gap is encoded as | 603 | /// A timeline with a hole in each lane, exported. The video gap is encoded as |
| 556 | /// black rather than skipped -- the file is as long as the timeline, frame for | 604 | /// black rather than skipped -- the file is as long as the timeline, frame for |
| 557 | /// frame -- and the audio gap is copied as real silent packets, so the audio | 605 | /// frame -- and the audio gap is copied as real silent packets, so the audio |