Skip to content

the ultimate vocal synthesizer - sing, speak, beatbox, and more

Rust

Keys: Z and X move the on-screen keyboard at once (it waited for the next note)

Jameson Hodge82f594f4ec02
diff --git a/src/ui/components/Keys.tsx b/src/ui/components/Keys.tsx
index 12876ed..b053b04 100644
--- a/src/ui/components/Keys.tsx
+++ b/src/ui/components/Keys.tsx
@@ -1,4 +1,4 @@
-import { useEffect, useRef } from "react";
+import { useEffect, useRef, useState } from "react";
import { useStore } from "../store.ts";
import { midiSupported } from "../midi.ts";
import { nearestBody, type BodyMix } from "../body.ts";
@@ -15,8 +15,15 @@ export function Keys() {
const noteOn = useStore((s) => s.noteOn);
const noteOff = useStore((s) => s.noteOff);
const start = useStore((s) => s.start);
+ // the key handler reads the ref (it is set up once); the state redraws the keys on Z and X
const octave = useRef(0);
- const base = BASE(body) + octave.current * 12;
+ const [shownOctave, setShownOctave] = useState(0);
+ const base = BASE(body) + shownOctave * 12;
+ /**
+ * on-screen key held by a pointer → the note it played, released when that pointer lifts
+ * anywhere: Z, X or a body change can redraw the keys under a held finger and drop its button
+ */
+ const byPointer = useRef(new Map<number, number>());
useEffect(() => {
/** pressed key → the note it played (released as that note, whatever the body or octave now) */
@@ -28,8 +35,10 @@ export function Keys() {
if ((target as HTMLInputElement).type !== "range" && (target as HTMLInputElement).type !== "checkbox") return;
}
const k = e.key.toLowerCase();
- if (k === "z") octave.current = Math.max(-2, octave.current - 1);
- if (k === "x") octave.current = Math.min(2, octave.current + 1);
+ if (k === "z" || k === "x") {
+ octave.current = Math.max(-2, Math.min(2, octave.current + (k === "z" ? -1 : 1)));
+ setShownOctave(octave.current);
+ }
if (!(k in KEYMAP) || down.has(k)) return;
const midi = BASE(useStore.getState().body) + octave.current * 12 + KEYMAP[k];
down.set(k, midi);
@@ -48,13 +57,23 @@ export function Keys() {
for (const midi of down.values()) noteOff(midi);
down.clear();
};
+ const onPointerUp = (e: PointerEvent) => {
+ const midi = byPointer.current.get(e.pointerId);
+ if (midi === undefined) return;
+ byPointer.current.delete(e.pointerId);
+ noteOff(midi);
+ };
window.addEventListener("keydown", onDown);
window.addEventListener("keyup", onUp);
window.addEventListener("blur", onBlur);
+ window.addEventListener("pointerup", onPointerUp);
+ window.addEventListener("pointercancel", onPointerUp);
return () => {
window.removeEventListener("keydown", onDown);
window.removeEventListener("keyup", onUp);
window.removeEventListener("blur", onBlur);
+ window.removeEventListener("pointerup", onPointerUp);
+ window.removeEventListener("pointercancel", onPointerUp);
};
}, [noteOn, noteOff, start]);
@@ -63,17 +82,17 @@ export function Keys() {
const whites = notes.filter((m) => !isBlack(m));
const press = (m: number) => (e: React.PointerEvent) => {
(e.target as Element).setPointerCapture(e.pointerId);
+ byPointer.current.set(e.pointerId, m);
void start();
noteOn(m);
};
- const release = (m: number) => () => noteOff(m);
return (
<div className="keys-row">
<MidiButton />
<div className="keys" data-layout="layered" aria-label="Keyboard. Computer keys A to semicolon play notes; Z and X change octave.">
<div className="white-row">
{whites.map((m) => (
- <button key={m} className={`key white${held.includes(m) ? " down" : ""}`} onPointerDown={press(m)} onPointerUp={release(m)} onPointerCancel={release(m)} aria-label={`Note ${m}`} />
+ <button key={m} className={`key white${held.includes(m) ? " down" : ""}`} onPointerDown={press(m)} aria-label={`Note ${m}`} />
))}
</div>
<div className="black-row">
@@ -81,7 +100,7 @@ export function Keys() {
const b = m + 1;
const hasBlack = isBlack(b) && i < whites.length - 1;
return hasBlack ? (
- <button key={b} className={`key black${held.includes(b) ? " down" : ""}`} style={{ left: `calc(${((i + 1) / whites.length) * 100}% - 1.1%)` }} onPointerDown={press(b)} onPointerUp={release(b)} onPointerCancel={release(b)} aria-label={`Note ${b}`} />
+ <button key={b} className={`key black${held.includes(b) ? " down" : ""}`} style={{ left: `calc(${((i + 1) / whites.length) * 100}% - 1.1%)` }} onPointerDown={press(b)} aria-label={`Note ${b}`} />
) : null;
})}
</div>
diff --git a/tools/e2e.ts b/tools/e2e.ts
index 3a77dee..af6845e 100644
--- a/tools/e2e.ts
+++ b/tools/e2e.ts
@@ -53,6 +53,30 @@ async function run(viewport: { width: number; height: number }, tag: string) {
await page.waitForTimeout(900);
await page.screenshot({ path: `${outDir}/${tag}-2-played.png` });
await page.keyboard.up("d");
+ // Z and X move the on-screen keys at once (they waited for the next note, and a click on them
+ // meanwhile played the old octave)
+ const firstKey = page.locator(".keys .key.white").first();
+ const lowest = await firstKey.getAttribute("aria-label");
+ await page.keyboard.press("x");
+ const raised = await firstKey.getAttribute("aria-label");
+ await page.keyboard.press("z");
+ const back = await firstKey.getAttribute("aria-label");
+ logs.push(`[${tag}] octave keys: ${lowest} → x → ${raised} → z → ${back}`);
+ const num = (l: string | null) => Number(l?.replace("Note ", ""));
+ if (num(raised) !== num(lowest) + 12 || back !== lowest) errors.push(`[${tag}] Z/X didn't move the on-screen keys: ${lowest} → ${raised} → ${back}`);
+ // a key held with the mouse while X redraws it away is still released when the mouse lifts
+ await firstKey.scrollIntoViewIfNeeded();
+ const kb = (await firstKey.boundingBox())!;
+ await page.mouse.move(kb.x + kb.width / 2, kb.y + kb.height - 10);
+ await page.mouse.down();
+ const pressed = await page.locator(".keys .key.down").count();
+ await page.keyboard.press("x");
+ await page.mouse.up();
+ await page.keyboard.press("z");
+ const stuck = await page.locator(".keys .key.down").count();
+ logs.push(`[${tag}] octave keys: ${pressed} held with the mouse across X, ${stuck} still down after release`);
+ if (pressed !== 1) errors.push(`[${tag}] the mouse didn't hold the lowest on-screen key (${pressed} down)`);
+ if (stuck) errors.push(`[${tag}] a key held with the mouse while X was pressed stayed down after the mouse lifted`);
const loadText = await page.locator(".load").textContent().catch(() => null);
logs.push(`[${tag}] load: ${loadText}`);
// speak mode
@@ -199,6 +223,6 @@ await run({ width: 1440, height: 980 }, "desktop");
await run({ width: 390, height: 844 }, "mobile");
await browser.close();
console.log("console errors:", errors.length ? "\n" + errors.join("\n") : "none");
-console.log(logs.filter((l) => /load|warn|transcription|creature|held|back to|beatbox|silence|sing/i.test(l)).slice(0, 40).join("\n"));
+console.log(logs.filter((l) => /load|warn|transcription|creature|held|back to|beatbox|silence|sing|octave/i.test(l)).slice(0, 40).join("\n"));
// a run that collected errors fails, so CI and agents can't read it as a pass
if (errors.length) process.exit(1);