File size: 32,262 Bytes
624a22c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15b2622
 
 
624a22c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// kale-tts-1 space: type -> speech in the browser. nano (default) or tiny, onnxruntime-web 1.31.1 through the kale-tts JS API.
// WebGPU first on desktops (nano's float32 graph); the CPU / wasm (int8) on phones (WebGPU ran an iPhone out of memory in our phone
// tests), and where WebGPU is missing, fails, or crashed before (gpuTrial.js). A failed WebGPU load reloads the page on the CPU, so
// only one runtime is ever in memory. A loaded model stays loaded when you switch to the other one (desktops; phones keep one), and
// its graph stays in the browser's cache, so switching back or reloading doesn't download it again. Everything runs in this tab.
import { Listener, drawHeroLine, onDemandLoop, reducedMotion } from "./visual.js";
import { Waterfall } from "./waterfall.js";
import { gpuTrial, webGpuWorks } from "./gpuTrial.js";

const SR = 24000;
// graph per backend; sha256 per graph file: the key of its copy in the browser's cache (ttsWorker.js), so a new build is downloaded once
const MODELS = {
  "kale-tts-1-nano": { params: "7.5M", size: "14.8 MB", graph: { webgpu: "fp32", wasm: "int8" }, shas: {
    "model.onnx": "4d5269974de4fba7a66f947608a9a5381b4e39c1a8a6073df6001ab7e7f76288",
    "model.fp32.onnx": "34d408a127f35f732a6860db5b1f7e8e1baac43ee3e32874f2007b5c6360e73a" } },
  "kale-tts-1-tiny": { params: "45.7M", size: "79.9 MB", graph: { webgpu: "fp32", wasm: "int8" }, shas: {
    "model.onnx": "8f255ef6f2b22c130cce15bdf92c5d420a821b2e5e39dbdef34064d52d6dc495",
    "model.fp32.onnx": "885675190d58a5f28f096f4698526fa2a2f2378d9d1897b6b0721596f585f64f" } },
};
// the 10 public voices by id: name, what they are, colour (women, then men)
const NAMES = {
  s057b: { name: "Kenzie", about: "esports hype woman", c: "#34d399" },
  f06: { name: "Simone", about: "deep and confident woman", c: "#fb923c" },
  d03: { name: "Amy", about: "british woman, crisp news-style", c: "#eab308" },
  w165: { name: "Frankie", about: "hard-boiled detective's partner", c: "#ec4899" },
  w602: { name: "Darlene", about: "rough low-voiced old trucker woman", c: "#a855f7" },
  m3: { name: "Owen", about: "bright young host", c: "#3b82f6" },
  t071: { name: "Toby", about: "mischievous prankster", c: "#22d3ee" },
  d13: { name: "Jack", about: "british man, crisp news-style", c: "#6366f1" },
  x328: { name: "Earl", about: "African American jazz granddad", c: "#8b5cf6" },
  h013f: { name: "Magnus", about: "deep dramatic trailer narrator", c: "#db2777" },
};
const PALETTE = ["#34d399", "#f472b6", "#22d3ee", "#facc15", "#a3e635", "#f87171"];   // voices of older builds, not in NAMES
const $ = (s, r = document) => r.querySelector(s), $$ = (s, r = document) => [...r.querySelectorAll(s)];
const isPhone = /Android|iPhone|iPad|iPod|Mobile/i.test(navigator.userAgent) || (navigator.maxTouchPoints > 1 && /Macintosh/.test(navigator.userAgent));
const toast = (m) => { const t = $("#toast"); t.textContent = m; t.classList.add("on"); clearTimeout(t._h); t._h = setTimeout(() => t.classList.remove("on"), 3200); };

