Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ async fn main() {
music: cap_editor::MusicTracks::new(),
audio_output,
telemetry: Some(telemetry),
playback_rate: 1.0,
};

let playback_handle = match playback.start(fps, resolution_base).await {
Expand Down
9 changes: 9 additions & 0 deletions apps/desktop/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4710,6 +4710,14 @@ async fn start_playback(
Ok(())
}

#[tauri::command]
#[specta::specta]
#[instrument(skip(editor_instance))]
async fn set_playback_rate(editor_instance: WindowEditorInstance, rate: f64) -> Result<(), String> {
editor_instance.set_playback_rate(rate);
Ok(())
}

#[tauri::command]
#[specta::specta]
#[instrument(skip(editor_instance))]
Expand Down Expand Up @@ -6824,6 +6832,7 @@ fn specta_builder() -> tauri_specta::Builder {
audio_library::add_audio_library_track,
audio_library::import_audio_track_file,
start_playback,
set_playback_rate,
stop_playback,
commit_editor_preparing_frame,
start_editor_handoff_playback,
Expand Down
81 changes: 79 additions & 2 deletions apps/desktop/src/routes/editor/Player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import { EditorButton, Slider } from "./ui";
import { useEditorShortcuts } from "./useEditorShortcuts";
import { formatTime } from "./utils";

const SHUTTLE_RATES = [0.5, 1, 1.5, 2, 3, 4];

export function PlayerContent(props: { compactness?: number }) {
const {
previewStyle,
Expand Down Expand Up @@ -263,7 +265,70 @@ export function PlayerContent(props: { compactness?: number }) {
}
});

const handlePlayPauseClick = async () => {
const [playbackRate, setPlaybackRate] = createSignal(1);

const applyPlaybackRate = async (rate: number) => {
if (rate === playbackRate()) return;
setPlaybackRate(rate);
await commands.setPlaybackRate(rate);
};

let playbackQueue: Promise<unknown> = Promise.resolve();
const serialized =
<T,>(task: () => Promise<T>) =>
() => {
const run = playbackQueue.then(task);
playbackQueue = run.catch(() => undefined);
return run;
};

const shuttle = async (rate: number) => {
if (isAtEnd()) return;
const pending = requestHandoffPlayback(true);
if (pending && (!(await pending) || rate === 1)) return;
const time = editorState.previewTime ?? editorState.playbackTime;
const frame = Math.max(Math.floor(time * FPS), 0);
try {
await applyPlaybackRate(rate);
if (playbackIntent()) {
await commands.stopPlayback();
setEditorState("playing", false);
}
setEditorState("playbackTime", time);
await commands.seekTo(frame);
await commands.startPlayback(FPS, previewResolutionBase());
Comment thread
YoavLevavi marked this conversation as resolved.
Resolved
setEditorState("playing", true);
setEditorState("previewTime", null);
} catch (error) {
console.error("Error changing playback speed:", error);
setEditorState("playing", false);
}
};

const shuttleFaster = serialized(() => {
const current = playbackIntent() ? playbackRate() : 0;
const next = SHUTTLE_RATES.find((rate) => rate > current && rate >= 1);
return shuttle(next ?? SHUTTLE_RATES[SHUTTLE_RATES.length - 1]);
});

const shuttleSlower = serialized(() => {
const current = playbackIntent() ? playbackRate() : 1;
const next = [...SHUTTLE_RATES].reverse().find((rate) => rate < current);
return shuttle(next ?? SHUTTLE_RATES[0]);
});

const shuttleStop = serialized(async () => {
const pending = requestHandoffPlayback(false);
if (pending) await pending;
else if (playbackIntent()) {
await commands.stopPlayback();
setEditorState("playing", false);
}
await applyPlaybackRate(1);
});

const handlePlayPauseClick = serialized(async () => {
await applyPlaybackRate(1);
const pending = requestHandoffPlayback(
isAtEnd() || !playbackIntent(),
isAtEnd() ? 0 : undefined,
Expand Down Expand Up @@ -292,7 +357,7 @@ export function PlayerContent(props: { compactness?: number }) {
console.error("Error handling play/pause:", error);
setEditorState("playing", false);
}
};
});

if (import.meta.env.DEV) {
createTauriEventListener<boolean>(
Expand Down Expand Up @@ -330,6 +395,13 @@ export function PlayerContent(props: { compactness?: number }) {
editorState.timeline.interactMode === "split" ? "seek" : "split",
),
},
{
combo: "V",
handler: () => setEditorState("timeline", "interactMode", "seek"),
},
{ combo: "L", handler: shuttleFaster },
{ combo: "J", handler: shuttleSlower },
{ combo: "K", handler: shuttleStop },
{
combo: "Mod+=",
handler: () =>
Expand Down Expand Up @@ -463,6 +535,11 @@ export function PlayerContent(props: { compactness?: number }) {
>
<IconCapPrev class="size-3.5" />
</button>
<Show when={playbackIntent() && playbackRate() !== 1}>
<span class="px-1.5 py-0.5 text-[11px] font-medium tabular-nums rounded-md bg-ed-ctl text-ed-text-1">
{playbackRate()}×
</span>
</Show>
<Tooltip kbd={["Space"]} content="Play/Pause video">
<button
type="button"
Expand Down
3 changes: 3 additions & 0 deletions apps/desktop/src/utils/tauri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ async importAudioTrackFile(sourcePath: string) : Promise<ImportedAudioTrack> {
async startPlayback(fps: number, resolutionBase: XY<number>) : Promise<null> {
return await TAURI_INVOKE("start_playback", { fps, resolutionBase });
},
async setPlaybackRate(rate: number) : Promise<null> {
return await TAURI_INVOKE("set_playback_rate", { rate });
},
async stopPlayback() : Promise<null> {
return await TAURI_INVOKE("stop_playback");
},
Expand Down
1 change: 1 addition & 0 deletions crates/editor/examples/editor-playback-benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ async fn main() {
music: cap_editor::MusicTracks::new(),
audio_output: audio_output.clone(),
telemetry: Some(telemetry.clone()),
playback_rate: 1.0,
};

let press_instant = Instant::now();
Expand Down
116 changes: 112 additions & 4 deletions crates/editor/src/audio_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ pub struct PlaySpec {
pub duration_secs: f64,
pub start_playhead_secs: f64,
pub playhead_rx: watch::Receiver<f64>,
/// Source seconds consumed per output second (J/K/L shuttle). Non-1x
/// rates are time-stretched on f32 devices, keeping the original pitch.
pub playback_rate: f64,
}

enum ControlMsg {
Expand Down Expand Up @@ -512,6 +515,11 @@ struct ActiveSource<T: FromSampleBytes> {
refresh_revision: u64,
buffer: ActiveSourceBuffer<T>,
playhead_rx: watch::Receiver<f64>,
playback_rate: f64,
channels: usize,
sample_rate: u32,
rate_scratch: Vec<T>,
stretch: Option<crate::time_stretch::TimeStretch>,
ack: Option<SourceAcknowledgement>,
preparing_request: Option<Arc<PreparingAudioRequest>>,
#[cfg(not(target_os = "windows"))]
Expand Down Expand Up @@ -782,16 +790,39 @@ fn render_source_block<T: FromSampleBytes + cpal::FromSample<f32>>(
}
match &mut source.buffer {
ActiveSourceBuffer::Ordinary(audio) => {
let rate = source.playback_rate;
// Audio the stretcher already pulled from the buffer but has not
// played yet sits between the buffer's read position and the ear.
let pending_secs = source.stretch.as_ref().map_or(0.0, |stretch| {
stretch.pending_source_secs(source.sample_rate)
});
let source_latency_secs = latency_secs * rate;
if source.playhead_rx.has_changed().unwrap_or(false) {
let video_playhead = *source.playhead_rx.borrow_and_update();
let audible_playhead = audio.current_audible_playhead(latency_secs);
let audible_playhead =
audio.current_audible_playhead(source_latency_secs + pending_secs);
let drift = (video_playhead - audible_playhead).abs();

if drift > 0.04 {
audio.set_playhead(video_playhead + latency_secs);
if drift > 0.04 * rate.max(1.0) {
audio.set_playhead(video_playhead + source_latency_secs);
if let Some(stretch) = &mut source.stretch {
stretch.reset();
}
}
}
audio.fill(buffer);
if (rate - 1.0).abs() < 1e-3 {
audio.fill(buffer);
} else if let Some(stretch) = &mut source.stretch {
fill_stretched(audio, buffer, stretch, &mut source.rate_scratch);
} else {
fill_at_rate(
audio,
buffer,
rate,
source.channels,
&mut source.rate_scratch,
);
}
}
ActiveSourceBuffer::Preparing(audio) => {
if audio.fill(buffer, latency_secs) == 0 {
Expand Down Expand Up @@ -819,6 +850,62 @@ fn render_source_block<T: FromSampleBytes + cpal::FromSample<f32>>(
}
}

/// Pitch-preserving shuttle audio via WSOLA. Only built for f32 streams (what
/// macOS output devices use); other formats fall back to `fill_at_rate`.
fn fill_stretched<T: FromSampleBytes + cpal::FromSample<f32>>(
audio: &mut crate::audio::PrerenderedAudioBuffer<T>,
buffer: &mut [T],
stretch: &mut crate::time_stretch::TimeStretch,
scratch: &mut Vec<T>,
) {
while stretch.ready_samples() < buffer.len() {
let shortfall = stretch.input_shortfall();
if shortfall > 0 {
scratch.resize((shortfall + 1024) * stretch.channels(), T::EQUILIBRIUM);
audio.fill(scratch);
let Some(samples) = (scratch as &mut dyn std::any::Any).downcast_mut::<Vec<f32>>()
else {
buffer.fill(T::EQUILIBRIUM);
return;
};
stretch.push_input(samples);
}
stretch.hop();
}
for slot in buffer.iter_mut() {
*slot = <T as cpal::Sample>::from_sample(stretch.pop_ready().unwrap_or(0.0));
}
}

/// Reads `rate` times as many source frames as the block holds and keeps
/// the nearest one per output frame. Crude (no filtering or pitch
/// correction) but cheap enough for the audio callback, and it only runs
/// while shuttling.
fn fill_at_rate<T: FromSampleBytes + cpal::FromSample<f32>>(
audio: &mut crate::audio::PrerenderedAudioBuffer<T>,
buffer: &mut [T],
rate: f64,
channels: usize,
scratch: &mut Vec<T>,
) {
let channels = channels.max(1);
let out_frames = buffer.len() / channels;
if out_frames == 0 {
buffer.fill(T::EQUILIBRIUM);
return;
}
let in_frames = ((out_frames as f64) * rate).round().max(1.0) as usize;
scratch.resize(in_frames * channels, T::EQUILIBRIUM);
audio.fill(scratch);
for frame in 0..out_frames {
let source_frame = ((frame as f64 * rate) as usize).min(in_frames - 1);
let out = frame * channels;
let src = source_frame * channels;
buffer[out..out + channels].copy_from_slice(&scratch[src..src + channels]);
}
buffer[out_frames * channels..].fill(T::EQUILIBRIUM);
}

/// Builds the per-playback source from a play spec and hands it to the
/// output via `install_tx`. `use_device_latency_hint` is false for the
/// headless sink, which models a zero-latency device.
Expand All @@ -844,7 +931,9 @@ fn install_source<T: FromSampleBytes + cpal::FromSample<f32>>(
duration_secs,
start_playhead_secs,
playhead_rx,
playback_rate,
} = *spec;
let output_channels = output_info.channels;

if !(duration_secs.is_finite() && duration_secs > 0.0) {
return Err(format!(
Expand Down Expand Up @@ -930,6 +1019,19 @@ fn install_source<T: FromSampleBytes + cpal::FromSample<f32>>(
refresh_revision,
buffer: ActiveSourceBuffer::Ordinary(buffer),
playhead_rx,
playback_rate: if playback_rate.is_finite() && playback_rate > 0.0 {
playback_rate
} else {
1.0
},
channels: output_channels,
sample_rate: output_info.sample_rate,
rate_scratch: Vec::new(),
stretch: (std::any::TypeId::of::<T>() == std::any::TypeId::of::<f32>()
&& playback_rate.is_finite()
&& playback_rate > 0.0
&& (playback_rate - 1.0).abs() >= 1e-3)
.then(|| crate::time_stretch::TimeStretch::new(output_channels, playback_rate)),
ack: Some(ack),
preparing_request,
#[cfg(not(target_os = "windows"))]
Expand Down Expand Up @@ -994,6 +1096,11 @@ fn install_progressive_source<T: FromSampleBytes + cpal::FromSample<f32>>(
refresh_revision: 0,
buffer: ActiveSourceBuffer::Preparing(buffer),
playhead_rx,
playback_rate: 1.0,
channels: 1,
sample_rate: 48_000,
rate_scratch: Vec::new(),
stretch: None,
ack: Some(SourceAcknowledgement::Preparing(request.clone())),
preparing_request: Some(request.clone()),
#[cfg(not(target_os = "windows"))]
Expand Down Expand Up @@ -1371,6 +1478,7 @@ mod tests {
duration_secs: 2.0,
start_playhead_secs: 0.0,
playhead_rx,
playback_rate: 1.0,
}),
0,
SourceAcknowledgement::Ordinary(ack_tx),
Expand Down
1 change: 1 addition & 0 deletions crates/editor/src/audio_output/native_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ fn audible_spec_with_duration(duration_secs: usize) -> PlaySpec {
duration_secs: duration_secs as f64,
start_playhead_secs: 0.0,
playhead_rx,
playback_rate: 1.0,
}
}

Expand Down
Loading