// Runs eeh (crates/eeh-wasm: the Rust front end and model in WebAssembly) off the main thread. A module worker. // Messages in: {type: "load", weights, dictionary} urls; ".gz" ones are gunzipped // {type: "speak", id, text, voice, speed} // Messages out: status, ready, sentence (with its audio, as soon as it is made), done (with the whole clip), // error (plain: true when the message is meant for the reader as it is). // The text is split into sentences in Rust (eeh-frontend), so nothing here knows about Japanese. import init, { Synthesizer } from "./pkg/eeh_wasm.js"; let synth; const status = (text, progress) => postMessage({ type: "status", text, progress }); // Bump when the format of the cached files changes. const STORE = "eeh-rust-1"; // The body of a response, reporting how much of it has come (as a fraction of content-length, when it has one). function watched(response, label) { const total = Number(response.headers.get("content-length")) || 0; if (!response.body || !total) return response.body; let received = 0; return response.body.pipeThrough(new TransformStream({ transform(chunk, controller) { received += chunk.length; status(label, Math.min(1, received / total)); controller.enqueue(chunk); }, })); } // A file of the demo as bytes. Gzipped files (the 36 MB dictionary is 9 MB that way; a host needs no compression // settings) are gunzipped here, and the plain bytes are kept in Cache Storage with the validators of the response: // the next visit asks the server only "has it changed?" (a 304 costs no body) and starts from the stored bytes. // Another origin is not asked (that would need a CORS preflight): its file stays until the address changes. async function asset(address, label) { const url = new URL(address, self.location.href); const key = url.href; let store = null, kept = null; try { store = await caches.open(STORE); kept = await store.match(key); } catch (error) { store = null; } const headers = {}; if (kept && url.origin === self.location.origin) { const etag = kept.headers.get("x-etag"), modified = kept.headers.get("x-last-modified"); if (etag) headers["If-None-Match"] = etag; if (modified) headers["If-Modified-Since"] = modified; } let response = null; if (!kept || Object.keys(headers).length) { try { response = await fetch(key, { headers }); } catch (error) { if (!kept) throw error; // offline, and kept: use what is stored } } if (kept && (!response || response.status === 304 || (!response.ok && !Object.keys(headers).length))) { return new Uint8Array(await kept.arrayBuffer()); } if (!response.ok) throw new Error(`${key}: ${response.status}`); let body = watched(response, label); // A host that gzips on its own has been undone by the browser already (content-encoding is set then). if (url.pathname.endsWith(".gz") && !response.headers.get("content-encoding")) { body = body.pipeThrough(new DecompressionStream("gzip")); } const bytes = new Uint8Array(await new Response(body).arrayBuffer()); if (store) { const info = { "x-etag": response.headers.get("etag") || "", "x-last-modified": response.headers.get("last-modified") || "" }; store.put(key, new Response(bytes, { headers: info })).catch(() => {}); } return bytes; } async function load({ weights, dictionary }) { const started = performance.now(); status("Loading the model and the dictionary", 0); const [files] = await Promise.all([ Promise.all([asset(weights, "Loading the model"), asset(dictionary, "Loading the dictionary")]), init(), ]); const fetched = performance.now(); synth = new Synthesizer(files[0], files[1]); const built = performance.now(); synth.speak("こんにちは。", synth.voices()[0], {}); // warm up const warm = performance.now(); postMessage({ type: "ready", voices: synth.voices(), sampleRate: synth.sampleRate(), times: { files: fetched - started, synthesizer: built - fetched, warmup: warm - built, total: warm - started } }); } // The model sometimes peaks above 1.0, which would clip in a 16-bit file: a clip is scaled down as a whole to PEAK, as // Synthesizer.speak does. A sentence that is played before the rest is made is scaled on its own. const PEAK = 0.99; function limitPeak(samples) { let loudest = 0; for (let i = 0; i < samples.length; i++) loudest = Math.max(loudest, Math.abs(samples[i])); if (loudest > PEAK) for (let i = 0; i < samples.length; i++) samples[i] *= PEAK / loudest; } // Shown when no sentence of a text could be read. const JAPANESE_ONLY = "eeh only reads Japanese for now."; function speak({ id, text, voice, speed }) { const started = performance.now(); const chunks = []; let index = 0, firstAudio = 0; try { synth.speakEach(text, voice, { speed, peak: null }, (sentence, audio) => { chunks.push(audio); if (!firstAudio) firstAudio = performance.now() - started; // The page plays this sentence at once, before the whole clip is known: a copy, kept below full scale by itself; the clip keeps the original. const heard = audio.slice(); limitPeak(heard); postMessage({ type: "sentence", id, index: index++, text: sentence, audio: heard, sampleRate: synth.sampleRate(), time: performance.now() - started }, [heard.buffer]); return true; }); } catch (error) { const message = String(error && error.message || error); postMessage({ type: "error", id, message: message.startsWith("nothing in the text could be read") ? JAPANESE_ONLY : message, plain: message.startsWith("nothing in the text could be read") }); return; } const audio = new Float32Array(chunks.reduce((sum, chunk) => sum + chunk.length, 0)); let offset = 0; for (const chunk of chunks) { audio.set(chunk, offset); offset += chunk.length; } limitPeak(audio); postMessage({ type: "done", id, audio, sampleRate: synth.sampleRate(), sentences: index, firstAudio, totalTime: performance.now() - started }, [audio.buffer]); } onmessage = async ({ data }) => { try { if (data.type === "load") await load(data); else if (data.type === "speak") speak(data); } catch (error) { postMessage({ type: "error", id: data.id, message: String(error && error.message || error) }); } };