File size: 1,290 Bytes
ee603ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// the release text front end (js/kale-tts.mjs -> frontend/) as a line-in/line-out helper for non-JS runtimes.
// stdin: one JSON per line {"text": "...", "accent": "en-us"}; stdout: one JSON per line {"sentences": [{"text", "ipa", "ids"}]}
// ids are Kokoro-table ids (tokenizer.json) without the 0 pads. needs only node >= 18, no npm packages.
import { createInterface } from "node:readline"; import { readFileSync } from "node:fs";
const RD = new URL("../", import.meta.url).pathname;
const { KaleTTS } = await import(RD + "js/kale-tts.mjs");
const vocab = JSON.parse(readFileSync(RD + "models/kale-tts-1-nano/tokenizer.json", "utf8")).model.vocab;
const fe = Object.assign(Object.create(KaleTTS.prototype), { feBase: RD + "frontend/", _fe: {} });
for await (const line of createInterface({ input: process.stdin })) {
  if (!line.trim()) continue;
  try {
    const { text, accent = "en-us" } = JSON.parse(line); const f = await fe._frontEnd(accent);
    const sentences = f.sentences(text).map((s) => { const ipa = f.ipa(s); return { text: s, ipa, ids: [...ipa].map((c) => vocab[c]).filter((x) => x !== undefined) }; });
    process.stdout.write(JSON.stringify({ sentences }) + "\n");
  } catch (e) { process.stdout.write(JSON.stringify({ error: String(e?.message ?? e) }) + "\n"); }
}