File size: 6,413 Bytes
a42c72e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// 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) });
  }
};