// ------------------------------------------------------------------------------------------ audio out
class Player {
  constructor() { this.ctx = null; this.src = []; this.segs = []; this.next = 0; this.onend = null; }
  async ensure() {
    if (!this.ctx) {
      this.ctx = new AudioContext({ sampleRate: SR }); this.out = this.ctx.createGain(); this.an = this.ctx.createAnalyser();
      this.an.fftSize = 2048; this.an.smoothingTimeConstant = 0.6; this.out.connect(this.an); this.an.connect(this.ctx.destination); L = new Listener(this.an);
    }
    if (this.ctx.state !== "running") await this.ctx.resume();
  }
  reset() { for (const s of this.src) { try { s.stop(); } catch {} } this.src = []; this.segs = []; this.next = 0; }
  push(audio, meta, gap = 0) {
    const b = this.ctx.createBuffer(1, audio.length, SR); b.copyToChannel(audio, 0); const s = this.ctx.createBufferSource(); s.buffer = b; s.connect(this.out);
    const at = Math.max(this.ctx.currentTime + 0.04, this.next + gap); s.start(at); this.next = at + b.duration; this.src.push(s); this.segs.push({ start: at, end: this.next, meta }); loop.kick();
    s.onended = () => { const i = this.src.indexOf(s); if (i < 0) return; this.src.splice(i, 1); if (!this.src.length) this.onend?.(); };
  }
  get now() { return this.ctx ? this.ctx.currentTime : 0; }
  get playing() { return this.src.length > 0; }
}
const player = new Player(); let L = null;
window.kaleDemo = { player, get level() { return L ? L.level : 0; } };   // for tests and recordings

// ------------------------------------------------------------------------------------------ where it runs
const gpu = gpuTrial("kale-tts-1", () => toast("WebGPU stopped this tab last time, so this device runs on the CPU now."));
let hasGpu = false;
const q = new URLSearchParams(location.search);   // ?model= ?backend= (tests, and the CPU reload), ?gpufail, ?embed
const preferred = () => (!isPhone && hasGpu && gpu.allowed() && !q.has("gpufail") ? "webgpu" : "wasm");
/** start over on the CPU in a fresh page: what a WebGPU load left behind (its runtime, GPU memory) goes with the old one */
function reloadOn(backend, extra = {}) {
  const u = new URL(location.href); u.searchParams.set("backend", backend); u.searchParams.delete("gpufail");
  for (const [k, v] of Object.entries(extra)) u.searchParams.set(k, v);
  gpu.passed(); location.replace(u); return new Promise(() => {});
}
const beName = (b) => (b === "webgpu" ? "WebGPU" : "CPU");
/** the status, for screen readers only: loading, ready, speaking or what went wrong */
function live(said, busy = false) {
  $("#status").textContent = said;   // read by screen readers only; loading shows on the model card, errors in a toast
  $("#retry").hidden = !(M?.b === "wasm" && hasGpu && gpu.benched() && !busy);
}
// the stats under the model cards: the backend in use, and the first sound and speed of the last sentence played here
const TYPICAL = { "kale-tts-1-nano": [50, 100], "kale-tts-1-tiny": [60, 50] };
// estimates lead with "≈"
const ms = (v, est) => `${est ? "≈" : ""}${Math.round(v)} ms`, x = (v, est) => `${est ? "≈" : ""}${Math.round(v)}×`;
const typical = (name) => { const [f, r] = TYPICAL[name] ?? [50, 100]; return { first: ms(f, true), rt: x(r, true), typical: true }; };
function metrics({ be, first, rt, typical } = {}) {
  if (be !== undefined) $("#m-be").textContent = be;
  if (first !== undefined) $("#m-first").textContent = first;
  if (rt !== undefined) $("#m-rt").textContent = rt;
  $(".metrics").classList.toggle("typical", !!typical);
  if (typical) $(".metrics").title = "typical: an estimate until you press play"; else $(".metrics").removeAttribute("title");
}
/** the status once a model is ready (for screen readers): ready, or the crash note if WebGPU is benched */
const idle = () => {
  const benched = M.b === "wasm" && hasGpu && gpu.benched();
  const said = heardSaid; heardSaid = null;
  live(benched ? "ready, on the CPU: WebGPU is off here after a crash" : said ?? "ready");
};
const liveReady = () => {
  idle();
  $(".metrics").classList.remove("loading");
  metrics({ be: beName(M.b), ...typical(M.name) });
};

