diff --git a/crates/engine/src/audio.rs b/crates/engine/src/audio.rs index c789bc1..3687daa 100644 --- a/crates/engine/src/audio.rs +++ b/crates/engine/src/audio.rs @@ -376,6 +376,22 @@ impl AudioSession { segs: &[(Option, f64, f64)], eqs: &[Option], speeds: &[Option], + ) -> crate::Result)>> { + Self::open_multi_streams_speed_at(sources, segs, eqs, speeds, None) + } + + /// [`open_multi_streams_speed`] with the project's own rate, when one was + /// picked ([`crate::edith::Document::sample_rate`]), overriding the source's + /// derived one. The per-segment [`Rate`] chain below already conforms any + /// segment whose own rate differs from `meta.sample_rate`, so naming a rate + /// here costs nothing new -- it only changes which rate that chain conforms + /// *to*. + pub fn open_multi_streams_speed_at( + sources: &[(PathBuf, usize)], + segs: &[(Option, f64, f64)], + eqs: &[Option], + speeds: &[Option], + sample_rate: Option, ) -> crate::Result)>> { // The first source that could have a track, which is not always index 0: // a still image has none, and one at the front of the list (a save @@ -395,7 +411,7 @@ impl AudioSession { // The timeline's meta is that source's: policy makes every other source // match it, and the checks below hold them to that. let meta = AudioMeta { - sample_rate: first.sample_rate(), + sample_rate: sample_rate.unwrap_or_else(|| first.sample_rate()), channels: first.channels(), total_samples: first.total_samples(), }; @@ -658,7 +674,23 @@ impl AudioSession { gains: &[f32], limiter: Limiter, ) -> crate::Result)>> { - Self::open_mixed_streams_live(sources, lanes, eqs, speeds, gains, limiter, None) + Self::open_mixed_streams_master_at(sources, lanes, eqs, speeds, gains, limiter, None) + } + + /// [`open_mixed_streams_master`] with the project's own rate + /// ([`crate::edith::Document::sample_rate`]) overriding the derived one, the + /// same rate every lane's meta agrees on ([`open_mixed_streams_live`]'s + /// invariant). + pub fn open_mixed_streams_master_at( + sources: &[(PathBuf, usize)], + lanes: &[Vec<(Option, f64, f64)>], + eqs: &[Vec>], + speeds: &[Vec>], + gains: &[f32], + limiter: Limiter, + sample_rate: Option, + ) -> crate::Result)>> { + Self::open_mixed_streams_live(sources, lanes, eqs, speeds, gains, limiter, None, sample_rate) } /// [`open_mixed_streams_master`](Self::open_mixed_streams_master) with the @@ -677,6 +709,7 @@ impl AudioSession { gains: &[f32], limiter: Limiter, live: Option<&Arc>, + sample_rate: Option, ) -> crate::Result)>> { let [first, ..] = lanes else { return Ok(None); @@ -684,11 +717,12 @@ impl AudioSession { if !Self::is_mixed(lanes.len(), gains, limiter) { let flat = Vec::new(); let plain = Vec::new(); - return Self::open_multi_streams_speed( + return Self::open_multi_streams_speed_at( sources, first, eqs.first().unwrap_or(&flat), speeds.first().unwrap_or(&plain), + sample_rate, ); } let mut meta = None; @@ -699,11 +733,12 @@ impl AudioSession { // timeline. let flat = Vec::new(); let plain = Vec::new(); - let Some((lane_meta, rx)) = Self::open_multi_streams_speed( + let Some((lane_meta, rx)) = Self::open_multi_streams_speed_at( sources, segs, eqs.get(i).unwrap_or(&flat), speeds.get(i).unwrap_or(&plain), + sample_rate, )? else { return Ok(None); diff --git a/crates/engine/src/edith.rs b/crates/engine/src/edith.rs index 6dfa3b0..6c177c9 100644 --- a/crates/engine/src/edith.rs +++ b/crates/engine/src/edith.rs @@ -216,7 +216,8 @@ use crate::subtitle::SubtitleTrack; /// What [`save`] writes. Read support goes back to `edith 1`; see the module /// docs for what those dialects looked like. -const MAGIC: &[u8] = b"edith 16"; +const MAGIC: &[u8] = b"edith 17"; +const MAGIC_V16: &[u8] = b"edith 16"; const MAGIC_V15: &[u8] = b"edith 15"; const MAGIC_V14: &[u8] = b"edith 14"; const MAGIC_V13: &[u8] = b"edith 13"; @@ -296,6 +297,15 @@ pub struct Document { /// for a v14 file that leaves the line out, which both mean the seat this /// machine has -- the only choice those projects offered. pub encoder: crate::export::EncoderSeat, + /// The rate the project's *sound* is mixed and exported at, chosen rather + /// than probed. `None` for every dialect before v17 and for a v17 file that + /// leaves the line out, which both mean what they always did: the rate of + /// the first source that has any ([`crate::PlaybackSession::open_project`]'s + /// `first_audio_of`). Playback and export both resample every other source + /// to whichever rate this resolves to, exactly as they already resample a + /// source shot at another rate than the timeline's -- an explicit choice + /// here only changes what that timeline's own rate *is*. + pub sample_rate: Option, pub playhead: u32, } @@ -321,6 +331,7 @@ pub fn save( auto_proxy: bool, encoder: crate::export::EncoderSeat, limiter: Limiter, + sample_rate: Option, playhead: u32, ) -> crate::Result<()> { let dir = project_dir(path); @@ -335,7 +346,7 @@ pub fn save( .and_then(|mut f| { f.write_all(&emit( &dir, sources, lanes, gains, subs, subtitles, eq, color, resolution, fps, tone, - proxy, auto_proxy, encoder, limiter, playhead, + proxy, auto_proxy, encoder, limiter, sample_rate, playhead, ))?; f.sync_all() }) @@ -383,6 +394,7 @@ fn emit( auto_proxy: bool, encoder: crate::export::EncoderSeat, limiter: Limiter, + sample_rate: Option, playhead: u32, ) -> Vec { let mut out = Vec::new(); @@ -395,6 +407,12 @@ fn emit( if let Some(fps) = fps.filter(|f| f.is_finite() && *f > 0.0) { out.extend_from_slice(format!("fps {fps:?}\n").as_bytes()); } + // Beside it, and by the same rule: a project nobody chose a sound rate for + // is the mix the source's own probe always gave it, which is what leaving + // this line out still means. + if let Some(rate) = sample_rate.filter(|&r| (1_000..=384_000).contains(&r)) { + out.extend_from_slice(format!("samplerate {rate}\n").as_bytes()); + } // Beside the rate, and written only when it is not the default: a project // holding no HDR media has nothing to say here, and saying nothing is what // a v10 file already said. @@ -580,8 +598,10 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result { // The dialects that wrote a source line without its stream field. Reading // one is the whole of what "an old project still opens" means here. let streamless = v1 || first == MAGIC_V2; - // The one whose captions may carry a group id... - let v16 = first == MAGIC; + // The one that carries an explicit sound rate... + let v17 = first == MAGIC; + // ...the one whose captions may carry a group id... + let v16 = v17 || first == MAGIC_V16; // ...the one that places the words it names... let v15 = v16 || first == MAGIC_V15; // ...the ones that say which encoder an export takes... @@ -653,6 +673,10 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result { // still means. encoder: crate::export::EncoderSeat::default(), fps: None, + // Nothing before v17 chose a sound rate: the source's own probe is + // still what the mix is cut at, which is what `None` means everywhere + // else it reaches (`PlaybackSession::open_project`). + sample_rate: None, playhead: 0, }; let mut playhead_seen = false; @@ -708,6 +732,20 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result { })?; doc.fps = Some(fps); } + b"samplerate" if v17 => { + if doc.sample_rate.is_some() || !doc.sources.is_empty() { + return Err(format!( + "line {n}: samplerate belongs once, before the sources" + ) + .into()); + } + let f = fields(rest, 1, "samplerate", n)?; + let rate = number(f[0], n)?; + if !(1_000..=384_000).contains(&rate) { + return Err(format!("line {n}: {rate} Hz is not a sound rate").into()); + } + doc.sample_rate = Some(rate); + } b"tonemap" if v11 => { if !doc.sources.is_empty() { return Err( @@ -1395,6 +1433,7 @@ mod tests { true, crate::export::EncoderSeat::default(), Limiter::default(), + None, playhead, ) } @@ -1442,6 +1481,7 @@ mod tests { true, crate::export::EncoderSeat::default(), Limiter::default(), + None, 0, ); let text = String::from_utf8_lossy(&bytes); @@ -1469,7 +1509,7 @@ mod tests { video 1 0 0 30 0 - - - fit 1000\n"; let old = parse(v9, &dir).expect("v9 still loads"); assert_eq!(old.subtitles, Vec::new()); - assert!(flat(&dir, &old.sources, &old.lanes, old.playhead).starts_with(b"edith 16\n")); + assert!(flat(&dir, &old.sources, &old.lanes, old.playhead).starts_with(b"edith 17\n")); // ...and the line itself is not a v9 line: a dialect may not be mixed. let mixed = parse(b"edith 9\nsource 0 a.mp4\nsubtitle - subs.srt\n", &dir) .unwrap_err() @@ -1522,6 +1562,7 @@ mod tests { true, crate::export::EncoderSeat::default(), Limiter::default(), + None, 0, ); let text = String::from_utf8_lossy(&bytes); @@ -1573,6 +1614,7 @@ mod tests { true, crate::export::EncoderSeat::default(), Limiter::default(), + None, 0, ) }; @@ -1636,6 +1678,7 @@ mod tests { auto, crate::export::EncoderSeat::default(), Limiter::default(), + None, 0, ) }; @@ -1701,6 +1744,7 @@ mod tests { true, seat, Limiter::default(), + None, 0, ) }; @@ -1749,6 +1793,72 @@ mod tests { } } + /// The v17 line: an explicit sound rate, written only when picked and read + /// back as the very number -- and a v16 file, which had no way to say one + /// and meant the source's own probe, still opens with `None`. + #[test] + fn the_sample_rate_round_trips_and_a_v16_file_leaves_it_derived() { + let dir = PathBuf::from("/proj"); + let (_, sources, lanes) = doc(); + let bytes = |rate| { + super::emit( + &dir, + &sources, + &lanes, + &[], + &[], + &[], + &[], + &[], + (1280, 720), + None, + crate::tonemap::Preset::default(), + false, + true, + crate::export::EncoderSeat::default(), + Limiter::default(), + rate, + 0, + ) + }; + // Unset is the line left out, so a project nobody has chosen a rate for + // is the bytes a v16 file was bar the version line. + let unset = bytes(None); + assert!( + !String::from_utf8_lossy(&unset).contains("samplerate"), + "no pick is the line left out: {}", + String::from_utf8_lossy(&unset) + ); + assert_eq!(parse(&unset, &dir).expect("v17 parses").sample_rate, None); + for rate in [44_100, 48_000, 96_000] { + let written = bytes(Some(rate)); + let text = String::from_utf8_lossy(&written).to_string(); + assert!(text.contains(&format!("samplerate {rate}\n")), "{text}"); + assert_eq!( + parse(&written, &dir).expect("v17 parses").sample_rate, + Some(rate) + ); + } + // A v16 project had no rate of its own to say: the field comes back + // `None`, which is what "derive it from the source" already meant. + let v16 = b"edith 16\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ + video 1 0 0 30 0 - - - fit 1000\n"; + assert_eq!(parse(v16, &dir).expect("v16 still loads").sample_rate, None); + // ...and the line is not a v16 line: a dialect may not be mixed. + let mixed = parse(b"edith 16\nsource 0 a.mp4\nsamplerate 48000\n", &dir) + .unwrap_err() + .to_string(); + assert_eq!(mixed, "line 3: unknown keyword \"samplerate\""); + // A number out of a sound rate's range is a corrupt line, by name. + for line in ["samplerate 0\n", "samplerate 500000\n", "samplerate nope\n"] { + let file = format!("edith 17\nsource 0 a.mp4\n{line}"); + assert!( + parse(file.as_bytes(), &dir).is_err(), + "{line:?} is not a sound rate line" + ); + } + } + /// The v11 line: the HDR rendition, written by name, read back as the very /// preset -- and the dialect before it, which had no way to say one and /// meant the published conversion. @@ -1773,6 +1883,7 @@ mod tests { true, crate::export::EncoderSeat::default(), Limiter::default(), + None, 0, ); let text = String::from_utf8_lossy(&bytes).to_string(); @@ -1866,7 +1977,7 @@ mod tests { let bytes = flat(&dir, &sources, &lanes, 12); assert_eq!( String::from_utf8_lossy(&bytes), - "edith 16\nplayhead 12\nresolution 1280 720\nsource 0 a.mp4\n\ + "edith 17\nplayhead 12\nresolution 1280 720\nsource 0 a.mp4\n\ source 2 /elsewhere/b.mp4\n\ video 1 0 0 30 0 0 - - fit 1000\nvideo 1 30 10 20 1 1 - - fit 1000\n\ audio 1 0 0 30 0 0 - - fit 1000\n", @@ -1903,7 +2014,7 @@ mod tests { let bytes = flat(&dir, &sources, &lanes, 7); assert_eq!( String::from_utf8_lossy(&bytes), - "edith 16\nplayhead 7\nresolution 1280 720\nsource 0 a.mp4\n\ + "edith 17\nplayhead 7\nresolution 1280 720\nsource 0 a.mp4\n\ video 1 0 0 30 0 4 - - fit 1000\naudio 1\n\ video 2 40 0 10 0 - - - fit 1000\naudio 2 0 0 30 0 4 - - fit 1000\n", "an empty lane is a line of its own; everything else is its clips" @@ -2011,7 +2122,7 @@ mod tests { let bytes = emit(&dir, &sources, &lanes, &eq, &[], (1280, 720), 0); assert_eq!( String::from_utf8_lossy(&bytes), - "edith 16\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ + "edith 17\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ eq 80.0:-3.0:0.707:ls 1000.0:4.5:1.0:pk\n\ eq 16777215.0:-0.1:3.918315e-39:hs\n\ eq\n\ @@ -2115,7 +2226,7 @@ mod tests { let bytes = emit(&dir, &sources, &lanes, &[], &color, (1280, 720), 0); assert_eq!( String::from_utf8_lossy(&bytes), - "edith 16\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ + "edith 17\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ color 0.1:1.2:0.9:-0.3\n\ color -1e-7:16777215.0:3.918315e-39:-0.0\n\ color 0.0:1.0:1.0:0.0\n\ @@ -2180,7 +2291,7 @@ mod tests { (1280, 720), old.playhead )), - "edith 16\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\ + "edith 17\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\ eq 80.0:-3.0:0.707:ls\n\ video 1 0 0 30 0 0 0 - fit 1000\naudio 1 0 0 30 0 0 - - fit 1000\n" ); @@ -2218,7 +2329,7 @@ mod tests { let bytes = emit(&dir, &sources, &lanes, &[], &[], (1280, 720), 0); assert_eq!( String::from_utf8_lossy(&bytes), - "edith 16\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ + "edith 17\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ video 1 0 0 30 0 - - - fit 2000\nvideo 1 15 30 40 0 - - - fit 250\n\ audio 1 0 0 30 0 - - - fit 2000\n", "the rate is the clip line's last field, in thousandths" @@ -2247,7 +2358,7 @@ mod tests { (1280, 720), old.playhead )), - "edith 16\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\ + "edith 17\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\ video 1 0 0 30 0 0 - - fit 1000\naudio 1 0 0 30 0 0 - - fit 1000\n" ); // A rate outside what the editor can set is a corrupt line, by name. @@ -2293,11 +2404,12 @@ mod tests { true, crate::export::EncoderSeat::default(), limiter, + None, 0, ); assert_eq!( String::from_utf8_lossy(&bytes), - "edith 16\nplayhead 0\nresolution 1280 720\nfps 23.976023976023978\n\ + "edith 17\nplayhead 0\nresolution 1280 720\nfps 23.976023976023978\n\ limiter -1.5 on\nsource 0 a.mp4\n\ video 1 0 0 30 0 - - - fit 1000\naudio 1 0 0 30 0 - - - fit 1000\n\ audio 2 0 0 30 0 - - - fit 1000\n\ @@ -2329,7 +2441,7 @@ mod tests { (1280, 720), old.playhead )), - "edith 16\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\ + "edith 17\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\ video 1 0 0 30 0 - - - fit 1000\naudio 1 0 0 30 0 - - - fit 1000\n" ); @@ -2493,7 +2605,7 @@ mod tests { assert!(old.eq.is_empty(), "nothing before v5 equalizes anything"); assert_eq!( String::from_utf8_lossy(&flat(&dir, &old.sources, &old.lanes, old.playhead)), - "edith 16\nplayhead 12\nresolution 1280 720\nsource 0 a.mp4\n\ + "edith 17\nplayhead 12\nresolution 1280 720\nsource 0 a.mp4\n\ source 2 /elsewhere/b.mp4\n\ video 1 0 0 30 0 0 - - fit 1000\nvideo 1 30 10 20 1 1 - - fit 1000\n\ audio 1 0 0 30 0 0 - - fit 1000\n" @@ -2546,7 +2658,7 @@ mod tests { let v5 = flat(&dir, &back.sources, &back.lanes, back.playhead); assert_eq!( String::from_utf8_lossy(&v5), - "edith 16\nplayhead 12\nresolution 1280 720\nsource 0 a.mp4\n\ + "edith 17\nplayhead 12\nresolution 1280 720\nsource 0 a.mp4\n\ source 0 /elsewhere/b.mp4\n\ video 1 0 0 30 0 0 - - fit 1000\nvideo 1 30 10 20 1 1 - - fit 1000\n\ audio 1 0 0 30 0 0 - - fit 1000\n", @@ -2578,7 +2690,7 @@ mod tests { // Saved again it is the current version, which round-trips to the // same document. let v5 = flat(&dir, &back.sources, &back.lanes, back.playhead); - assert!(v5.starts_with(b"edith 16\n")); + assert!(v5.starts_with(b"edith 17\n")); let again = parse(&v5, &dir).expect("v5 parses"); assert_eq!(again.lanes, back.lanes); // A dialect may not be mixed: lane lines under v1, `clip` under v2. @@ -2681,13 +2793,14 @@ mod tests { true, crate::export::EncoderSeat::default(), Limiter::default(), + None, 0, ) .expect("save"); let bytes = std::fs::read(&path).expect("read back"); assert_eq!( String::from_utf8_lossy(&bytes), - "edith 16\nplayhead 0\nresolution 1280 720\nsource 1 a.mp4\n\ + "edith 17\nplayhead 0\nresolution 1280 720\nsource 1 a.mp4\n\ video 1 0 0 30 0 - - - fit 1000\naudio 1 0 0 30 0 - - - fit 1000\n" ); // Loading rejoins the *given* directory, so the file is reached by the @@ -2732,10 +2845,10 @@ mod tests { #[test] fn a_wrong_first_line_is_refused_by_name() { let dir = PathBuf::from("/proj"); - let err = parse(b"edith 17\nsource 0 a.mp4\nvideo 0 0 5 0 -\n", &dir) + let err = parse(b"edith 18\nsource 0 a.mp4\nvideo 0 0 5 0 -\n", &dir) .unwrap_err() .to_string(); - assert_eq!(err, "line 1: unsupported version 17"); + assert_eq!(err, "line 1: unsupported version 18"); for junk in [&b""[..], b"{}\n", b"source a.mp4\n"] { assert_eq!( parse(junk, &dir).unwrap_err().to_string(), diff --git a/crates/engine/src/export.rs b/crates/engine/src/export.rs index d6726ef..4410c50 100644 --- a/crates/engine/src/export.rs +++ b/crates/engine/src/export.rs @@ -443,6 +443,7 @@ pub fn start( meta: VideoMeta, out: &Path, settings: &ExportSettings, + sample_rate: Option, ) -> ExportHandle { let settings = settings.clone(); let shared = Arc::new(Shared { @@ -498,7 +499,7 @@ pub fn start( ) .into()), format if format.has_video() => { - let first = run(&project, &meta, &part, &worker, &settings); + let first = run(&project, &meta, &part, &worker, &settings, sample_rate); match first { // The hardware encoder's *process* died under the driver // ([`crate::hwproc`]) -- the editor is still here, and the @@ -525,12 +526,12 @@ pub fn start( // run backwards. let mut software = settings.clone(); software.seat = EncoderSeat::Software; - run(&project, &meta, &part, &worker, &software) + run(&project, &meta, &part, &worker, &software, sample_rate) } other => other, } } - _ => run_audio(&project, &meta, &part, &worker, &settings), + _ => run_audio(&project, &meta, &part, &worker, &settings, sample_rate), }; let result = written.and_then(|()| std::fs::rename(&part, &out).map_err(Into::into)); if result.is_err() { @@ -1309,6 +1310,7 @@ fn copy_audio( kbps: u32, mkv: bool, shared: &Shared, + sample_rate: Option, ) -> crate::Result> { // The segments name their source, and the copy carries its packet-rounding // debt across a source join exactly as across a cut, so a timeline spanning @@ -1350,7 +1352,7 @@ fn copy_audio( } } let [segments] = &lanes[..] else { - return encode_audio(project, meta, kbps, mkv, shared); + return encode_audio(project, meta, kbps, mkv, shared, sample_rate); }; // ...and the same list names *which* lane those clips sit on, which is the // only way to ask what has been done to them. @@ -1388,7 +1390,7 @@ fn copy_audio( // such a lane would write it at unity and unlimited, silently. let speeded = project.lane(lane).iter().any(|c| !c.speed.is_normal()); if speeded || equalized(project) || mastered(project) { - return encode_audio(project, meta, kbps, mkv, shared); + return encode_audio(project, meta, kbps, mkv, shared, sample_rate); } // What is left is a copy the *sources* may still not be able to give: AAC // inside a Matroska file (readable, but not out of a sample table this walks @@ -1408,7 +1410,7 @@ fn copy_audio( // (they are not in a sample table) and every one of them re-encodes. opus_pre_skip: None, })), - Err(_) => encode_audio(project, meta, kbps, mkv, shared), + Err(_) => encode_audio(project, meta, kbps, mkv, shared, sample_rate), } } @@ -1663,6 +1665,7 @@ fn encode_audio( kbps: u32, mkv: bool, shared: &Shared, + sample_rate: Option, ) -> crate::Result> { let sources = project.audio_sources(); let segs = project.audio_segments_from(0, meta.frame_rate); @@ -1673,13 +1676,14 @@ fn encode_audio( // is written, and which minutes went where is the first question asked of a // slow export. Three lines an export, so they stay on. let mixed = std::time::Instant::now(); - let Some((audio, chunks)) = AudioSession::open_mixed_streams_master( + let Some((audio, chunks)) = AudioSession::open_mixed_streams_master_at( &sources, &segs, &eqs, &speeds, &project.audio_gains(), project.limiter(), + sample_rate, )? else { return Ok(None); // no audio to write, exactly as a copy of nothing is @@ -2138,6 +2142,7 @@ fn run( out: &Path, shared: &Arc, settings: &ExportSettings, + sample_rate: Option, ) -> crate::Result<()> { // The media's length and not the timeline's: a caption placed past the last // picture holds the *timeline* open ([`Project::timeline_frames`]) and there @@ -2205,6 +2210,7 @@ fn run( audio_kbps_of(&settings), settings.format.is_mkv(), &shared, + sample_rate, ) })? }; @@ -2743,6 +2749,7 @@ fn run_audio( out: &Path, shared: &Shared, settings: &ExportSettings, + sample_rate: Option, ) -> crate::Result<()> { let format = settings.format; let sources = project.audio_sources(); @@ -2762,13 +2769,14 @@ fn run_audio( // limiter out of it. The same opener playback's feeder reads from, so a // file is written at the levels it was heard at -- there is no second // place here that could mix it differently. - let Some((audio, chunks)) = AudioSession::open_mixed_streams_master( + let Some((audio, chunks)) = AudioSession::open_mixed_streams_master_at( &sources, &segs, &eqs, &speeds, &project.audio_gains(), project.limiter(), + sample_rate, )? else { return Err("this timeline has no audio to export".into()); diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index c91efbd..fe593ba 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -407,6 +407,16 @@ pub struct PlaybackSession { /// two switches above, and read by the front end: the engine starts no /// export of its own. encoder_seat: crate::export::EncoderSeat, + /// The rate this project's mix was picked to run at + /// ([`crate::edith::Document::sample_rate`]), overriding the one the first + /// audio source would otherwise hand the mixer. `None` -- every project + /// before the field existed, and any project nobody has picked one for -- + /// leaves the derived rate alone. Saved with the project like the switches + /// above it, and read by the front end: the engine picks no rate of its own. + /// + /// Setting it takes effect at the next audio rebuild (a seek, an edit, a + /// reopen); there is no live resample of an audio device already playing. + sample_rate: Option, } /// What an edit dirtied -- which half of the pipeline has to be rebuilt for the @@ -545,6 +555,7 @@ impl PlaybackSession { proxies: false, auto_proxies: true, encoder_seat: crate::export::EncoderSeat::default(), + sample_rate: None, }) } @@ -663,6 +674,7 @@ impl PlaybackSession { proxies: false, auto_proxies: true, encoder_seat: crate::export::EncoderSeat::default(), + sample_rate: None, }) } @@ -745,6 +757,7 @@ impl PlaybackSession { proxies: false, auto_proxies: true, encoder_seat: crate::export::EncoderSeat::default(), + sample_rate: None, }) } @@ -1053,6 +1066,7 @@ impl PlaybackSession { // ...and which encoder it exports with, which is the seat this // machine has for every dialect before v14. encoder_seat: doc.encoder, + sample_rate: doc.sample_rate, }; // The scaffolding above opened source 0 from its first frame and the // whole of its audio; this puts both onto the clip the playhead is @@ -1324,6 +1338,10 @@ impl PlaybackSession { // again after a reload. self.encoder_seat, self.project.limiter(), + // ...and the rate the mix was picked to run at, for the same + // reason: a project delivered at a chosen rate is delivered at it + // again after a reload. + self.sample_rate, playhead, ) } @@ -2293,6 +2311,20 @@ impl PlaybackSession { self.encoder_seat = seat; } + /// The rate this project's mix was picked to run at + /// ([`Self::set_sample_rate`]), or `None` for the rate the first audio + /// source derives it to. + pub fn sample_rate(&self) -> Option { + self.sample_rate + } + + /// Pick the mix's own rate, overriding the derived one. Nothing to reseek + /// here: it takes effect at the next audio rebuild (a seek, an edit, a + /// reopen), not on the device already playing. + pub fn set_sample_rate(&mut self, rate: Option) { + self.sample_rate = rate; + } + /// Whether an imported film that wants a stand-in gets one made for it /// there and then ([`Self::set_auto_proxies`]). pub fn auto_proxies(&self) -> bool { @@ -3236,6 +3268,10 @@ impl PlaybackSession { // more -- the open runs on the feeder. let mixed = crate::audio::AudioSession::is_mixed(segs.len(), &gains, limiter); let worker_controls = Arc::clone(&controls); + // ...and the rate the mix runs at, if the project picked one: the + // same override [`crate::edith::save`] writes and [`Self::open_project`] + // reads back. + let sample_rate = self.sample_rate; // ...and the open itself -- every track a segment names, its packet // table, its priming -- happens there too. That is the whole point: // one `pread` on a cold 25 GB film is seconds, and this is called @@ -3249,6 +3285,7 @@ impl PlaybackSession { &gains, limiter, Some(&worker_controls), + sample_rate, ) { Ok(Some((_, rx))) => Some(rx), // A silent timeline, and a source that will not open: both @@ -3327,7 +3364,13 @@ impl PlaybackSession { out: &Path, settings: &crate::export::ExportSettings, ) -> crate::ExportHandle { - crate::export::start(self.project.export_snapshot(), self.meta, out, settings) + crate::export::start( + self.project.export_snapshot(), + self.meta, + out, + settings, + self.sample_rate, + ) } /// Moves the clock forward; the caller runs this once per rendered frame. @@ -3775,6 +3818,32 @@ mod tests { .join(name) } + /// A picked project rate overrides the derived one, and its absence leaves + /// the source's own -- the seam every explicit-sample-rate caller + /// (playback rebuild, both export encoders) goes through + /// ([`crate::AudioSession::open_multi_streams_speed_at`]). The per-segment + /// conform chain resamples to whatever `meta` names, so the meta *is* the + /// behaviour. + #[test] + fn a_picked_sample_rate_overrides_the_derived_one() { + let file = asset("test_av.mp4"); + let sources = [(file, 0)]; + let segs = [(Some(0), 0., 1.)]; + let derived = crate::AudioSession::open_multi_streams_speed_at(&sources, &segs, &[], &[], None) + .expect("open the fixture") + .expect("the fixture has sound") + .0 + .sample_rate; + let picked = + crate::AudioSession::open_multi_streams_speed_at(&sources, &segs, &[], &[], Some(32000)) + .expect("open the fixture") + .expect("the fixture has sound") + .0 + .sample_rate; + assert_ne!(derived, 32000, "a fixture at the picked rate proves nothing"); + assert_eq!(picked, 32000); + } + /// Which source the timeline's sound is opened on -- the one rule /// [`open_project`](PlaybackSession::open_project), the probe every import /// is held to and the audio worker all read through. A still at index 0 is diff --git a/crates/engine/src/proxy.rs b/crates/engine/src/proxy.rs index cd23432..645cc4b 100644 --- a/crates/engine/src/proxy.rs +++ b/crates/engine/src/proxy.rs @@ -524,6 +524,9 @@ fn started(source: &Path, only_if: fn(&VideoMeta) -> bool) -> crate::Result