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
169 changes: 159 additions & 10 deletions apps/desktop-gpui/src/editor_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,7 @@ pub struct Desired {
pub seek: Option<u32>,
pub seek_gen: u64,
pub resolution: XY<u32>,
pub rate: f64,
}

impl Default for Desired {
Expand All @@ -1005,10 +1006,17 @@ impl Default for Desired {
seek: None,
seek_gen: 0,
resolution: default_preview_resolution(),
rate: 1.0,
}
}
}

// A running session plays at the rate it started with, so a new rate needs a
// fresh session even when the UI's pause and play collapsed into one write.
fn rate_needs_restart(applied_playing: bool, applied_rate: f64, want: &Desired) -> bool {
applied_playing && want.playing && want.rate.to_bits() != applied_rate.to_bits()
}

/// The UI half. Cheap to clone, safe to call from the main thread: writing it
/// takes an uncontended `Mutex` and a `try_send` on a one-slot channel.
#[derive(Clone)]
Expand Down Expand Up @@ -1070,6 +1078,10 @@ impl TransportHandle {
self.modify(|desired| desired.resolution = resolution);
}

pub fn set_rate(&self, rate: f64) {
self.modify(|desired| desired.rate = rate);
}

pub fn play_from(&self, frame: u32) {
self.modify(|desired| {
desired.playing = true;
Expand Down Expand Up @@ -1102,6 +1114,7 @@ pub async fn run_transport(instance: Arc<EditorInstance>, driver: TransportDrive
let fps = EDITOR_PREVIEW_FPS;
let mut applied_playing = false;
let mut applied_gen = 0u64;
let mut applied_rate = 1.0f64;
// The engine's own liveness, epoch-guarded inside `EditorInstance` so a
// deliberate stop/start transition can never masquerade as a death.
let mut active_rx = instance.playback_watch();
Expand Down Expand Up @@ -1148,7 +1161,9 @@ pub async fn run_transport(instance: Arc<EditorInstance>, driver: TransportDrive
// instead of paying stop/warmup/audio-attach per tick -- the round
// trip the source pays in `seekPlayheadTo`
// (`Timeline/index.tsx:829-853`).
if applied_playing && !want.playing {
if (applied_playing && !want.playing)
|| rate_needs_restart(applied_playing, applied_rate, &want)
{
applied_playing = false;
let handle = {
let mut state = instance.state.lock().await;
Expand Down Expand Up @@ -1197,6 +1212,8 @@ pub async fn run_transport(instance: Arc<EditorInstance>, driver: TransportDrive
}

if want.playing && !applied_playing {
instance.set_playback_rate(want.rate);
applied_rate = want.rate;
match instance
.start_playback_with_handle(fps, want.resolution, None)
.await
Expand Down Expand Up @@ -1370,6 +1387,7 @@ pub struct EditorWindow {
/// Kept alive for the window's lifetime: dropping the last `Arc` is what
/// tears the decoders down, and `dispose()` on close does it explicitly.
pub(crate) instance: Option<Arc<EditorInstance>>,
playback_rate: f64,
focus: FocusHandle,

// -- Transport ----------------------------------------------------------
Expand Down Expand Up @@ -2081,6 +2099,7 @@ impl EditorWindow {
timeline_view,
frame_layout: None,
instance: None,
playback_rate: 1.0,
focus: cx.focus_handle(),
playing: false,
playhead: 0.0,
Expand Down Expand Up @@ -3253,6 +3272,7 @@ impl EditorWindow {
/// `handlePlayPauseClick` (`Player.tsx:212-233`), verbatim: at the end,
/// restart from 0; playing, stop; otherwise seek to the playhead and go.
pub fn toggle_play(&mut self, _window: &mut Window, cx: &mut Context<Self>) {
self.set_playback_rate(1.0);
if self.preparing_transport_active() {
if self.preparing_controls_ready() {
let seek = self.is_at_end().then_some(0.0);
Expand Down Expand Up @@ -3283,6 +3303,63 @@ impl EditorWindow {
}
}

fn set_playback_rate(&mut self, rate: f64) {
self.playback_rate = rate;
if let Some(transport) = &self.transport {
transport.set_rate(rate);
}
}

fn shuttle(&mut self, rate: f64, window: &mut Window, cx: &mut Context<Self>) {
if self.is_at_end() || self.preparing_transport_active() {
return;
}
self.set_playback_rate(rate);
let from = self.playhead;
if self.playing {
self.stop_playback(cx);
}
self.start_playback(from, cx);
Comment on lines +3319 to +3322

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Speed changes may not apply

During playback, shuttle sends pause and play back to back. TransportHandle keeps only the latest desired state, so these can collapse into one request with playing = true. run_transport then calls seek_playback instead of starting a new session. Playback keeps its old speed while the badge shows the new one.

Send an explicit restart request, or include the rate in the desired state and restart when it changes.

Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/desktop-gpui/src/editor_window.rs
Line: 3302-3305

Comment:
**Speed changes may not apply**

During playback, `shuttle` sends pause and play back to back. `TransportHandle` keeps only the latest desired state, so these can collapse into one request with `playing = true`. `run_transport` then calls `seek_playback` instead of starting a new session. Playback keeps its old speed while the badge shows the new one.

Send an explicit restart request, or include the rate in the desired state and restart when it changes.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

window.refresh();
}

fn shuttle_faster(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let current = if self.playing {
self.playback_rate
} else {
0.0
};
let next = SHUTTLE_RATES
.iter()
.copied()
.find(|rate| *rate > current && *rate >= 1.0)
.unwrap_or(SHUTTLE_RATES[SHUTTLE_RATES.len() - 1]);
self.shuttle(next, window, cx);
}

fn shuttle_slower(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let current = if self.playing {
self.playback_rate
} else {
1.0
};
let next = SHUTTLE_RATES
.iter()
.rev()
.copied()
.find(|rate| *rate < current)
.unwrap_or(SHUTTLE_RATES[0]);
self.shuttle(next, window, cx);
}

fn shuttle_stop(&mut self, window: &mut Window, cx: &mut Context<Self>) {
if self.playing {
self.stop_playback(cx);
}
self.set_playback_rate(1.0);
window.refresh();
}

/// `seekPlayheadTo` (`Timeline/index.tsx:829-853`), minus the source's
/// stop/seek/restart round trip: the driver live-seeks the running engine
/// (`PlaybackHandle::seek`), so a scrub during playback holds the last
Expand Down Expand Up @@ -3770,6 +3847,18 @@ impl EditorWindow {
cx.stop_propagation();
self.split_at_playhead(window, cx);
}
"j" => {
cx.stop_propagation();
self.shuttle_slower(window, cx);
}
"k" => {
cx.stop_propagation();
self.shuttle_stop(window, cx);
}
"l" => {
cx.stop_propagation();
self.shuttle_faster(window, cx);
}
"escape" => {
cx.stop_propagation();
if self.lane_reorder.is_some() {
Expand Down Expand Up @@ -10659,6 +10748,19 @@ impl EditorWindow {
.flex_none()
.text_size(px(13.))
.when(narrow, |clock| clock.text_size(px(12.)))
.children(
(self.playing && (self.playback_rate - 1.0).abs() > 1e-3).then(|| {
div()
.mr(px(6.))
.px(px(5.))
.rounded(px(4.))
.bg(Hsla::from(editor.accent))
.text_color(Hsla::from(editor.card))
.text_size(px(11.))
.font_weight(FontWeight::SEMIBOLD)
.child(SharedString::from(shuttle_rate_label(self.playback_rate)))
}),
)
.child(
div()
.font_weight(FontWeight::MEDIUM)
Expand Down Expand Up @@ -10881,6 +10983,7 @@ impl EditorWindow {
self.playing,
self.playhead_epoch_live,
self.last_playhead_redraw.elapsed().as_secs_f64(),
self.playback_rate,
);
if ahead > 0.0 {
view.playhead = (view.playhead + ahead).min(self.total_duration());
Expand Down Expand Up @@ -11794,11 +11897,26 @@ const MAX_PLAYHEAD_EXTRAPOLATION: f64 = 0.25;
/// back: the "playhead jumps on play, then goes back" glitch. So: no epoch
/// sample yet, no extrapolation -- the line holds still, which is exactly
/// what the engine is doing.
fn playhead_extrapolation(playing: bool, epoch_has_sample: bool, since_last_sample: f64) -> f64 {
fn playhead_extrapolation(
playing: bool,
epoch_has_sample: bool,
since_last_sample: f64,
rate: f64,
) -> f64 {
if !playing || !epoch_has_sample {
return 0.0;
}
since_last_sample.clamp(0.0, MAX_PLAYHEAD_EXTRAPOLATION)
let rate = if rate.is_finite() && rate > 0.0 {
rate
} else {
1.0
};
since_last_sample.clamp(0.0, MAX_PLAYHEAD_EXTRAPOLATION) * rate
}

fn shuttle_rate_label(rate: f64) -> String {
let text = format!("{rate:.2}");
format!("{}x", text.trim_end_matches('0').trim_end_matches('.'))
}

fn dismiss_indexed_sidebar_menu(menu: &mut Option<crate::editor_tabs::OpenMenu>) {
Expand Down Expand Up @@ -11829,6 +11947,8 @@ fn dismiss_indexed_sidebar_menu(menu: &mut Option<crate::editor_tabs::OpenMenu>)
}
}

const SHUTTLE_RATES: [f64; 6] = [0.5, 1.0, 1.5, 2.0, 3.0, 4.0];

fn is_playback_shortcut(
keystroke: &gpui::Keystroke,
text_input_focused: bool,
Expand Down Expand Up @@ -14489,24 +14609,53 @@ mod tests {
#[test]
fn playhead_never_extrapolates_before_the_epochs_first_sample() {
// Freshly pressed play, stale anchor: hold the line still.
assert_eq!(playhead_extrapolation(true, false, 0.206), 0.0);
assert_eq!(playhead_extrapolation(true, false, 1.081), 0.0);
assert_eq!(playhead_extrapolation(true, false, 0.206, 1.0), 0.0);
assert_eq!(playhead_extrapolation(true, false, 1.081, 1.0), 0.0);
// Paused, no matter what the anchor says.
assert_eq!(playhead_extrapolation(false, true, 0.5), 0.0);
assert_eq!(playhead_extrapolation(false, false, 0.5), 0.0);
assert_eq!(playhead_extrapolation(false, true, 0.5, 1.0), 0.0);
assert_eq!(playhead_extrapolation(false, false, 0.5, 1.0), 0.0);
}

#[test]
fn a_shuttle_speed_change_restarts_even_when_pause_and_play_collapse() {
let (handle, driver, _stopped) = transport();
handle.play_from(60);
handle.set_rate(2.0);
handle.pause();
handle.play_from(90);
let want = *driver.desired.lock().unwrap();
assert!(want.playing);
assert_eq!(want.rate, 2.0);
assert!(rate_needs_restart(true, 1.0, &want));
assert!(!rate_needs_restart(true, 2.0, &want));
assert!(!rate_needs_restart(false, 1.0, &want));
}

#[test]
fn the_speed_badge_reads_like_the_shuttle_rates() {
assert_eq!(shuttle_rate_label(2.0), "2x");
assert_eq!(shuttle_rate_label(0.5), "0.5x");
assert_eq!(shuttle_rate_label(0.25), "0.25x");
assert_eq!(shuttle_rate_label(4.0), "4x");
}

/// Once the epoch is live the line glides by the wall clock between
/// samples, capped so a render stall cannot run it far ahead.
#[test]
fn playhead_extrapolation_tracks_the_wall_clock_and_caps() {
assert_eq!(playhead_extrapolation(true, true, 0.016), 0.016);
assert_eq!(playhead_extrapolation(true, true, 0.016, 1.0), 0.016);
assert_eq!(
playhead_extrapolation(true, true, 3.0),
playhead_extrapolation(true, true, 3.0, 1.0),
MAX_PLAYHEAD_EXTRAPOLATION
);
assert_eq!(playhead_extrapolation(true, true, 0.016, 2.0), 0.032);
assert_eq!(playhead_extrapolation(true, true, 0.016, 0.5), 0.008);
assert_eq!(
playhead_extrapolation(true, true, 3.0, 4.0),
MAX_PLAYHEAD_EXTRAPOLATION * 4.0
);
// A clock hiccup must not pull the playhead backwards.
assert_eq!(playhead_extrapolation(true, true, -0.01), 0.0);
assert_eq!(playhead_extrapolation(true, true, -0.01, 1.0), 0.0);
}

/// A paused seek stores exactly the time its engine echo carries, so the
Expand Down
1 change: 1 addition & 0 deletions crates/editor/examples/audio-output-idle-benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ fn spec(tone: &Arc<AudioData>) -> PlaySpec {
duration_secs: CLIP_SECS,
start_playhead_secs: 0.0,
playhead_rx,
playback_rate: 1.0,
}
}

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 @@ -643,6 +643,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
Loading
Loading