// ------------------------------------------------------------------------------------------ the model (one in use, others kept)
let M = null, loading = null, booted = false;   // booted: the first use() has started
// loaded models by name + backend: switching back to one is instant (no download, no new session). Phones keep only the one in use
// (memory); its graph is still in the browser's cache there, so loading it again doesn't download it.
const kept = new Map(), keyOf = (name, b) => `${name}|${b}`;
/** the model in its own worker (ttsWorker.js), behind the same small surface the page used before: voices, backend, stream() */
function workerTTS(name, b, opts) {
  const w = new Worker(new URL("./ttsWorker.js", import.meta.url), { type: "module" });
  let seq = 0, loaded = null; const on = new Map();
  w.onmessage = ({ data: m }) => on.get(m.id)?.(m);
  w.onerror = (e) => { e.preventDefault?.(); for (const f of on.values()) f({ type: "error", message: e.message || "the model's worker failed" });
    if (loaded && kept.get(keyOf(name, b))?.tts === loaded) kept.delete(keyOf(name, b));
    if (b === "webgpu" && loaded && M?.tts === loaded) reloadOn("wasm", { model: name, gpufail: "1" }); };
  const call = (msg, handler) => { const id = ++seq; on.set(id, handler); w.postMessage({ ...msg, id }); return id; };
  return new Promise((resolve, reject) => call({ type: "load", name, b, shas: MODELS[name].shas, ...opts }, (m) => {
    if (m.type === "status") cardState(name, m.text);
    else if (m.type === "progress") progress(name, m.got, m.total);
    else if (m.type === "error") { w.terminate(); reject(new Error(m.message)); }
    else if (m.type === "loaded") resolve(loaded = {
      voices: m.voices, backend: { graph: m.graph },
      session: { release: async () => w.terminate() },
      async *stream(text, { voice }) {
        const q = []; let wake = null, done = false, err = null;
        const id = call({ type: "stream", text, voice }, (x) => {
          if (x.type === "chunk") q.push(x); else { done = true; if (x.type === "error") err = new Error(x.message); }
          wake?.();
        });
        try {
          for (;;) { if (q.length) { yield q.shift(); continue; } if (done) break; await new Promise((r) => (wake = r)); wake = null; }
          if (err) throw err;
        } finally { on.delete(id); if (!done) w.postMessage({ type: "cancel", id }); }
      },
    });
  }));
}
async function openModel(name, b) {
  const tts = await workerTTS(name, b, { graph: MODELS[name].graph[b],
    threads: self.crossOriginIsolated ? Math.min(4, navigator.hardwareConcurrency || 4) : 1,
    sessionOptions: b === "wasm" && isPhone ? { graphOptimizationLevel: "disabled" } : {} });
  return { tts, name, b };
}
async function use(name, b) {
  if (!MODELS[name].graph[b]) b = "wasm";
  if (M && M.name === name && M.b === b) return;
  gen++; busy = false; player.reset(); setInert(true); markModel(name);
  $$(".mcard").forEach((x) => x.setAttribute("aria-disabled", String(x.dataset.m !== name)));
  const old = M; M = null; goState(); gpu.passed();
  if (old && isPhone) { kept.delete(keyOf(old.name, old.b)); try { await old.tts.session.release?.(); } catch {} }   // phones: one model in memory
  const have = kept.get(keyOf(name, b));
  if (have) {   // loaded before: use it again as it is
    M = have; if (M.b === "webgpu") gpu.begin(); else gpu.passed();
    $$(".mcard").forEach((x) => x.removeAttribute("aria-disabled"));
    $("#loadbar").className = "loadbar off"; cardState(name, null); liveReady();
    buildKeys(M.tts.voices); setInert(false); goState(); return;
  }
  said50 = false; cardState(name, "starting…", { p: 0 });
  $("#loadbar").className = "loadbar wait"; live(`loading ${name}${b === "webgpu" ? " on WebGPU" : ""}…`, true);
  $(".metrics").classList.add("loading"); metrics({ be: beName(b), ...typical(name) });   // while loading: the typical values
  if (b === "webgpu") gpu.begin();
  // a GPU that hangs (after a crash, a lost device) must not leave the page loading forever: give up and use the CPU
  const hung = new Promise((_, no) => setTimeout(() => no(new Error("WebGPU didn't answer")), b === "webgpu" ? 45000 : 180000));
  try { M = await (loading = Promise.race([openModel(name, b), hung])); }
  catch (e) {
    console.warn(e); gpu.passed();
    if (b === "webgpu") return reloadOn("wasm", { model: name, gpufail: "1" });
    $("#loadbar").className = "loadbar off"; $(".metrics").classList.remove("loading"); cardState(name, "couldn't load · retry", { error: true });
    live("couldn't load the model · press its card to try again"); toast("Couldn't load the model. Press its card to try again."); return;
  }
  finally { loading = null; $$(".mcard").forEach((x) => x.removeAttribute("aria-disabled")); }
  kept.set(keyOf(name, b), M);
  if (M.b === "wasm") gpu.passed();   // on WebGPU the mark stays for as long as the model runs there (see render)
  $("#loadbar").className = "loadbar off"; cardState(name, null); liveReady();
  buildKeys(M.tts.voices); setInert(false); goState();
}
/** the card of the model being loaded shows where it is: a thin bar for the download and a line of text (in place of its traits) */
function cardState(name, text, { p = null, error = false } = {}) {
  const c = $(`.mcard[data-m="${name}"]`); if (!c) return;
  const bar = $("#mprogress"); bar.hidden = text == null || error || p == null; bar.setAttribute("aria-label", `Downloading ${name}`);
  if (p != null) bar.setAttribute("aria-valuenow", String(Math.round(p * 100)));
  c.classList.toggle("busy", text != null); c.classList.toggle("failed", error);
  $(".mstate", c).textContent = text ?? "";
  if (p != null) c.style.setProperty("--p", String(p)); else if (text == null) c.style.removeProperty("--p");
}
let said50 = false;   // the status (for screen readers) says it at the start, at half way and when ready, not every percent
function progress(name, got, total) {
  const p = total ? Math.min(1, got / total) : null;
  // a percentage only: sizes belong to the cards' CPU / GPU facts; a second MB figure next to them read as one of them
  cardState(name, p != null ? `downloading ${Math.round(p * 100)}%` : "downloading…", { p: p ?? 0 });
  if (p >= 0.5 && !said50) { said50 = true; live(`downloading ${name} · 50%`, true); }
}
function markModel(name) {
  $("#mtitle").textContent = name; document.title = `${name} · speech in your browser`;
  $$(".mcard").forEach((x) => { x.setAttribute("aria-checked", String(x.dataset.m === name)); x.tabIndex = x.dataset.m === name ? 0 : -1; });
  fx?.setMode(name.endsWith("tiny") ? 1 : 0); placeHalo(); loop.kick();   // nano: the waterfall; tiny: mountains rising up the page
}

