Skip to content

Commit 81d53b6

Browse files
committed
fix(studio): player UX — keyboard timeline, honest waveform, keyframe menu actions
1 parent 97e5054 commit 81d53b6

17 files changed

Lines changed: 464 additions & 141 deletions

‎packages/studio/src/player/components/AudioWaveform.tsx‎

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,17 @@ function extractPeaks(channelData: Float32Array, barCount: number): number[] {
4141
return peaks.map((p) => p / maxPeak);
4242
}
4343

44-
/** Deterministic fake waveform as fallback (matches demo app). */
45-
function fakePeaks(url: string, count: number): number[] {
46-
let seed = 0;
47-
for (let i = 0; i < url.length; i++) seed = ((seed << 5) - seed + url.charCodeAt(i)) | 0;
48-
seed = Math.abs(seed) || 42;
49-
const rand = () => {
50-
seed = (seed * 16807) % 2147483647;
51-
return (seed & 0x7fffffff) / 2147483647;
52-
};
53-
const peaks: number[] = [];
54-
for (let i = 0; i < count; i++) {
55-
const t = i / count;
56-
const envelope = 0.3 + 0.3 * Math.sin(t * Math.PI * 3.2) + 0.2 * Math.sin(t * Math.PI * 7.1);
57-
peaks.push(Math.max(0.05, Math.min(1, envelope * (0.4 + 0.6 * rand()))));
58-
}
59-
return peaks;
60-
}
61-
6244
// Module-level cache so decoded audio persists across re-renders and re-mounts
6345
const peaksCache = new Map<string, number[]>();
64-
const decodeInFlight = new Map<string, Promise<number[]>>();
46+
const decodeInFlight = new Map<string, Promise<number[] | null>>();
47+
// URLs whose fetch/decode failed — render the degraded state instead of
48+
// refetching (and never fabricate plausible-looking peaks the user might
49+
// trim or beat-align against).
50+
const decodeFailed = new Set<string>();
6551

