Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(desktop-gpui): don't apply captions from stale trim ranges

Aditya Garud8d32ae6c501e
diff --git a/apps/desktop-gpui/src/editor_tabs.rs b/apps/desktop-gpui/src/editor_tabs.rs
index 3a808180d..9a4841a53 100644
--- a/apps/desktop-gpui/src/editor_tabs.rs
+++ b/apps/desktop-gpui/src/editor_tabs.rs
@@ -25,7 +25,7 @@ use cap_project::{
BackgroundBlurConfig, BackgroundBlurMode, CameraShape, CameraXPosition, CameraYPosition,
CaptionSegment, CaptionSettings, CornerStyle, CursorAnimationStyle, CursorRippleConfig,
KeyboardData, KeyboardSettings, ProjectConfiguration, RecordingMeta, ShadowConfiguration,
- StereoMode,
+ StereoMode, transcription_ranges_cover, transcription_source_ranges,
};
use gpui::{
AnyElement, Bounds, Context, EntityId, FontWeight, Hsla, InteractiveElement, IntoElement,
@@ -3177,6 +3177,8 @@ impl EditorWindow {
let recording_durations = summary.clip_display_durations.clone();
let recording_duration = summary.recording_duration;
let timeline = self.project.timeline.clone();
+ let transcribed_ranges =
+ transcription_source_ranges(timeline.as_ref(), &recording_durations);
let project_path = self.project_path.clone();
let model = self.selected_caption_model().name;
let language = self.sidebar.caption_language;
@@ -3207,10 +3209,19 @@ impl EditorWindow {
Err(error) => Err(format!("Transcription task failed: {error}")),
};
- match result {
- Ok(segments) => {
- transcription::finish_generation(&project_path, None);
- let _ = this.update_in(cx, |this, window, cx| {
+ let result = match result {
+ Ok(segments) => this
+ .update_in(cx, |this, window, cx| {
+ let current_ranges = transcription_source_ranges(
+ this.project.timeline.as_ref(),
+ &recording_durations,
+ );
+ if !transcription_ranges_cover(&transcribed_ranges, &current_ranges) {
+ return Err(
+ "The timeline changed during generation. Generate captions again."
+ .to_string(),
+ );
+ }
this.apply_generated_captions(
segments,
&recording_durations,
@@ -3218,7 +3229,15 @@ impl EditorWindow {
window,
cx,
);
- });
+ Ok(())
+ })
+ .unwrap_or(Ok(())),
+ Err(error) => Err(error),
+ };
+
+ match result {
+ Ok(()) => {
+ transcription::finish_generation(&project_path, None);
}
Err(error) => {
tracing::error!("caption generation failed: {error}");
diff --git a/crates/project/src/caption_timing.rs b/crates/project/src/caption_timing.rs
index 6b6b5e622..5c32d1a66 100644
--- a/crates/project/src/caption_timing.rs
+++ b/crates/project/src/caption_timing.rs
@@ -186,6 +186,17 @@ pub fn transcription_source_ranges(
merged
}
+pub fn transcription_ranges_cover(
+ transcribed: &[std::ops::Range<f64>],
+ required: &[std::ops::Range<f64>],
+) -> bool {
+ required.iter().all(|range| {
+ transcribed
+ .iter()
+ .any(|source| source.start <= range.start && source.end >= range.end)
+ })
+}
+
fn map_time_range_within_mapping(
start: f64,
end: f64,
@@ -505,6 +516,52 @@ mod tests {
);
}
+ #[test]
+ fn transcription_coverage_accepts_identical_ranges() {
+ let ranges = [1.0..4.0, 6.0..10.0];
+ assert!(transcription_ranges_cover(&ranges, &ranges));
+ }
+
+ #[test]
+ fn transcription_coverage_accepts_further_trims_and_reordered_clips() {
+ let timeline = serde_json::from_value(serde_json::json!({
+ "segments": [
+ {"start":7.0,"end":9.0,"timescale":2.0},
+ {"start":2.0,"end":3.0,"timescale":0.5}
+ ], "zoomSegments": []
+ }))
+ .unwrap();
+ let current = transcription_source_ranges(Some(&timeline), &[10.0]);
+ assert!(transcription_ranges_cover(&[1.0..4.0, 6.0..10.0], &current));
+ }
+
+ #[test]
+ fn transcription_coverage_rejects_audio_restored_by_undo() {
+ let timeline = serde_json::from_value(serde_json::json!({
+ "segments": [{"start":0.0,"end":10.0,"timescale":1.0}],
+ "zoomSegments": []
+ }))
+ .unwrap();
+ let current = transcription_source_ranges(Some(&timeline), &[10.0]);
+ assert!(!transcription_ranges_cover(
+ &[0.0..4.0, 6.0..10.0],
+ &current
+ ));
+ assert!(!transcription_ranges_cover(&[1.0..10.0], &current));
+ assert!(!transcription_ranges_cover(&[0.0..9.0], &current));
+ assert!(!transcription_ranges_cover(&[], &current));
+ assert!(!transcription_ranges_cover(
+ &[0.0..4.0],
+ &[0.0..4.0, 6.0..10.0]
+ ));
+ }
+
+ #[test]
+ fn transcription_coverage_accepts_an_empty_timeline() {
+ assert!(transcription_ranges_cover(&[0.0..10.0], &[]));
+ assert!(transcription_ranges_cover(&[], &[]));
+ }
+
fn project(source_timed: bool, start: f32, end: f32) -> ProjectConfiguration {
ProjectConfiguration {
timeline: Some(serde_json::from_value(serde_json::json!({
diff --git a/crates/project/src/lib.rs b/crates/project/src/lib.rs
index 6e82e759a..e4cead314 100644
--- a/crates/project/src/lib.rs
+++ b/crates/project/src/lib.rs
@@ -8,7 +8,7 @@ mod meta;
pub use animated_gradient::*;
pub use caption_timing::{
derive_caption_track_segments, source_caption_id, synchronize_captions,
- transcription_source_ranges,
+ transcription_ranges_cover, transcription_source_ranges,
};
pub use configuration::*;
pub use cursor::*;