// ------------------------------------------------------------------------------------------ state + ui
let voice = null, keys = [], gen = 0, busy = false;
// the terrain and its waterfall (one shader); without WebGL2 a plain 2D line in the stage
let fx = null; try { fx = new Waterfall($("#fx")); if (!fx.ok) fx = null; } catch (e) { console.warn(e); fx = null; }
document.documentElement.classList.toggle("nofx", !fx);
const look = {}; const colorOf = (id) => look[id]?.c ?? "#34d399";
function setColor(id) { const c = colorOf(id); document.documentElement.style.setProperty("--vc", c); fx?.setColor(c); }

/** the voice keys. Drawn straight away from the known ten (no keys popping in later); inert until a model is loaded. Redrawn
 *  only if a loaded model has a different set of voices. */
const STATIC_VOICES = Object.keys(NAMES).map((id, order) => ({ id, name: NAMES[id].name, order }));
let builtFor = "";
function buildKeys(list = STATIC_VOICES) {
  const vs = [...list].sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
  const ids = vs.map((v) => v.id).join(",");
  if (ids === builtFor) return;
  builtFor = ids;
  vs.forEach((v, i) => { look[v.id] = NAMES[v.id] ?? { name: v.name.split("(")[0].trim(), about: (v.name.match(/\(([^)]*)\)/)?.[1] ?? ""), c: PALETTE[i % PALETTE.length] }; });
  const box = $("#keys");
  box.innerHTML = vs.map((v) => { const l = look[v.id];
    return `<button class="key" data-v="${v.id}" style="--c:${l.c}" aria-label="${l.name}, ${l.about}" title="${l.about}"><kbd></kbd><b>${l.name}</b><small class="own" id="own-${v.id}"></small></button>`; }).join("");
  keys = $$(".key").map((el) => ({ el, id: el.dataset.v }));
  keys.forEach((k) => (k.el.onclick = () => speak(k.id)));
  labelKeys(); showLines();
  if (!vs.some((v) => v.id === voice)) voice = vs[0].id; select(voice);
}
/** the picked voice: coloured, and current for screen readers (keys replay, they don't toggle) */
const markSel = (id) => keys.forEach((k) => { k.el.classList.toggle("sel", k.id === id); if (k.id === id) k.el.setAttribute("aria-current", "true"); else k.el.removeAttribute("aria-current"); });
function select(id) { voice = id; markSel(id); setColor(id); goState(); loop.redraw(); }
/** before a model is ready the keys are there but can't be played */
function setInert(on) { $("#keys").classList.toggle("inert", on); keys.forEach((k) => (on ? k.el.setAttribute("aria-disabled", "true") : k.el.removeAttribute("aria-disabled"))); }
// ------------------------------------------------------------------------------------------ one line per voice (the a-line-per-voice Off | On picker)
// Off (the default): every voice speaks the text in the composer. On: every voice has its own line, shown on its key; picking a
// voice puts its line in the composer, and what you type there becomes that voice's line. Play the keys like an instrument.
// every voice starts without a line of its own and speaks the shared sentence until you type one (no examples)
const own = (() => { try { localStorage.removeItem("kale-tts-1:lines"); return JSON.parse(localStorage.getItem("kale-tts-1:voice-lines") || "{}"); } catch { return {}; } })();
let perKey = (() => { try { return localStorage.getItem("kale-tts-1:per-key") === "1"; } catch { return false; } })();
let shared = $("#text").value;                       // the sentence every voice speaks (and the fallback in the lines mode)
const textFor = (id) => (perKey ? own[id]?.trim() || shared : $("#text").value).trim();
/** the composer shows what you'd edit: in the lines mode the picked voice's own line (empty if it has none), else the shared one */
function fillComposer(id = voice) {
  const t = $("#text");
  if (perKey) { t.value = own[id] ?? ""; t.placeholder = `Write ${look[id]?.name ?? "this voice"}'s line, or leave it empty to use the shared text.`; }
  else { t.value = shared; t.placeholder = "Type something to say"; }
}
function showLines() {
  syncSegs(); $("#keys").classList.toggle("lines-on", perKey);
  keys.forEach((k) => { $(".own", k.el).textContent = own[k.id]?.trim() ?? "";
    if (perKey) k.el.setAttribute("aria-describedby", `own-${k.id}`); else k.el.removeAttribute("aria-describedby"); });
}
function setPerKey(on) {
  perKey = on; try { localStorage.setItem("kale-tts-1:per-key", on ? "1" : "0"); } catch {}
  fillComposer(); showLines(); goState();
}

