Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review: clamp keyboard height, de-churn ResizeObserver, keep pre-roll in the clock

[._.]/ Adam Eivy283087772874
diff --git a/client/src/components/songs/PianoRoll.jsx b/client/src/components/songs/PianoRoll.jsx
index 3f61d58828..504cf1ff1b 100644
--- a/client/src/components/songs/PianoRoll.jsx
+++ b/client/src/components/songs/PianoRoll.jsx
@@ -67,6 +67,10 @@ export default function PianoRoll({ parts, tempo, getPosition, playing, height =
// hands a fresh closure each render; the rAF loop should not restart for that).
const getPositionRef = useRef(getPosition);
getPositionRef.current = getPosition;
+ // Read inside draw() (a ref, not a dep) so a key only lights while audio is
+ // actually sounding — paused/idle freezes the bars but presses no keys.
+ const playingRef = useRef(playing);
+ playingRef.current = playing;
// Parse + schedule every part once per parts/tempo change → flat note list.
const { notes, range } = useMemo(() => {
@@ -88,8 +92,15 @@ export default function PianoRoll({ parts, tempo, getPosition, playing, height =
const ctx = canvas.getContext('2d');
if (!ctx) return;
- const pos = Math.max(0, getPositionRef.current?.() ?? 0);
- const hitLineY = height - KEYBOARD_HEIGHT;
+ // Keep the negative LEAD-in pre-roll intact (only guard non-finite) so a
+ // falling note reaches the hit line exactly when its audio starts.
+ const rawPos = getPositionRef.current?.() ?? 0;
+ const pos = Number.isFinite(rawPos) ? rawPos : 0;
+ const isPlaying = playingRef.current;
+ // Clamp the keyboard band so a small `height` degrades gracefully instead of
+ // a negative hitLineY (which would invert the fall direction).
+ const kbH = Math.min(KEYBOARD_HEIGHT, height);
+ const hitLineY = height - kbH;
const pps = hitLineY / VISIBLE_SECONDS; // pixels per second of fall
const layout = buildKeyboardLayout({ lowMidi: range.lowMidi, highMidi: range.highMidi, width });
const keyByMidi = new Map(layout.keys.map((k) => [k.midi, k]));
@@ -117,7 +128,7 @@ export default function PianoRoll({ parts, tempo, getPosition, playing, height =
const h = Math.max(2, n.durSec * pps);
const bottom = hitLineY - (n.startSec - pos) * pps;
const top = bottom - h;
- if (n.startSec <= pos && pos < n.startSec + n.durSec) active.set(n.midi, n.color);
+ if (isPlaying && n.startSec <= pos && pos < n.startSec + n.durSec) active.set(n.midi, n.color);
if (top >= hitLineY || bottom <= 0) return; // outside the visible fall window
const pad = key.isBlack ? 1 : 1.5;
roundRect(ctx, key.x + pad, top, Math.max(2, key.w - pad * 2), h, NOTE_RADIUS);
@@ -134,20 +145,20 @@ export default function PianoRoll({ parts, tempo, getPosition, playing, height =
// Keyboard — white keys first, then black overlaid. Active keys glow in the
// sounding note's layer color.
- const blackH = KEYBOARD_HEIGHT * BLACK_KEY_HEIGHT_RATIO;
+ const blackH = kbH * BLACK_KEY_HEIGHT_RATIO;
layout.keys.filter((k) => !k.isBlack).forEach((k) => {
const lit = active.get(k.midi);
ctx.fillStyle = lit || WHITE_KEY_FILL;
- ctx.fillRect(k.x, hitLineY, k.w, KEYBOARD_HEIGHT);
+ ctx.fillRect(k.x, hitLineY, k.w, kbH);
ctx.strokeStyle = WHITE_KEY_EDGE;
ctx.lineWidth = 1;
- ctx.strokeRect(k.x + 0.5, hitLineY + 0.5, k.w - 1, KEYBOARD_HEIGHT - 1);
+ ctx.strokeRect(k.x + 0.5, hitLineY + 0.5, k.w - 1, kbH - 1);
// Octave label on each C.
if (k.midi % 12 === 0 && k.w > 14) {
ctx.fillStyle = lit ? '#0c0c0e' : '#71717a';
ctx.font = '9px ui-sans-serif, system-ui, sans-serif';
ctx.textAlign = 'center';
- ctx.fillText(midiNoteName(k.midi), k.x + k.w / 2, hitLineY + KEYBOARD_HEIGHT - 5);
+ ctx.fillText(midiNoteName(k.midi), k.x + k.w / 2, hitLineY + kbH - 5);
}
});
layout.keys.filter((k) => k.isBlack).forEach((k) => {
@@ -161,6 +172,12 @@ export default function PianoRoll({ parts, tempo, getPosition, playing, height =
});
}, [notes, range, height]);
+ // Latest draw without making it an observer dependency — keeps the
+ // ResizeObserver created once per height (not torn down on every notes/tempo
+ // change, which only needs a redraw, not a re-observe).
+ const drawRef = useRef(draw);
+ drawRef.current = draw;
+
// Size the canvas to the container (devicePixelRatio-aware) and redraw.
useEffect(() => {
const el = wrapRef.current;
@@ -178,13 +195,13 @@ export default function PianoRoll({ parts, tempo, getPosition, playing, height =
canvas.style.height = `${height}px`;
const ctx = canvas.getContext('2d');
if (ctx) ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
- draw();
+ drawRef.current();
};
resize();
const ro = new ResizeObserver(resize);
ro.observe(el);
return () => ro.disconnect();
- }, [height, draw]);
+ }, [height]);
// rAF clock: animate the fall while playing, otherwise draw a single frame.
useEffect(() => {
diff --git a/client/src/components/songs/PianoRoll.test.jsx b/client/src/components/songs/PianoRoll.test.jsx
index d694cb62ba..b253bfeb22 100644
--- a/client/src/components/songs/PianoRoll.test.jsx
+++ b/client/src/components/songs/PianoRoll.test.jsx
@@ -62,6 +62,24 @@ describe('<PianoRoll>', () => {
expect(getPosition).toHaveBeenCalled();
});
+ it('starts the rAF loop while playing and cancels it on unmount', () => {
+ const raf = vi.fn(() => 1); // return a handle; do not invoke the loop (no recursion)
+ const caf = vi.fn();
+ vi.stubGlobal('requestAnimationFrame', raf);
+ vi.stubGlobal('cancelAnimationFrame', caf);
+ const { unmount } = render(
+ <PianoRoll
+ parts={[{ id: 'm', label: 'Melody', color: '#3b82f6', score: 'time: 4/4\ntempo: 120\n| C4q |' }]}
+ tempo={120}
+ getPosition={() => 0}
+ playing
+ />,
+ );
+ expect(raf).toHaveBeenCalled(); // animation loop scheduled while playing
+ unmount();
+ expect(caf).toHaveBeenCalledWith(1); // and torn down on unmount (no leak)
+ });
+
it('renders the default keyboard without crashing when there are no parts', () => {
expect(() => render(
<PianoRoll parts={[]} tempo={120} getPosition={() => 0} playing={false} />,
diff --git a/client/src/lib/scorePlayback.js b/client/src/lib/scorePlayback.js
index 1aa5457387..38a506ce4e 100644
--- a/client/src/lib/scorePlayback.js
+++ b/client/src/lib/scorePlayback.js
@@ -468,11 +468,13 @@ export const createMultiScorePlayer = (parts, options = {}) => {
};
// Current playback head in score-seconds, for a continuous visualizer (the
- // piano-roll). While playing it's the live audio clock (clamped to the score
- // length); paused/stopped it's the remembered resume offset. Note: during the
- // LEAD-in this can read slightly below 0 — callers clamp for display.
+ // piano-roll). While playing it's the live audio clock (capped at the score
+ // length); paused/stopped it's the remembered resume offset. During the LEAD-in
+ // before beat 0 sounds this is NEGATIVE (down to −LEAD) — intentionally NOT
+ // clamped to 0, so the visualizer keeps the first note above the hit line until
+ // its oscillator actually starts rather than lighting the key early.
const position = () => (playing
- ? Math.min(Math.max(0, ctx().currentTime - startTime), totalSec)
+ ? Math.min(ctx().currentTime - startTime, totalSec)
: offsetSec);
return {
diff --git a/client/src/lib/scorePlayback.test.js b/client/src/lib/scorePlayback.test.js
index 80bd6548fe..c9657ce4c4 100644
--- a/client/src/lib/scorePlayback.test.js
+++ b/client/src/lib/scorePlayback.test.js
@@ -261,6 +261,9 @@ describe('createMultiScorePlayer', () => {
expect(player.duration()).toBeCloseTo(2.0, 6); // 4 quarters @120 = the longest voice
expect(player.position()).toBe(0); // idle → resume offset 0
await player.play();
+ // During the LEAD-in (before beat 0 sounds) the head is NEGATIVE, not clamped
+ // to 0 — so the piano roll keeps the first note above the hit line until audio.
+ expect(player.position()).toBeLessThan(0);
audio.now = 1.0; // advance the fake audio clock
expect(player.position()).toBeGreaterThan(0.5);
expect(player.position()).toBeLessThanOrEqual(player.duration());