6652
/**
6753
* Audio waveform rendered from real PCM data via Web Audio API.
68-
* Falls back to a deterministic fake pattern if decoding fails.
54+
* Shows an explicit "waveform unavailable" degraded state if decoding fails.
6955
* Bars grow from bottom to top, rendered as CSS divs for zoom resilience.
7056
*/
7157
export const AudioWaveform = memo(function AudioWaveform({
@@ -81,9 +67,10 @@ export const AudioWaveform = memo(function AudioWaveform({
8167
const roRef = useRef<ResizeObserver | null>(null);
8268
const cacheKey = waveformUrl ?? audioUrl;
8369
const [peaks, setPeaks] = useState<number[] | null>(peaksCache.get(cacheKey) ?? null);
70+
const [decodeError, setDecodeError] = useState(() => decodeFailed.has(cacheKey));
8471

8572
useEffect(() => {
86-
if (peaks || !cacheKey) return;
73+
if (peaks || decodeError || !cacheKey) return;
8774

8875
let cancelled = false;
8976

@@ -105,23 +92,28 @@ export const AudioWaveform = memo(function AudioWaveform({
10592
})
10693
.then((decoded) => extractPeaks(decoded.getChannelData(0), 4000))
10794
)
108-
.catch(() => fakePeaks(cacheKey, 4000))
109-
.then((p) => {
95+
.then((p: number[]) => {
11096
peaksCache.set(cacheKey, p);
111-
return p;
97+
return p as number[] | null;
98+
})
99+
.catch(() => {
100+
decodeFailed.add(cacheKey);
101+
return null;
112102
})
113103
.finally(() => decodeInFlight.delete(cacheKey));
114104

115105
decodeInFlight.set(cacheKey, promise);
116106
}
117107

118108
promise.then((p) => {
119-
if (!cancelled) setPeaks(p);
109+
if (cancelled) return;
110+
if (p) setPeaks(p);
111+
else setDecodeError(true);
120112
});
121113
return () => {
122114
cancelled = true;
123115
};
124-
}, [audioUrl, waveformUrl, cacheKey, peaks]);
116+
}, [audioUrl, waveformUrl, cacheKey, peaks, decodeError]);
125117

126118
// Draw bars into the container using innerHTML (fast, zoom-resilient)
127119
const draw = useCallback(() => {
@@ -185,16 +177,37 @@ export const AudioWaveform = memo(function AudioWaveform({
185177
<div ref={setContainerRef} className="absolute inset-0 overflow-hidden">
186178
<div ref={barsRef} className="absolute left-0 right-0 bottom-0" style={{ top: 16 }} />
187179
{/* Shimmer while decoding */}
188-
{!peaks && (
180+
{!peaks && !decodeError && (
189181
<div
190-
className="absolute left-0 right-0 bottom-0 animate-pulse"
182+
className="absolute left-0 right-0 bottom-0 animate-pulse motion-reduce:animate-none"
191183
style={{
192184
top: 16,
193185
background:
194186
"linear-gradient(90deg, rgba(255,255,255,0.02) 0%, rgba(255,255,255,0.05) 50%, rgba(255,255,255,0.02) 100%)",
195187
}}
196188
/>
197189
)}
190+
{/* Degraded state — decode failed; render an explicit flat placeholder
191+
instead of fabricated peaks the user might edit against. */}
192+
{decodeError && (
193+
<div
194+
className="absolute left-0 right-0 flex items-center justify-center gap-1.5"
195+
style={{ top: 16, bottom: 0 }}
196+
>
197+
<div
198+
className="absolute left-0 right-0"
199+
style={{
200+
bottom: "20%",
201+
height: 2,
202+
background:
203+
"repeating-linear-gradient(90deg, rgba(75,163,210,0.35) 0 2px, transparent 2px 5px)",
204+
}}
205+
/>
206+
<span className="relative text-[8px] text-neutral-500 bg-black/50 px-1 rounded">
207+
waveform unavailable
208+
</span>
209+
</div>
210+
)}
198211
<div className="absolute top-0 left-0 right-0 px-1.5 py-0.5 z-10">
199212
<span
200213
className="text-[9px] font-semibold truncate block leading-tight"

‎packages/studio/src/player/components/BeatStrip.tsx‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { usePlayerStore } from "../store/playerStore";
44
import { CLIP_Y } from "./timelineLayout";
55

66
export const BEAT_BAND_H = 14; // dark band height at top of track
7-
const BEAT_HIT_W = 12; // grab width per beat (px)
7+
const BEAT_HIT_W = 24; // grab width per beat (px) — ≥24px pointer target
88

99
/** Hide both layers when beats are packed tighter than this (px) — too dense to read. */
1010
function beatsTooDense(beatTimes: number[], pps: number): boolean {
@@ -58,8 +58,9 @@ export const BeatBackgroundLines = memo(function BeatBackgroundLines({
5858

5959
/**
6060
* Green beat dots on the music track's row. Drag a dot to move its beat,
61-
* double-click to delete; both scrub the audio. Dot size/brightness scale with
62-
* beat loudness (gamma-curved for contrast).
61+
* ⌥-click to delete (kept off double-click so a stuttered drag can't destroy
62+
* a beat); both scrub the audio. Dot size/brightness scale with beat loudness
63+
* (gamma-curved for contrast). Deletes remain undoable via ⌘Z.
6364
*/
6465
export const BeatStrip = memo(function BeatStrip({
6566
beatTimes,
@@ -93,7 +94,7 @@ export const BeatStrip = memo(function BeatStrip({
9394
<div
9495
key={`${t}-${i}`}
9596
className="absolute select-none"
96-
title="Drag to move · double-click to delete"
97+
title="Drag to move · ⌥-click to delete"
9798
draggable={false}
9899
style={{
99100
left: x - BEAT_HIT_W / 2,
@@ -138,13 +139,13 @@ export const BeatStrip = memo(function BeatStrip({
138139
const newTime = Math.max(0, d.origTime + dx / pps);
139140
moveBeatCompositionTime(d.origTime, newTime);
140141
usePlayerStore.getState().requestSeek(newTime); // park scrubber at new beat
142+
} else if (e.altKey) {
143+
// ⌥-click deletes. Deliberately NOT double-click: a stuttered
144+
// drag attempt reads as a double-click and would nuke the beat.
145+
deleteBeatAtCompositionTime(t);
146+
usePlayerStore.getState().requestSeek(Math.max(0, t)); // park scrubber at deleted beat
141147
}
142148
}}
143-
onDoubleClick={(e) => {
144-
e.stopPropagation();
145-
deleteBeatAtCompositionTime(t);
146-
usePlayerStore.getState().requestSeek(Math.max(0, t)); // park scrubber at deleted beat
147-
}}
148149
>
149150
<div
150151
className="absolute"

‎packages/studio/src/player/components/ClipContextMenu.tsx‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { createPortal } from "react-dom";
33
import type { TimelineElement } from "../store/playerStore";
44
import { canSplitElement } from "../../utils/timelineElementSplit";
55
import { useContextMenuDismiss } from "../../hooks/useContextMenuDismiss";
6+
import { useMenuKeyboardNav } from "./menuKeyboardNav";
67

78
interface ClipContextMenuProps {
89
x: number;
@@ -24,6 +25,7 @@ export const ClipContextMenu = memo(function ClipContextMenu({
2425
onDelete,
2526
}: ClipContextMenuProps) {
2627
const menuRef = useContextMenuDismiss(onClose);
28+
useMenuKeyboardNav(menuRef);
2729

2830
const menuWidth = 200;
2931
const menuHeight = 80;
@@ -44,14 +46,17 @@ export const ClipContextMenu = memo(function ClipContextMenu({
4446
return createPortal(
4547
<div
4648
ref={menuRef}
49+
role="menu"
50+
aria-label="Clip actions"
4751
className="fixed z-50 bg-neutral-900 border border-neutral-700 rounded-md shadow-lg py-1 min-w-[180px]"
4852
style={{ left: adjustedX, top: adjustedY }}
4953
>
5054
{splitLabel && (
5155
<>
5256
<button
5357
type="button"
54-
className={`w-full flex items-center justify-between px-3 py-1.5 text-xs text-left ${
58+
role="menuitem"
59+
className={`w-full flex items-center justify-between px-3 py-1.5 text-xs text-left outline-none focus-visible:bg-neutral-800 ${
5560
canSplit
5661
? "text-neutral-300 hover:bg-neutral-800 cursor-pointer"
5762
: "text-neutral-600 cursor-not-allowed"
@@ -73,7 +78,8 @@ export const ClipContextMenu = memo(function ClipContextMenu({
7378

7479
<button
7580
type="button"
76-
className="w-full flex items-center justify-between px-3 py-1.5 text-xs text-red-400 hover:bg-neutral-800 cursor-pointer text-left"
81+
role="menuitem"
82+
className="w-full flex items-center justify-between px-3 py-1.5 text-xs text-red-400 hover:bg-neutral-800 focus-visible:bg-neutral-800 outline-none cursor-pointer text-left"
7783
onClick={() => {
7884
onDelete(element);
7985
onClose();

‎packages/studio/src/player/components/EditModal.tsx‎

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,44 @@ interface EditPopoverProps {
1313
onClose: () => void;
1414
}
1515

16+
function draftKey(start: number, end: number): string {
17+
return `hf-edit-draft:${start.toFixed(2)}:${end.toFixed(2)}`;
18+
}
19+
20+
function readDraft(key: string): string {
21+
try {
22+
return sessionStorage.getItem(key) ?? "";
23+
} catch {
24+
return "";
25+
}
26+
}
27+
28+
function writeDraft(key: string, value: string): void {
29+
try {
30+
if (value) sessionStorage.setItem(key, value);
31+
else sessionStorage.removeItem(key);
32+
} catch {
33+
/* storage unavailable — draft persistence degrades gracefully */
34+
}
35+
}
36+
1637
export function EditPopover({ rangeStart, rangeEnd, anchorX, anchorY, onClose }: EditPopoverProps) {
1738
const elements = usePlayerStore((s) => s.elements);
18-
const [prompt, setPrompt] = useState("");
39+
const start = Math.min(rangeStart, rangeEnd);
40+
const end = Math.max(rangeStart, rangeEnd);
41+
// Persist the typed prompt per range so Escape/outside-click doesn't destroy it.
42+
const storageKey = draftKey(start, end);
43+
const [prompt, setPromptState] = useState(() => readDraft(storageKey));
44+
const setPrompt = (value: string) => {
45+
setPromptState(value);
46+
writeDraft(storageKey, value);
47+
};
1948
const [copiedAgentPrompt, setCopiedAgentPrompt] = useState(false);
2049
const [copiedPromptOnly, setCopiedPromptOnly] = useState(false);
50+
const [copyError, setCopyError] = useState(false);
2151
const popoverRef = useRef<HTMLDivElement>(null);
2252
const textareaRef = useRef<HTMLTextAreaElement>(null);
2353

24-
const start = Math.min(rangeStart, rangeEnd);
25-
const end = Math.max(rangeStart, rangeEnd);
26-
2754
const elementsInRange = useMemo(() => {
2855
return elements.filter((el) => {
2956
const elEnd = el.start + el.duration;
@@ -64,19 +91,28 @@ export function EditPopover({ rangeStart, rangeEnd, anchorX, anchorY, onClose }:
6491

6592
const handleCopy = useCallback(async () => {
6693
const copied = await copyTextToClipboard(buildClipboardText());
67-
if (!copied) return;
94+
if (!copied) {
95+
setCopyError(true);
96+
return;
97+
}
98+
setCopyError(false);
99+
writeDraft(storageKey, "");
68100
setCopiedAgentPrompt(true);
69101
setTimeout(() => {
70102
setCopiedAgentPrompt(false);
71103
onClose();
72104
}, 800);
73-
}, [buildClipboardText, onClose]);
105+
}, [buildClipboardText, onClose, storageKey]);
74106

75107
const handleCopyPrompt = useCallback(async () => {
76108
const promptText = buildPromptCopyText(prompt);
77109
if (!promptText) return;
78110
const copied = await copyTextToClipboard(promptText);
79-
if (!copied) return;
111+
if (!copied) {
112+
setCopyError(true);
113+
return;
114+
}
115+
setCopyError(false);
80116
setCopiedPromptOnly(true);
81117
setTimeout(() => {
82118
setCopiedPromptOnly(false);
@@ -137,6 +173,11 @@ export function EditPopover({ rangeStart, rangeEnd, anchorX, anchorY, onClose }:
137173
</div>
138174

139175
{/* Action */}
176+
{copyError && (
177+
<p className="px-3 pb-2 text-[10px] text-red-400" role="alert">
178+
Copy failed — check clipboard permissions and try again.
179+
</p>
180+
)}
140181
<div className="grid grid-cols-2 gap-2 px-3 pb-3">
141182
<button
142183
onClick={handleCopyPrompt}

0 commit comments

Comments
 (0)