Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: align clip waveforms with the audio timing repair offset

Yoav Levavia15503118295
diff --git a/apps/desktop/src-tauri/src/audio.rs b/apps/desktop/src-tauri/src/audio.rs
index d2cb5708c..82f835df3 100644
--- a/apps/desktop/src-tauri/src/audio.rs
+++ b/apps/desktop/src-tauri/src/audio.rs
@@ -55,3 +55,41 @@ impl AppSounds {
pub fn get_waveform(audio: &DecodedAudio) -> Vec<f32> {
cap_audio::waveform_peaks(audio.sample_slices().flatten(), audio.channels())
}
+
+const WAVEFORM_PEAKS_PER_SECOND: f64 = 10.0;
+const WAVEFORM_SILENCE_DB: f32 = -60.0;
+
+/// Playback reads each track at source time plus its timing repair offset
+/// (`SegmentAudioTimingRepair`), so the peaks are shifted the same way; the
+/// editor then only has to add the user's clip offset.
+pub fn align_waveform_to_playback(peaks: Vec<f32>, timing_offset_secs: f32) -> Vec<f32> {
+ let shift = (f64::from(timing_offset_secs) * WAVEFORM_PEAKS_PER_SECOND).round() as isize;
+ if shift > 0 {
+ peaks.into_iter().skip(shift.unsigned_abs()).collect()
+ } else if shift < 0 {
+ std::iter::repeat_n(WAVEFORM_SILENCE_DB, shift.unsigned_abs())
+ .chain(peaks)
+ .collect()
+ } else {
+ peaks
+ }
+}
+
+#[cfg(test)]
+mod waveform_alignment_tests {
+ use super::align_waveform_to_playback;
+
+ #[test]
+ fn shifts_peaks_by_the_timing_repair_offset() {
+ let peaks = vec![-10.0, -20.0, -30.0, -40.0];
+ assert_eq!(
+ align_waveform_to_playback(peaks.clone(), 0.2),
+ vec![-30.0, -40.0]
+ );
+ assert_eq!(
+ align_waveform_to_playback(peaks.clone(), -0.1),
+ vec![-60.0, -10.0, -20.0, -30.0, -40.0]
+ );
+ assert_eq!(align_waveform_to_playback(peaks.clone(), 0.02), peaks);
+ }
+}
diff --git a/apps/desktop/src-tauri/src/lib.rs b/apps/desktop/src-tauri/src/lib.rs
index a78c37064..473c4b98f 100644
--- a/apps/desktop/src-tauri/src/lib.rs
+++ b/apps/desktop/src-tauri/src/lib.rs
@@ -6207,7 +6207,10 @@ async fn get_mic_waveforms(editor_instance: WindowEditorInstance) -> Result<Vec<
// Waits for the background decode; a failed track just renders as an
// empty waveform (playback/export surface the actual error).
match segment.audio.get().await {
- Ok(Some(audio)) => out.push(audio::get_waveform(&audio)),
+ Ok(Some(audio)) => out.push(audio::align_waveform_to_playback(
+ audio::get_waveform(&audio),
+ segment.audio_timing_repair.mic_offset_secs,
+ )),
Ok(None) => out.push(Vec::new()),
Err(error) => {
warn!(%error, "Mic audio failed to load; returning empty waveform");
@@ -6229,7 +6232,10 @@ async fn get_system_audio_waveforms(
for segment in editor_instance.segment_medias.iter() {
match segment.system_audio.get().await {
- Ok(Some(audio)) => out.push(audio::get_waveform(&audio)),
+ Ok(Some(audio)) => out.push(audio::align_waveform_to_playback(
+ audio::get_waveform(&audio),
+ segment.audio_timing_repair.system_audio_offset_secs,
+ )),
Ok(None) => out.push(Vec::new()),
Err(error) => {
warn!(%error, "System audio failed to load; returning empty waveform");