Skip to content

The VS Code of video editing, built for agents.

TypeScript

Add shuttle and stop playback shortcuts

konstantin-paulus1b5962b33eac
diff --git a/apps/web/src/engine/input/shortcuts.ts b/apps/web/src/engine/input/shortcuts.ts
index eb8da1c9..914e8417 100644
--- a/apps/web/src/engine/input/shortcuts.ts
+++ b/apps/web/src/engine/input/shortcuts.ts
@@ -25,6 +25,8 @@ import {
isGroupLike,
isSequence,
setPlayhead,
+ shuttlePlayback,
+ stopPlayback,
store,
togglePlayback,
} from '@diffusionstudio/runtime';
@@ -185,6 +187,20 @@ function toggleActivePlayback(world: World): void {
if (scene) togglePlayback(world, scene);
}
+/**
+ * J and L, the shuttle pair: each press runs the active scene faster the way
+ * it is already going, or turns it around at 1x. K parks it.
+ */
+const shuttle = (direction: 1 | -1) => (world: World): void => {
+ const scene = getActiveEntity(world);
+ if (scene) shuttlePlayback(world, scene, direction);
+};
+
+function stopActivePlayback(world: World): void {
+ const scene = getActiveEntity(world);
+ if (scene) stopPlayback(world, scene);
+}
+
function onSpacePressed(world: World): void {
// The hand waits out the delay wherever the pointer is; what the pointer
// decides is only when playback gets its toggle. Over the stage the press
@@ -413,6 +429,9 @@ const PRESSED_SHORTCUTS: readonly Shortcut[] = [
{ keys: ['d', '!mod'], action: seekFrames(1) },
{ keys: ['w', '!mod'], action: seekSeconds(1) },
{ keys: ['s', '!mod'], action: seekSeconds(-1) },
+ { keys: ['j', '!mod'], action: shuttle(-1) },
+ { keys: ['k', '!mod'], action: stopActivePlayback },
+ { keys: ['l', '!mod'], action: shuttle(1) },
{ keys: [']', '!mod'], action: restack('front') },
{ keys: ['[', '!mod'], action: restack('back') },
{ keys: ['\\', '!mod'], action: selectParents },
diff --git a/packages/runtime/src/actions/playback.ts b/packages/runtime/src/actions/playback.ts
index d1be771f..f443bde3 100644
--- a/packages/runtime/src/actions/playback.ts
+++ b/packages/runtime/src/actions/playback.ts
@@ -74,29 +74,73 @@ export function setPlayhead(world: World, scene: Entity, frame: number): void {
}
/**
- * Starts or stops `scene` at 1x. The audio context is gated until it has seen
- * a gesture and a play is one, so it is resumed here rather than at every
- * call site that can start playback.
+ * Moves the playhead off the end it is parked against, in the direction play
+ * is about to take: parked there it would stop on the first advance, so a
+ * finished scene plays again from the top of its window and one sitting at
+ * the top runs backwards from its end.
*/
+function rewindIfParked(world: World, scene: Entity, direction: 1 | -1): void {
+ const computed = store(world, Computed);
+ const eid = scene.id();
+ const workarea = scene.has(Workarea) ? scene.get(Workarea) : undefined;
+ const start = workarea?.start ?? 0;
+ const end = workarea?.end ?? computed.duration[eid] ?? 0;
+ if (end <= 0) return;
+
+ const frame = computed.localTime[eid] ?? 0;
+
+ if (direction === 1 && frame >= end) setPlayhead(world, scene, start);
+ if (direction === -1 && frame <= start) setPlayhead(world, scene, end);
+}
+
+/**
+ * The audio context is gated until it has seen a gesture, and starting
+ * playback is one, so it is resumed wherever play begins rather than at every
+ * call site that can start it.
+ */
+function resumeAudioContext(world: World): void {
+ const context = world.get(AudioEngine)?.context;
+ if (context instanceof AudioContext) void context.resume();
+}
+
+/** Starts or stops `scene` at 1x — what the play toggle and space do. */
export function togglePlayback(world: World, scene: Entity): void {
const playback = scene.get(Playback);
if (!playback) return;
- // Starting parked at the end would stop on the first advance, so a
- // finished scene plays again from the top (of its workarea, if any).
- if (!playback.playing) {
- const computed = store(world, Computed);
- const eid = scene.id();
- const workarea = scene.has(Workarea) ? scene.get(Workarea) : undefined;
- const end = workarea?.end ?? computed.duration[eid] ?? 0;
-
- if (end > 0 && (computed.localTime[eid] ?? 0) >= end) {
- setPlayhead(world, scene, workarea?.start ?? 0);
- }
- }
+ if (!playback.playing) rewindIfParked(world, scene, 1);
scene.set(Playback, { playing: !playback.playing, speed: 1 });
+ resumeAudioContext(world);
+}
- const context = world.get(AudioEngine)?.context;
- if (context instanceof AudioContext) void context.resume();
+/** Stops `scene` wherever it is, at whatever speed it was going — K. */
+export function stopPlayback(_world: World, scene: Entity): void {
+ if (!scene.has(Playback)) return;
+
+ scene.set(Playback, { playing: false, speed: 1 });
+}
+
+/** The rungs J and L climb, as multiples of real time. */
+export const SHUTTLE_SPEEDS: readonly number[] = [1, 2, 4, 8, 16];
+
+/**
+ * Shuttles `scene` in `direction` — L forwards, J back.
+ */
+export function shuttlePlayback(world: World, scene: Entity, direction: 1 | -1): void {
+ const playback = scene.get(Playback);
+ if (!playback) return;
+
+ const speed = playback.playing ? playback.speed : 0;
+
+ // A speed off the ladder (indexOf -1) starts the climb over at its first
+ // rung, which is also where a stop or a turnaround starts.
+ const rung = Math.sign(speed) === direction
+ ? SHUTTLE_SPEEDS[Math.min(SHUTTLE_SPEEDS.indexOf(Math.abs(speed)) + 1, SHUTTLE_SPEEDS.length - 1)]!
+ : SHUTTLE_SPEEDS[0]!;
+
+ rewindIfParked(world, scene, direction);
+
+ scene.set(Playback, { playing: true, speed: direction * rung });
+ resumeAudioContext(world);
}
diff --git a/packages/runtime/src/systems/playback.ts b/packages/runtime/src/systems/playback.ts
index 09b0c3c6..8e963368 100644
--- a/packages/runtime/src/systems/playback.ts
+++ b/packages/runtime/src/systems/playback.ts
@@ -49,6 +49,7 @@ function advancePlayhead(world: World, entity: Entity): void {
const computed = store(world, Computed);
const eid = entity.id();
const fps = world.get(FrameRate)?.value ?? 30;
+ const speed = playback.speed[eid] || 1;
let looped = false;
// Only advance the playhead while playing. The audio play/stop bookkeeping
@@ -58,7 +59,6 @@ function advancePlayhead(world: World, entity: Entity): void {
// and play silently.
if (playback.playing[eid] === true) {
const dt = (world.get(Time)?.delta ?? 0) / 1000;
- const speed = playback.speed[eid] || 1;
const previousTime = computed.localTimeInSeconds[eid]!;
let time = previousTime + dt * speed;
@@ -103,7 +103,8 @@ function advancePlayhead(world: World, entity: Entity): void {
const playing = playback.playing[eid] ?? false;
const wasPlaying = audioPlayback.wasPlaying[eid] ?? false;
- if ((playing && !wasPlaying) || looped) {
+ // Shuttling (J/L at anything but 1x) plays silent
+ if ((playing && !wasPlaying) || looped || (playing && speed !== 1)) {
audioPlayback.contextOffsetInSeconds[eid] = ctx?.currentTime ?? 0;
audioPlayback.timelineOffsetInSeconds[eid] = computed.localTimeInSeconds[eid]!;
}
@@ -184,7 +185,7 @@ function forwardAudioDecoder(world: World, scene: Entity, entity: Entity, audioS
if (!decoder.ready) {
framePromises(world)?.push(initPromise);
- } else if (computed.visibility[eid] === 1 && playback.playing[sid] === true) {
+ } else if (computed.visibility[eid] === 1 && playback.playing[sid] === true && (playback.speed[sid] || 1) === 1) {
const playPromise = decoder.playTo(bus, {
relativeFrom: localFrame / fps,
relativeTo: (localFrame + 15) / fps,