/** the typed text in one voice: stream sentence by sentence into the speakers; the stats line under the cards shows how fast */
async function render(id, { gap = 0, my }) {
  const text = textFor(id), t0 = performance.now(); let first = null, audioS = 0, chunks = 0;
  for await (const ch of M.tts.stream(text, { voice: id })) {
    if (my !== gen) return null; const now = performance.now();
    if (first === null) first = now - t0;
    player.push(ch.audio, { id }, chunks ? 0.12 : gap); if (!chunks++) goState(); audioS += ch.audio.length / SR;
    metrics({ first: ms(first), rt: x(audioS / ((now - t0) / 1000)) });
  }
  if (first !== null) { const rt = audioS / ((performance.now() - t0) / 1000);   // written (and said) once it has been heard
    heardStats = { first: ms(first), rt: x(rt), said: `First sound ${Math.round(first)} ms, real time ${Math.round(rt)} times` }; }
  return true;
}
/** the play button is a stop button while something is being made or heard */
let heardStats = null, heardSaid = null;   // heardSaid: the measured stats, said once by the hidden status after playback
function goState() {
  const on = busy || player.playing, b = $("#go"); b.classList.toggle("stop", on); b.setAttribute("aria-label", on ? "Stop" : "Play");
  b.disabled = !on && !(M && textFor(voice));   // nothing to say (or no model yet): no Play that does nothing
  b.title = !b.disabled ? "" : M ? "Type something to say" : "Loading the model…";
  b.classList.toggle("playing", player.playing);
}
/** resolves when what was queued has been heard (or this run was replaced); one timer per wait, no polling loop */
const heard = (my) => new Promise((done) => { const check = () => (my !== gen || !player.playing ? done() : setTimeout(check, Math.max(30, (player.next - player.now) * 1000))); check(); });
async function speak(id = voice) {
  if (!M) return;   // keys are inert while a model loads: nothing changes, not even the composer
  if (perKey && id !== voice) fillComposer(id);   // the composer shows (and edits) the picked voice's line
  if (!textFor(id)) { select(id); toast("Type something to say first."); return; }
  const my = ++gen; let failed = false;
  select(id); busy = true; goState();   // Stop shows in the same frame as the press
  try {
    await player.ensure(); player.reset(); heardStats = null;
    live("speaking");   // for screen readers; on screen the Stop icon and its ring say it
    // Stop stays up until the sentence has been heard to its end
    if (await render(id, { my }) && my === gen) await heard(my);
  } catch (e) {
    console.warn(e); failed = true;
    if (my === gen) { player.reset(); live("couldn't say that · press play to try again"); toast("Couldn't say that. Press play to try again."); }
  } finally {
    if (my === gen) { busy = false; if (!failed) idle(); } goState();
  }
}
function stop() { gen++; busy = false; player.reset(); goState(); if (M) idle(); loop.redraw(); }

