Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/editor/src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2110,6 +2110,7 @@ fn audio_project_key(project: &ProjectConfiguration) -> Option<Vec<u8>> {
screen_motion_blur: _,
screen_movement_spring: _,
color_correction: _,
clip_alignment_version: _,
text_size_version: _,
text_anim_version: _,
markers: _,
Expand Down
31 changes: 31 additions & 0 deletions crates/editor/src/editor_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,12 +524,27 @@ impl EditorInstance {
}
}

// Before any offsets are filled in below: those are already on the
// current segment clock.
let migrated = cap_project::migrate_late_audio_alignment(&mut project, meta, || {
match &preloaded_recordings {
Some(recordings) => recordings
.segments
.iter()
.map(|segment| segment.display.duration)
.collect(),
None => display_durations(&recording_meta, meta),
}
});

if project.clips.is_empty() {
project.clips = initial_clip_configuration(&recording_meta.project_path, meta);

if let Err(e) = project.write(&recording_meta.project_path) {
warn!("Failed to save auto-generated clip offsets: {}", e);
}
} else if migrated && let Err(e) = project.write(&recording_meta.project_path) {
warn!("Failed to save late audio alignment: {}", e);
}

// Segment setup (decoder init + kicking off audio decodes) is
Expand Down Expand Up @@ -1984,6 +1999,22 @@ pub fn initial_clip_configuration(
}
}

/// Each clip's display duration, the source clock captions are timed on.
fn display_durations(recording_meta: &RecordingMeta, meta: &StudioRecordingMeta) -> Vec<f64> {
match meta {
StudioRecordingMeta::SingleSegment { segment } => {
vec![display_video_duration(&recording_meta.path(&segment.display.path)).unwrap_or(0.0)]
}
StudioRecordingMeta::MultipleSegments { inner } => inner
.segments
.iter()
.map(|segment| {
display_video_duration(&recording_meta.path(&segment.display.path)).unwrap_or(0.0)
})
.collect(),
}
}

