forge
Explore
Log in

Don't you dare go Hollow

The project's sample rate is its own to name (.edith v17)

Browse files at this commit

3a2e5ead8a106ae97497029f7d43ca6868daba8fAhmet Kaan GümüşParent: 490cfd331981de
  • feat
    • the project's sample rate is its own to name (.edith v17)
Show full message
✨ feat(engine): the project's sample rate is its own to name (.edith v17)
crates/engine/src/audio.rsmodified+39 -4
@@ -376,6 +376,22 @@ impl AudioSession {
376376 segs: &[(Option<usize>, f64, f64)],
377377 eqs: &[Option<EqParams>],
378378 speeds: &[Option<crate::project::Stretch>],
379+ ) -> crate::Result<Option<(AudioMeta, Receiver<AudioChunk>)>> {
380+ Self::open_multi_streams_speed_at(sources, segs, eqs, speeds, None)
381+ }
382+
383+ /// [`open_multi_streams_speed`] with the project's own rate, when one was
384+ /// picked ([`crate::edith::Document::sample_rate`]), overriding the source's
385+ /// derived one. The per-segment [`Rate`] chain below already conforms any
386+ /// segment whose own rate differs from `meta.sample_rate`, so naming a rate
387+ /// here costs nothing new -- it only changes which rate that chain conforms
388+ /// *to*.
389+ pub fn open_multi_streams_speed_at(
390+ sources: &[(PathBuf, usize)],
391+ segs: &[(Option<usize>, f64, f64)],
392+ eqs: &[Option<EqParams>],
393+ speeds: &[Option<crate::project::Stretch>],
394+ sample_rate: Option<u32>,
379395 ) -> crate::Result<Option<(AudioMeta, Receiver<AudioChunk>)>> {
380396 // The first source that could have a track, which is not always index 0:
381397 // a still image has none, and one at the front of the list (a save
@@ -395,7 +411,7 @@ impl AudioSession {
395411 // The timeline's meta is that source's: policy makes every other source
396412 // match it, and the checks below hold them to that.
397413 let meta = AudioMeta {
398- sample_rate: first.sample_rate(),
414+ sample_rate: sample_rate.unwrap_or_else(|| first.sample_rate()),
399415 channels: first.channels(),
400416 total_samples: first.total_samples(),
401417 };
@@ -658,7 +674,23 @@ impl AudioSession {
658674 gains: &[f32],
659675 limiter: Limiter,
660676 ) -> crate::Result<Option<(AudioMeta, Receiver<AudioChunk>)>> {
661- Self::open_mixed_streams_live(sources, lanes, eqs, speeds, gains, limiter, None)
677+ Self::open_mixed_streams_master_at(sources, lanes, eqs, speeds, gains, limiter, None)
678+ }
679+
680+ /// [`open_mixed_streams_master`] with the project's own rate
681+ /// ([`crate::edith::Document::sample_rate`]) overriding the derived one, the
682+ /// same rate every lane's meta agrees on ([`open_mixed_streams_live`]'s
683+ /// invariant).
684+ pub fn open_mixed_streams_master_at(
685+ sources: &[(PathBuf, usize)],
686+ lanes: &[Vec<(Option<usize>, f64, f64)>],
687+ eqs: &[Vec<Option<EqParams>>],
688+ speeds: &[Vec<Option<crate::project::Stretch>>],
689+ gains: &[f32],
690+ limiter: Limiter,
691+ sample_rate: Option<u32>,
692+ ) -> crate::Result<Option<(AudioMeta, Receiver<AudioChunk>)>> {
693+ Self::open_mixed_streams_live(sources, lanes, eqs, speeds, gains, limiter, None, sample_rate)
662694 }
663695
664696 /// [`open_mixed_streams_master`](Self::open_mixed_streams_master) with the
@@ -677,6 +709,7 @@ impl AudioSession {
677709 gains: &[f32],
678710 limiter: Limiter,
679711 live: Option<&Arc<MixControls>>,
712+ sample_rate: Option<u32>,
680713 ) -> crate::Result<Option<(AudioMeta, Receiver<AudioChunk>)>> {
681714 let [first, ..] = lanes else {
682715 return Ok(None);
@@ -684,11 +717,12 @@ impl AudioSession {
684717 if !Self::is_mixed(lanes.len(), gains, limiter) {
685718 let flat = Vec::new();
686719 let plain = Vec::new();
687- return Self::open_multi_streams_speed(
720+ return Self::open_multi_streams_speed_at(
688721 sources,
689722 first,
690723 eqs.first().unwrap_or(&flat),
691724 speeds.first().unwrap_or(&plain),
725+ sample_rate,
692726 );
693727 }
694728 let mut meta = None;
@@ -699,11 +733,12 @@ impl AudioSession {
699733 // timeline.
700734 let flat = Vec::new();
701735 let plain = Vec::new();
702- let Some((lane_meta, rx)) = Self::open_multi_streams_speed(
736+ let Some((lane_meta, rx)) = Self::open_multi_streams_speed_at(
703737 sources,
704738 segs,
705739 eqs.get(i).unwrap_or(&flat),
706740 speeds.get(i).unwrap_or(&plain),
741+ sample_rate,
707742 )?
708743 else {
709744 return Ok(None);
crates/engine/src/edith.rsmodified+133 -20
@@ -216,7 +216,8 @@ use crate::subtitle::SubtitleTrack;
216216
217217 /// What [`save`] writes. Read support goes back to `edith 1`; see the module
218218 /// docs for what those dialects looked like.
219-const MAGIC: &[u8] = b"edith 16";
219+const MAGIC: &[u8] = b"edith 17";
220+const MAGIC_V16: &[u8] = b"edith 16";
220221 const MAGIC_V15: &[u8] = b"edith 15";
221222 const MAGIC_V14: &[u8] = b"edith 14";
222223 const MAGIC_V13: &[u8] = b"edith 13";
@@ -296,6 +297,15 @@ pub struct Document {
296297 /// for a v14 file that leaves the line out, which both mean the seat this
297298 /// machine has -- the only choice those projects offered.
298299 pub encoder: crate::export::EncoderSeat,
300+ /// The rate the project's *sound* is mixed and exported at, chosen rather
301+ /// than probed. `None` for every dialect before v17 and for a v17 file that
302+ /// leaves the line out, which both mean what they always did: the rate of
303+ /// the first source that has any ([`crate::PlaybackSession::open_project`]'s
304+ /// `first_audio_of`). Playback and export both resample every other source
305+ /// to whichever rate this resolves to, exactly as they already resample a
306+ /// source shot at another rate than the timeline's -- an explicit choice
307+ /// here only changes what that timeline's own rate *is*.
308+ pub sample_rate: Option<u32>,
299309 pub playhead: u32,
300310 }
301311
@@ -321,6 +331,7 @@ pub fn save(
321331 auto_proxy: bool,
322332 encoder: crate::export::EncoderSeat,
323333 limiter: Limiter,
334+ sample_rate: Option<u32>,
324335 playhead: u32,
325336 ) -> crate::Result<()> {
326337 let dir = project_dir(path);
@@ -335,7 +346,7 @@ pub fn save(
335346 .and_then(|mut f| {
336347 f.write_all(&emit(
337348 &dir, sources, lanes, gains, subs, subtitles, eq, color, resolution, fps, tone,
338- proxy, auto_proxy, encoder, limiter, playhead,
349+ proxy, auto_proxy, encoder, limiter, sample_rate, playhead,
339350 ))?;
340351 f.sync_all()
341352 })
@@ -383,6 +394,7 @@ fn emit(
383394 auto_proxy: bool,
384395 encoder: crate::export::EncoderSeat,
385396 limiter: Limiter,
397+ sample_rate: Option<u32>,
386398 playhead: u32,
387399 ) -> Vec<u8> {
388400 let mut out = Vec::new();
@@ -395,6 +407,12 @@ fn emit(
395407 if let Some(fps) = fps.filter(|f| f.is_finite() && *f > 0.0) {
396408 out.extend_from_slice(format!("fps {fps:?}\n").as_bytes());
397409 }
410+ // Beside it, and by the same rule: a project nobody chose a sound rate for
411+ // is the mix the source's own probe always gave it, which is what leaving
412+ // this line out still means.
413+ if let Some(rate) = sample_rate.filter(|&r| (1_000..=384_000).contains(&r)) {
414+ out.extend_from_slice(format!("samplerate {rate}\n").as_bytes());
415+ }
398416 // Beside the rate, and written only when it is not the default: a project
399417 // holding no HDR media has nothing to say here, and saying nothing is what
400418 // a v10 file already said.
@@ -580,8 +598,10 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result<Document> {
580598 // The dialects that wrote a source line without its stream field. Reading
581599 // one is the whole of what "an old project still opens" means here.
582600 let streamless = v1 || first == MAGIC_V2;
583- // The one whose captions may carry a group id...
584- let v16 = first == MAGIC;
601+ // The one that carries an explicit sound rate...
602+ let v17 = first == MAGIC;
603+ // ...the one whose captions may carry a group id...
604+ let v16 = v17 || first == MAGIC_V16;
585605 // ...the one that places the words it names...
586606 let v15 = v16 || first == MAGIC_V15;
587607 // ...the ones that say which encoder an export takes...
@@ -653,6 +673,10 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result<Document> {
653673 // still means.
654674 encoder: crate::export::EncoderSeat::default(),
655675 fps: None,
676+ // Nothing before v17 chose a sound rate: the source's own probe is
677+ // still what the mix is cut at, which is what `None` means everywhere
678+ // else it reaches (`PlaybackSession::open_project`).
679+ sample_rate: None,
656680 playhead: 0,
657681 };
658682 let mut playhead_seen = false;
@@ -708,6 +732,20 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result<Document> {
708732 })?;
709733 doc.fps = Some(fps);
710734 }
735+ b"samplerate" if v17 => {
736+ if doc.sample_rate.is_some() || !doc.sources.is_empty() {
737+ return Err(format!(
738+ "line {n}: samplerate belongs once, before the sources"
739+ )
740+ .into());
741+ }
742+ let f = fields(rest, 1, "samplerate", n)?;
743+ let rate = number(f[0], n)?;
744+ if !(1_000..=384_000).contains(&rate) {
745+ return Err(format!("line {n}: {rate} Hz is not a sound rate").into());
746+ }
747+ doc.sample_rate = Some(rate);
748+ }
711749 b"tonemap" if v11 => {
712750 if !doc.sources.is_empty() {
713751 return Err(
@@ -1395,6 +1433,7 @@ mod tests {
13951433 true,
13961434 crate::export::EncoderSeat::default(),
13971435 Limiter::default(),
1436+ None,
13981437 playhead,
13991438 )
14001439 }
@@ -1442,6 +1481,7 @@ mod tests {
14421481 true,
14431482 crate::export::EncoderSeat::default(),
14441483 Limiter::default(),
1484+ None,
14451485 0,
14461486 );
14471487 let text = String::from_utf8_lossy(&bytes);
@@ -1469,7 +1509,7 @@ mod tests {
14691509 video 1 0 0 30 0 - - - fit 1000\n";
14701510 let old = parse(v9, &dir).expect("v9 still loads");
14711511 assert_eq!(old.subtitles, Vec::new());
1472- assert!(flat(&dir, &old.sources, &old.lanes, old.playhead).starts_with(b"edith 16\n"));
1512+ assert!(flat(&dir, &old.sources, &old.lanes, old.playhead).starts_with(b"edith 17\n"));
14731513 // ...and the line itself is not a v9 line: a dialect may not be mixed.
14741514 let mixed = parse(b"edith 9\nsource 0 a.mp4\nsubtitle - subs.srt\n", &dir)
14751515 .unwrap_err()
@@ -1522,6 +1562,7 @@ mod tests {
15221562 true,
15231563 crate::export::EncoderSeat::default(),
15241564 Limiter::default(),
1565+ None,
15251566 0,
15261567 );
15271568 let text = String::from_utf8_lossy(&bytes);
@@ -1573,6 +1614,7 @@ mod tests {
15731614 true,
15741615 crate::export::EncoderSeat::default(),
15751616 Limiter::default(),
1617+ None,
15761618 0,
15771619 )
15781620 };
@@ -1636,6 +1678,7 @@ mod tests {
16361678 auto,
16371679 crate::export::EncoderSeat::default(),
16381680 Limiter::default(),
1681+ None,
16391682 0,
16401683 )
16411684 };
@@ -1701,6 +1744,7 @@ mod tests {
17011744 true,
17021745 seat,
17031746 Limiter::default(),
1747+ None,
17041748 0,
17051749 )
17061750 };
@@ -1749,6 +1793,72 @@ mod tests {
17491793 }
17501794 }
17511795
1796+ /// The v17 line: an explicit sound rate, written only when picked and read
1797+ /// back as the very number -- and a v16 file, which had no way to say one
1798+ /// and meant the source's own probe, still opens with `None`.
1799+ #[test]
1800+ fn the_sample_rate_round_trips_and_a_v16_file_leaves_it_derived() {
1801+ let dir = PathBuf::from("/proj");
1802+ let (_, sources, lanes) = doc();
1803+ let bytes = |rate| {
1804+ super::emit(
1805+ &dir,
1806+ &sources,
1807+ &lanes,
1808+ &[],
1809+ &[],
1810+ &[],
1811+ &[],
1812+ &[],
1813+ (1280, 720),
1814+ None,
1815+ crate::tonemap::Preset::default(),
1816+ false,
1817+ true,
1818+ crate::export::EncoderSeat::default(),
1819+ Limiter::default(),
1820+ rate,
1821+ 0,
1822+ )
1823+ };
1824+ // Unset is the line left out, so a project nobody has chosen a rate for
1825+ // is the bytes a v16 file was bar the version line.
1826+ let unset = bytes(None);
1827+ assert!(
1828+ !String::from_utf8_lossy(&unset).contains("samplerate"),
1829+ "no pick is the line left out: {}",
1830+ String::from_utf8_lossy(&unset)
1831+ );
1832+ assert_eq!(parse(&unset, &dir).expect("v17 parses").sample_rate, None);
1833+ for rate in [44_100, 48_000, 96_000] {
1834+ let written = bytes(Some(rate));
1835+ let text = String::from_utf8_lossy(&written).to_string();
1836+ assert!(text.contains(&format!("samplerate {rate}\n")), "{text}");
1837+ assert_eq!(
1838+ parse(&written, &dir).expect("v17 parses").sample_rate,
1839+ Some(rate)
1840+ );
1841+ }
1842+ // A v16 project had no rate of its own to say: the field comes back
1843+ // `None`, which is what "derive it from the source" already meant.
1844+ let v16 = b"edith 16\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\
1845+ video 1 0 0 30 0 - - - fit 1000\n";
1846+ assert_eq!(parse(v16, &dir).expect("v16 still loads").sample_rate, None);
1847+ // ...and the line is not a v16 line: a dialect may not be mixed.
1848+ let mixed = parse(b"edith 16\nsource 0 a.mp4\nsamplerate 48000\n", &dir)
1849+ .unwrap_err()
1850+ .to_string();
1851+ assert_eq!(mixed, "line 3: unknown keyword \"samplerate\"");
1852+ // A number out of a sound rate's range is a corrupt line, by name.
1853+ for line in ["samplerate 0\n", "samplerate 500000\n", "samplerate nope\n"] {
1854+ let file = format!("edith 17\nsource 0 a.mp4\n{line}");
1855+ assert!(
1856+ parse(file.as_bytes(), &dir).is_err(),
1857+ "{line:?} is not a sound rate line"
1858+ );
1859+ }
1860+ }
1861+
17521862 /// The v11 line: the HDR rendition, written by name, read back as the very
17531863 /// preset -- and the dialect before it, which had no way to say one and
17541864 /// meant the published conversion.
@@ -1773,6 +1883,7 @@ mod tests {
17731883 true,
17741884 crate::export::EncoderSeat::default(),
17751885 Limiter::default(),
1886+ None,
17761887 0,
17771888 );
17781889 let text = String::from_utf8_lossy(&bytes).to_string();
@@ -1866,7 +1977,7 @@ mod tests {
18661977 let bytes = flat(&dir, &sources, &lanes, 12);
18671978 assert_eq!(
18681979 String::from_utf8_lossy(&bytes),
1869- "edith 16\nplayhead 12\nresolution 1280 720\nsource 0 a.mp4\n\
1980+ "edith 17\nplayhead 12\nresolution 1280 720\nsource 0 a.mp4\n\
18701981 source 2 /elsewhere/b.mp4\n\
18711982 video 1 0 0 30 0 0 - - fit 1000\nvideo 1 30 10 20 1 1 - - fit 1000\n\
18721983 audio 1 0 0 30 0 0 - - fit 1000\n",
@@ -1903,7 +2014,7 @@ mod tests {
19032014 let bytes = flat(&dir, &sources, &lanes, 7);
19042015 assert_eq!(
19052016 String::from_utf8_lossy(&bytes),
1906- "edith 16\nplayhead 7\nresolution 1280 720\nsource 0 a.mp4\n\
2017+ "edith 17\nplayhead 7\nresolution 1280 720\nsource 0 a.mp4\n\
19072018 video 1 0 0 30 0 4 - - fit 1000\naudio 1\n\
19082019 video 2 40 0 10 0 - - - fit 1000\naudio 2 0 0 30 0 4 - - fit 1000\n",
19092020 "an empty lane is a line of its own; everything else is its clips"
@@ -2011,7 +2122,7 @@ mod tests {
20112122 let bytes = emit(&dir, &sources, &lanes, &eq, &[], (1280, 720), 0);
20122123 assert_eq!(
20132124 String::from_utf8_lossy(&bytes),
2014- "edith 16\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\
2125+ "edith 17\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\
20152126 eq 80.0:-3.0:0.707:ls 1000.0:4.5:1.0:pk\n\
20162127 eq 16777215.0:-0.1:3.918315e-39:hs\n\
20172128 eq\n\
@@ -2115,7 +2226,7 @@ mod tests {
21152226 let bytes = emit(&dir, &sources, &lanes, &[], &color, (1280, 720), 0);
21162227 assert_eq!(
21172228 String::from_utf8_lossy(&bytes),
2118- "edith 16\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\
2229+ "edith 17\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\
21192230 color 0.1:1.2:0.9:-0.3\n\
21202231 color -1e-7:16777215.0:3.918315e-39:-0.0\n\
21212232 color 0.0:1.0:1.0:0.0\n\
@@ -2180,7 +2291,7 @@ mod tests {
21802291 (1280, 720),
21812292 old.playhead
21822293 )),
2183- "edith 16\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\
2294+ "edith 17\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\
21842295 eq 80.0:-3.0:0.707:ls\n\
21852296 video 1 0 0 30 0 0 0 - fit 1000\naudio 1 0 0 30 0 0 - - fit 1000\n"
21862297 );
@@ -2218,7 +2329,7 @@ mod tests {
22182329 let bytes = emit(&dir, &sources, &lanes, &[], &[], (1280, 720), 0);
22192330 assert_eq!(
22202331 String::from_utf8_lossy(&bytes),
2221- "edith 16\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\
2332+ "edith 17\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\
22222333 video 1 0 0 30 0 - - - fit 2000\nvideo 1 15 30 40 0 - - - fit 250\n\
22232334 audio 1 0 0 30 0 - - - fit 2000\n",
22242335 "the rate is the clip line's last field, in thousandths"
@@ -2247,7 +2358,7 @@ mod tests {
22472358 (1280, 720),
22482359 old.playhead
22492360 )),
2250- "edith 16\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\
2361+ "edith 17\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\
22512362 video 1 0 0 30 0 0 - - fit 1000\naudio 1 0 0 30 0 0 - - fit 1000\n"
22522363 );
22532364 // A rate outside what the editor can set is a corrupt line, by name.
@@ -2293,11 +2404,12 @@ mod tests {
22932404 true,
22942405 crate::export::EncoderSeat::default(),
22952406 limiter,
2407+ None,
22962408 0,
22972409 );
22982410 assert_eq!(
22992411 String::from_utf8_lossy(&bytes),
2300- "edith 16\nplayhead 0\nresolution 1280 720\nfps 23.976023976023978\n\
2412+ "edith 17\nplayhead 0\nresolution 1280 720\nfps 23.976023976023978\n\
23012413 limiter -1.5 on\nsource 0 a.mp4\n\
23022414 video 1 0 0 30 0 - - - fit 1000\naudio 1 0 0 30 0 - - - fit 1000\n\
23032415 audio 2 0 0 30 0 - - - fit 1000\n\
@@ -2329,7 +2441,7 @@ mod tests {
23292441 (1280, 720),
23302442 old.playhead
23312443 )),
2332- "edith 16\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\
2444+ "edith 17\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\
23332445 video 1 0 0 30 0 - - - fit 1000\naudio 1 0 0 30 0 - - - fit 1000\n"
23342446 );
23352447
@@ -2493,7 +2605,7 @@ mod tests {
24932605 assert!(old.eq.is_empty(), "nothing before v5 equalizes anything");
24942606 assert_eq!(
24952607 String::from_utf8_lossy(&flat(&dir, &old.sources, &old.lanes, old.playhead)),
2496- "edith 16\nplayhead 12\nresolution 1280 720\nsource 0 a.mp4\n\
2608+ "edith 17\nplayhead 12\nresolution 1280 720\nsource 0 a.mp4\n\
24972609 source 2 /elsewhere/b.mp4\n\
24982610 video 1 0 0 30 0 0 - - fit 1000\nvideo 1 30 10 20 1 1 - - fit 1000\n\
24992611 audio 1 0 0 30 0 0 - - fit 1000\n"
@@ -2546,7 +2658,7 @@ mod tests {
25462658 let v5 = flat(&dir, &back.sources, &back.lanes, back.playhead);
25472659 assert_eq!(
25482660 String::from_utf8_lossy(&v5),
2549- "edith 16\nplayhead 12\nresolution 1280 720\nsource 0 a.mp4\n\
2661+ "edith 17\nplayhead 12\nresolution 1280 720\nsource 0 a.mp4\n\
25502662 source 0 /elsewhere/b.mp4\n\
25512663 video 1 0 0 30 0 0 - - fit 1000\nvideo 1 30 10 20 1 1 - - fit 1000\n\
25522664 audio 1 0 0 30 0 0 - - fit 1000\n",
@@ -2578,7 +2690,7 @@ mod tests {
25782690 // Saved again it is the current version, which round-trips to the
25792691 // same document.
25802692 let v5 = flat(&dir, &back.sources, &back.lanes, back.playhead);
2581- assert!(v5.starts_with(b"edith 16\n"));
2693+ assert!(v5.starts_with(b"edith 17\n"));
25822694 let again = parse(&v5, &dir).expect("v5 parses");
25832695 assert_eq!(again.lanes, back.lanes);
25842696 // A dialect may not be mixed: lane lines under v1, `clip` under v2.
@@ -2681,13 +2793,14 @@ mod tests {
26812793 true,
26822794 crate::export::EncoderSeat::default(),
26832795 Limiter::default(),
2796+ None,
26842797 0,
26852798 )
26862799 .expect("save");
26872800 let bytes = std::fs::read(&path).expect("read back");
26882801 assert_eq!(
26892802 String::from_utf8_lossy(&bytes),
2690- "edith 16\nplayhead 0\nresolution 1280 720\nsource 1 a.mp4\n\
2803+ "edith 17\nplayhead 0\nresolution 1280 720\nsource 1 a.mp4\n\
26912804 video 1 0 0 30 0 - - - fit 1000\naudio 1 0 0 30 0 - - - fit 1000\n"
26922805 );
26932806 // Loading rejoins the *given* directory, so the file is reached by the
@@ -2732,10 +2845,10 @@ mod tests {
27322845 #[test]
27332846 fn a_wrong_first_line_is_refused_by_name() {
27342847 let dir = PathBuf::from("/proj");
2735- let err = parse(b"edith 17\nsource 0 a.mp4\nvideo 0 0 5 0 -\n", &dir)
2848+ let err = parse(b"edith 18\nsource 0 a.mp4\nvideo 0 0 5 0 -\n", &dir)
27362849 .unwrap_err()
27372850 .to_string();
2738- assert_eq!(err, "line 1: unsupported version 17");
2851+ assert_eq!(err, "line 1: unsupported version 18");
27392852 for junk in [&b""[..], b"{}\n", b"source a.mp4\n"] {
27402853 assert_eq!(
27412854 parse(junk, &dir).unwrap_err().to_string(),
crates/engine/src/export.rsmodified+16 -8
@@ -443,6 +443,7 @@ pub fn start(
443443 meta: VideoMeta,
444444 out: &Path,
445445 settings: &ExportSettings,
446+ sample_rate: Option<u32>,
446447 ) -> ExportHandle {
447448 let settings = settings.clone();
448449 let shared = Arc::new(Shared {
@@ -498,7 +499,7 @@ pub fn start(
498499 )
499500 .into()),
500501 format if format.has_video() => {
501- let first = run(&project, &meta, &part, &worker, &settings);
502+ let first = run(&project, &meta, &part, &worker, &settings, sample_rate);
502503 match first {
503504 // The hardware encoder's *process* died under the driver
504505 // ([`crate::hwproc`]) -- the editor is still here, and the
@@ -525,12 +526,12 @@ pub fn start(
525526 // run backwards.
526527 let mut software = settings.clone();
527528 software.seat = EncoderSeat::Software;
528- run(&project, &meta, &part, &worker, &software)
529+ run(&project, &meta, &part, &worker, &software, sample_rate)
529530 }
530531 other => other,
531532 }
532533 }
533- _ => run_audio(&project, &meta, &part, &worker, &settings),
534+ _ => run_audio(&project, &meta, &part, &worker, &settings, sample_rate),
534535 };
535536 let result = written.and_then(|()| std::fs::rename(&part, &out).map_err(Into::into));
536537 if result.is_err() {
@@ -1309,6 +1310,7 @@ fn copy_audio(
13091310 kbps: u32,
13101311 mkv: bool,
13111312 shared: &Shared,
1313+ sample_rate: Option<u32>,
13121314 ) -> crate::Result<Option<ExportAudio>> {
13131315 // The segments name their source, and the copy carries its packet-rounding
13141316 // debt across a source join exactly as across a cut, so a timeline spanning
@@ -1350,7 +1352,7 @@ fn copy_audio(
13501352 }
13511353 }
13521354 let [segments] = &lanes[..] else {
1353- return encode_audio(project, meta, kbps, mkv, shared);
1355+ return encode_audio(project, meta, kbps, mkv, shared, sample_rate);
13541356 };
13551357 // ...and the same list names *which* lane those clips sit on, which is the
13561358 // only way to ask what has been done to them.
@@ -1388,7 +1390,7 @@ fn copy_audio(
13881390 // such a lane would write it at unity and unlimited, silently.
13891391 let speeded = project.lane(lane).iter().any(|c| !c.speed.is_normal());
13901392 if speeded || equalized(project) || mastered(project) {
1391- return encode_audio(project, meta, kbps, mkv, shared);
1393+ return encode_audio(project, meta, kbps, mkv, shared, sample_rate);
13921394 }
13931395 // What is left is a copy the *sources* may still not be able to give: AAC
13941396 // inside a Matroska file (readable, but not out of a sample table this walks
@@ -1408,7 +1410,7 @@ fn copy_audio(
14081410 // (they are not in a sample table) and every one of them re-encodes.
14091411 opus_pre_skip: None,
14101412 })),
1411- Err(_) => encode_audio(project, meta, kbps, mkv, shared),
1413+ Err(_) => encode_audio(project, meta, kbps, mkv, shared, sample_rate),
14121414 }
14131415 }
14141416
@@ -1663,6 +1665,7 @@ fn encode_audio(
16631665 kbps: u32,
16641666 mkv: bool,
16651667 shared: &Shared,
1668+ sample_rate: Option<u32>,
16661669 ) -> crate::Result<Option<ExportAudio>> {
16671670 let sources = project.audio_sources();
16681671 let segs = project.audio_segments_from(0, meta.frame_rate);
@@ -1673,13 +1676,14 @@ fn encode_audio(
16731676 // is written, and which minutes went where is the first question asked of a
16741677 // slow export. Three lines an export, so they stay on.
16751678 let mixed = std::time::Instant::now();
1676- let Some((audio, chunks)) = AudioSession::open_mixed_streams_master(
1679+ let Some((audio, chunks)) = AudioSession::open_mixed_streams_master_at(
16771680 &sources,
16781681 &segs,
16791682 &eqs,
16801683 &speeds,
16811684 &project.audio_gains(),
16821685 project.limiter(),
1686+ sample_rate,
16831687 )?
16841688 else {
16851689 return Ok(None); // no audio to write, exactly as a copy of nothing is
@@ -2138,6 +2142,7 @@ fn run(
21382142 out: &Path,
21392143 shared: &Arc<Shared>,
21402144 settings: &ExportSettings,
2145+ sample_rate: Option<u32>,
21412146 ) -> crate::Result<()> {
21422147 // The media's length and not the timeline's: a caption placed past the last
21432148 // picture holds the *timeline* open ([`Project::timeline_frames`]) and there
@@ -2205,6 +2210,7 @@ fn run(
22052210 audio_kbps_of(&settings),
22062211 settings.format.is_mkv(),
22072212 &shared,
2213+ sample_rate,
22082214 )
22092215 })?
22102216 };
@@ -2743,6 +2749,7 @@ fn run_audio(
27432749 out: &Path,
27442750 shared: &Shared,
27452751 settings: &ExportSettings,
2752+ sample_rate: Option<u32>,
27462753 ) -> crate::Result<()> {
27472754 let format = settings.format;
27482755 let sources = project.audio_sources();
@@ -2762,13 +2769,14 @@ fn run_audio(
27622769 // limiter out of it. The same opener playback's feeder reads from, so a
27632770 // file is written at the levels it was heard at -- there is no second
27642771 // place here that could mix it differently.
2765- let Some((audio, chunks)) = AudioSession::open_mixed_streams_master(
2772+ let Some((audio, chunks)) = AudioSession::open_mixed_streams_master_at(
27662773 &sources,
27672774 &segs,
27682775 &eqs,
27692776 &speeds,
27702777 &project.audio_gains(),
27712778 project.limiter(),
2779+ sample_rate,
27722780 )?
27732781 else {
27742782 return Err("this timeline has no audio to export".into());
crates/engine/src/playback.rsmodified+70 -1
@@ -407,6 +407,16 @@ pub struct PlaybackSession {
407407 /// two switches above, and read by the front end: the engine starts no
408408 /// export of its own.
409409 encoder_seat: crate::export::EncoderSeat,
410+ /// The rate this project's mix was picked to run at
411+ /// ([`crate::edith::Document::sample_rate`]), overriding the one the first
412+ /// audio source would otherwise hand the mixer. `None` -- every project
413+ /// before the field existed, and any project nobody has picked one for --
414+ /// leaves the derived rate alone. Saved with the project like the switches
415+ /// above it, and read by the front end: the engine picks no rate of its own.
416+ ///
417+ /// Setting it takes effect at the next audio rebuild (a seek, an edit, a
418+ /// reopen); there is no live resample of an audio device already playing.
419+ sample_rate: Option<u32>,
410420 }
411421
412422 /// What an edit dirtied -- which half of the pipeline has to be rebuilt for the
@@ -545,6 +555,7 @@ impl PlaybackSession {
545555 proxies: false,
546556 auto_proxies: true,
547557 encoder_seat: crate::export::EncoderSeat::default(),
558+ sample_rate: None,
548559 })
549560 }
550561
@@ -663,6 +674,7 @@ impl PlaybackSession {
663674 proxies: false,
664675 auto_proxies: true,
665676 encoder_seat: crate::export::EncoderSeat::default(),
677+ sample_rate: None,
666678 })
667679 }
668680
@@ -745,6 +757,7 @@ impl PlaybackSession {
745757 proxies: false,
746758 auto_proxies: true,
747759 encoder_seat: crate::export::EncoderSeat::default(),
760+ sample_rate: None,
748761 })
749762 }
750763
@@ -1053,6 +1066,7 @@ impl PlaybackSession {
10531066 // ...and which encoder it exports with, which is the seat this
10541067 // machine has for every dialect before v14.
10551068 encoder_seat: doc.encoder,
1069+ sample_rate: doc.sample_rate,
10561070 };
10571071 // The scaffolding above opened source 0 from its first frame and the
10581072 // whole of its audio; this puts both onto the clip the playhead is
@@ -1324,6 +1338,10 @@ impl PlaybackSession {
13241338 // again after a reload.
13251339 self.encoder_seat,
13261340 self.project.limiter(),
1341+ // ...and the rate the mix was picked to run at, for the same
1342+ // reason: a project delivered at a chosen rate is delivered at it
1343+ // again after a reload.
1344+ self.sample_rate,
13271345 playhead,
13281346 )
13291347 }
@@ -2293,6 +2311,20 @@ impl PlaybackSession {
22932311 self.encoder_seat = seat;
22942312 }
22952313
2314+ /// The rate this project's mix was picked to run at
2315+ /// ([`Self::set_sample_rate`]), or `None` for the rate the first audio
2316+ /// source derives it to.
2317+ pub fn sample_rate(&self) -> Option<u32> {
2318+ self.sample_rate
2319+ }
2320+
2321+ /// Pick the mix's own rate, overriding the derived one. Nothing to reseek
2322+ /// here: it takes effect at the next audio rebuild (a seek, an edit, a
2323+ /// reopen), not on the device already playing.
2324+ pub fn set_sample_rate(&mut self, rate: Option<u32>) {
2325+ self.sample_rate = rate;
2326+ }
2327+
22962328 /// Whether an imported film that wants a stand-in gets one made for it
22972329 /// there and then ([`Self::set_auto_proxies`]).
22982330 pub fn auto_proxies(&self) -> bool {
@@ -3236,6 +3268,10 @@ impl PlaybackSession {
32363268 // more -- the open runs on the feeder.
32373269 let mixed = crate::audio::AudioSession::is_mixed(segs.len(), &gains, limiter);
32383270 let worker_controls = Arc::clone(&controls);
3271+ // ...and the rate the mix runs at, if the project picked one: the
3272+ // same override [`crate::edith::save`] writes and [`Self::open_project`]
3273+ // reads back.
3274+ let sample_rate = self.sample_rate;
32393275 // ...and the open itself -- every track a segment names, its packet
32403276 // table, its priming -- happens there too. That is the whole point:
32413277 // one `pread` on a cold 25 GB film is seconds, and this is called
@@ -3249,6 +3285,7 @@ impl PlaybackSession {
32493285 &gains,
32503286 limiter,
32513287 Some(&worker_controls),
3288+ sample_rate,
32523289 ) {
32533290 Ok(Some((_, rx))) => Some(rx),
32543291 // A silent timeline, and a source that will not open: both
@@ -3327,7 +3364,13 @@ impl PlaybackSession {
33273364 out: &Path,
33283365 settings: &crate::export::ExportSettings,
33293366 ) -> crate::ExportHandle {
3330- crate::export::start(self.project.export_snapshot(), self.meta, out, settings)
3367+ crate::export::start(
3368+ self.project.export_snapshot(),
3369+ self.meta,
3370+ out,
3371+ settings,
3372+ self.sample_rate,
3373+ )
33313374 }
33323375
33333376 /// Moves the clock forward; the caller runs this once per rendered frame.
@@ -3775,6 +3818,32 @@ mod tests {
37753818 .join(name)
37763819 }
37773820
3821+ /// A picked project rate overrides the derived one, and its absence leaves
3822+ /// the source's own -- the seam every explicit-sample-rate caller
3823+ /// (playback rebuild, both export encoders) goes through
3824+ /// ([`crate::AudioSession::open_multi_streams_speed_at`]). The per-segment
3825+ /// conform chain resamples to whatever `meta` names, so the meta *is* the
3826+ /// behaviour.
3827+ #[test]
3828+ fn a_picked_sample_rate_overrides_the_derived_one() {
3829+ let file = asset("test_av.mp4");
3830+ let sources = [(file, 0)];
3831+ let segs = [(Some(0), 0., 1.)];
3832+ let derived = crate::AudioSession::open_multi_streams_speed_at(&sources, &segs, &[], &[], None)
3833+ .expect("open the fixture")
3834+ .expect("the fixture has sound")
3835+ .0
3836+ .sample_rate;
3837+ let picked =
3838+ crate::AudioSession::open_multi_streams_speed_at(&sources, &segs, &[], &[], Some(32000))
3839+ .expect("open the fixture")
3840+ .expect("the fixture has sound")
3841+ .0
3842+ .sample_rate;
3843+ assert_ne!(derived, 32000, "a fixture at the picked rate proves nothing");
3844+ assert_eq!(picked, 32000);
3845+ }
3846+
37783847 /// Which source the timeline's sound is opened on -- the one rule
37793848 /// [`open_project`](PlaybackSession::open_project), the probe every import
37803849 /// is held to and the audio worker all read through. A still at index 0 is
crates/engine/src/proxy.rsmodified+3 -0
@@ -524,6 +524,9 @@ fn started(source: &Path, only_if: fn(&VideoMeta) -> bool) -> crate::Result<Opti
524524 bitrate: Some(bitrate),
525525 ..Default::default()
526526 },
527+ // A stand-in keeps its film's own rate: the override is the
528+ // timeline's affair, not the proxy's.
529+ None,
527530 );
528531 Ok(Some(Job {
529532 out,