File size: 25,117 Bytes
48732a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e28eb44
 
 
 
 
 
 
 
48732a6
 
 
 
 
 
 
 
9cfaac3
48732a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e28eb44
 
 
 
 
 
 
48732a6
 
 
 
 
 
 
 
9cfaac3
48732a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e28eb44
 
48732a6
 
 
 
 
 
 
 
 
e28eb44
48732a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9cfaac3
48732a6
 
 
 
 
 
 
 
 
 
 
e28eb44
9cfaac3
48732a6
 
e28eb44
48732a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e28eb44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48732a6
 
 
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
<!doctype html>
<html lang="de">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>apptor-tiny-tts</title>
<meta name="description" content="Eine kleine deutsche Stimme, die auch (etwas) österreichisch kann. Läuft komplett im Browser, Apache 2.0.">
<meta property="og:title" content="apptor-tiny-tts: vom Fischbrötchen zum Schnitzel">
<meta property="og:description" content="Kleine deutsche Stimme mit Akzent-Regler, hochdeutsch oder österreichisch. Läuft komplett im Browser, Apache 2.0.">
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Crect width='32' height='32' rx='8' fill='%23c8102e'/%3E%3Crect y='11' width='32' height='10' fill='%23fff'/%3E%3C/svg%3E">
<style>
:root {
  --ink: #1d232b; --muted: #5b6672; --card: #ffffffee; --line: #d9dee4; --accent: #c8102e; --accent-ink: #fff;
  --bg-a: #2f7aa0; --bg-b: #d8eef6; --shadow: 0 10px 30px #0002; --track: #e7ebef;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root { --ink: #eef2f5; --muted: #a6b1bc; --card: #1b2229ee; --line: #34404b; --shadow: 0 10px 30px #0006; --track: #2b343d; }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  min-height: 100vh; color: var(--ink);
  background: linear-gradient(160deg, var(--bg-a), var(--bg-b)); background-attachment: fixed;
  transition: background 0.6s;
}
main { max-width: 760px; margin: 0 auto; padding: 28px 16px 40px; }
header { text-align: center; color: #fff; text-shadow: 0 2px 8px #0004; }
header h1 { font-size: clamp(2.2rem, 7vw, 3.6rem); margin: 0; letter-spacing: -0.02em; transition: transform 0.25s; }
header p { margin: 6px 0 0; font-size: 1.05rem; opacity: 0.95; }
.ver { display: inline-block; margin-top: 10px; padding: 3px 12px; border-radius: 999px; background: #fff3; border: 1px solid #fff8;
  font-size: 0.8rem; font-weight: 600; text-shadow: none; }
.card { background: var(--card); border-radius: 18px; box-shadow: var(--shadow); padding: 20px; margin-top: 18px; backdrop-filter: blur(6px); }
.food { position: relative; height: 190px; display: flex; align-items: center; justify-content: center; }
.food svg { position: absolute; width: min(260px, 70vw); height: 180px; transition: opacity 0.35s, transform 0.35s; }
.dish { text-align: center; font-weight: 600; margin: 2px 0 10px; min-height: 1.4em; }
.slider-wrap { padding: 4px 4px 0; }
input[type=range] { width: 100%; accent-color: var(--accent); height: 28px; }
.stops { position: relative; height: 1.9em; font-size: 0.85rem; color: var(--muted); margin-top: 2px; }
.stops button { position: absolute; top: 0; white-space: nowrap; }
.stops button { background: none; border: 0; color: inherit; font: inherit; cursor: pointer; padding: 4px 2px; }
.stops button:hover { color: var(--ink); text-decoration: underline; }
.value { text-align: center; color: var(--muted); font-size: 0.9rem; margin-top: 4px; }
textarea { width: 100%; min-height: 200px; resize: vertical; border: 1px solid var(--line); border-radius: 12px; padding: 12px;
  font: inherit; font-size: 1.05rem; background: transparent; color: var(--ink); }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-top: 12px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 999px; overflow: hidden; }