fn load_calibration_store(project_path: &std::path::Path) -> cap_audio::CalibrationStore {
let calibration_dir = project_path
.parent()
Expand Down
110 changes: 107 additions & 3 deletions crates/export/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ impl ExporterBuilder {
&mut project_config,
&recording_meta.project_path,
studio_meta,
&recordings
.segments
.iter()
.map(|segment| segment.display.duration)
.collect::<Vec<_>>(),
);

cap_project::clamp_timeline_to_sources(
Expand Down Expand Up @@ -839,12 +844,16 @@ mod forward_read_tests {
/// editor fills them in from the tracks' start times (and the devices'
/// calibration) when it first opens the project. An export of a project the
/// editor never opened would otherwise play every audio track from its own
/// start, late against the screen by how much earlier it started.
/// start, late against the screen by how much earlier it started. A project
/// saved before late audio stopped setting a segment's start is first moved
/// onto the current segment clock, the way the editor does on open.
fn fill_clip_offsets(
config: &mut ProjectConfiguration,
project_path: &std::path::Path,
studio_meta: &StudioRecordingMeta,
recording_durations: &[f64],
) {
cap_project::migrate_late_audio_alignment(config, studio_meta, || recording_durations.to_vec());
if config.clips.is_empty() {
config.clips = cap_editor::initial_clip_configuration(project_path, studio_meta);
}
Expand Down Expand Up @@ -895,7 +904,7 @@ mod clip_offset_tests {
let meta = camera_recording();
let mut config = ProjectConfiguration::default();
assert!(config.clips.is_empty());
fill_clip_offsets(&mut config, dir.path(), &meta);
fill_clip_offsets(&mut config, dir.path(), &meta, &[10.0]);
let offsets = config.clips[0].offsets;
assert!((offsets.mic - 0.1695).abs() < 1e-4, "mic {}", offsets.mic);
assert!(
Expand All @@ -921,9 +930,104 @@ mod clip_offset_tests {
}],
..Default::default()
};
fill_clip_offsets(&mut config, dir.path(), &camera_recording());
fill_clip_offsets(&mut config, dir.path(), &camera_recording(), &[10.0]);
assert_eq!(config.clips[0].offsets.mic, 0.5);
}

fn late_mic_recording(mic_start: f64) -> StudioRecordingMeta {
let mut meta = camera_recording();
let StudioRecordingMeta::MultipleSegments { inner } = &mut meta else {
unreachable!()
};
inner.segments[0].mic.as_mut().unwrap().start_time = Some(mic_start);
meta
}

fn auto_clip(mic: f32, system_audio: f32, auto: bool) -> ProjectConfiguration {
ProjectConfiguration {
clips: vec![ClipConfiguration {
index: 0,
offsets: ClipOffsets {
camera: 0.0,
mic,
system_audio,
},
offsets_auto_calculated: auto,
}],
clip_alignment_version: 0,
..Default::default()
}
}

// Offsets saved while a late mic set the segment's start (mic 0, system
// audio late - 0) move to the screen's start.
#[test]
fn saved_auto_offsets_of_a_late_mic_are_recomputed() {
let dir = tempfile::tempdir().unwrap();
let mut config = auto_clip(0.0, 5.2026, true);
fill_clip_offsets(
&mut config,
dir.path(),
&late_mic_recording(5.2026),
&[10.0],
);
let offsets = config.clips[0].offsets;
assert!((offsets.mic - -5.0).abs() < 1e-4, "mic {}", offsets.mic);
assert!((offsets.system_audio - 0.2026).abs() < 1e-4);
assert_eq!(offsets.camera, 0.0);
assert!(config.clips[0].offsets_auto_calculated);
}

// Offsets set by hand keep the sync the user chose against the video.
#[test]
fn user_offsets_of_a_late_mic_keep_their_sync() {
let dir = tempfile::tempdir().unwrap();
let mut config = auto_clip(0.25, 5.2026, false);
fill_clip_offsets(
&mut config,
dir.path(),
&late_mic_recording(5.2026),
&[10.0],
);
let offsets = config.clips[0].offsets;
assert!((offsets.mic - -4.75).abs() < 1e-4, "mic {}", offsets.mic);
assert!((offsets.system_audio - 0.2026).abs() < 1e-4);
assert!(!config.clips[0].offsets_auto_calculated);
}

// A project saved by this version is already on the current clock.
#[test]
fn current_projects_are_not_moved_again() {
let dir = tempfile::tempdir().unwrap();
let mut config = auto_clip(-5.0, 0.2026, true);
config.clip_alignment_version = cap_project::CLIP_ALIGNMENT_VERSION;
fill_clip_offsets(
&mut config,
dir.path(),
&late_mic_recording(5.2026),
&[10.0],
);
assert_eq!(config.clips[0].offsets.mic.to_bits(), (-5.0f32).to_bits());
assert_eq!(
config.clips[0].offsets.system_audio.to_bits(),
0.2026f32.to_bits()
);
}

// Only segments with late audio are touched: saved automatic offsets of
// a normal recording stay bit-identical even if they differ from a fresh
// computation (for example a calibration made after they were saved).
#[test]
fn saved_auto_offsets_of_a_normal_recording_are_kept() {
let dir = tempfile::tempdir().unwrap();
let mut config = auto_clip(0.123, 0.456, true);
fill_clip_offsets(&mut config, dir.path(), &camera_recording(), &[10.0]);
assert_eq!(config.clips[0].offsets.mic.to_bits(), 0.123f32.to_bits());
assert_eq!(
config.clips[0].offsets.system_audio.to_bits(),
0.456f32.to_bits()
);
}
}

#[cfg(test)]
Expand Down
12 changes: 12 additions & 0 deletions crates/project/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3074,6 +3074,15 @@ pub struct ProjectConfiguration {
/// grade.
#[serde(default)]
pub color_correction: ColorCorrectionConfiguration,
/// How clip audio offsets, markers and caption words of a segment with a
/// late audio track are timed. 0 (legacy): against the late audio start,
/// which also set where the segment's video began. 1: against the screen
/// and camera start (`MultipleSegment::latest_start_time`); legacy configs
/// are migrated by `migrate_late_audio_alignment` when the recording is
/// opened. The field-level default keeps old files at 0 while
/// `Default::default()` produces the current version.
#[serde(default)]
pub clip_alignment_version: u32,
/// How text segment font sizes are interpreted. 0 (legacy): the renderer
/// multiplied `font_size` by `size.y / 0.2`, coupling glyph size to the
/// box. 1: `font_size` alone determines glyph size (1080p-relative);
Expand All @@ -3098,6 +3107,7 @@ pub struct ProjectConfiguration {

pub const TEXT_SIZE_VERSION: u32 = 1;
pub const TEXT_ANIM_VERSION: u32 = 1;
pub const CLIP_ALIGNMENT_VERSION: u32 = 1;

/// The keys [`ProjectConfiguration::load`] migrates on presence, read in a pass
/// that skips every value instead of building a JSON tree of a file that can
Expand Down Expand Up @@ -3144,6 +3154,7 @@ impl Default for ProjectConfiguration {
screen_motion_blur: Self::default_screen_motion_blur(),
screen_movement_spring: Default::default(),
color_correction: Default::default(),
clip_alignment_version: CLIP_ALIGNMENT_VERSION,
text_size_version: TEXT_SIZE_VERSION,
text_anim_version: TEXT_ANIM_VERSION,
markers: Vec::new(),
Expand Down Expand Up @@ -3235,6 +3246,7 @@ impl ProjectConfiguration {
(None, None) => None,
};
preset.timeline = self.timeline.clone();
preset.clip_alignment_version = self.clip_alignment_version;
preset.text_size_version = self.text_size_version;
preset.text_anim_version = self.text_anim_version;
preset.overlay_order = self.overlay_order.clone();
Expand Down
Loading
Loading