From f0af7368e1c29dc30b816a870ba7591e0e2084c2 Mon Sep 17 00:00:00 2001 From: Brendan Allan Date: Thu, 28 Nov 2024 02:11:13 +0800 Subject: [PATCH] fix single-segment audio --- apps/desktop/src/utils/tauri.ts | 2 +- crates/editor/src/playback.rs | 22 ++++++++-------- crates/export/src/lib.rs | 45 +++++++++++++++++---------------- 3 files changed, 35 insertions(+), 34 deletions(-) diff --git a/apps/desktop/src/utils/tauri.ts b/apps/desktop/src/utils/tauri.ts index 345a38c38..07d44f777 100644 --- a/apps/desktop/src/utils/tauri.ts +++ b/apps/desktop/src/utils/tauri.ts @@ -214,7 +214,7 @@ export type Audio = { duration: number; sample_rate: number; channels: number } export type AudioConfiguration = { mute: boolean; improve: boolean } export type AudioInputLevelChange = number export type AudioMeta = { path: string } -export type AuthStore = { token: string; user_id: string; expires: number; plan: Plan | null } +export type AuthStore = { token: string; user_id: string | null; expires: number; plan: Plan | null } export type AuthenticationInvalid = null export type BackgroundConfiguration = { source: BackgroundSource; blur: number; padding: number; rounding: number; inset: number; crop: Crop | null } export type BackgroundSource = { type: "wallpaper"; id: number } | { type: "image"; path: string | null } | { type: "color"; value: [number, number, number] } | { type: "gradient"; from: [number, number, number]; to: [number, number, number]; angle?: number } diff --git a/crates/editor/src/playback.rs b/crates/editor/src/playback.rs index 64a93799b..93568321c 100644 --- a/crates/editor/src/playback.rs +++ b/crates/editor/src/playback.rs @@ -61,17 +61,17 @@ impl Playback { .map(|t| t.duration()) .unwrap_or(f64::MAX); - // Lock the mutex and check if audio data is available - // if let Some(audio_data) = self.audio.as_ref() { - // AudioPlayback { - // audio: audio_data.clone(), - // stop_rx: stop_rx.clone(), - // start_frame_number: self.start_frame_number, - // duration, - // project: self.project.clone(), - // } - // .spawn(); - // }; + // TODO: make this work with >1 segment + if let Some(audio_data) = self.segments[0].audio.as_ref() { + AudioPlayback { + audio: audio_data.clone(), + stop_rx: stop_rx.clone(), + start_frame_number: self.start_frame_number, + duration, + project: self.project.clone(), + } + .spawn(); + }; loop { if frame_number as f64 > FPS as f64 * duration { diff --git a/crates/export/src/lib.rs b/crates/export/src/lib.rs index d088382eb..be65e8d42 100644 --- a/crates/export/src/lib.rs +++ b/crates/export/src/lib.rs @@ -92,28 +92,29 @@ pub async fn export_video_to_file( let audio_dir = tempfile::tempdir().unwrap(); let video_dir = tempfile::tempdir().unwrap(); - let mut audio = None::; - // if let Some(audio_data) = audio.as_ref() { - // let (tx, rx) = tokio::sync::mpsc::channel::>(30); - - // let pipe_path = - // cap_utils::create_channel_named_pipe(rx, audio_dir.path().join("audio.pipe")); - - // ffmpeg.add_input(cap_ffmpeg_cli::FFmpegRawAudioInput { - // input: pipe_path, - // sample_format: "f64le".to_string(), - // sample_rate: audio_data.info.sample_rate, - // channels: audio_data.info.channels as u16, - // }); - - // let buffer = AudioFrameBuffer::new(audio_data.clone()); - // Some(AudioRender { - // buffer, - // pipe_tx: tx, - // }) - // } else { - // None - // }; + let mut audio = if let Some(audio_data) = + audio_segments.get(0).and_then(|d| d.as_ref().as_ref()) + { + let (tx, rx) = tokio::sync::mpsc::channel::>(30); + + let pipe_path = + cap_utils::create_channel_named_pipe(rx, audio_dir.path().join("audio.pipe")); + + ffmpeg.add_input(cap_ffmpeg_cli::FFmpegRawAudioInput { + input: pipe_path, + sample_format: "f64le".to_string(), + sample_rate: audio_data.info.sample_rate, + channels: audio_data.info.channels as u16, + }); + + let buffer = AudioFrameBuffer::new(audio_data.clone()); + Some(AudioRender { + buffer, + pipe_tx: tx, + }) + } else { + None + }; let video_tx = { let (tx, rx) = tokio::sync::mpsc::channel::>(30);