FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
feat(engine): dynamically expand audio tracks on multi-clip placement
Idiotic78ed3edd845e
diff --git a/crates/engine/src/audio_placement_tests.rs b/crates/engine/src/audio_placement_tests.rsnew file mode 100644index 000000000..c8b6cc212--- /dev/null+++ b/crates/engine/src/audio_placement_tests.rs@@ -0,0 +1,77 @@+//! Multi-clip audio placement: `audio_placement_specs` and dynamic audio track allocation.++use filmcraft_project::{AudioChannels, ItemId, TrackKind};++use serde_json::json;++use crate::Session;+use crate::commands::{AudioPlacementSpec, audio_placement_specs, ensure_audio_tracks};++fn demo() -> Session {+ let mut s = Session::default();+ s.execute("file.openDemoProject", json!({})).unwrap();+ s+}++fn active(s: &Session) -> ItemId {+ s.state.active_sequence.expect("demo session has an active sequence")+}++#[test]+fn specs_always_place_one_clip_on_the_destination() {+ assert_eq!(audio_placement_specs(&[]), vec![AudioPlacementSpec { track_offset: 0, source_channels: None }]);+ assert_eq!(audio_placement_specs(&[vec![0, 1]]).len(), 1);+}++#[test]+fn specs_put_further_clips_on_the_tracks_below() {+ let specs = audio_placement_specs(&[vec![0], vec![1], vec![2, 3]]);+ let got: Vec<(usize, Option<Vec<u16>>)> = specs.into_iter().map(|s| (s.track_offset, s.source_channels)).collect();+ assert_eq!(got, vec![(0, None), (1, Some(vec![1])), (2, Some(vec![2, 3]))]);+}++#[test]+fn specs_are_capped_for_hostile_channel_maps() {+ let clips = vec![vec![0u16]; 100_000];+ assert!(audio_placement_specs(&clips).len() <= crate::sequence_tools::MAX_TRACKS);+}++#[test]+fn missing_audio_tracks_are_cloned_from_the_destination() {+ let s = demo();+ let seq = active(&s);+ let mut p = (*s.project).clone();+ let have = p.sequence(seq).map(|q| q.audio_tracks.len()).unwrap_or(0);+ assert!(have >= 1);+ if let Some(t) = p.sequence_mut(seq).and_then(|q| q.audio_tracks.get_mut(0)) {+ t.channels = AudioChannels::Mono;+ t.volume_db = -3.0;+ t.locked = true;+ }+ let ids = ensure_audio_tracks(&mut p, seq, 0, have + 3, "test").unwrap();+ assert_eq!(ids.len(), have + 3);+ let q = p.sequence(seq).unwrap();+ for t in &q.audio_tracks[have..] {+ assert_eq!(t.kind, TrackKind::Audio);+ assert_eq!(t.channels, AudioChannels::Mono);+ assert_eq!(t.volume_db, -3.0);+ assert!(t.items.is_empty() && !t.locked);+ }+ let mut unique = ids.clone();+ unique.sort_by_key(|t| t.0);+ unique.dedup();+ assert_eq!(unique.len(), ids.len(), "track ids are unique");+ // enough tracks already: nothing is added+ let again = ensure_audio_tracks(&mut p, seq, 0, have + 3, "test").unwrap();+ assert_eq!(again, ids);+}++#[test]+fn hostile_track_counts_are_an_error_not_a_crash() {+ let s = demo();+ let seq = active(&s);+ let mut p = (*s.project).clone();+ assert!(ensure_audio_tracks(&mut p, seq, usize::MAX, usize::MAX, "test").is_err());+ assert!(ensure_audio_tracks(&mut p, seq, 0, 1_000_000, "test").is_err());+ assert!(ensure_audio_tracks(&mut p, ItemId(u64::MAX), 0, 1, "test").is_err());+}diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rsindex acdccda75..0c4c96314 100644--- a/crates/engine/src/commands.rs+++ b/crates/engine/src/commands.rs@@ -8,8 +8,8 @@ use filmcraft_edit::{Edge, TrimMode};use filmcraft_media::Generator;use filmcraft_media::generators::GeneratorSource;use filmcraft_project::{- ClipId, ItemId, ItemKind, Label, Marker, MarkerId, MarkerKind, MediaClip, MediaRef, ParamValue, SequenceSettings, TrackId, TrackKind, Transition,- TransitionId, resolve_auto_points,+ ClipId, ItemId, ItemKind, Label, Marker, MarkerId, MarkerKind, MediaClip, MediaRef, ParamValue, Project, SequenceSettings, Track, TrackId, TrackKind,+ Transition, TransitionId, resolve_auto_points,};use filmcraft_time::{FrameRate, TICKS_PER_SECOND, Tick, TimeRange, parse_timecode};use serde_json::{Value, json};@@ -485,6 +485,57 @@ fn set_matte_color(s: &mut Session, p: &Value) -> Result<Value> {Ok(json!({"item": item.0, "color": filmcraft_color::to_hex(color)}))}+/// One audio clip of a placement: how many tracks below the destination it goes, and the source+/// channels it plays (`None` = the clip's own default, used for the first one).+#[derive(Clone, Debug, PartialEq, Eq)]+pub(crate) struct AudioPlacementSpec {+ pub track_offset: usize,+ pub source_channels: Option<Vec<u16>>,+}++/// The audio clips to place for a source whose Modify ▸ Audio Channels map has `clips` (one entry+/// per clip): always at least one (on the destination track), then one per further entry on the+/// tracks below. The count is capped at [`crate::sequence_tools::MAX_TRACKS`] (the map comes from+/// project files and scripts: never trusted).+pub(crate) fn audio_placement_specs(clips: &[Vec<u16>]) -> Vec<AudioPlacementSpec> {+ let mut specs = vec![AudioPlacementSpec { track_offset: 0, source_channels: None }];+ let extra = clips.iter().skip(1).take(crate::sequence_tools::MAX_TRACKS.saturating_sub(1));+ specs.extend(extra.enumerate().map(|(k, chans)| AudioPlacementSpec { track_offset: k.saturating_add(1), source_channels: Some(chans.clone()) }));+ specs+}++/// Make sure the sequence has audio tracks for `count` clips starting at track index `first`,+/// adding tracks at the bottom when it has fewer. A new track is a copy of the destination track+/// (channel format, volume, pan, inserts) without its clips, transitions, lock / mute / solo+/// state or mixer routing. Returns the ids of all the sequence's audio tracks, in order.+pub(crate) fn ensure_audio_tracks(p: &mut Project, seq_id: ItemId, first: usize, count: usize, label: &str) -> Result<Vec<TrackId>> {+ let (have, template) = {+ let q = p.sequence(seq_id).ok_or(EngineError::NoSequence)?;+ (q.audio_tracks.len(), q.audio_tracks.get(first).or(q.audio_tracks.last()).cloned())+ };+ let need = first.saturating_add(count);+ if need > crate::sequence_tools::MAX_TRACKS {+ return Err(bad(label, format!("a sequence has at most {} audio tracks", crate::sequence_tools::MAX_TRACKS)));+ }+ if need > have {+ let ids: Vec<(usize, TrackId)> = (have..need).map(|k| (k, TrackId(p.alloc_id()))).collect();+ let q = p.sequence_mut(seq_id).ok_or(EngineError::NoSequence)?;+ for (k, id) in ids {+ let mut t = template.clone().unwrap_or_else(|| Track::new(id, TrackKind::Audio, String::new()));+ t.id = id;+ t.name = format!("Audio {}", k + 1);+ t.items.clear();+ t.transitions.clear();+ t.locked = false;+ t.muted = false;+ t.solo = false;+ t.mixer = Default::default();+ q.audio_tracks.push(t);+ }+ }+ Ok(p.sequence(seq_id).map(|q| q.audio_tracks.iter().map(|t| t.id).collect()).unwrap_or_default())+}+/// Place a project item on the timeline (drag from Project, or Insert/Overwrite from source).pub(crate) fn place_item(s: &mut Session,@@ -539,21 +590,28 @@ pub(crate) fn place_item(let (arange, aat) = audio_split.unwrap_or((range, at));let mut a = p.make_track_item(item, TrackKind::Audio, aat, arange, rate).ok_or_else(|| bad(label, "bad item"))?;// Modify ▸ Audio Channels with several audio clips: one per clip, on the tracks below- let extra: Vec<Vec<u16>> = p- .item(item)- .and_then(|i| i.as_media())- .and_then(|m| m.interpret.audio_channels.as_ref())- .map(|m| m.clips.iter().skip(1).cloned().collect())- .unwrap_or_default();- let tracks: Vec<TrackId> = p.sequence(seq_id).map(|q| q.audio_tracks.iter().map(|t| t.id).collect()).unwrap_or_default();- let first = tracks.iter().position(|t| *t == adest).unwrap_or(0);- a.link = if link.is_none() && !extra.is_empty() { Some(p.alloc_id()) } else { link };- placements.push((adest, a.clone()));- for (k, chans) in extra.into_iter().enumerate() {- let Some(tid) = tracks.get(first + k + 1) else { break };+ let clips: Vec<Vec<u16>> =+ p.item(item).and_then(|i| i.as_media()).and_then(|m| m.interpret.audio_channels.as_ref()).map(|m| m.clips.clone()).unwrap_or_default();+ let specs = audio_placement_specs(&clips);+ let first = p.sequence(seq_id).and_then(|q| q.audio_tracks.iter().position(|t| t.id == adest)).unwrap_or(0);+ // too few audio tracks below the destination: grow the sequence from the destination track+ let tracks = ensure_audio_tracks(p, seq_id, first, specs.len(), label)?;+ a.link = if link.is_none() && specs.len() > 1 { Some(p.alloc_id()) } else { link };+ for spec in specs {+ let tid = match spec.track_offset {+ 0 => &adest,+ off => match first.checked_add(off).and_then(|i| tracks.get(i)) {+ Some(t) => t,+ None => continue,+ },+ };let mut b = a.clone();- b.id = ClipId(p.alloc_id());- b.source_channels = chans;+ if spec.track_offset > 0 {+ b.id = ClipId(p.alloc_id());+ }+ if let Some(chans) = spec.source_channels {+ b.source_channels = chans;+ }placements.push((*tid, b));}}diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rsindex 922ebdd41..38adc8f89 100644--- a/crates/engine/src/lib.rs+++ b/crates/engine/src/lib.rs@@ -1022,6 +1022,8 @@ mod aaf_omf_tests;#[cfg(test)]mod audio_effects_tests;#[cfg(test)]+mod audio_placement_tests;+#[cfg(test)]mod autosave_tests;#[cfg(test)]mod clip_ops_tests;diff --git a/crates/engine/src/sequence_tools.rs b/crates/engine/src/sequence_tools.rsindex 5183c10e4..de1e02182 100644--- a/crates/engine/src/sequence_tools.rs+++ b/crates/engine/src/sequence_tools.rs@@ -587,7 +587,7 @@ pub(crate) fn place_sequence_clips(/// The most tracks one `sequence.addTracks` adds of a kind, and the most a sequence may then/// have of it. (Amounts come from dialogs, scripts and the control channel: never trusted.)const MAX_ADDED_TRACKS: u64 = 99;-const MAX_TRACKS: usize = 999;+pub(crate) const MAX_TRACKS: usize = 999;/// Where `sequence.addTracks` puts new tracks among the `len` tracks of their kind: the number of/// tracks before them. `key` holds `"first"` (Before First Track), a track of that kind by name