.seg button { border: 0; background: transparent; color: var(--ink); padding: 9px 16px; font: inherit; cursor: pointer; }
.seg button[aria-pressed=true] { background: var(--ink); color: var(--card); }
.speak { margin-left: auto; border: 0; border-radius: 999px; padding: 11px 26px; font: inherit; font-weight: 700; font-size: 1.05rem;
  background: var(--accent); color: var(--accent-ink); cursor: pointer; box-shadow: 0 4px 14px #c8102e55; }
.speak:disabled { opacity: 0.5; cursor: wait; box-shadow: none; }
.examples { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.examples button { border: 1px solid var(--line); background: transparent; color: var(--muted); border-radius: 999px; padding: 6px 12px;
  font: inherit; font-size: 0.85rem; cursor: pointer; }
.examples button:hover { color: var(--ink); border-color: var(--ink); }
.source { font-size: 0.8rem; color: var(--muted); margin-top: 4px; }
.status { font-size: 0.88rem; color: var(--muted); margin-top: 12px; min-height: 1.2em; }
.bar { height: 6px; background: var(--track); border-radius: 99px; overflow: hidden; margin-top: 6px; }
.bar i { display: block; height: 100%; width: 0; background: var(--accent); transition: width 0.2s; }
label.check { display: inline-flex; gap: 6px; align-items: center; font-size: 0.9rem; color: var(--muted); }
.info { font-size: 0.92rem; line-height: 1.55; color: var(--muted); }
.info h2 { color: var(--ink); font-size: 1.05rem; margin: 0 0 6px; }
.info p + h2 { margin-top: 16px; }
.info p { margin: 0 0 10px; }
.info a { color: var(--accent); }
.use { margin: 0 0 12px; padding-left: 18px; }
.use li { margin-bottom: 8px; }
.use b { color: var(--ink); }
.links { display: flex; gap: 10px; flex-wrap: wrap; margin: 4px 0 12px; }
.btn { display: inline-block; border: 1px solid var(--line); border-radius: 999px; padding: 8px 18px; text-decoration: none;
  font-weight: 600; color: var(--ink) !important; }
.btn:hover { border-color: var(--ink); }
.small { font-size: 0.82rem; }
footer { text-align: center; color: #1d232bcc; font-size: 0.9rem; margin-top: 22px; }
footer a { color: #1d232b; font-weight: 600; }
.wobble { animation: wob 0.5s; }
@keyframes wob { 30% { transform: rotate(-3deg) scale(1.04); } 60% { transform: rotate(2deg); } }
.filerow { justify-content: flex-start; margin-top: 10px; padding-top: 10px; border-top: 1px dashed var(--line); }
.ghost { border: 1px solid var(--line); background: transparent; color: var(--ink); border-radius: 999px; padding: 8px 16px;
  font: inherit; font-size: 0.92rem; cursor: pointer; }
.ghost:hover:not(:disabled) { border-color: var(--ink); }
.ghost:disabled { opacity: 0.5; cursor: wait; }
.filerow select { border: 1px solid var(--line); background: transparent; color: var(--ink); border-radius: 999px; padding: 8px 12px; font: inherit; font-size: 0.92rem; }
.filerow select option { color: #1d232b; }
.filerow .hint { font-size: 0.8rem; color: var(--muted); flex-basis: 100%; }
@media (max-width: 520px) { .speak { margin-left: 0; width: 100%; } }
</style>
</head>
<body>
<main>
  <header>
    <h1 id="greet">Servus!</h1>
    <p>Eine kleine deutsche Stimme, die auch (etwas) österreichisch kann. Direkt in deinem Browser.</p>
    <span class="ver">Version 0.2 · frühe Vorschau</span>
  </header>

  <section class="card" aria-label="Akzent">
    <div class="food" id="food" aria-hidden="true">
      <!-- Fischbrötchen -->
      <svg viewBox="0 0 260 180" data-i="0">
        <ellipse cx="130" cy="150" rx="110" ry="14" fill="#0002"/>
        <path d="M30 112 Q130 150 230 112 L224 128 Q130 168 36 128Z" fill="#d79a4a"/>
        <path d="M44 98 Q70 84 100 92 Q130 80 160 92 Q190 82 216 98 L214 110 Q130 124 46 110Z" fill="#7dbb4a"/>
        <path d="M8 96 Q40 70 120 78 Q200 72 236 92 L252 78 L248 104 L252 126 L234 108 Q190 118 120 112 Q40 116 8 96Z" fill="#c9cfd6"/>
        <path d="M30 92 Q120 84 230 96" stroke="#8d97a3" stroke-width="3" fill="none"/>
        <circle cx="32" cy="92" r="4" fill="#24303a"/>
        <circle cx="96" cy="104" r="9" fill="none" stroke="#b07ac0" stroke-width="4"/>
        <circle cx="160" cy="104" r="9" fill="none" stroke="#b07ac0" stroke-width="4"/>
        <path d="M34 92 Q130 30 226 92 Q130 70 34 92Z" fill="#e2ad5f"/>
        <path d="M70 66 l6 -4 M110 52 l6 -3 M150 54 l6 3 M188 66 l6 4" stroke="#fff8" stroke-width="3" stroke-linecap="round"/>
      </svg>
      <!-- Wiener Schnitzel -->
      <svg viewBox="0 0 260 180" data-i="1">
        <ellipse cx="130" cy="98" rx="122" ry="72" fill="#f5f2ec" stroke="#d8d2c6" stroke-width="3"/>
        <path d="M34 98 Q30 52 92 46 Q130 30 178 48 Q232 56 226 102 Q232 146 170 150 Q130 164 86 150 Q28 146 34 98Z" fill="#d9952f"/>
        <path d="M50 96 Q48 62 96 58 Q130 46 172 60 Q214 66 210 100 Q214 134 166 138 Q130 150 92 138 Q50 134 50 96Z" fill="#e8ad46"/>
        <g fill="#b97821"><circle cx="80" cy="80" r="3"/><circle cx="120" cy="66" r="3"/><circle cx="160" cy="84" r="3"/><circle cx="104" cy="112" r="3"/><circle cx="150" cy="120" r="3"/><circle cx="186" cy="104" r="3"/><circle cx="70" cy="118" r="3"/></g>
        <path d="M168 40 a26 26 0 0 1 44 22 Z" fill="#f4e04d" stroke="#d9c22c" stroke-width="3"/>
        <path d="M172 44 L200 58 M186 38 L198 58" stroke="#fff7" stroke-width="2"/>
        <path d="M46 52 q10 -14 22 -4 q8 -12 18 0" stroke="#3f8f3a" stroke-width="5" fill="none" stroke-linecap="round"/>
      </svg>
    </div>
    <p class="dish" id="dish">Wiener Schnitzel</p>
    <div class="slider-wrap">
      <label for="accent" class="value" style="display:block">Akzentstärke: <b id="accent-val">1,0</b></label>
      <input id="accent" type="range" min="0" max="1.6" step="0.05" value="1" aria-describedby="dish">
      <div class="stops">
        <button data-v="0" style="left:0">Hochdeutsch</button><button data-v="1.6" style="right:0">Österreichisch</button>
      </div>
    </div>
  </section>

  <section class="card" aria-label="Sprechen">
    <label for="text" class="value" style="display:block;text-align:left;margin:0 0 6px">Was soll ich sagen?</label>
    <textarea id="text" rows="8">Da streiten sich die Leut herum
oft um den Wert des Glücks,
der eine heißt den andern dumm,
am End weiß keiner nix.
Da ist der allerärmste Mann
dem andern viel zu reich,
das Schicksal setzt den Hobel an
und hobelt alles gleich.</textarea>
    <div class="source">Ferdinand Raimund, „Hobellied“ (1834). Oder schreib einfach etwas Eigenes.</div>
    <div class="row">
      <div class="seg" role="group" aria-label="Stimme">
        <button data-voice="M2" aria-pressed="true">Mann</button><button data-voice="F2" aria-pressed="false">Frau</button>
      </div>
      <label class="check"><input type="checkbox" id="vocos" checked disabled> Klangfilter</label>
      <button class="speak" id="speak" disabled>Lade …</button>
    </div>
    <div class="row filerow">
      <button class="ghost" id="open" type="button">Textdatei laden</button>
      <input type="file" id="file" accept=".txt,.md,text/plain" hidden>
      <select id="fmt" aria-label="Dateiformat"><option value="mp3">MP3</option><option value="wav">WAV</option></select>
      <button class="ghost" id="save" type="button" disabled>Als Datei speichern</button>
      <span class="hint">Auch lange Texte: einfügen oder als .txt laden, mit Stimme und Akzent von oben. Gerechnet wird auf deinem Gerät, nichts wird hochgeladen.</span>
    </div>
    <div class="status" id="status">Lade die Modelle (einmalig, danach im Browser gespeichert) …</div>
    <div class="bar"><i id="bar"></i></div>
  </section>

  <section class="card info">
    <h2>Warum österreichisch?</h2>
    <p>Sprachausgaben klingen fast immer bundesdeutsch. apptor-tiny-tts soll eine Alternative sein, und zwar dort, wo große
      Modelle keinen Platz haben: im Browser, am Handy, in kleinen Apps. Der Akzent ist noch dezent und sitzt nicht in jedem
      Wort: besser als nichts, aber noch nicht ganz österreichisch. Das hier ist Version 0.2, trainiert auf einer einzigen Grafikkarte mit freien Aufnahmen von Freiwilligen
      aus <a href="https://commonvoice.mozilla.org/de" rel="noopener">Mozilla Common Voice</a>. Die beiden Stimmen sind
      synthetisch; das Modell hat den Tonfall gelernt, nicht die Stimmen der Sprecherinnen und Sprecher.</p>
    <p>Was hier passiert: Beim ersten Besuch lädt dein Browser einmalig rund 50 MB (Aussprache, Stimme, Klangfilter) und
      rechnet danach alles selbst. Dein Text verlässt das Gerät nicht.</p>
  </section>

  <section class="card info">
    <h2>Selbst verwenden</h2>
    <ul class="use">
      <li><b>Im Web.</b> Diese Seite ist das ganze Paket: ein paar Dateien, kein Build, keine Abhängigkeiten. Den Ordner von
        Hugging Face auf den eigenen Webserver legen, und die Stimme läuft auch dort.</li>
      <li><b>In Python.</b> Dieselben Modelle als ONNX, dazu ein kleines Skript. Es braucht nur onnxruntime, kein eSpeak, keine GPU.</li>
      <li><b>Als ganzer Sprachassistent.</b> <a href="https://github.com/ViktorsGitUser/flash-voice" rel="noopener">flash-voice</a>
        auf GitHub verbindet Hören, Entscheiden und Sprechen; apptor-tiny-tts ist dort die deutsche Stimme.</li>
    </ul>
    <div class="links">
      <a class="btn" href="https://huggingface.co/apptor/apptor-tiny-tts-at" rel="noopener">Hugging Face</a>
      <a class="btn" href="https://github.com/ViktorsGitUser/flash-voice" rel="noopener">GitHub</a>
    </div>
    <p class="small">Apache 2.0. Gebaut auf <a href="https://github.com/n4ze3m/tiny-voca" rel="noopener">tiny-voca</a> von Nazeem,
      Klangfilter <a href="https://github.com/gemelo-ai/vocos" rel="noopener">Vocos</a> (MIT), Laufzeit
      <a href="https://onnxruntime.ai" rel="noopener">ONNX Runtime Web</a> (MIT), MP3 mit
      <a href="https://lame.sourceforge.io" rel="noopener">LAME</a> (lamejs, LGPL, als eigene Datei <a href="lame/lame.min.js">lame/</a>).</p>
  </section>
  <footer>
    <a href="https://apptor.at" rel="noopener">apptor.at</a> ·
    Feedback gern auf <a href="https://x.com/apptor_at" rel="noopener">X @apptor_at</a>
  </footer>
</main>

<script type="module">
import * as ort from "./ort/ort.wasm.min.mjs";
import {G2P, makeFrontEnd, Voice} from "./apptor-tts.js?v=9";

ort.env.wasm.wasmPaths = new URL("./ort/", location.href).href;
ort.env.wasm.numThreads = self.crossOriginIsolated ? Math.min(4, navigator.hardwareConcurrency || 2) : 1;
ort.env.wasm.proxy = true;   // run the models in a Web Worker, so the page stays responsive while it computes

const $ = id => document.getElementById(id);
const STOPS = [
  {v: 0, hi: "Moin!", dish: "Fischbrötchen", bg: ["#2f7aa0", "#d8eef6"]},
  {v: 1.0, hi: "Servus!", dish: "Wiener Schnitzel", bg: ["#c8102e", "#f6e9ea"]},
];
const svgs = [...document.querySelectorAll("#food svg")];
let voiceName = "M2", voice = null, playing = null, ctx = null, spoken = false;

function mix(a, b, t) {
  const p = h => [1, 3, 5].map(i => parseInt(h.slice(i, i + 2), 16));
  const x = p(a), y = p(b);
  return "#" + x.map((v, i) => Math.round(v + (y[i] - v) * t).toString(16).padStart(2, "0")).join("");
}

function showAccent(v) {
  $("accent-val").textContent = v.toFixed(2).replace(/0$/, "").replace(".", ",");
  let i = 0;
  while (i < STOPS.length - 2 && v > STOPS[i + 1].v) i++;
  const a = STOPS[i], b = STOPS[i + 1], t = Math.max(0, Math.min(1, (v - a.v) / (b.v - a.v)));
  svgs.forEach((s, k) => {
    const o = k === i ? 1 - t : k === i + 1 ? t : 0;
    s.style.opacity = o; s.style.transform = `scale(${0.8 + 0.2 * o}) rotate(${(1 - o) * (k > i ? 8 : -8)}deg)`;
  });
  const near = t < 0.5 ? a : b;
  if ($("greet").textContent !== near.hi) { $("greet").textContent = near.hi; $("greet").classList.remove("wobble"); void $("greet").offsetWidth; $("greet").classList.add("wobble"); }
  $("dish").textContent = near.dish;
  document.body.style.setProperty("--bg-a", mix(a.bg[0], b.bg[0], t));
  document.body.style.setProperty("--bg-b", mix(a.bg[1], b.bg[1], t));
}

$("accent").addEventListener("input", e => showAccent(Number(e.target.value)));
$("accent").addEventListener("change", () => { if (spoken) speak(); });
document.querySelectorAll(".stops button").forEach(b => b.addEventListener("click", () => {
  $("accent").value = b.dataset.v; showAccent(Number(b.dataset.v)); if (spoken) speak();
}));
document.querySelectorAll(".seg button").forEach(b => b.addEventListener("click", () => {
  voiceName = b.dataset.voice;
  document.querySelectorAll(".seg button").forEach(x => x.setAttribute("aria-pressed", String(x === b)));
  if (spoken) speak();
}));
showAccent(1.0);

// ---- loading with progress, kept in the Cache Storage for the next visit
const CACHE = "apptor-tiny-tts-v1";
async function fetchBytes(url, onProgress) {
  let cache = null;
  try { cache = await caches.open(CACHE); const hit = await cache.match(url); if (hit) { const b = await hit.arrayBuffer(); onProgress(b.byteLength, b.byteLength); return new Uint8Array(b); } } catch {}
  const r = await fetch(url);
  if (!r.ok) throw new Error(`${url}: ${r.status}`);
  const total = Number(r.headers.get("content-length")) || 0, reader = r.body.getReader(), parts = [];
  let got = 0;
  for (;;) { const {done, value} = await reader.read(); if (done) break; parts.push(value); got += value.length; onProgress(got, total); }
  const out = new Uint8Array(got); let o = 0; for (const p of parts) { out.set(p, o); o += p.length; }
  try { await cache?.put(url, new Response(out.slice(), {headers: {"content-type": "application/octet-stream"}})); } catch {}
  return out;
}

const SIZES = {"models/apptor-g2p-de.onnx": 12.4, "models/apptor-tiny-tts-at2.onnx": 21.4, "models/apptor-vocos-de.int8.onnx": 16.7};
const done = {};
function progress(url, got, total) {
  done[url] = got / 1e6;
  const all = Object.values(SIZES).reduce((a, b) => a + b, 0), have = Object.values(done).reduce((a, b) => a + b, 0);
  $("bar").style.width = `${Math.min(100, 100 * have / all)}%`;
  $("status").textContent = `Lade die Modelle: ${have.toFixed(0)} von ${all.toFixed(0)} MB (einmalig, danach im Browser gespeichert)`;
}
const json = async u => (await fetch(u)).json();
const opts = {executionProviders: ["wasm"], graphOptimizationLevel: "all"};

async function load() {
  const [F, exceptions, austrian, g2pMeta, ttsMeta] = await Promise.all(["frontend-de.json", "exceptions-de.json?v=5", "austrian-de.json?v=3", "models/apptor-g2p-de.json", "models/apptor-tiny-tts-at2.json"].map(json));
  const [g2pBytes, ttsBytes] = await Promise.all(["models/apptor-g2p-de.onnx", "models/apptor-tiny-tts-at2.onnx"].map(u => fetchBytes(u, (g, t) => progress(u, g, t))));
  const g2p = new G2P(ort, await ort.InferenceSession.create(g2pBytes, opts), g2pMeta, exceptions, austrian);
  voice = new Voice(ort, await ort.InferenceSession.create(ttsBytes, opts), ttsMeta, makeFrontEnd(F, g2p));
  $("speak").disabled = false; $("speak").textContent = "Sprechen"; $("save").disabled = false;
  $("status").textContent = "Bereit. Der Klangfilter lädt noch im Hintergrund.";
  const u = "models/apptor-vocos-de.int8.onnx";
  const vb = await fetchBytes(u, (g, t) => progress(u, g, t));
  voice.setVocos(await ort.InferenceSession.create(vb, opts));
  $("vocos").disabled = false;
  $("status").textContent = "Bereit. Alles ist geladen und bleibt im Browser gespeichert.";
}
load().catch(e => { $("status").textContent = "Das Laden hat nicht geklappt: " + e.message; console.error(e); });

let busy = false, again = false, sources = [];
function stopAudio() { for (const x of sources) { try { x.stop(); } catch {} } sources = []; }
async function speak() {
  if (!voice) return;
  if (busy) { again = true; return; }   // the running job stops after its current sentence, then this starts
  busy = true; spoken = true;
  $("speak").disabled = true; $("speak").textContent = "Rechne …";
  const keep = voice.vocos;
  try {
    ctx ??= new (window.AudioContext || window.webkitAudioContext)();
    await ctx.resume();
    stopAudio();
    if (!$("vocos").checked) voice.vocos = null;
    const sr = voice.meta.sample_rate, t = performance.now();
    let at = 0, first = 0;
    const audio = await voice.speak($("text").value, {voice: voiceName, accent: Number($("accent").value), cancelled: () => again,
      onChunk: (a, i) => {   // play each sentence as soon as it is ready
        const buf = ctx.createBuffer(1, a.length, sr);
        buf.copyToChannel(a, 0);
        const src = ctx.createBufferSource(); src.buffer = buf; src.connect(ctx.destination);
        if (i === 0) { first = performance.now() - t; at = ctx.currentTime + 0.05; } else at += 0.12;
        at = Math.max(at, ctx.currentTime + 0.02);
        src.start(at); at += buf.duration; sources.push(src);
      }});
    if (again) return;
    if (!audio.length) throw new Error("kein Text zum Sprechen");
    const ms = performance.now() - t;
    $("status").textContent = `${(audio.length / sr).toFixed(1)} s Sprache, auf deinem Gerät gerechnet: erster Satz nach ${(first / 1000).toFixed(2)} s, alles nach ${(ms / 1000).toFixed(2)} s.`;
  } catch (e) {
    $("status").textContent = "Fehler: " + e.message; console.error(e);
  } finally {
    voice.vocos = keep;
    busy = false; $("speak").disabled = false; $("speak").textContent = "Sprechen";
    if (again) { again = false; speak(); }
  }
}
$("speak").addEventListener("click", speak);

// ---- long texts: load a text file, save the speech as MP3 or WAV (all on this device)
$("open").addEventListener("click", () => $("file").click());
$("file").addEventListener("change", async e => {
  const f = e.target.files[0];
  if (!f) return;
  const bytes = new Uint8Array(await f.arrayBuffer());
  let text;
  try { text = new TextDecoder("utf-8", {fatal: true}).decode(bytes); } catch { text = new TextDecoder("windows-1252").decode(bytes); }
  $("text").value = text.replace(/^/, "");
  $("status").textContent = `„${f.name}“ geladen: ${text.length.toLocaleString("de-AT")} Zeichen.`;
  e.target.value = "";
});

let lame = null;
function loadLame() {   // the MP3 encoder (LAME, LGPL) as its own file, only when it is needed
  return lame ??= new Promise((ok, fail) => {
    const s = document.createElement("script"); s.src = "lame/lame.min.js";
    s.onload = () => ok(window.lamejs); s.onerror = () => { lame = null; fail(new Error("MP3-Encoder nicht geladen")); };
    document.head.append(s);
  });
}
function wavFile(parts, sr) {
  const n = parts.reduce((a, p) => a + p.length, 0), h = new DataView(new ArrayBuffer(44));
  const w = (o, t) => [...t].forEach((c, i) => h.setUint8(o + i, c.charCodeAt(0)));
  w(0, "RIFF"); h.setUint32(4, 36 + n * 2, true); w(8, "WAVEfmt "); h.setUint32(16, 16, true); h.setUint16(20, 1, true);
  h.setUint16(22, 1, true); h.setUint32(24, sr, true); h.setUint32(28, sr * 2, true); h.setUint16(32, 2, true);
  h.setUint16(34, 16, true); w(36, "data"); h.setUint32(40, n * 2, true);
  return new Blob([h, ...parts], {type: "audio/wav"});
}
let saving = false, stopSave = false;
$("save").addEventListener("click", async () => {
  if (saving) { stopSave = true; return; }
  if (!voice || busy) return;
  const text = $("text").value.trim();
  if (!text) { $("status").textContent = "Kein Text."; return; }
  saving = true; stopSave = false;
  $("save").textContent = "Abbrechen"; $("speak").disabled = true;
  const fmt = $("fmt").value, keep = voice.vocos, sr = voice.meta.sample_rate, gap = new Int16Array(Math.round(0.12 * sr));
  const out = [];   // encoded MP3 blocks or 16-bit PCM pieces - the audio itself is not kept
  let seconds = 0, enc = null;
  const t0 = performance.now();
  try {
    if (fmt === "mp3") enc = new (await loadLame()).Mp3Encoder(1, sr, 64);
    if (!$("vocos").checked) voice.vocos = null;
    const push = pcm => {
      if (!enc) { out.push(pcm); return; }
      for (let i = 0; i < pcm.length; i += 1152) { const b = enc.encodeBuffer(pcm.subarray(i, i + 1152)); if (b.length) out.push(new Uint8Array(b)); }
    };
    await voice.speak(text, {voice: voiceName, accent: Number($("accent").value), keep: false, cancelled: () => stopSave,
      onChunk: (a, i) => {
        const pcm = new Int16Array(a.length);
        for (let k = 0; k < a.length; k++) pcm[k] = Math.max(-32768, Math.min(32767, Math.round(a[k] * 32767)));
        if (i) push(gap);
        push(pcm); seconds += a.length / sr + (i ? 0.12 : 0);
      },
      onProgress: (d, n) => {
        $("bar").style.width = `${100 * d / n}%`;
        $("status").textContent = `Datei: ${d} von ${n} Sätzen, ${Math.round(seconds)} s Sprache …`;
      }});
    if (stopSave) { $("status").textContent = "Abgebrochen."; return; }
    if (!seconds) throw new Error("kein Text zum Sprechen");
    if (enc) { const b = enc.flush(); if (b.length) out.push(new Uint8Array(b)); }
    const blob = enc ? new Blob(out, {type: "audio/mpeg"}) : wavFile(out, sr);
    const a = document.createElement("a");
    a.href = URL.createObjectURL(blob);
    a.download = `apptor-tiny-tts-${voiceName === "M2" ? "mann" : "frau"}-akzent-${$("accent").value.replace(".", "_")}.${fmt}`;
    document.body.append(a); a.click(); a.remove();
    setTimeout(() => URL.revokeObjectURL(a.href), 60000);
    const mm = Math.floor(seconds / 60), ss = Math.round(seconds % 60);
    $("status").textContent = `Fertig: ${mm ? mm + " min " : ""}${ss} s Sprache als ${fmt.toUpperCase()} (${(blob.size / 1e6).toFixed(1)} MB), gerechnet in ${((performance.now() - t0) / 1000).toFixed(0)} s.`;
  } catch (e) {
    $("status").textContent = "Fehler: " + e.message; console.error(e);
  } finally {
    voice.vocos = keep; saving = false;
    $("save").textContent = "Als Datei speichern"; $("speak").disabled = false;
  }
});
</script>
</body>
</html>