File size: 2,261 Bytes
a4b7e51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!doctype html>
<html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>SmollerTTS local example</title>
<style>body{font:16px/1.6 system-ui;max-width:720px;margin:60px auto;padding:0 24px;color:#171717}textarea{box-sizing:border-box;width:100%;min-height:140px;padding:16px;font:inherit}button{font:inherit;padding:8px 18px;margin:12px 0}audio{display:block;width:100%;margin:16px 0}</style>
<h1>SmollerTTS</h1><p>English text to speech on your browser's CPU.</p>
<label for="text">Text</label><textarea id="text">The meeting moved to 3:30 pm on March 14th, so we have about 45 minutes left. It costs $12.99.</textarea>
<button id="generate" disabled>Generate speech</button><p id="status" role="status">Loading voice…</p>
<audio id="audio" controls></audio><a id="download" hidden download="smollertts.wav">Download WAV</a>
<script type="module">
import {wavBlob} from './audio.mjs';
const $=id=>document.getElementById(id);
const worker=new Worker(new URL('./worker.mjs',import.meta.url),{type:'module'});
let chunks=[],url,job=0;
worker.onmessage=({data})=>{
  if(data.type==='ready') {window.ready=true;$('generate').disabled=false;$('status').textContent='Ready';}
  if(data.type==='chunk') {chunks.push(data.wave);$('status').textContent=`Generating ${data.index+1} of ${data.total}…`;}
  if(data.type==='complete') {
    const wave=new Float32Array(chunks.reduce((n,a)=>n+a.length,0));let offset=0;
    for(const chunk of chunks){wave.set(chunk,offset);offset+=chunk.length;}
    if(url)URL.revokeObjectURL(url);url=URL.createObjectURL(wavBlob(wave));
    $('audio').src=url;$('download').href=url;$('download').hidden=false;
    $('generate').disabled=false;$('status').textContent='Ready to play';window.lastResult=data;
  }
  if(data.type==='error'||data.type==='load-error') {$('status').textContent=data.message;$('generate').disabled=!window.ready;window.failure=data.message;}
};
$('generate').onclick=()=>{chunks=[];window.lastResult=null;$('generate').disabled=true;$('status').textContent='Generating…';worker.postMessage({type:'generate',id:++job,text:$('text').value,controls:{speed:1,pitch:0,energy:0},pronunciation:{splitCompounds:true,punctuation:true}});};
</script></html>