little-tts / web /worker.js
TuringLogic's picture
Narrative examples, voice 92 recommendation, and context-preserving browser generation
57494e6 verified
Raw History Blame Contribute Delete
5.82 kB
// Source attribution and licence notices are retained in NOTICE.
// Runs the phonemizer (dizione, through Pyodide) and the model (ONNX Runtime Web)
// off the main thread. Messages in: {type: "load"} and {type: "speak", id, text,
// voice, speed}. Messages out: status, ready, sentence, done, error.
const PYODIDE = "https://cdn.jsdelivr.net/pyodide/v0.26.4/full/";
const ORT = "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.20.1/dist/";
const DATA = ["lexicon.tsv", "align.json", "g2p.json", "g2p.npz", "homographs.json"];
importScripts(PYODIDE + "pyodide.js", ORT + "ort.wasm.min.js", "speech-plan.js");
let pyodide, phonemize, session, config;
const status = (text, progress) => postMessage({ type: "status", text, progress });
// Bump when the files under assets/ change, so browsers fetch them again.
const VERSION = "little-tts-104000-5";
// Downloaded files are kept in the browser's cache storage: a second visit starts without the network.
async function download(url, label) {
const key = `${url}?v=${VERSION}`;
let store = null;
try {
store = await caches.open("little-tts-" + VERSION);
const hit = await store.match(key);
if (hit) return new Uint8Array(await hit.arrayBuffer());
} catch (error) { store = null; }
const bytes = await fetchBytes(key, label);
if (store) store.put(key, new Response(bytes)).catch(() => {});
return bytes;
}
async function fetchBytes(url, label) {
const response = await fetch(url);
if (!response.ok) throw new Error(`${url}: ${response.status}`);
const total = Number(response.headers.get("content-length")) || 0;
if (!response.body || !total) return new Uint8Array(await response.arrayBuffer());
const reader = response.body.getReader();
const chunks = [];
let received = 0;
for (;;) {
const { done, value } = await reader.read();
if (done) break;
chunks.push(value);
received += value.length;
status(label, received / total);
}
const bytes = new Uint8Array(received);
let offset = 0;
for (const chunk of chunks) { bytes.set(chunk, offset); offset += chunk.length; }
return bytes;
}
async function loadPhonemizer() {
status("Loading the phonemizer");
pyodide = await loadPyodide({ indexURL: PYODIDE });
await pyodide.loadPackage("numpy");
const [source, ...files] = await Promise.all([
download("assets/dizione.zip", "Loading the phonemizer"),
...DATA.map((name) => download("assets/phonemizer/" + name, "Loading the dictionary")),
]);
status("Installing the pronunciation dictionary");
pyodide.FS.mkdirTree("/dizione-data");
DATA.forEach((name, index) => pyodide.FS.writeFile("/dizione-data/" + name, files[index]));
pyodide.unpackArchive(source.buffer, "zip", { extractDir: "/dizione-src" });
status("Starting the pronunciation engine");
pyodide.runPython(`
import json, os, sys
os.environ["DIZIONE_DATA"] = "/dizione-data"
sys.path.insert(0, "/dizione-src")
import dizione
def phonemize_json(text):
result = dizione.phonemize(text)
return json.dumps({"ids": list(result.ids), "stress": list(result.stress), "text": str(result)})
phonemize_json("Warm up.")
`);
phonemize = pyodide.globals.get("phonemize_json");
status("Pronunciation engine ready");
}
async function loadModel() {
config = await (await fetch("assets/config.json")).json();
const bytes = await download("assets/model.onnx", "Loading the model");
status("Starting the model");
ort.env.wasm.wasmPaths = ORT;
ort.env.wasm.numThreads = 1;
ort.env.wasm.proxy = false;
session = await ort.InferenceSession.create(bytes, { executionProviders: ["wasm"] });
status("ONNX model ready");
}
async function synthesize(phones, voice, speed) {
const count = phones.ids.length;
const integers = (values) => new ort.Tensor("int64", BigInt64Array.from(values.map(BigInt)), [1, count]);
const scalar = (value) => new ort.Tensor("float32", Float32Array.from([value]), [1]);
const output = await session.run({
tokens: integers(phones.ids), stress: integers(phones.stress),
voice: new ort.Tensor("int64", BigInt64Array.from([BigInt(voice)]), [1]),
speed: scalar(speed), pitch_shift: scalar(0), pitch_range: scalar(1),
});
return output.audio.data;
}
async function speak({ id, text, voice, speed }) {
if (!Number.isInteger(voice) || voice < 0 || voice >= config.voices.length) throw new Error("Choose a valid voice.");
const started = performance.now();
const planStarted = performance.now();
const list = planSpeech(text, part => JSON.parse(phonemize(part)), Math.min(config.max_tokens - 12, 256));
const chunks = [];
const phonemeTime = performance.now() - planStarted;
let modelTime = 0;
for (let index = 0; index < list.length; index++) {
const phones = list[index];
const mark = performance.now();
const audio = await synthesize(phones, voice, speed);
modelTime += performance.now() - mark;
chunks.push(audio);
postMessage({ type: "sentence", id, index, total: list.length, phonemes: phones.text });
}
const length = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
const audio = new Float32Array(length);
let offset = 0;
for (const chunk of chunks) { audio.set(chunk, offset); offset += chunk.length; }
postMessage({ type: "done", id, voice: config.voices[voice], audio, sampleRate: config.sample_rate, phonemeTime, modelTime,
totalTime: performance.now() - started }, [audio.buffer]);
}
onmessage = async ({ data }) => {
try {
if (data.type === "load") {
await Promise.all([loadPhonemizer(), loadModel()]);
postMessage({ type: "ready", voices: config.voices });
} else if (data.type === "speak") {
await speak(data);
}
} catch (error) {
postMessage({ type: "error", id: data.id, message: String(error && error.message || error) });
}
};