// ------------------------------------------------------------------------------------------ drawing (only while sound plays)
let shownId = null;
const frame = (dt, active) => {
  const now = player.now; let seg = null; for (const s of player.segs) if (now >= s.start - 0.06 && now < s.end) seg = s;
  const id = seg?.meta.id ?? null, moving = active && !reducedMotion.matches;
  if (L && dt > 0) L.update(dt);
  if (id && id !== shownId) { setColor(id); markSel(id); }
  if (fx) fx.draw(L, dt, moving, player.playing); else drawHeroLine($("#line"), L, colorOf(id ?? voice), moving);
  if (id !== shownId) { shownId = id; goState(); }
};
/** the waterfall always flows (flat when nobody speaks), but only while the page is shown and the water is on screen; phones at
 *  30 fps; reduced motion: one still picture. Without the shader the 2D line draws only while sound plays. */
function flowing(frame) {
  let raf = 0, last = 0, inView = true;
  const slow = matchMedia("(max-width: 760px)").matches, want = () => !document.hidden && inView && !reducedMotion.matches;
  const step = (now) => {
    const dt = (now - last) / 1000;
    if (slow && dt < 1 / 31) { raf = requestAnimationFrame(step); return; }
    last = now; frame(Math.min(0.05, dt), true);
    raf = want() ? requestAnimationFrame(step) : 0;
  };
  const start = () => { if (!raf && want()) { last = performance.now(); raf = requestAnimationFrame(step); } };
  document.addEventListener("visibilitychange", start);
  new IntersectionObserver(([e]) => { inView = e.isIntersecting; start(); }).observe($("#fx"));
  reducedMotion.addEventListener?.("change", start);
  return { kick: start, redraw() { if (!raf) frame(0, false); }, get running() { return !!raf; } };
}
const loop = fx ? flowing(frame) : onDemandLoop(frame, () => player.playing);
window.kaleDemo.loop = loop; window.kaleDemo.fx = fx;
// the end of the sound restores Play (an event, so it works with reduced motion, when no frames run) and announces the stats;
// a run that is still going keeps its Stop
player.onend = () => { if (heardStats) { metrics(heardStats); heardSaid = heardStats.said; heardStats = null; } if (!busy) goState(); loop.redraw(); };

let halo = null;
/** the haze the lines come out of: as wide as the lines (plus a margin), centred on the line where they start (nano: the water's
 *  edge; tiny: the live line in front), gliding along with the picture when the model changes */
function placeHalo() {
  if (!halo) return; const tiny = $("#mtitle").textContent.endsWith("tiny"), el = $("#halo");
  el.style.top = `${(tiny ? halo.rise : halo.fall) - halo.h / 2}px`;
}
/** the shader's canvas: from just above the edge to the end of the fall (the page grows to fit it if it is shorter), from left of
 *  the stage to the right edge; and the glow on the edge. The fall is a fixed length, so every screen ends it the same way. */
