edith
Clone
https://git.tahinli.com/Tahinli/edith.git
0 watching·0 forks·Rust·18.3 MiB·ff99ad6·Tahinli·6 days ago·1055 commits
| 1 | //! Multicam clips through the session: cameras synced by the sound they share (a tone burst heard |
| 2 | //! 1.3 s later by one of them), the angle switched at the playhead, the picture the render shows |
| 3 | //! being the active angle's own, flattening, the file (v46) and undo. |
| 4 | //! |
| 5 | //! ```text |
| 6 | //! cargo test -p engine --release --test multicam |
| 7 | //! ``` |
| 8 | |
| 9 | use std::path::{Path, PathBuf}; |
| 10 | use std::process::Command; |
| 11 | |
| 12 | use engine::project::{Angle, Lane}; |
| 13 | use engine::scratch::Scratch; |
| 14 | use engine::{AngleSync, MulticamSync, PlaybackSession}; |
| 15 | |
| 16 | /// A 5 s clip of `pattern` whose sound is a quiet hum and a 0.6 s tone burst at `burst` seconds |
| 17 | /// (and a second, shorter one 2.5 s later, so the match is not a single event). |
| 18 | fn camera(dir: &Path, name: &str, pattern: &str, burst: f64) -> PathBuf { |
| 19 | let out = dir.join(name); |
| 20 | let sound = format!( |
| 21 | "aevalsrc='0.02*sin(2*PI*220*t)+if(between(t,{burst},{}),0.5*sin(2*PI*1000*t),0)+if(between(t,{},{}),0.4*sin(2*PI*600*t),0)':s=44100:d=5", |
| 22 | burst + 0.6, |
| 23 | burst + 2.5, |
| 24 | burst + 2.8 |
| 25 | ); |
| 26 | let ok = Command::new("ffmpeg") |
| 27 | .args(["-v", "error", "-y", "-f", "lavfi", "-i", &format!("{pattern}=s=320x180:r=25:d=5"), "-f", "lavfi", "-i", &sound]) |
| 28 | .args(["-ac", "2", "-c:v", "libx264", "-preset", "ultrafast", "-crf", "20", "-pix_fmt", "yuv420p", "-c:a", "aac", "-shortest"]) |
| 29 | .arg(&out) |
| 30 | .status() |
| 31 | .expect("ffmpeg runs") |
| 32 | .success(); |
| 33 | assert!(ok, "ffmpeg made {name}"); |
| 34 | out |
| 35 | } |
| 36 | |
| 37 | /// Two cameras, the second hearing the burst 1.3 s after the first: a session with both imported. |
| 38 | fn rig(dir: &Path) -> (PlaybackSession, PathBuf, PathBuf) { |
| 39 | let a = camera(dir, "cam_a.mp4", "testsrc2", 1.0); |
| 40 | let b = camera(dir, "cam_b.mp4", "smptebars", 2.3); |
| 41 | let mut s = PlaybackSession::open(&a).expect("open"); |
| 42 | s.set_gain(0.0); |
| 43 | assert_eq!(s.import(&b).expect("import"), 1); |
| 44 | (s, a, b) |
| 45 | } |
| 46 | |
| 47 | fn clips(s: &PlaybackSession, lane: Lane) -> Vec<(u32, u32, u32, usize, Angle)> { |
| 48 | s.lane_clips(lane).iter().map(|c| (c.start, c.in_frame, c.out_frame, c.source, c.angle)).collect() |
| 49 | } |
| 50 | |
| 51 | #[test] |
| 52 | fn the_sync_finds_the_delay_of_a_tone_burst() { |
| 53 | let dir = Scratch::dir("ve_mcam_sync"); |
| 54 | let (mut s, ..) = rig(&dir); |
| 55 | let r = s.make_multicam_at(&[0, 1], MulticamSync::Audio, Some("Take"), Lane::V1, 0.0).expect("multicam"); |
| 56 | assert_eq!(r.how[0], AngleSync::Reference); |
| 57 | assert!(matches!(r.how[1], AngleSync::Audio { score } if score > 0.8), "{:?}", r.how); |
| 58 | assert!((r.offsets_secs[1] - 1.3).abs() < 0.04, "{:?}", r.offsets_secs); |
| 59 | // The camera that heard the burst later started earlier: it sits at the start, the other 1.3 s in. |
| 60 | assert_eq!(r.starts[1], 0); |
| 61 | assert!((i64::from(r.starts[0]) - 33).abs() <= 1, "{:?}", r.starts); |
| 62 | assert_eq!(s.lane_clips(Lane::V1).len(), 1); |
| 63 | assert_eq!(s.sequence_name(r.made.sequence).as_deref(), Some("Take")); |
| 64 | assert!(s.undo()); |
| 65 | assert_eq!(s.sequences().len(), 1, "one undo takes the whole multicam back"); |
| 66 | assert!(!s.lane_clips(Lane::V1).iter().any(|c| engine::project::is_sequence_source(c.source))); |
| 67 | } |
| 68 | |
| 69 | #[test] |
| 70 | fn start_sync_lines_the_cameras_up_at_the_start() { |
| 71 | let dir = Scratch::dir("ve_mcam_start"); |
| 72 | let (mut s, ..) = rig(&dir); |
| 73 | let r = s.make_multicam_at(&[0, 1], MulticamSync::Start, None, Lane::V1, 0.0).expect("multicam"); |
| 74 | assert_eq!(r.starts, vec![0, 0]); |
| 75 | assert_eq!(r.how, vec![AngleSync::Start; 2]); |
| 76 | assert!(s.make_multicam_at(&[0, 0], MulticamSync::Start, None, Lane::V1, 0.0).is_err(), "one camera twice"); |
| 77 | assert!(s.make_multicam_at(&[0], MulticamSync::Start, None, Lane::V1, 0.0).is_err(), "one camera"); |
| 78 | } |
| 79 | |
| 80 | #[test] |
| 81 | fn the_render_shows_the_active_angle_and_a_switch_is_one_undo_step() { |
| 82 | let dir = Scratch::dir("ve_mcam_switch"); |
| 83 | let (mut s, a, b) = rig(&dir); |
| 84 | let only_a = PlaybackSession::open(&a).expect("open a"); |
| 85 | only_a.set_gain(0.0); |
| 86 | let only_b = PlaybackSession::open(&b).expect("open b"); |
| 87 | only_b.set_gain(0.0); |
| 88 | let made = s.make_multicam_at(&[0, 1], MulticamSync::Audio, None, Lane::V1, 0.0).expect("multicam"); |
| 89 | // Camera A joins about 1.3 s in: timeline frame 40 shows its frame 40 - start. |
| 90 | let start_a = made.starts[0]; |
| 91 | let first = s.compose_at(80, 160, 90).expect("frame"); |
| 92 | let pre = s.undo_depth(); |
| 93 | let touched = s.switch_angle_at(None, 2.0, Some(1), None).expect("switch"); |
| 94 | assert_eq!(touched.len(), 2, "the picture and its sound"); |
| 95 | assert_eq!(s.undo_depth(), pre + 1, "one undo step"); |
| 96 | assert_eq!(s.lane_clips(Lane::V1).len(), 2); |
| 97 | assert_eq!(s.lane_clips(Lane::A1).len(), 2); |
| 98 | assert_eq!(s.lane_clips(Lane::V1)[1].angle, Angle { video: 1, audio: None }); |
| 99 | // Before the cut camera A's frame, after it camera B's -- by pixels, against single-camera sessions. |
| 100 | let before = s.compose_at(40, 160, 90).expect("frame"); |
| 101 | let ref_a = only_a.compose_at(40 - start_a, 160, 90).expect("frame"); |
| 102 | assert_eq!(before.bgra, ref_a.bgra, "frame 40 is camera A's own"); |
| 103 | let after = s.compose_at(80, 160, 90).expect("frame"); |
| 104 | let ref_b = only_b.compose_at(80, 160, 90).expect("frame"); |
| 105 | assert_eq!(after.bgra, ref_b.bgra, "frame 80 is camera B's own"); |
| 106 | assert_ne!(first.bgra, after.bgra, "and the switch changed what frame 80 shows"); |
| 107 | assert!(s.undo()); |
| 108 | assert_eq!(s.lane_clips(Lane::V1).len(), 1); |
| 109 | assert_eq!(s.compose_at(80, 160, 90).expect("frame").bgra, first.bgra); |
| 110 | assert!(s.redo()); |
| 111 | assert_eq!(s.compose_at(80, 160, 90).expect("frame").bgra, after.bgra); |
| 112 | } |
| 113 | |
| 114 | #[test] |
| 115 | fn flattening_keeps_the_render_and_leaves_plain_clips() { |
| 116 | let dir = Scratch::dir("ve_mcam_flatten"); |
| 117 | let (mut s, ..) = rig(&dir); |
| 118 | s.make_multicam_at(&[0, 1], MulticamSync::Audio, None, Lane::V1, 0.0).expect("multicam"); |
| 119 | s.switch_angle_at(None, 2.0, Some(1), None).expect("switch"); |
| 120 | s.switch_angle_at(None, 3.0, Some(0), Some(Some(1))).expect("switch back, sound pinned to B"); |
| 121 | let frames = [0u32, 20, 40, 52, 60, 74, 75, 90, 110, 130]; |
| 122 | let want: Vec<Vec<u8>> = frames.iter().map(|&f| s.compose_at(f, 160, 90).expect("frame").bgra).collect(); |
| 123 | let (p, _) = s.export_snapshot(); |
| 124 | let fps = s.meta().frame_rate; |
| 125 | let sound = p.audio_segments_from(0, fps); |
| 126 | s.flatten_multicam(Lane::V1, 0).expect("flatten"); |
| 127 | assert!(s.lane_clips(Lane::V1).iter().chain(s.lane_clips(Lane::A1)).all(|c| !engine::project::is_sequence_source(c.source))); |
| 128 | assert_eq!(s.lane_clips(Lane::V1).len(), 3); |
| 129 | for (f, w) in frames.iter().zip(&want) { |
| 130 | assert_eq!(&s.compose_at(*f, 160, 90).expect("frame").bgra, w, "frame {f} after flatten"); |
| 131 | } |
| 132 | let (p, _) = s.export_snapshot(); |
| 133 | assert_eq!(p.audio_segments_from(0, fps), sound, "and the sound is the same segments"); |
| 134 | assert!(s.flatten_multicam(Lane::V1, 0).is_err(), "nothing multicam left"); |
| 135 | assert!(s.undo()); |
| 136 | assert!(s.lane_clips(Lane::V1).iter().all(|c| engine::project::is_sequence_source(c.source))); |
| 137 | } |
| 138 | |
| 139 | #[test] |
| 140 | fn a_multicam_project_round_trips_as_v46_and_a_plain_one_stays_v45() { |
| 141 | let dir = Scratch::dir("ve_mcam_file"); |
| 142 | let (mut s, ..) = rig(&dir); |
| 143 | // Several sequences but no multicam: still the v45 dialect. |
| 144 | s.new_sequence(Some("Spare")).expect("new"); |
| 145 | let plain = dir.join("plain.edith"); |
| 146 | s.save_project(&plain).expect("save"); |
| 147 | assert!(std::fs::read_to_string(&plain).expect("read").starts_with("edith 45\n")); |
| 148 | let r = s.make_multicam_at(&[0, 1], MulticamSync::Audio, Some("Take"), Lane::V1, 0.0).expect("multicam"); |
| 149 | s.switch_angle_at(None, 2.0, Some(1), None).expect("switch"); |
| 150 | s.switch_angle_at(None, 3.0, Some(0), Some(Some(1))).expect("switch"); |
| 151 | let path = dir.join("m.edith"); |
| 152 | s.save_project(&path).expect("save"); |
| 153 | let text = std::fs::read_to_string(&path).expect("read"); |
| 154 | assert!(text.starts_with("edith 47\n"), "{}", text.lines().next().unwrap_or("")); |
| 155 | assert!(text.contains(&format!("\nmcam {} cam_a\n", r.made.sequence)) && text.contains("\nangle video 1 1 2 -\n"), "{text}"); |
| 156 | let back = PlaybackSession::open_project(&path).expect("reopen"); |
| 157 | back.set_gain(0.0); |
| 158 | assert_eq!(clips(&back, Lane::V1), clips(&s, Lane::V1)); |
| 159 | assert_eq!(clips(&back, Lane::A1), clips(&s, Lane::A1)); |
| 160 | assert_eq!(back.angle_names(r.made.sequence), s.angle_names(r.made.sequence)); |
| 161 | for f in [10u32, 60, 100] { |
| 162 | assert_eq!(back.compose_at(f, 160, 90).expect("frame").bgra, s.compose_at(f, 160, 90).expect("frame").bgra, "frame {f}"); |
| 163 | } |
| 164 | // A v45 reader's file with an angle line, and a bad angle, are refused by name. |
| 165 | let old = text.replacen("edith 47", "edith 46", 1); |
| 166 | let p45 = dir.join("v45.edith"); |
| 167 | std::fs::write(&p45, old).expect("write"); |
| 168 | assert!(PlaybackSession::open_project(&p45).is_err(), "a v45 file has no angle lines"); |
| 169 | let bad = text.replace("angle video 1 1 2 -", "angle video 1 1 12 -"); |
| 170 | let pbad = dir.join("bad.edith"); |
| 171 | std::fs::write(&pbad, bad).expect("write"); |
| 172 | let err = PlaybackSession::open_project(&pbad).err().expect("refused").to_string(); |
| 173 | assert!(err.contains("angle"), "{err}"); |
| 174 | // Reopening and saving again writes the same bytes. |
| 175 | let again = dir.join("m2.edith"); |
| 176 | back.save_project(&again).expect("save"); |
| 177 | assert_eq!(std::fs::read_to_string(&again).expect("read"), text); |
| 178 | } |