Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
Refactor audio rendering and playback logic
Cursor Agent562924c3b395
diff --git a/crates/audio/src/renderer.rs b/crates/audio/src/renderer.rsindex bb22ae7ea..16e9803a8 100644--- a/crates/audio/src/renderer.rs+++ b/crates/audio/src/renderer.rs@@ -13,7 +13,6 @@ pub struct AudioRendererTrack<'a> {pub offset: isize,}-// Renders a combination of audio tracks into a single stereo bufferpub fn render_audio(tracks: &[AudioRendererTrack],offset: usize,@@ -24,7 +23,15 @@ pub fn render_audio(let samples = samples.min(tracks.iter()- .flat_map(|t| (t.data.samples().len() / t.data.channels() as usize).checked_sub(offset))+ .filter_map(|t| {+ let track_samples = t.data.samples().len() / t.data.channels() as usize;+ let available = track_samples as isize - offset as isize - t.offset;+ if available > 0 {+ Some(available as usize)+ } else {+ None+ }+ }).max().unwrap_or(0),);diff --git a/crates/editor/src/audio.rs b/crates/editor/src/audio.rsindex 215018ff0..c34fc85fb 100644--- a/crates/editor/src/audio.rs+++ b/crates/editor/src/audio.rs@@ -1,13 +1,13 @@use cap_audio::{- AudioData, AudioRendererTrack, FromSampleBytes, StereoMode, cast_f32_slice_to_bytes,+ cast_f32_slice_to_bytes, AudioData, AudioRendererTrack, FromSampleBytes, StereoMode,};use cap_media::MediaError;use cap_media_info::AudioInfo;use cap_project::{AudioConfiguration, ClipOffsets, ProjectConfiguration, TimelineConfiguration};-use ffmpeg::{ChannelLayout, format as avformat, frame::Audio as FFAudio, software::resampling};+use ffmpeg::{format as avformat, frame::Audio as FFAudio, software::resampling, ChannelLayout};use ringbuf::{- HeapRb,traits::{Consumer, Observer, Producer},+ HeapRb,};use std::sync::Arc;@@ -132,8 +132,9 @@ impl AudioRenderer {};let cursor_diff = new_cursor.samples as isize - self.cursor.samples as isize;+ let frame_samples = (AudioData::SAMPLE_RATE as usize) / 30;if new_cursor.clip_index != self.cursor.clip_index- || cursor_diff.unsigned_abs() > (AudioData::SAMPLE_RATE as usize) / 5+ || cursor_diff.unsigned_abs() > frame_samples{self.cursor = new_cursor;}@@ -184,9 +185,22 @@ impl AudioRenderer {return None;}+ let start = self.cursor;++ let offsets = project+ .clips+ .iter()+ .find(|c| c.index == start.clip_index)+ .map(|c| c.offsets)+ .unwrap_or_default();+let max_samples = tracks.iter()- .map(|t| t.data().sample_count())+ .map(|t| {+ let track_offset_samples = (t.offset(&offsets) * Self::SAMPLE_RATE as f32) as isize;+ let available = t.data().sample_count() as isize - track_offset_samples;+ available.max(0) as usize+ }).max().unwrap();@@ -197,31 +211,24 @@ impl AudioRenderer {let samples = samples.min(max_samples - self.cursor.samples);- let start = self.cursor;-let mut ret = vec![0.0; samples * 2];let track_datas = tracks.iter()- .map(|t| {- let offsets = project- .clips- .iter()- .find(|c| c.index == start.clip_index)- .map(|c| c.offsets)- .unwrap_or_default();-- AudioRendererTrack {- data: t.data().as_ref(),- gain: if project.audio.mute {+ .map(|t| AudioRendererTrack {+ data: t.data().as_ref(),+ gain: if project.audio.mute {+ f32::NEG_INFINITY+ } else {+ let g = t.gain(&project.audio);+ if g < -30.0 {f32::NEG_INFINITY} else {- let g = t.gain(&project.audio);- if g < -30.0 { f32::NEG_INFINITY } else { g }- },- stereo_mode: t.stereo_mode(&project.audio),- offset: (t.offset(&offsets) * Self::SAMPLE_RATE as f32) as isize,- }+ g+ }+ },+ stereo_mode: t.stereo_mode(&project.audio),+ offset: (t.offset(&offsets) * Self::SAMPLE_RATE as f32) as isize,}).collect::<Vec<_>>();diff --git a/crates/editor/src/playback.rs b/crates/editor/src/playback.rsindex 4096abfb1..5ec4846bb 100644--- a/crates/editor/src/playback.rs+++ b/crates/editor/src/playback.rs@@ -934,7 +934,7 @@ impl AudioPlayback {let headroom_for_stream = headroom_samples;let mut playhead_rx_for_stream = playhead_rx.clone();let mut last_video_playhead = playhead;- const SYNC_THRESHOLD_SECS: f64 = 0.15;+ const SYNC_THRESHOLD_SECS: f64 = 0.05;let stream_result = device.build_output_stream(&config,diff --git a/crates/recording/src/sources/audio_mixer.rs b/crates/recording/src/sources/audio_mixer.rsindex 5157aa137..4aa448c44 100644--- a/crates/recording/src/sources/audio_mixer.rs+++ b/crates/recording/src/sources/audio_mixer.rs@@ -383,10 +383,10 @@ impl AudioMixer {filtered.set_rate(output_rate_i32 as u32);let elapsed = Duration::from_secs_f64(self.samples_out as f64 / output_rate);- let timestamp = start.instant() + start_timestamp.duration_since(start) + elapsed;+ let output_timestamp = &start_timestamp + elapsed;let frame_samples = filtered.samples();- let mut frame = AudioFrame::new(filtered, Timestamp::Instant(timestamp));+ let mut frame = AudioFrame::new(filtered, output_timestamp);loop {match self.output.try_send(frame) {