Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178#!/usr/bin/env node/** * Look at the seek row instead of guessing at it. * * Most of "that waveform looks bad" is not measurable, and the app cannot be screenshotted into a * judgement call without a logged-in server in front of it. So this renders `scripts/wavebench/` — * which draws with the app's own `wavePath` and the app's own CSS — under a list of settings, and * writes one PNG per setting at a zoom where a 20 px row is actually legible. * * node scripts/wave-look.mjs # the built-in comparison sheet * node scripts/wave-look.mjs --out /tmp/look # somewhere to read it from * node scripts/wave-look.mjs --configs '[{"label":"bars","shape":"bars","bpp":1}]' */import { mkdirSync, writeFileSync } from "node:fs";import { join } from "node:path";
const argv = process.argv.slice(2);const arg = (name, fallback) => { const i = argv.indexOf(`--${name}`); return i >= 0 && argv[i + 1] !== undefined && !argv[i + 1].startsWith("--") ? argv[i + 1] : fallback;};const origin = arg("origin", "http://127.0.0.1:3999");const port = arg("port", "9445");const outDir = arg("out", "/tmp/wave-look");const path = arg("path", `/@fs${process.cwd()}/scripts/wavebench/index.html`);
/** The sheet: the shipped drawing beside the knobs it is sensitive to, each labelled on the image. */const SHEET = [ { label: "as shipped — envelope at 4 bars/px, region as a wash", marks: 1 }, { label: "gamma 1.8 — the loud half of the row stops being flat", gamma: 1.8, marks: 1, waved: 1, }, { label: "gamma 2.2", gamma: 2.2, marks: 1, waved: 1 }, { label: "gamma 1.8, 1 bar/px", gamma: 1.8, bpp: 1, marks: 1, waved: 1 }, { label: "gamma 1.8, bars every 3px (the stripe look)", gamma: 1.8, bpp: 0.34, ratio: 0.7, marks: 1, waved: 1, }, { label: "gamma 1.8, bars shape 1/px", gamma: 1.8, shape: "bars", bpp: 1, marks: 1, waved: 1 }, { label: "gamma 1.8, −24 dB floor", gamma: 1.8, floor: -24, marks: 1, waved: 1 }, { label: "region wash over the bars (why it reads as a slab)", gamma: 1.8, marks: 1 }, { label: "region edges only, detected", gamma: 1.8, marks: 1, waved: 1, detected: 1 }, { label: "no region at all", gamma: 1.8 },];
const configs = arg("configs", "") ? JSON.parse(arg("configs")) : SHEET;
mkdirSync(outDir, { recursive: true });
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
class Cdp { #socket; #waiting = new Map(); #id = 0; static open(url) { const c = new Cdp(); return new Promise((resolve, reject) => { const s = new WebSocket(url); s.addEventListener("open", () => ((c.#socket = s), resolve(c))); s.addEventListener("error", () => reject(new Error("cannot reach the debugger"))); s.addEventListener("message", (e) => { const m = JSON.parse(e.data); if (m.id !== undefined && c.#waiting.has(m.id)) { const { done, fail } = c.#waiting.get(m.id); c.#waiting.delete(m.id); if (m.error) fail(new Error(`${m.method}: ${m.message}`)); else done(m.result); } }); }); } send(method, params = {}) { const id = ++this.#id; return new Promise((done, fail) => { this.#waiting.set(id, { done, fail }); this.#socket.send(JSON.stringify({ id, method, params })); }); } close() { this.#socket.close(); }}
async function findPage() { for (let i = 0; i < 40; i++) { try { const t = await (await fetch(`http://127.0.0.1:${port}/json`)).json(); const p = t.find((x) => x.type === "page" && x.url.includes("wavebench")); if (p) return p; } catch { /* not up yet */ } await sleep(250); } throw new Error("no bench page on the debug port");}
const { spawn } = await import("node:child_process");const chrome = arg("chrome", process.env.CHROME || "google-chrome");const child = spawn( chrome, [ "--headless=new", `--remote-debugging-port=${port}`, `--user-data-dir=/tmp/wave-look-profile-${port}`, "--no-first-run", "--no-default-browser-check", "--hide-scrollbars", `--window-size=1800,${configs.length * 140 + 120}`, `${origin}${path}`, ], { stdio: "ignore" },);
const page = await findPage();const cdp = await Cdp.open(page.webSocketDebuggerUrl);await cdp.send("Page.enable");await cdp.send("Runtime.enable");// 3× so a 20 px row is 60 px of pixels on the page, then scale the shot up again.const zoom = Number(arg("zoom", "3"));// A detail crop, as fractions of the row: a 4200 px sheet downscaled in a viewer is exactly the// horizontal detail that decides whether a thin waveform reads as music or as noise.const from = Number(arg("from", "0"));const to = Number(arg("to", "1"));const scale = Number(arg("scale", "1"));
const files = [];for (const cfg of configs) { const q = new URLSearchParams({ still: "1", zoom: String(zoom), ...Object.fromEntries(Object.entries(cfg).map(([k, v]) => [k, String(v)])), }); await cdp.send("Page.navigate", { url: `${origin}${path}?${q}` }); await sleep(700); const box = await cdp.send("Runtime.evaluate", { expression: `(() => { const b = document.querySelector("#stage > div"); if (!b) return null; const r = b.getBoundingClientRect(); return JSON.stringify({x:r.x,y:r.y,w:r.width,h:r.height}); })()`, returnByValue: true, }); if (!box.result?.value) throw new Error("the bench page did not render a row"); const r = JSON.parse(box.result.value); const x = r.x + r.w * from; const w = Math.max(20, r.w * (to - from)); const shot = await cdp.send("Page.captureScreenshot", { format: "png", clip: { x: Math.max(0, x - 8), y: Math.max(0, r.y - 26), width: w + 16, height: r.h + 34, scale, }, captureBeyondViewport: true, }); const file = join( outDir, `${String(files.length).padStart(2, "0")}-${(cfg.label ?? "row").replace(/[^a-z0-9]+/gi, "-").toLowerCase()}.png`, ); writeFileSync(file, Buffer.from(shot.data, "base64")); files.push(file); console.log(file);}
cdp.close();child.kill("SIGTERM");console.log(`\n${files.length} rows written to ${outDir}`);