Spaces:
Running
Running
Voice switching: one model in memory at a time (previous session released), loads serialized, models cached in the browser; defaults T 0.8 / tempo 0.8
Browse files- index.html +22 -9
index.html
CHANGED
|
@@ -59,7 +59,7 @@ footer img{height:30px;display:block}
|
|
| 59 |
<details><summary style="font-size:12px;color:var(--mut);cursor:pointer">advanced: pitch range, temperature, tempo, English words, ODE steps, backend</summary>
|
| 60 |
<div style="margin-top:8px;display:flex;flex-direction:column;gap:8px">
|
| 61 |
<label class="f" style="margin:0" title="how much the pitch moves within a sentence (style token, normal tempo, statement)">Pitch range<select id="style"></select></label>
|
| 62 |
-
<div class="knob"><span>temperature</span><input type="range" id="temperature" min="0" max="1.5" step="0.05" value="
|
| 63 |
<div class="knob"><span>tempo (length scale)</span><input type="range" id="length_scale" min="0.6" max="1.5" step="0.05" value="0.8" oninput="upd()"><b id="length_scale_v"></b></div>
|
| 64 |
<label class="f" style="margin:0;flex-direction:row;align-items:center;gap:6px" title="On: English words in Polish text (meeting, feedback, YouTube) are found automatically and read in English. Off: everything is read as Polish; words in {braces} are still read in English."><input type="checkbox" id="english" checked> read English words in English</label>
|
| 65 |
<details style="margin-top:2px"><summary style="font-size:12px;color:var(--mut);cursor:pointer">pronunciation dictionary</summary>
|
|
@@ -95,9 +95,9 @@ footer img{height:30px;display:block}
|
|
| 95 |
<script>window.coi={coepCredentialless:()=>true}</script><script src="coi-serviceworker.js"></script>
|
| 96 |
<script src="https://cdn.jsdelivr.net/npm/onnxruntime-web@1.30.0/dist/ort.all.min.js" crossorigin="anonymous"></script>
|
| 97 |
<script type="module">
|
| 98 |
-
import { textToIpa, ipaToIds, initPhonemizer } from "./cleaner.js?v=
|
| 99 |
-
import { initEnglish, markEnglish } from "./english.js?v=
|
| 100 |
-
import { EFFECTS, DEFAULTS, UI_DEFAULTS, applyChain } from "./effects.js?v=
|
| 101 |
const $=id=>document.getElementById(id); const SR=22050;
|
| 102 |
const LOCAL = location.hostname==="localhost"||location.hostname==="127.0.0.1";
|
| 103 |
const MODEL_BASE = LOCAL ? "/release-v2/" : "https://huggingface.co/machinekind/Matcha-TTS-PL/resolve/main/";
|
|
@@ -120,12 +120,22 @@ async function fetchWithRetry(url, onp, tries=4){
|
|
| 120 |
async function fetchWithProgress(url, onp){ const r=await fetch(url); if(!r.ok) throw new Error(r.status+" "+url); const total=+r.headers.get("content-length")||0; const reader=r.body.getReader(); const chunks=[]; let got=0; for(;;){ const {done,value}=await reader.read(); if(done) break; chunks.push(value); got+=value.length; if(total) onp(got/total) } const buf=new Uint8Array(got); let o=0; for(const c of chunks){ buf.set(c,o); o+=c.length } return buf }
|
| 121 |
function tensor(seq){ return {x:new ort.Tensor('int64',BigInt64Array.from(seq.map(BigInt)),[1,seq.length]), x_lengths:new ort.Tensor('int64',BigInt64Array.from([BigInt(seq.length)]),[1])} }
|
| 122 |
let LOADING=null, LOADING_KEY=null;
|
| 123 |
-
function loadModel(){ const key=VOICE.id+'|'+$('model').value+'|'+$('backend').value; if(sess && sessKey===key) return Promise.resolve(sess); if(LOADING && LOADING_KEY===key) return LOADING; LOADING_KEY=key; LOADING=_loadModel().finally(()=>{ LOADING=null; LOADING_KEY=null }); return LOADING }
|
| 124 |
-
async function _loadModel(){ const kind=$('model').value, be=$('backend').value, key=VOICE.id+'|'+kind+'|'+be; if(sess && sessKey===key) return sess; $('prog').hidden=false; $('status').textContent='downloading model\u2026'; const t0=performance.now(); const bytes=await
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
document.querySelectorAll('#voices button').forEach(b=>b.classList.toggle('on', b.dataset.v===VOICE.id));
|
| 127 |
VJ=await (await fetch(MODEL_BASE+VOICE.path+'voices.json',{cache:'no-cache'})).json(); setMix(VJ.presets[VJ.default].weights);
|
| 128 |
-
if(VOICE.defaults
|
| 129 |
if(load) await prefetch() }
|
| 130 |
async function init(){ try{ try{ const ix=await (await fetch(MODEL_BASE+'onnx/voices_index.json',{cache:'no-cache'})).json(); if(ix.voices&&ix.voices.length) VOICES=ix.voices }catch(e){ console.warn('voices_index', e) }
|
| 131 |
$('voices').innerHTML=VOICES.map(v=>`<button class="sec" data-v="${v.id}" title="${(v.description||'').replace(/"/g,'"')}">${v.label}</button>`).join('');
|
|
@@ -153,7 +163,10 @@ function lexRules(){
|
|
| 153 |
function applyLexicon(text){ let t=text; for(const [rx,rep] of lexRules()) t=t.replace(rx, rep); return t }
|
| 154 |
async function ttsChunk(s, text){
|
| 155 |
const t=applyLexicon(text); const ipa=await textToIpa($('english').checked?markEnglish(t):t); const {seq,unknown}=ipaToIds(ipa); if(unknown.length) console.warn('unknown symbols',unknown);
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
| 157 |
const n=Number(out.wav_lengths.data[0]); const w=Float32Array.from(out.wav.data.slice(0,n));
|
| 158 |
return VOICE.highpass_hz ? highpass(w, SR, VOICE.highpass_hz) : w; // per-voice post-filter (voices_index.json), e.g. female_voxcpm_1: removes "p" pops learned from its reference clip
|
| 159 |
}
|
|
|
|
| 59 |
<details><summary style="font-size:12px;color:var(--mut);cursor:pointer">advanced: pitch range, temperature, tempo, English words, ODE steps, backend</summary>
|
| 60 |
<div style="margin-top:8px;display:flex;flex-direction:column;gap:8px">
|
| 61 |
<label class="f" style="margin:0" title="how much the pitch moves within a sentence (style token, normal tempo, statement)">Pitch range<select id="style"></select></label>
|
| 62 |
+
<div class="knob"><span>temperature</span><input type="range" id="temperature" min="0" max="1.5" step="0.05" value="0.8" oninput="upd()"><b id="temperature_v"></b></div>
|
| 63 |
<div class="knob"><span>tempo (length scale)</span><input type="range" id="length_scale" min="0.6" max="1.5" step="0.05" value="0.8" oninput="upd()"><b id="length_scale_v"></b></div>
|
| 64 |
<label class="f" style="margin:0;flex-direction:row;align-items:center;gap:6px" title="On: English words in Polish text (meeting, feedback, YouTube) are found automatically and read in English. Off: everything is read as Polish; words in {braces} are still read in English."><input type="checkbox" id="english" checked> read English words in English</label>
|
| 65 |
<details style="margin-top:2px"><summary style="font-size:12px;color:var(--mut);cursor:pointer">pronunciation dictionary</summary>
|
|
|
|
| 95 |
<script>window.coi={coepCredentialless:()=>true}</script><script src="coi-serviceworker.js"></script>
|
| 96 |
<script src="https://cdn.jsdelivr.net/npm/onnxruntime-web@1.30.0/dist/ort.all.min.js" crossorigin="anonymous"></script>
|
| 97 |
<script type="module">
|
| 98 |
+
import { textToIpa, ipaToIds, initPhonemizer } from "./cleaner.js?v=20261012a";
|
| 99 |
+
import { initEnglish, markEnglish } from "./english.js?v=20261012a";
|
| 100 |
+
import { EFFECTS, DEFAULTS, UI_DEFAULTS, applyChain } from "./effects.js?v=20261012a";
|
| 101 |
const $=id=>document.getElementById(id); const SR=22050;
|
| 102 |
const LOCAL = location.hostname==="localhost"||location.hostname==="127.0.0.1";
|
| 103 |
const MODEL_BASE = LOCAL ? "/release-v2/" : "https://huggingface.co/machinekind/Matcha-TTS-PL/resolve/main/";
|
|
|
|
| 120 |
async function fetchWithProgress(url, onp){ const r=await fetch(url); if(!r.ok) throw new Error(r.status+" "+url); const total=+r.headers.get("content-length")||0; const reader=r.body.getReader(); const chunks=[]; let got=0; for(;;){ const {done,value}=await reader.read(); if(done) break; chunks.push(value); got+=value.length; if(total) onp(got/total) } const buf=new Uint8Array(got); let o=0; for(const c of chunks){ buf.set(c,o); o+=c.length } return buf }
|
| 121 |
function tensor(seq){ return {x:new ort.Tensor('int64',BigInt64Array.from(seq.map(BigInt)),[1,seq.length]), x_lengths:new ort.Tensor('int64',BigInt64Array.from([BigInt(seq.length)]),[1])} }
|
| 122 |
let LOADING=null, LOADING_KEY=null;
|
| 123 |
+
function loadModel(){ const key=VOICE.id+'|'+$('model').value+'|'+$('backend').value; if(sess && sessKey===key) return Promise.resolve(sess); if(LOADING && LOADING_KEY===key) return LOADING; if(LOADING) return LOADING.catch(()=>{}).then(()=>loadModel()); /* one load at a time */ LOADING_KEY=key; LOADING=_loadModel().finally(()=>{ LOADING=null; LOADING_KEY=null }); return LOADING }
|
| 124 |
+
async function _loadModel(){ const kind=$('model').value, be=$('backend').value, key=VOICE.id+'|'+kind+'|'+be; if(sess && sessKey===key) return sess; $('prog').hidden=false; $('status').textContent='downloading model\u2026'; const t0=performance.now(); const bytes=await cachedModel(MODEL_BASE+VOICE.path+`matcha_pl_${kind}.onnx`, VOICE.rev||'1', p=>{$('prog').value=Math.round(p*100)}); $('status').textContent=`model ${(bytes.length/1e6).toFixed(0)} MB in ${((performance.now()-t0)/1000).toFixed(1)} s \u00b7 creating session\u2026`; if(sess && sessKey===key){ $('prog').hidden=true; return sess } /* loaded meanwhile by a queued call */ await releaseSession(); sess=await ort.InferenceSession.create(bytes,{executionProviders:[be==='webgpu'?{name:'webgpu',preferredLayout:'NCHW'}:'wasm','wasm'],graphOptimizationLevel:'all'}); sessKey=key; $('status').textContent='warming up\u2026'; try{ await sess.run({...tensor([0,73,0]), scales:new ort.Tensor('float32',Float32Array.from([0.5,1]),[2]), spk_emb:new ort.Tensor('float32',Float32Array.from(VJ.speakers[0].emb),[1,64])}) }catch(e){ console.warn('warm-up',e) } $('prog').hidden=true; return sess }
|
| 125 |
+
// only one model in memory: free the previous voice's session (after any synthesis still running on it) before creating the next
|
| 126 |
+
let RUNS=0;
|
| 127 |
+
async function releaseSession(){ if(!sess) return; const old=sess; sess=null; sessKey=null; while(RUNS>0) await new Promise(r=>setTimeout(r,50)); try{ await old.release() }catch(e){ console.warn('release',e) } }
|
| 128 |
+
// downloaded models are kept in the browser's Cache Storage, so switching back to a voice does not download 140 MB again
|
| 129 |
+
async function cachedModel(url, rev, onp){
|
| 130 |
+
const key=url+(url.includes('?')?'&':'?')+'rev='+encodeURIComponent(rev); let c=null;
|
| 131 |
+
try{ c=await caches.open('matcha-tts-pl-models'); const hit=await c.match(key); if(hit){ $('status').textContent='loading model from the browser cache\u2026'; return new Uint8Array(await hit.arrayBuffer()) } }catch(e){ c=null }
|
| 132 |
+
const bytes=await fetchWithRetry(url, onp);
|
| 133 |
+
if(c){ try{ await c.put(key, new Response(bytes, {headers:{'content-type':'application/octet-stream'}})) }catch(e){ console.warn('model cache full or unavailable', e) } }
|
| 134 |
+
return bytes }
|
| 135 |
+
async function selectVoice(id, load=true){ if(RT) RT.stop=true; /* a running real-time stream ends before its model is released */ VOICE=VOICES.find(v=>v.id===id)||VOICES[0]; try{ localStorage.setItem('tts_pl_v2_voice', VOICE.id) }catch(e){}
|
| 136 |
document.querySelectorAll('#voices button').forEach(b=>b.classList.toggle('on', b.dataset.v===VOICE.id));
|
| 137 |
VJ=await (await fetch(MODEL_BASE+VOICE.path+'voices.json',{cache:'no-cache'})).json(); setMix(VJ.presets[VJ.default].weights);
|
| 138 |
+
if(VOICE.defaults){ if(VOICE.defaults.length_scale) $('length_scale').value=VOICE.defaults.length_scale; if(VOICE.defaults.temperature!=null) $('temperature').value=VOICE.defaults.temperature; upd() }
|
| 139 |
if(load) await prefetch() }
|
| 140 |
async function init(){ try{ try{ const ix=await (await fetch(MODEL_BASE+'onnx/voices_index.json',{cache:'no-cache'})).json(); if(ix.voices&&ix.voices.length) VOICES=ix.voices }catch(e){ console.warn('voices_index', e) }
|
| 141 |
$('voices').innerHTML=VOICES.map(v=>`<button class="sec" data-v="${v.id}" title="${(v.description||'').replace(/"/g,'"')}">${v.label}</button>`).join('');
|
|
|
|
| 163 |
function applyLexicon(text){ let t=text; for(const [rx,rep] of lexRules()) t=t.replace(rx, rep); return t }
|
| 164 |
async function ttsChunk(s, text){
|
| 165 |
const t=applyLexicon(text); const ipa=await textToIpa($('english').checked?markEnglish(t):t); const {seq,unknown}=ipaToIds(ipa); if(unknown.length) console.warn('unknown symbols',unknown);
|
| 166 |
+
// the session must still be the live one when the run starts (a queued voice switch may have released it meanwhile);
|
| 167 |
+
// once RUNS is counted, releaseSession() waits for this run to finish
|
| 168 |
+
for(;;){ if(!sess || s!==sess) s=await loadModel(); RUNS++; if(s===sess) break; RUNS-- }
|
| 169 |
+
let out; try{ out=await s.run({...tensor(seq), scales:new ort.Tensor('float32',Float32Array.from([+$('temperature').value,+$('length_scale').value]),[2]), spk_emb:new ort.Tensor('float32',spkEmb(),[1,64])}) } finally{ RUNS-- }
|
| 170 |
const n=Number(out.wav_lengths.data[0]); const w=Float32Array.from(out.wav.data.slice(0,n));
|
| 171 |
return VOICE.highpass_hz ? highpass(w, SR, VOICE.highpass_hz) : w; // per-voice post-filter (voices_index.json), e.g. female_voxcpm_1: removes "p" pops learned from its reference clip
|
| 172 |
}
|