@lantun/voice-kitten 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.js +2 -2
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@ Browser voices differ by device, and many sound robotic. This add-on gives every
|
|
|
6
6
|
|
|
7
7
|
**[Hear them in the live demo →](https://lantun.rzknkbr.xyz)** Open an English article, press play, and pick a voice from the voice menu.
|
|
8
8
|
|
|
9
|
-
<img src="https://raw.githubusercontent.com/rizkianakbar/lantun/v0.1.
|
|
9
|
+
<img src="https://raw.githubusercontent.com/rizkianakbar/lantun/v0.1.3/docs/screenshots/voice-dark.png" alt="The Lantun voice picker with lifelike and built-in voices">
|
|
10
10
|
|
|
11
11
|
## Install
|
|
12
12
|
|
package/dist/index.js
CHANGED
|
@@ -41,7 +41,7 @@ var n = `data:audio/wav;base64,${btoa(String.fromCharCode(...new Uint8Array(t(/*
|
|
|
41
41
|
Rosie: .9,
|
|
42
42
|
Hugo: .87,
|
|
43
43
|
Leo: .98
|
|
44
|
-
}, s = "https://huggingface.co/KittenML/kitten-tts-nano-0.8-int8/resolve/
|
|
44
|
+
}, s = "https://huggingface.co/KittenML/kitten-tts-nano-0.8-int8/resolve/84781d74e29ee25217551556398b42f80593a813", c = "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.24.2/dist", l = {
|
|
45
45
|
model: `${s}/kitten_tts_nano_v0_8.onnx`,
|
|
46
46
|
voices: `${s}/voices.npz`,
|
|
47
47
|
config: `${s}/config.json`,
|
|
@@ -350,7 +350,7 @@ function T(e, a = {}, s = window) {
|
|
|
350
350
|
};
|
|
351
351
|
}
|
|
352
352
|
// src/worker/worker.ts?worker-source
|
|
353
|
-
var E = "var R=Float32Array.from({length:512},(t,e)=>.5-.5*Math.cos(2*Math.PI*e/512)),Q=Float64Array.from({length:256},(t,e)=>Math.cos(2*Math.PI*e/512)),Y=Float64Array.from({length:256},(t,e)=>Math.sin(2*Math.PI*e/512)),X=tt(512);function tt(t){let e=new Uint16Array(t),r=Math.log2(t);for(let o=0;o<t;o++){let n=0;for(let i=0;i<r;i++)n|=(o>>i&1)<<r-1-i;e[o]=n}return e}function et(t,e,r=75){let o=2*Math.PI*r/e,n=Math.sin(o)/Math.SQRT2,i=Math.cos(o),c=1+n,s=(1+i)/2/c,a=-(1+i)/c,l=-2*i/c,f=(1-n)/c,p=0,m=0,y=0,h=0;for(let d=0;d<t.length;d++){let b=t[d],u=s*b+a*p+s*m-l*y-f*h;m=p,p=b,h=y,y=u,t[d]=u}}function k(t,e,r){for(let n=0;n<512;n++){let i=X[n];if(n<i){let c=t[n],s=e[n];t[n]=t[i],e[n]=e[i],t[i]=c,e[i]=s}}let o=r?1:-1;for(let n=2;n<=512;n<<=1){let i=n>>1,c=512/n;for(let s=0;s<512;s+=n)for(let a=0;a<i;a++){let l=Q[a*c],f=o*Y[a*c],p=s+a,m=p+i,y=t[m],h=e[m],d=y*l-h*f,b=y*f+h*l,u=t[p],w=e[p];t[m]=u-d,e[m]=w-b,t[p]=u+d,e[p]=w+b}}}function nt(t,e){let r=new Float64Array(e);for(let c=0;c<e;c++){let s=0;for(let a=0;a<257;a++)s+=t[c*257+a]**2;r[c]=s}let n=Array.from({length:e},(c,s)=>s).sort((c,s)=>r[c]-r[s]).slice(0,Math.max(4,Math.floor(e*.15))),i=new Float32Array(257);for(let c=0;c<257;c++){let s=0,a=0;for(let p of n){let m=t[p*257+c]??0;s+=m,a+=m*m}let l=s/n.length,f=Math.max(0,a/n.length-l*l);i[c]=l+1.5*Math.sqrt(f)}return i}function rt(t){let e=Math.min(120,t.length>>1);for(let r=0;r<e;r++){let o=r/e,n=t.length-1-r;t[r]=t[r]*o,t[n]=t[n]*o}}function I(t,e){let r=Float32Array.from(t);et(r,e);let o=Math.max(1,Math.ceil((r.length+512)/128)),n=new Float32Array(o*128+512);n.set(r,256);let i=new Float32Array(o*512),c=new Float32Array(o*512),s=new Float32Array(o*257);for(let h=0;h<o;h++){let d=i.subarray(h*512,(h+1)*512),b=c.subarray(h*512,(h+1)*512);for(let u=0;u<512;u++)d[u]=n[h*128+u]*R[u];k(d,b,!1);for(let u=0;u<257;u++){let w=d[u],A=b[u];s[h*257+u]=Math.sqrt(w*w+A*A)}}let a=nt(s,o),l=new Float32Array(n.length),f=new Float32Array(n.length),p=new Float32Array(257),m=new Float32Array(257).fill(1);for(let h=0;h<o;h++){for(let u=0;u<257;u++){let w=s[h*257+u]/(a[u]+1e-9),A=Math.min(1,Math.max(0,(w-1)/1));p[u]=.06+(1-.06)*A*A*(3-2*A)}for(let u=0;u<257;u++){let w=p[u],A=((p[u-1]??w)+2*w+(p[u+1]??w))/4;m[u]=Math.max(A,m[u]*.5)}let d=i.subarray(h*512,(h+1)*512),b=c.subarray(h*512,(h+1)*512);for(let u=0;u<257;u++){let w=m[u];d[u]=d[u]*w,b[u]=b[u]*w,u>0&&u<256&&(d[512-u]=d[u],b[512-u]=-b[u])}k(d,b,!0);for(let u=0;u<512;u++){let w=R[u],A=h*128+u;l[A]=l[A]+d[u]/512*w,f[A]=f[A]+w*w}}let y=new Float32Array(r.length);for(let h=0;h<r.length;h++){let d=f[h+256];y[h]=d>1e-6?l[h+256]/d:0}return rt(y),y}function L(t,e){let r=Math.round(e*.02),o=[],n=0;for(let s=0;s+r<=t.length;s+=r){let a=0;for(let l=s;l<s+r;l++){let f=t[l];a+=f*f,n=Math.max(n,Math.abs(f))}o.push(Math.sqrt(a/r))}if(!o.length||n===0)return t;o.sort((s,a)=>s-a);let i=o[Math.floor(o.length*.9)]??0,c=Math.min(2,Math.max(1/2,.2/(i||1)),.95/n);for(let s=0;s<t.length;s++)t[s]=t[s]*c;return t}function ot(t,e){let r=t.length*2,o=new ArrayBuffer(44+r),n=new DataView(o);function i(c,s){for(let a=0;a<s.length;a++)n.setUint8(c+a,s.charCodeAt(a))}i(0,\"RIFF\"),n.setUint32(4,36+r,!0),i(8,\"WAVE\"),i(12,\"fmt \"),n.setUint32(16,16,!0),n.setUint16(20,1,!0),n.setUint16(22,1,!0),n.setUint32(24,e,!0),n.setUint32(28,e*2,!0),n.setUint16(32,2,!0),n.setUint16(34,16,!0),i(36,\"data\"),n.setUint32(40,r,!0);for(let c=0;c<t.length;c++){let s=Math.max(-1,Math.min(1,t[c]));n.setInt16(44+c*2,s*32767,!0)}return o}var Tt=`data:audio/wav;base64,${btoa(String.fromCharCode(...new Uint8Array(ot(new Float32Array(240),24e3))))}`;async function st(t){let e=new Response(t).body;if(!e)throw new Error(\"Streams are unavailable\");let r=e.pipeThrough(new DecompressionStream(\"deflate-raw\"));return new Uint8Array(await new Response(r).arrayBuffer())}async function at(t){let e=new DataView(t),r=t.byteLength-22;for(;r>=0&&e.getUint32(r,!0)!==101010256;)r--;if(r<0)throw new Error(\"Not a zip file\");let o=e.getUint16(r+10,!0),n=e.getUint32(r+16,!0),i=new Map,c=new TextDecoder;for(let s=0;s<o;s++){if(e.getUint32(n,!0)!==33639248)throw new Error(\"Corrupt zip directory\");let a=e.getUint16(n+10,!0),l=e.getUint32(n+20,!0),f=e.getUint16(n+28,!0),p=e.getUint16(n+30,!0),m=e.getUint16(n+32,!0),y=e.getUint32(n+42,!0),h=c.decode(new Uint8Array(t,n+46,f)),d=y+30+e.getUint16(y+26,!0)+e.getUint16(y+28,!0),b=new Uint8Array(t,d,l);if(a===0)i.set(h,b);else if(a===8)i.set(h,await st(b));else throw new Error(`Unsupported zip method ${a}`);n+=46+f+p+m}return i}function it(t){let e=new DataView(t.buffer,t.byteOffset,t.byteLength),r=t[6]!==void 0&&t[6]>=2,o=r?e.getUint32(8,!0):e.getUint16(8,!0),n=(r?12:10)+o,i=new TextDecoder().decode(t.subarray(r?12:10,n)),c=/'descr':\\s*'([^']+)'/.exec(i)?.[1],s=(/'shape':\\s*\\(([^)]*)\\)/.exec(i)?.[1]??\"\").split(\",\").map(m=>m.trim()).filter(Boolean).map(Number);if(/'fortran_order':\\s*True/.test(i))throw new Error(\"Fortran-order arrays are not supported\");let a=s.length>1?s[0]??1:1,l=s.slice(s.length>1?1:0).reduce((m,y)=>m*y,1),f=t.slice(n),p;if(c===\"<f4\")p=new Float32Array(f.buffer);else if(c===\"<f8\")p=Float32Array.from(new Float64Array(f.buffer));else throw new Error(`Unsupported npy dtype ${c}`);return{data:p,rows:a,cols:l}}async function C(t){let e=new Map;for(let[r,o]of await at(t))r.endsWith(\".npy\")&&e.set(r.slice(0,-4),it(o));return e}var B=`[${';:,.!?\\xA1\\xBF\\u2014\\u2026\"\\xAB\\xBB\\u201C\\u201D()[]{}'.replace(/[\\]\\\\[^-]/g,\"\\\\$&\")}]`,ct=[\"$\",...';:,.!?\\xA1\\xBF\\u2014\\u2026\"\\xAB\\xBB\"\" ',...\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\",...\"\\u0251\\u0250\\u0252\\xE6\\u0253\\u0299\\u03B2\\u0254\\u0255\\xE7\\u0257\\u0256\\xF0\\u02A4\\u0259\\u0258\\u025A\\u025B\\u025C\\u025D\\u025E\\u025F\\u0284\\u0261\\u0260\\u0262\\u029B\\u0266\\u0267\\u0127\\u0265\\u029C\\u0268\\u026A\\u029D\\u026D\\u026C\\u026B\\u026E\\u029F\\u0271\\u026F\\u0270\\u014B\\u0273\\u0272\\u0274\\xF8\\u0275\\u0278\\u03B8\\u0153\\u0276\\u0298\\u0279\\u027A\\u027E\\u027B\\u0280\\u0281\\u027D\\u0282\\u0283\\u0288\\u02A7\\u0289\\u028A\\u028B\\u2C71\\u028C\\u0263\\u0264\\u028D\\u03C7\\u028E\\u028F\\u0291\\u0290\\u0292\\u0294\\u02A1\\u0295\\u02A2\\u01C0\\u01C1\\u01C2\\u01C3\\u02C8\\u02CC\\u02D0\\u02D1\\u02BC\\u02B4\\u02B0\\u02B1\\u02B2\\u02B7\\u02E0\\u02E4\\u02DE\\u2193\\u2191\\u2192\\u2197\\u2198'\\u0329'\\u1D7B\"],T=new Map(ct.map((t,e)=>[t,e])),N=/[\\p{L}\\p{M}\\p{N}_]+|[^\\p{L}\\p{M}\\p{N}_\\s]/gu,z=new RegExp(`^(${B}*)(.*?)(${B}*)$`,\"su\"),O=T.get(\" \");function ut(t){return t.replace(/[‘’‚‛′]/g,\"'\").replace(/[“”„‟″]/g,'\"').replace(/[–―]/g,\"\\u2014\").replace(/\\s+/g,\" \").trim()}function lt(t){return/[.!?,;:]$/.test(t)?t:`${t},`}function ft(t){let e=[];for(let r of(t.match(N)??[]).join(\" \")){let o=T.get(r);o!==void 0&&e.push(o)}return e}function mt(t,e){if(t.length===e)return t.map((s,a)=>a);let r=t.map(s=>Math.max(1,s.length)),o=r.reduce((s,a)=>s+a,0),n=[],i=0,c=r[0]??0;for(let s=0;s<e;s++){let a=(s+.5)/e*o;for(;a>c&&i<t.length-1;)c+=r[++i]??0;n.push(i)}return n}async function D(t,e){let r=t.match(/\\S+/g)??[],o=r.map((l,f)=>{let[,p=\"\",m=\"\",y=\"\"]=z.exec(ut(f===r.length-1?lt(l):l))??[];return{lead:p,core:m,trail:y}}),n=o.map(()=>\"\"),i=[];async function c(){let l=i.filter(m=>o[m]?.core);if(i=[],!l.length)return;let f=(await e(l.map(m=>o[m]?.core).join(\" \"))).trim().split(/\\s+/).filter(Boolean),p=mt(l.map(m=>o[m]?.core??\"\"),f.length);f.forEach((m,y)=>{let h=l[p[y]??0]??0;n[h]=n[h]?`${n[h]} ${m}`:m})}for(let[l,f]of o.entries())f.lead&&await c(),i.push(l),f.trail&&await c();await c();let s=[0],a=[];for(let[l,f]of o.entries()){let p=ft(f.lead+(n[l]??\"\")+f.trail);p.length&&s.length>1&&s.push(O),a.push(s.length),s.push(...p)}return s.push(10,0),{ids:s,words:a}}function H(t,e,r){let o=new Float64Array(e.length+1);for(let n=0;n<e.length;n++)o[n+1]=o[n]+Number(e[n]);return Float32Array.from(t,n=>o[Math.min(n,e.length)]*r)}var $=\"lantun-voice-kitten-v1\";var G=0,P=null,M=[],_=!1;function x(t,e=[]){self.postMessage(t,e)}function V(t){return String(t?.message??t)}function K(t){G+=t,x({type:\"progress\",loaded:G})}function gt(t,e){let r=new Uint8Array(e),o=0;for(let n of t)r.set(n,o),o+=n.length;return r}function yt(){return new Promise(t=>setTimeout(t,0))}async function F(t){let e=await caches.open($).catch(()=>null),r=await e?.match(t);if(r){let a=await r.arrayBuffer();return K(a.byteLength),a}let o=await fetch(t);if(!o.ok||!o.body)throw new Error(`HTTP ${o.status} for ${t}`);let n=o.body.getReader(),i=[],c=0;for(;;){let{done:a,value:l}=await n.read();if(a)break;i.push(l),c+=l.length,K(l.length)}let s=gt(i,c);return await e?.put(t,new Response(s)).catch(()=>{}),s.buffer}async function wt(t){let[e,r,o,n,i,c]=await Promise.all([import(t.ort),import(t.phonemizer),F(t.model),F(t.ortWasm),F(t.voices).then(C),F(t.config).then(f=>JSON.parse(new TextDecoder().decode(f)))]);e.env.wasm.wasmBinary=n,e.env.wasm.wasmPaths=t.ortWasm.slice(0,t.ortWasm.lastIndexOf(\"/\")+1),e.env.wasm.numThreads=self.crossOriginIsolated?Math.min(4,navigator.hardwareConcurrency||1):1;let s=await e.InferenceSession.create(o,{executionProviders:[\"wasm\"],graphOptimizationLevel:\"all\"}),a=new Map;async function l(f){let p=a.get(f);if(p!==void 0)return p;let m=(await r.phonemize(f,\"en-us\")).join(\" \").replace(/_/g,\"\").replace(/\\n/g,\" \");return a.size>5e3&&a.clear(),a.set(f,m),m}return{ort:e,session:s,phonemize:l,voices:i,config:c}}async function bt(t,e,r,o){let n=t.voices.get(t.config.voice_aliases?.[r]??r);if(!n)throw new Error(`Unknown voice ${r}`);let{ids:i,words:c}=await D(e,t.phonemize),s=Math.min(e.length,n.rows-1),{Tensor:a}=t.ort,l=await t.session.run({input_ids:new a(\"int64\",BigInt64Array.from(i,BigInt),[1,i.length]),style:new a(\"float32\",n.data.slice(s*n.cols,(s+1)*n.cols),[1,n.cols]),speed:new a(\"float32\",new Float32Array([o]),[1])}),f=l.waveform?.data,p=l.duration?.data;if(!f||!p)throw new Error(\"Model returned no audio\");let m=Math.max(0,Number(p[0]??0)*600-1200),y=H(c,p,600/24e3).map(d=>Math.max(0,d-m/24e3)),h=f.subarray(m,Math.max(m,f.length-5e3));return{samples:L(I(h,24e3),24e3),starts:y}}async function At(t){try{let e=await P,r=performance.now(),{samples:o,starts:n}=await bt(e,t.text,t.voice,t.speed);x({type:\"audio\",id:t.id,samples:o,starts:n,ms:performance.now()-r},[o.buffer,n.buffer])}catch(e){x({type:\"error\",id:t.id,message:V(e)})}}async function q(){if(!(_||!P)){_=!0;try{let t=M.shift();for(;t;)await At(t),await yt(),t=M.shift()}finally{_=!1}}}function Et(t){let e=t.data;switch(e.type){case\"load\":P??(P=wt(e.sources)),P.then(()=>x({type:\"ready\"}),r=>x({type:\"error\",message:V(r)})),q();return;case\"synth\":{let r=e.urgent?M.findIndex(o=>!o.urgent):-1;M.splice(r<0?M.length:r,0,e),q();return}case\"drop\":{let r=new Set(e.keep);M=M.filter(o=>r.has(o.id))}}}self.onmessage=Et;\n";
|
|
353
|
+
var E = "var R=Float32Array.from({length:512},(t,e)=>.5-.5*Math.cos(2*Math.PI*e/512)),Q=Float64Array.from({length:256},(t,e)=>Math.cos(2*Math.PI*e/512)),Y=Float64Array.from({length:256},(t,e)=>Math.sin(2*Math.PI*e/512)),X=tt(512);function tt(t){let e=new Uint16Array(t),r=Math.log2(t);for(let o=0;o<t;o++){let n=0;for(let i=0;i<r;i++)n|=(o>>i&1)<<r-1-i;e[o]=n}return e}function et(t,e,r=75){let o=2*Math.PI*r/e,n=Math.sin(o)/Math.SQRT2,i=Math.cos(o),c=1+n,s=(1+i)/2/c,a=-(1+i)/c,l=-2*i/c,f=(1-n)/c,p=0,m=0,y=0,h=0;for(let d=0;d<t.length;d++){let b=t[d],u=s*b+a*p+s*m-l*y-f*h;m=p,p=b,h=y,y=u,t[d]=u}}function k(t,e,r){for(let n=0;n<512;n++){let i=X[n];if(n<i){let c=t[n],s=e[n];t[n]=t[i],e[n]=e[i],t[i]=c,e[i]=s}}let o=r?1:-1;for(let n=2;n<=512;n<<=1){let i=n>>1,c=512/n;for(let s=0;s<512;s+=n)for(let a=0;a<i;a++){let l=Q[a*c],f=o*Y[a*c],p=s+a,m=p+i,y=t[m],h=e[m],d=y*l-h*f,b=y*f+h*l,u=t[p],w=e[p];t[m]=u-d,e[m]=w-b,t[p]=u+d,e[p]=w+b}}}function nt(t,e){let r=new Float64Array(e);for(let c=0;c<e;c++){let s=0;for(let a=0;a<257;a++)s+=t[c*257+a]**2;r[c]=s}let n=Array.from({length:e},(c,s)=>s).sort((c,s)=>r[c]-r[s]).slice(0,Math.max(4,Math.floor(e*.15))),i=new Float32Array(257);for(let c=0;c<257;c++){let s=0,a=0;for(let p of n){let m=t[p*257+c]??0;s+=m,a+=m*m}let l=s/n.length,f=Math.max(0,a/n.length-l*l);i[c]=l+1.5*Math.sqrt(f)}return i}function rt(t){let e=Math.min(120,t.length>>1);for(let r=0;r<e;r++){let o=r/e,n=t.length-1-r;t[r]=t[r]*o,t[n]=t[n]*o}}function I(t,e){let r=Float32Array.from(t);et(r,e);let o=Math.max(1,Math.ceil((r.length+512)/128)),n=new Float32Array(o*128+512);n.set(r,256);let i=new Float32Array(o*512),c=new Float32Array(o*512),s=new Float32Array(o*257);for(let h=0;h<o;h++){let d=i.subarray(h*512,(h+1)*512),b=c.subarray(h*512,(h+1)*512);for(let u=0;u<512;u++)d[u]=n[h*128+u]*R[u];k(d,b,!1);for(let u=0;u<257;u++){let w=d[u],A=b[u];s[h*257+u]=Math.sqrt(w*w+A*A)}}let a=nt(s,o),l=new Float32Array(n.length),f=new Float32Array(n.length),p=new Float32Array(257),m=new Float32Array(257).fill(1);for(let h=0;h<o;h++){for(let u=0;u<257;u++){let w=s[h*257+u]/(a[u]+1e-9),A=Math.min(1,Math.max(0,(w-1)/1));p[u]=.06+(1-.06)*A*A*(3-2*A)}for(let u=0;u<257;u++){let w=p[u],A=((p[u-1]??w)+2*w+(p[u+1]??w))/4;m[u]=Math.max(A,m[u]*.5)}let d=i.subarray(h*512,(h+1)*512),b=c.subarray(h*512,(h+1)*512);for(let u=0;u<257;u++){let w=m[u];d[u]=d[u]*w,b[u]=b[u]*w,u>0&&u<256&&(d[512-u]=d[u],b[512-u]=-b[u])}k(d,b,!0);for(let u=0;u<512;u++){let w=R[u],A=h*128+u;l[A]=l[A]+d[u]/512*w,f[A]=f[A]+w*w}}let y=new Float32Array(r.length);for(let h=0;h<r.length;h++){let d=f[h+256];y[h]=d>1e-6?l[h+256]/d:0}return rt(y),y}function L(t,e){let r=Math.round(e*.02),o=[],n=0;for(let s=0;s+r<=t.length;s+=r){let a=0;for(let l=s;l<s+r;l++){let f=t[l];a+=f*f,n=Math.max(n,Math.abs(f))}o.push(Math.sqrt(a/r))}if(!o.length||n===0)return t;o.sort((s,a)=>s-a);let i=o[Math.floor(o.length*.9)]??0,c=Math.min(2,Math.max(1/2,.2/(i||1)),.95/n);for(let s=0;s<t.length;s++)t[s]=t[s]*c;return t}function ot(t,e){let r=t.length*2,o=new ArrayBuffer(44+r),n=new DataView(o);function i(c,s){for(let a=0;a<s.length;a++)n.setUint8(c+a,s.charCodeAt(a))}i(0,\"RIFF\"),n.setUint32(4,36+r,!0),i(8,\"WAVE\"),i(12,\"fmt \"),n.setUint32(16,16,!0),n.setUint16(20,1,!0),n.setUint16(22,1,!0),n.setUint32(24,e,!0),n.setUint32(28,e*2,!0),n.setUint16(32,2,!0),n.setUint16(34,16,!0),i(36,\"data\"),n.setUint32(40,r,!0);for(let c=0;c<t.length;c++){let s=Math.max(-1,Math.min(1,t[c]));n.setInt16(44+c*2,s*32767,!0)}return o}var Tt=`data:audio/wav;base64,${btoa(String.fromCharCode(...new Uint8Array(ot(new Float32Array(240),24e3))))}`;async function st(t){let e=new Response(t).body;if(!e)throw new Error(\"Streams are unavailable\");let r=e.pipeThrough(new DecompressionStream(\"deflate-raw\"));return new Uint8Array(await new Response(r).arrayBuffer())}async function at(t){let e=new DataView(t),r=t.byteLength-22;for(;r>=0&&e.getUint32(r,!0)!==101010256;)r--;if(r<0)throw new Error(\"Not a zip file\");let o=e.getUint16(r+10,!0),n=e.getUint32(r+16,!0),i=new Map,c=new TextDecoder;for(let s=0;s<o;s++){if(e.getUint32(n,!0)!==33639248)throw new Error(\"Corrupt zip directory\");let a=e.getUint16(n+10,!0),l=e.getUint32(n+20,!0),f=e.getUint16(n+28,!0),p=e.getUint16(n+30,!0),m=e.getUint16(n+32,!0),y=e.getUint32(n+42,!0),h=c.decode(new Uint8Array(t,n+46,f)),d=y+30+e.getUint16(y+26,!0)+e.getUint16(y+28,!0),b=new Uint8Array(t,d,l);if(a===0)i.set(h,b);else if(a===8)i.set(h,await st(b));else throw new Error(`Unsupported zip method ${a}`);n+=46+f+p+m}return i}function it(t){let e=new DataView(t.buffer,t.byteOffset,t.byteLength),r=t[6]!==void 0&&t[6]>=2,o=r?e.getUint32(8,!0):e.getUint16(8,!0),n=(r?12:10)+o,i=new TextDecoder().decode(t.subarray(r?12:10,n)),c=/'descr':\\s*'([^']+)'/.exec(i)?.[1],s=(/'shape':\\s*\\(([^)]*)\\)/.exec(i)?.[1]??\"\").split(\",\").map(m=>m.trim()).filter(Boolean).map(Number);if(/'fortran_order':\\s*True/.test(i))throw new Error(\"Fortran-order arrays are not supported\");let a=s.length>1?s[0]??1:1,l=s.slice(s.length>1?1:0).reduce((m,y)=>m*y,1),f=t.slice(n),p;if(c===\"<f4\")p=new Float32Array(f.buffer);else if(c===\"<f8\")p=Float32Array.from(new Float64Array(f.buffer));else throw new Error(`Unsupported npy dtype ${c}`);return{data:p,rows:a,cols:l}}async function C(t){let e=new Map;for(let[r,o]of await at(t))r.endsWith(\".npy\")&&e.set(r.slice(0,-4),it(o));return e}var B=`[${';:,.!?\\xA1\\xBF\\u2014\\u2026\"\\xAB\\xBB\\u201C\\u201D()[]{}'.replace(/[\\]\\\\[^-]/g,\"\\\\$&\")}]`,ct=[\"$\",...';:,.!?\\xA1\\xBF\\u2014\\u2026\"\\xAB\\xBB\"\" ',...\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\",...\"\\u0251\\u0250\\u0252\\xE6\\u0253\\u0299\\u03B2\\u0254\\u0255\\xE7\\u0257\\u0256\\xF0\\u02A4\\u0259\\u0258\\u025A\\u025B\\u025C\\u025D\\u025E\\u025F\\u0284\\u0261\\u0260\\u0262\\u029B\\u0266\\u0267\\u0127\\u0265\\u029C\\u0268\\u026A\\u029D\\u026D\\u026C\\u026B\\u026E\\u029F\\u0271\\u026F\\u0270\\u014B\\u0273\\u0272\\u0274\\xF8\\u0275\\u0278\\u03B8\\u0153\\u0276\\u0298\\u0279\\u027A\\u027E\\u027B\\u0280\\u0281\\u027D\\u0282\\u0283\\u0288\\u02A7\\u0289\\u028A\\u028B\\u2C71\\u028C\\u0263\\u0264\\u028D\\u03C7\\u028E\\u028F\\u0291\\u0290\\u0292\\u0294\\u02A1\\u0295\\u02A2\\u01C0\\u01C1\\u01C2\\u01C3\\u02C8\\u02CC\\u02D0\\u02D1\\u02BC\\u02B4\\u02B0\\u02B1\\u02B2\\u02B7\\u02E0\\u02E4\\u02DE\\u2193\\u2191\\u2192\\u2197\\u2198'\\u0329'\\u1D7B\"],T=new Map(ct.map((t,e)=>[t,e])),N=/[\\p{L}\\p{M}\\p{N}_]+|[^\\p{L}\\p{M}\\p{N}_\\s]/gu,z=new RegExp(`^(${B}*)(.*?)(${B}*)$`,\"su\"),O=T.get(\" \");function ut(t){return t.replace(/[‘’‚‛′]/g,\"'\").replace(/[“”„‟″]/g,'\"').replace(/[–―]/g,\"\\u2014\").replace(/\\s+/g,\" \").trim()}function lt(t){return/[.!?,;:]$/.test(t)?t:`${t},`}function ft(t){let e=[];for(let r of(t.match(N)??[]).join(\" \")){let o=T.get(r);o!==void 0&&e.push(o)}return e}function mt(t,e){if(t.length===e)return t.map((s,a)=>a);let r=t.map(s=>Math.max(1,s.length)),o=r.reduce((s,a)=>s+a,0),n=[],i=0,c=r[0]??0;for(let s=0;s<e;s++){let a=(s+.5)/e*o;for(;a>c&&i<t.length-1;)c+=r[++i]??0;n.push(i)}return n}async function D(t,e){let r=t.match(/\\S+/g)??[],o=r.map((l,f)=>{let[,p=\"\",m=\"\",y=\"\"]=z.exec(ut(f===r.length-1?lt(l):l))??[];return{lead:p,core:m,trail:y}}),n=o.map(()=>\"\"),i=[];async function c(){let l=i.filter(m=>o[m]?.core);if(i=[],!l.length)return;let f=(await e(l.map(m=>o[m]?.core).join(\" \"))).trim().split(/\\s+/).filter(Boolean),p=mt(l.map(m=>o[m]?.core??\"\"),f.length);f.forEach((m,y)=>{let h=l[p[y]??0]??0;n[h]=n[h]?`${n[h]} ${m}`:m})}for(let[l,f]of o.entries())f.lead&&await c(),i.push(l),f.trail&&await c();await c();let s=[0],a=[];for(let[l,f]of o.entries()){let p=ft(f.lead+(n[l]??\"\")+f.trail);p.length&&s.length>1&&s.push(O),a.push(s.length),s.push(...p)}return s.push(10,0),{ids:s,words:a}}function H(t,e,r){let o=new Float64Array(e.length+1);for(let n=0;n<e.length;n++)o[n+1]=o[n]+Number(e[n]);return Float32Array.from(t,n=>o[Math.min(n,e.length)]*r)}var $=\"lantun-voice-kitten-v1\";var G=0,P=null,M=[],_=!1;function x(t,e=[]){self.postMessage(t,e)}function V(t){return String(t?.message??t)}function K(t){G+=t,x({type:\"progress\",loaded:G})}function gt(t,e){let r=new Uint8Array(e),o=0;for(let n of t)r.set(n,o),o+=n.length;return r}function yt(){return new Promise(t=>setTimeout(t,0))}async function F(t){let e=await caches.open($).catch(()=>null),r=await e?.match(t);if(r){let a=await r.arrayBuffer();return K(a.byteLength),a}let o=await fetch(t);if(!o.ok||!o.body)throw new Error(`HTTP ${o.status} for ${t}`);let n=o.body.getReader(),i=[],c=0;for(;;){let{done:a,value:l}=await n.read();if(a)break;i.push(l),c+=l.length,K(l.length)}let s=gt(i,c);return await e?.put(t,new Response(s)).catch(()=>{}),s.buffer}async function wt(t){let[e,r,o,n,i,c]=await Promise.all([import(t.ort),import(t.phonemizer),F(t.model),F(t.ortWasm),F(t.voices).then(C),F(t.config).then(f=>JSON.parse(new TextDecoder().decode(f)))]);e.env.wasm.wasmBinary=n,e.env.wasm.wasmPaths=t.ortWasm.slice(0,t.ortWasm.lastIndexOf(\"/\")+1),e.env.wasm.numThreads=self.crossOriginIsolated?Math.min(4,navigator.hardwareConcurrency||1):1;let s=await e.InferenceSession.create(o,{executionProviders:[\"wasm\"],graphOptimizationLevel:\"all\"}),a=new Map;async function l(f){let p=a.get(f);if(p!==void 0)return p;let m=(await r.phonemize(f,\"en-us\")).join(\" \").replace(/_/g,\"\").replace(/\\n/g,\" \");return a.size>5e3&&a.clear(),a.set(f,m),m}return{ort:e,session:s,phonemize:l,voices:i,config:c}}async function bt(t,e,r,o){let n=t.voices.get(t.config.voice_aliases?.[r]??r);if(!n)throw new Error(`Unknown voice ${r}`);let{ids:i,words:c}=await D(e,t.phonemize),s=Math.min(e.length,n.rows-1),{Tensor:a}=t.ort,l=await t.session.run({input_ids:new a(\"int64\",BigInt64Array.from(i,BigInt),[1,i.length]),style:new a(\"float32\",n.data.slice(s*n.cols,(s+1)*n.cols),[1,n.cols]),speed:new a(\"float32\",new Float32Array([o]),[1])}),f=l.waveform?.data,p=l.duration?.data;if(!f||!p)throw new Error(\"Model returned no audio\");let m=Math.max(0,Number(p[0]??0)*600-1200),y=H(c,p,600/24e3).map(d=>Math.max(0,d-m/24e3)),h=f.subarray(m,Math.max(m,f.length-5e3));return{samples:L(I(h,24e3),24e3),starts:y}}async function At(t){try{let e=await P,r=performance.now(),{samples:o,starts:n}=await bt(e,t.text,t.voice,t.speed);x({type:\"audio\",id:t.id,samples:o,starts:n,ms:performance.now()-r},[o.buffer,n.buffer])}catch(e){x({type:\"error\",id:t.id,message:V(e)})}}async function q(){if(!(_||!P)){_=!0;try{let t=M.shift();for(;t;)await At(t),await yt(),t=M.shift()}finally{_=!1}}}function Et(t){let e=t.data;switch(e.type){case\"load\":P??(P=wt(e.sources)),P.then(()=>x({type:\"ready\"}),r=>x({type:\"error\",message:V(r)})),q();return;case\"synth\":{let r=e.urgent?M.findIndex(o=>!o.urgent):-1;M.splice(r<0?M.length:r,0,e),q();return}case\"drop\":{let r=new Set(e.keep);M=M.filter(o=>r.has(o.id))}}}self.onmessage=Et;export{Et as onMessage};\n";
|
|
354
354
|
// src/index.ts
|
|
355
355
|
function D() {
|
|
356
356
|
let e = URL.createObjectURL(new Blob([E], { type: "text/javascript" }));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lantun/voice-kitten",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Lifelike, on-device English voices for Lantun, powered by Kitten TTS. Nothing downloads until a reader picks one.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"text-to-speech",
|
|
@@ -34,10 +34,10 @@
|
|
|
34
34
|
"access": "public"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
|
-
"@lantun/core": "^0.1.
|
|
37
|
+
"@lantun/core": "^0.1.3"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@lantun/core": "^0.1.
|
|
40
|
+
"@lantun/core": "^0.1.3",
|
|
41
41
|
"esbuild": "^0.28.2",
|
|
42
42
|
"jsdom": "^30.1.3",
|
|
43
43
|
"typescript": "^7.0.2",
|