Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
improve(editor): mix export audio from contiguous slices
Kyzer Phnehd6b1a8e669e4
diff --git a/crates/editor/src/export_audio.rs b/crates/editor/src/export_audio.rsindex 8df5e4df7..b3f28b615 100644--- a/crates/editor/src/export_audio.rs+++ b/crates/editor/src/export_audio.rs@@ -8,6 +8,7 @@ use cap_project::{ClipOffsets, ProjectConfiguration, RecordingMeta, StudioRecorduse std::{collections::HashMap,fmt,+ ops::Range,path::{Path, PathBuf},sync::{Arc,@@ -704,6 +705,11 @@ impl AudioSampleSource for ExportAudioView<'_> {.checked_sub(self.source_start * self.channels as usize).and_then(|index| self.samples.get(index))}+ fn sample_slice(&self, range: Range<usize>) -> Option<&[f32]> {+ let base = self.source_start * self.channels as usize;+ self.samples+ .get(range.start.checked_sub(base)?..range.end.checked_sub(base)?)+ }}#[cfg(test)]@@ -1696,6 +1702,158 @@ mod tests {assert!(!ExportAudioRenderer::eligible_sources(&project, 3));}+ #[test]+ fn offset_view_slice_and_indexed_mixing_are_bit_identical() {+ struct Indexed<'a>(&'a ExportAudioView<'a>);++ impl AudioSampleSource for Indexed<'_> {+ fn channels(&self) -> u16 {+ self.0.channels()+ }++ fn sample_count(&self) -> usize {+ self.0.sample_count()+ }++ fn sample(&self, index: usize) -> Option<&f32> {+ self.0.sample(index)+ }+ }++ fn track<'a, T: AudioSampleSource>(+ data: &'a T,+ channels: u16,+ mono_right: bool,+ ) -> AudioRendererTrack<'a, T> {+ AudioRendererTrack {+ data,+ gain: -6.0,+ stereo_mode: if channels == 1 {+ StereoMode::Stereo+ } else if mono_right {+ StereoMode::MonoR+ } else {+ StereoMode::Stereo+ },+ offset: 0,+ }+ }++ for channels in [1u16, 2] {+ for mono_right in [false, true] {+ if channels == 1 && mono_right {+ continue;+ }+ let source_start = 1_537usize;+ let frames = source_start + 5_000;+ let width = usize::from(channels);+ let samples = (0..frames * width)+ .map(|index| {+ let raw = (index.wrapping_mul(73) % 60_001) as i32 - 30_000;+ raw as f32 / 32_768.0+ })+ .collect::<Vec<_>>();+ let window = &samples[source_start * width..];+ let view = ExportAudioView {+ samples: window,+ channels,+ source_start,+ available_end: frames,+ };+ let short = ExportAudioView {+ samples: &window[..100 * width],+ channels,+ source_start,+ available_end: frames,+ };+ for index in 0..frames * width {+ let indexed = view.sample(index).copied().map(f32::to_bits);+ let sliced = view+ .sample_slice(index..index + 1)+ .map(|slice| slice[0].to_bits());+ assert_eq!(indexed, sliced, "channels {channels} index {index}");+ }+ let aligned = source_start * width..(source_start + 128) * width;+ let slice = view.sample_slice(aligned.clone()).unwrap();+ assert_eq!(slice.len(), aligned.end - aligned.start);+ assert!(view.sample_slice(0..width).is_none());+ assert!(+ view.sample_slice((frames - 1) * width..(frames + 1) * width)+ .is_none()+ );+ assert!(view.sample((frames - 1) * width).is_some());+ assert!(+ short+ .sample_slice(source_start * width..(source_start + 101) * width)+ .is_none()+ );+ assert!(+ short+ .sample_slice(source_start * width..(source_start + 100) * width)+ .is_some()+ );++ let indexed = Indexed(&view);+ let indexed_short = Indexed(&short);+ let mut slice_out = vec![13.0f32; 4_096 * 2];+ let mut indexed_out = slice_out.clone();+ for (cursor, request, source_view, source_indexed) in [+ (0usize, 64usize, &view, &indexed),+ (source_start - 3, 4_096, &view, &indexed),+ (source_start, 4_096, &view, &indexed),+ (source_start + 17, 1, &view, &indexed),+ (source_start + 100, 7, &view, &indexed),+ (frames - 10, 4_096, &view, &indexed),+ (frames, 8, &view, &indexed),+ (source_start, 4_096, &short, &indexed_short),+ (source_start + 90, 4_096, &short, &indexed_short),+ ] {+ let rendered_slice = cap_audio::render_audio(+ &[track(source_view, channels, mono_right)],+ cursor,+ request,+ 0,+ &mut slice_out,+ );+ let rendered_indexed = cap_audio::render_audio(+ &[track(source_indexed, channels, mono_right)],+ cursor,+ request,+ 0,+ &mut indexed_out,+ );+ assert_eq!(rendered_slice, rendered_indexed);+ if cursor >= source_start+ && cursor + rendered_slice <= source_view.sample_count()+ && rendered_slice > 0+ && (cursor + rendered_slice).saturating_sub(source_start) * width+ <= source_view.samples.len()+ {+ assert!(+ source_view+ .sample_slice(cursor * width..(cursor + rendered_slice) * width)+ .is_some(),+ "channels {channels} cursor {cursor}"+ );+ }+ assert_eq!(+ slice_out[..rendered_slice * 2]+ .iter()+ .copied()+ .map(f32::to_bits)+ .collect::<Vec<_>>(),+ indexed_out[..rendered_indexed * 2]+ .iter()+ .copied()+ .map(f32::to_bits)+ .collect::<Vec<_>>(),+ "channels {channels} cursor {cursor} mono_right {mono_right}"+ );+ }+ }+ }+ }+#[test]fn source_windows_reject_backward_reads_and_keep_failures_sticky() {let directory = tempfile::tempdir().unwrap();