function placeFall() {
  if (!fx) return;
  // the whole page wide, from the top (tiny's mountains rise up to it) to the end of nano's fall; the picture is centred in the
  // right column, its live line (where the water falls from) at the height of the model cards' middle
  const cv = $("#fx"), mc = $(".models").getBoundingClientRect(), vw = document.documentElement.clientWidth;
  const fall = vw <= 760 ? 560 : 820, edge = Math.round(mc.top + scrollY + mc.height / 2);
  const height = Math.max(document.documentElement.scrollHeight, edge + fall + 60);
  Object.assign(cv.style, { left: "0px", top: "0px", width: `${vw}px`, height: `${height}px` });
  // the right column's middle (the whole width on a phone, where the column goes away)
  const art = $(".art").getBoundingClientRect(), wide = art.width > 40, cx = wide ? art.left + art.width / 2 : vw / 2;
  const w = wide ? Math.min(art.width * 1.15, 980) : Math.min(vw * 0.94, 980), h = vw <= 760 ? 220 : 300;
  // the projection puts the edge at 0.848 of the box height: shift the box so the edge lands where we want it
  const box = { x: cx - w / 2, y: edge - h * 0.848, w, h };
  // tiny's mountains start lower: from the middle of the first row of voices (the women)
  const row = $("#keys .key")?.getBoundingClientRect(), riseEdge = row ? Math.round(row.top + scrollY + row.height / 2) : edge;
  fx.place({ ...box, streamX: cx, fall, rise: Math.max(120, riseEdge - 16), riseDy: riseEdge - edge });
  halo = { fall: edge, rise: riseEdge, w: w * 0.83 * 1.18, h: h * 0.3 };
  Object.assign($("#halo").style, { left: `${cx - halo.w / 2}px`, width: `${halo.w}px`, height: `${halo.h}px` }); placeHalo();
  loop.redraw();
}

// ------------------------------------------------------------------------------------------ controls
const pickModel = (x) => { if (!booted || loading || M?.name === x.dataset.m) return; use(x.dataset.m, preferred()); };
$$(".mcard").forEach((x) => (x.onclick = () => pickModel(x)));
// one radio group: Tab reaches the checked card, the arrows move to (and pick) the other
$(".models").addEventListener("keydown", (e) => {
  const step = { ArrowLeft: -1, ArrowUp: -1, ArrowRight: 1, ArrowDown: 1 }[e.key]; if (!step) return;
  e.preventDefault(); if (!booted || loading) return;   // focus never lands on a card that can't be picked
  const cards = $$(".mcard"), x = cards[(cards.indexOf(e.target.closest(".mcard")) + step + cards.length) % cards.length];
  x.focus(); pickModel(x);
});
$("#retry").onclick = () => { gpu.reset(); reloadOn("webgpu", M ? { model: M.name } : {}); };
$("#go").onclick = () => (busy || player.playing ? stop() : speak());

// ------------------------------------------------------------------------------------------ keyboard: the home row plays the voices
// Bound to physical keys (KeyboardEvent.code), so the same row works on every layout; only the printed letters differ. The labels
// come from the browser's layout map where it has one (Chrome, Edge), else from the layout picked with the keyboard button.
const ROW = ["KeyA", "KeyS", "KeyD", "KeyF", "KeyG", "KeyH", "KeyJ", "KeyK", "KeyL", "Semicolon"];
const LAYOUTS = {
  QWERTY: "A S D F G H J K L ;", QWERTZ: "A S D F G H J K L Ö", AZERTY: "Q S D F G H J K L M", Dvorak: "A O E U I D H T N S",
};
const PICKS = ["auto", ...Object.keys(LAYOUTS)];
const stored = (() => { try { return localStorage.getItem("kale-tts-1:keys"); } catch { return null; } })();
let layout = PICKS.includes(stored) ? stored : "auto", detected = null;
// the key letters (and the home row that plays the voices) on or off: off for a new visitor; a saved choice stays, and so does an
// older saved layout pick (letters on, as they were then)
let lettersOn = (() => { try { const v = localStorage.getItem("kale-tts-1:key-letters"); return v === null ? PICKS.includes(stored) : v === "1"; } catch { return false; } })();
navigator.keyboard?.getLayoutMap?.().then((map) => { detected = ROW.map((c) => (map.get(c) ?? "").toUpperCase()); labelKeys(); }).catch(() => {});
function labelKeys() {
  const marks = !lettersOn ? [] : layout === "auto" ? (detected?.every(Boolean) ? detected : LAYOUTS.QWERTY.split(" ")) : LAYOUTS[layout].split(" ");
  keys.forEach((k, i) => { const kbd = $("kbd", k.el), m = marks[i]; kbd.textContent = m ?? ""; kbd.hidden = !m;
    if (m) k.el.setAttribute("aria-keyshortcuts", m); else k.el.removeAttribute("aria-keyshortcuts"); });
  syncSegs();
  $("#layout").textContent = `layout: ${layout}`; $("#layout").classList.toggle("off", !lettersOn);
  $("#layout").setAttribute("aria-label", `keyboard layout: ${layout === "auto" ? `auto${detected ? "" : " (QWERTY)"}` : layout}`);
}
function setLetters(on) {
  lettersOn = on;
  try { localStorage.setItem("kale-tts-1:key-letters", lettersOn ? "1" : "0"); } catch {}
  labelKeys();
}
/** the Off | On pickers (radio groups) of the two options: checked, in the tab order, and the label brighter while On */
function syncSegs() {
  for (const b of $$(".seg [role=radio]")) {
    const on = b.dataset.seg === "lines" ? perKey : lettersOn, picked = on === (b.dataset.v === "1");
    b.setAttribute("aria-checked", String(picked)); b.tabIndex = picked ? 0 : -1; b.closest(".opt").classList.toggle("on", on);
  }
}
const pickSeg = (b) => (b.dataset.seg === "lines" ? setPerKey(b.dataset.v === "1") : setLetters(b.dataset.v === "1"));
$$(".seg [role=radio]").forEach((b) => (b.onclick = () => pickSeg(b)));
$$(".seg").forEach((g) => g.addEventListener("keydown", (e) => {
  if (!["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown"].includes(e.key)) return;
  e.preventDefault(); const other = $$("[role=radio]", g).find((b) => b !== e.target); other.focus(); pickSeg(other);
}));
$("#layout").onclick = () => {
  layout = PICKS[(PICKS.indexOf(layout) + 1) % PICKS.length];
  try { localStorage.setItem("kale-tts-1:keys", layout); } catch {}
  labelKeys();
};
addEventListener("keydown", (e) => {
  if (e.repeat || e.ctrlKey || e.metaKey || e.altKey) return;
  // Enter with nothing focused plays the picked voice (a focused button or the composer handles its own Enter)
  if (e.key === "Enter" && (e.target === document.body || e.target === document.documentElement)) { e.preventDefault(); speak(); return; }
  if (!lettersOn || e.target.closest?.("textarea, input, select, [contenteditable]")) return;
  const i = ROW.indexOf(e.code); if (i < 0 || !keys[i]) return;
  e.preventDefault(); speak(keys[i].id);
});
// the text stays on its 24px line grid as it scrolls (Chrome's caret scroll can stop a few pixels short)
$("#text").addEventListener("input", () => {
  requestAnimationFrame(() => { const t = $("#text"); t.scrollTop = Math.round(t.scrollTop / 24) * 24; });
  if (perKey) { own[voice] = $("#text").value; try { localStorage.setItem("kale-tts-1:voice-lines", JSON.stringify(own)); } catch {} showLines(); }
  else shared = $("#text").value;
  goState();
});
$("#text").addEventListener("keydown", (e) => { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); speak(); } });
/** the Kale wordmark as wide as the "brought to you by" line above it */
const sizeBrand = () => { const t = $(".brand span"), l = $(".brand svg"); if (t && l) l.style.width = `${t.getBoundingClientRect().width}px`; };
sizeBrand(); document.fonts?.ready.then(sizeBrand);
let rz; addEventListener("resize", () => { clearTimeout(rz); rz = setTimeout(placeFall, 120); });
reducedMotion.addEventListener?.("change", () => loop.redraw());

// ?embed (the canvas card's frame): no tall empty end. Not "inside a frame", since Hugging Face frames every Space.
if (q.has("embed")) document.documentElement.classList.add("framed");
buildKeys(); setInert(true);
setColor("s057b"); placeFall(); loop.kick();
await gpu.ready(); hasGpu = await Promise.race([webGpuWorks(), new Promise((r) => setTimeout(() => r(false), 4000))]);
if (q.has("gpufail")) toast("WebGPU didn't start here, so the CPU runs it.");
const qModel = MODELS[q.get("model")] ? q.get("model") : "kale-tts-1-nano", qB = q.get("backend");
booted = true;
await use(qModel, qB === "wasm" || (qB === "webgpu" && hasGpu) ? qB : preferred());
if (perKey) fillComposer();   // the lines mode was on last time
placeFall();
window.__ready = true;