alkove 0.4.2 → 0.5.0
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/NOTICE +1 -1
- package/README.md +5 -5
- package/dist/alkove-engine.wasm +0 -0
- package/dist/alkove-worker.js +1 -1
- package/dist/api-5Z64WCXH.js +1 -0
- package/dist/api.d.ts +15 -9
- package/dist/assets-5RV3UURM.js +2 -0
- package/dist/chunk-BWNLE4ZO.js +1 -0
- package/dist/chunk-LB4OLDBE.js +1 -0
- package/dist/chunk-SSJ7CLCP.js +1 -0
- package/dist/cli.js +2 -2
- package/dist/core/chain.d.ts +1 -0
- package/dist/core/native.d.ts +2 -0
- package/dist/core/spot/spot.d.ts +0 -1
- package/dist/engine/alk.d.ts +2 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/loader.d.ts +0 -1
- package/dist/manifest.d.ts +1 -0
- package/dist/node-KQMEHMZQ.js +1 -0
- package/dist/process.d.ts +9 -2
- package/dist/react.js +1 -1
- package/dist/realtime/worker.d.ts +4 -4
- package/dist/recipes/input.d.ts +41 -31
- package/package.json +2 -2
- package/dist/api-63K47HT3.js +0 -1
- package/dist/assets-CPGXIA43.js +0 -2
- package/dist/chunk-FC4ZSMUX.js +0 -1
- package/dist/chunk-IMUNEOSF.js +0 -1
- package/dist/chunk-MZOMHWYI.js +0 -1
- package/dist/node-VEIFZSW6.js +0 -1
package/dist/chunk-MZOMHWYI.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{a as R,b as m,c as A,d as V,e as W,i as M}from"./chunk-IMUNEOSF.js";import{A as T,a as C,b as d,c as N,e as F,f as S,g as x,h,i as b,k as P,n as y,o as v,p as B,s as L,v as E,z as I}from"./chunk-FC4ZSMUX.js";var c=class{static langs=F.langs;static inventory=S.map(r=>r.normalize("NFC"));static distance(r,t){let n=typeof r=="string"?h(r.replace(/^\/|\/$/g,""))??[]:r,o=typeof t=="string"?h(t.replace(/^\/|\/$/g,""))??[]:t;if(!n.length||!o.length)return n.length===o.length?0:1;let a=Array.from({length:o.length+1},(s,l)=>l);for(let s=1;s<=n.length;s++){let l=[s];for(let i=1;i<=o.length;i++){let f=n[s-1]===o[i-1]?0:x[n[s-1]-1].includes(o[i-1])?.5:1;l.push(Math.min(a[i]+1,l[i-1]+1,a[i-1]+f))}a=l}return a[o.length]/Math.max(n.length,o.length)}};var D=()=>typeof process<"u"&&!!process.versions?.node&&typeof window>"u",H=0,q=null;async function u(e,r){if(D()){let n=await(q??=import("./node-VEIFZSW6.js").then(o=>new o.NodeBackend));return await n.ready(e),{call:async(o,a,s,l)=>n.calls[o](...a,l),hold:()=>()=>{}}}let t=A.get();return await t.models("background",e,r),{call:async(n,o,a=[],s)=>(await t.models("background",e)).call(n,o,a,s),hold:()=>t.hold()}}function U(e,r={}){let t=e==="wake"?["spot"]:["focus","cue"];return r.voiceprint&&t.push("timbre"),r.typedWords&&t.push("lex"),t}async function K(e){if("pcm"in e&&"rate"in e)return m.all(e.pcm,e.rate,d);if(typeof AudioBuffer<"u"&&e instanceof AudioBuffer)return m.all(j(e),e.sampleRate,d);let r=typeof Blob<"u"&&e instanceof Blob?new Uint8Array(await e.arrayBuffer()):e instanceof Uint8Array?e:new Uint8Array(e);if(String.fromCharCode(...r.subarray(0,4))==="RIFF"){let{pcm:o,rate:a}=T(r);return m.all(o,a,d)}if(typeof OfflineAudioContext>"u")throw new b("browser.unsupported","Outside a browser only WAV is decoded: decode the file and pass { pcm, rate }.");let n=await new OfflineAudioContext(1,1,44100).decodeAudioData(r.slice().buffer);return m.all(j(n),n.sampleRate,d)}function j(e){if(e.numberOfChannels===1)return e.getChannelData(0).slice();let r=new Float32Array(e.length);for(let t=0;t<e.numberOfChannels;t++){let n=e.getChannelData(t);for(let o=0;o<r.length;o++)r[o]+=n[o]/e.numberOfChannels}return r}var we={configure:B,stream:Q,supported:V,micPermission:W,onDiag:L,async preload(e,r={}){let t=U(e,r);if(D()){await u(t);return}let n=A.get(),o=t.filter(i=>i!=="timbre"&&i!=="lex"),a=t.filter(i=>i==="timbre"||i==="lex"),s=[{loaded:0,total:0},{loaded:0,total:0}],l=i=>f=>{s[i]=f,r.onProgress?.({loaded:s[0].loaded+s[1].loaded,total:s[0].total+s[1].total})};await Promise.all([n.models("realtime",o,l(0)),a.length?n.models("background",a,l(1)):null])},async cached(e,r={}){let t=[C,...U(e,r).map(n=>N[n])];return(await Promise.all(t.map(E))).every(Boolean)},async process(e,r={}){let t=await K(e);r.signal?.throwIfAborted();let o=await(await u(G(r))).call("process",[t,_(r)],[],r.onProgress);return{...o,toWav(a="audio"){return I(a==="both"?[o.raw,o.audio]:[a==="raw"?o.raw:o.audio],d)}}}};function G(e){let r=[];return e.focus?r.push("focus","cue"):e.cue&&r.push("cue"),e.spot?.words.length&&r.push("spot"),r}function _(e){let{onProgress:r,signal:t,...n}=e;return{...n,focus:typeof e.focus=="object"?{...e.focus,voiceprint:e.focus.voiceprint instanceof y?e.focus.voiceprint.toJSON():e.focus.voiceprint}:e.focus,spot:e.spot?{...e.spot,words:e.spot.words.map(o=>P(o).toJSON())}:void 0}}async function*Q(e,r={}){let t=await u(G(r)),n=++H,o=t.hold();await t.call("streamOpen",[n,_(r)]).catch(p=>{throw o(),p});let a=r.rate??d,s=new m(a,d,!0),l=0,i=0,f=Symbol.asyncIterator in e?e:Y(e);try{for await(let g of f){r.signal?.throwIfAborted();let w=[];l+=g.length,s.push(g,X=>w.push(X)),i+=w.length,w.length&&(yield await t.call("streamPush",[n,Float32Array.from(w)]))}let p=[],k=Math.round(l*d/a)-i;k>0&&s.push(new Float32Array(m.HALF*2+8),g=>p.length<k&&p.push(g)),p.length&&(yield await t.call("streamPush",[n,Float32Array.from(p)]))}finally{try{yield await t.call("streamEnd",[n])}finally{o()}}}async function*Y(e){let r=e.getReader();try{for(;;){let{done:t,value:n}=await r.read();if(t)return;yield n}}finally{r.releaseLock()}}var O=class e{constructor(r){this.b=r}static async create(){return new e(await u(["timbre"]))}async embed(r,t={}){return y.fromJSON(await this.b.call("embed",[r,t.source??"raw"]))}verify(r,t,n={}){return v.verify(r,t,n)}async identify(r,t,n={}){let o=r instanceof y?r:await this.embed(r);return v.identify(o,t,n)}},z=class e{constructor(r){this.b=r}static inventory=c.inventory;static langs=c.langs;static distance=c.distance;static async create(){return new e(await u(["lex"]))}async phonemize(r,t){if(!c.langs.includes(t))throw new b("lang.unsupported",`No ${t}: ${c.langs.join(", ")} only.`);let o=(await this.b.call("phonemize",[Array.isArray(r)?r:[r],t])).map(a=>a.map(({ipa:s,p:l})=>({ipa:s,p:l})));return Array.isArray(r)?o:o[0]}},Z={async compile(e,r={}){for(let o of[r.lang??[]].flat())if(!c.langs.includes(o))throw new b("lang.unsupported",`No ${o}: ${c.langs.join(", ")} only.`);let t=/^\s*\/.+\/\s*$/.test(e),n=await u(t?[]:["lex"]);return P(await n.call("compile",[e,{lang:r.lang,chain:r.chain}]))},enroll(e,r={}){return new R({compile:()=>Z.compile(e,r),open:async t=>{let n=await M(r,t,r.takes??3);return{on:n.on.bind(n),stop:()=>n.dispose(),source:"raw"}},embed:async(t,n)=>(await O.create()).embed(t,{source:n})})}},J=e=>e?new y(e.centers,e.source,e.model,e.windows):null,ee=e=>({alkove:"voiceprint",model:e.model,source:e.source,centers:e.centers,windows:e.windows}),re=typeof FinalizationRegistry>"u"?null:new FinalizationRegistry(e=>e()),$=class e{constructor(r,t,n,o){this.b=r;this.id=t;this.state={progress:n.progress,ready:n.ready,voiceprint:J(n.voiceprint)},re?.register(this,()=>void r.call("learnerClose",[t]).catch(()=>{}).finally(o))}state;static async create(r={}){let t=await u(["timbre"]),n=++H,o=t.hold();try{let a=await t.call("learnerOpen",[n,r.voiceprint?ee(r.voiceprint):null]);return new e(t,n,a,o)}catch(a){throw o(),a}}get progress(){return this.state.progress}get ready(){return this.state.ready}get voiceprint(){return this.state.voiceprint}async offer(r){let t=await this.b.call("learnerOffer",[this.id,r]);return this.state={progress:t.progress,ready:t.ready,voiceprint:J(t.voiceprint)},{accepted:t.accepted,rejected:t.rejected,progress:t.progress,ready:t.ready}}forget(){this.state={progress:0,ready:!1,voiceprint:null},this.b.call("learnerForget",[this.id])}};export{we as a,O as b,z as c,Z as d,$ as e};
|
package/dist/node-VEIFZSW6.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{C as W,D as H,a as S,d as R,i as z,w as $,x as D,y as I}from"./chunk-FC4ZSMUX.js";var y=class{constructor(e){this.size=e;this.buf=new Float32Array(e)}buf;fill=0;push(e,r){let t=0;for(;t<e.length;){let n=Math.min(this.size-this.fill,e.length-t);this.buf.set(e.subarray(t,t+n),this.fill),this.fill+=n,t+=n,this.fill===this.size&&(this.fill=0,r(this.buf))}}get pending(){return this.fill}reset(){this.fill=0}};var d=R.chunk,j=["silence","forced","wake"],B=s=>new Uint8Array(s.buffer,s.byteOffset,s.byteLength),v=class{constructor(e,r,t,n={}){this.alk=e;let a={gate:!1,normalize:!0,strength:1,sleep:!0,...n},i={...I,...n.speech},l=(h,c)=>e.x.alk_chain(r.ptr,t.ptr,+a.gate,+a.normalize,+a.sleep,a.strength,i.confidence,i.exitConfidence,i.startSecs,i.stopSecs,h,c),m=n.print?Float32Array.from(n.print):null;this.ptr=m?e.with(B(m),h=>l(h,m.length)):l(0,0),this.ptr||e.fail("chain"),this.delay=e.x.alk_chain_get(this.ptr,4)}delay;ptr;hops=new y(d);mix=new Float32Array(d);focusOut=new Float32Array(d);onFocus=null;onCue=null;get speaking(){return this.alk.x.alk_chain_get(this.ptr,0)===1}get sleeping(){return this.alk.x.alk_chain_get(this.ptr,1)===1}get time(){return this.alk.x.alk_chain_get(this.ptr,2)}get gainDb(){return this.alk.x.alk_chain_get(this.ptr,3)}release(){this.ptr&&this.alk.x.alk_chain_free(this.ptr),this.ptr=0}setPrint(e){let r=this.alk.x;if(!e)r.alk_chain_print(this.ptr,0,0);else{let t=Float32Array.from(e);this.alk.with(B(t),n=>r.alk_chain_print(this.ptr,n,t.length))<0&&this.alk.fail("print")}}setStrength(e){this.alk.x.alk_chain_strength(this.ptr,e)}setEnabled(e){this.alk.x.alk_chain_enabled(this.ptr,+e)}step(e,r){if(this.hops.pending||e.length!==d)throw new RangeError("step() takes whole hops only");return this.hop(e,r)}push(e,r){let t=new Float32Array(Math.floor((this.hops.pending+e.length)/d)*d),n=0;return this.hops.push(e,a=>{t.set(this.hop(a,r),n),n+=d}),t}hop(e,r){let{alk:t,ptr:n}=this,a=t.x;new Float32Array(t.heap,a.alk_chain_hop(n),d).set(e);let i=a.alk_chain_step(n);this.mix.set(new Float32Array(t.heap,i,d));let l=new Float64Array(t.heap,a.alk_chain_events(n),4*a.alk_chain_event_count(n)).slice();for(let m=0;m<l.length;m+=4){let[h,c,u]=[l[m+1],l[m+2],l[m+3]];switch(l[m]){case 0:r({type:"speechstart",start:h});break;case 1:r({type:"speechend",start:h,end:c});break;case 2:r({type:"idle",asleep:h===1,at:c});break;case 3:r({type:"diag",code:"focus.print",level:"info",data:{print:h===1,after:c,why:j[u]}});break;case 4:r({type:"diag",code:"focus.reset",level:"info",data:{after:h,why:j[c]}});break;case 5:this.onFocus&&(this.focusOut.set(new Float32Array(t.heap,a.alk_chain_focus(n),d)),this.onFocus(this.focusOut,h));break;case 6:this.onCue?.(h,c)}}return this.mix}};var X=new TextEncoder,Z=new TextDecoder;function ee(s){let e=[],r=[],t=0,n=()=>{e.length&&(r.push(Uint8Array.from(e)),t+=e.length,e.length=0)},a=u=>{n(),r.push(u),t+=u.length},i=u=>e.push(u&255,u>>>8&255,u>>>16&255,u>>>24),l=u=>{let k=X.encode(u);i(k.length),a(k)},m=u=>{if(u==null)e.push(0);else if(typeof u=="boolean")e.push(u?2:1);else if(typeof u=="number")e.push(3),a(new Uint8Array(Float64Array.of(u).buffer));else if(typeof u=="string")e.push(4),l(u);else if(u instanceof Uint8Array)e.push(7),i(u.length),a(u.slice());else if(u instanceof Float32Array)e.push(8),i(u.length),a(new Uint8Array(u.slice().buffer));else if(Array.isArray(u)||ArrayBuffer.isView(u)){let k=Array.from(u);e.push(5),i(k.length);for(let f of k)m(f)}else{let k=Object.entries(u).filter(([,f])=>f!==void 0);e.push(6),i(k.length);for(let[f,O]of k)l(f),m(O)}};m(s),n();let h=new Uint8Array(t),c=0;for(let u of r)h.set(u,c),c+=u.length;return h}function re(s){let e=new DataView(s.buffer,s.byteOffset,s.byteLength),r=0,t=()=>(r+=4,e.getUint32(r-4,!0)),n=()=>{let i=t();return r+=i,Z.decode(s.subarray(r-i,r))},a=()=>{switch(s[r++]){case 0:return null;case 1:return!1;case 2:return!0;case 3:return r+=8,e.getFloat64(r-8,!0);case 4:return n();case 5:return Array.from({length:t()},a);case 6:return Object.fromEntries(Array.from({length:t()},()=>[n(),a()]));case 7:{let i=t();return r+=i,s.slice(r-i,r)}case 8:{let i=t();return r+=4*i,new Float32Array(s.slice(r-4*i,r).buffer)}default:throw new Error("bad value from the core")}};return a()}function b(s,e){let r=ee(e),t=s.x.alk_value_in(r.length);new Uint8Array(s.heap,t,r.length).set(r)}function p(s,e){let r=re(new Uint8Array(s.heap,s.x.alk_value_out(),s.x.alk_value_len()).slice());if(e>=0)return r;let t=r;throw t.kind==="TypeError"?new TypeError(t.message):t.kind==="RangeError"?new RangeError(t.message):t.kind==="AlkoveError"?new z(t.code,t.message):new Error(t.message)}var o=s=>{if(!s)throw new Error("used after release");return s},_=(s,e,r)=>s.with(new Uint8Array(e.buffer,e.byteOffset,e.byteLength),t=>r(t,e.length)),w=class{constructor(e,r){this.alk=e;this.ptr=e.x.alk_lex(r.ptr),this.ptr||e.fail("lex")}ptr;get handle(){return this.ptr}phonemize(e,r){return b(this.alk,{texts:e,lang:r}),p(this.alk,this.alk.x.alk_lex_phonemize(o(this.ptr)))}release(){this.ptr&&this.alk.x.alk_lex_free(this.ptr),this.ptr=0}};function J(s,e,r,t,n){return b(s,{text:r,lang:t,chain:n}),p(s,s.x.alk_compile(e?.handle??0))}var x=class{constructor(e,r,t){this.alk=e;b(e,t),this.ptr=e.x.alk_processor(r.focus?.ptr??0,r.cue?.ptr??0,r.spot?.ptr??0),this.ptr||e.fail("processor")}ptr;part(e){let{x:r}=this.alk,t=p(this.alk,e),n=new Float32Array(this.alk.heap,r.alk_processor_audio(o(this.ptr)),r.alk_processor_audio_len(o(this.ptr))).slice(),{start:a,...i}=t;return{start:a,audio:n,...i}}push(e){return this.part(_(this.alk,e,(r,t)=>this.alk.x.alk_processor_push(o(this.ptr),r,t)))}end(){return this.part(this.alk.x.alk_processor_end(o(this.ptr)))}release(){this.ptr&&this.alk.x.alk_processor_free(this.ptr),this.ptr=0}},A=class{constructor(e,r,t,n){this.alk=e;b(e,{words:t,context:n}),this.ptr=e.x.alk_listener(r.ptr),this.ptr||e.fail("listener")}ptr;setWords(e,r){b(this.alk,{words:e,context:r}),p(this.alk,this.alk.x.alk_listener_words(o(this.ptr)))}push(e){return p(this.alk,_(this.alk,e,(r,t)=>this.alk.x.alk_listener_push(o(this.ptr),r,t)))}release(){this.ptr&&this.alk.x.alk_listener_free(this.ptr),this.ptr=0}},F=class{constructor(e,r,t,n,a){this.alk=e;b(e,{word:t,takes:n,chain:a}),this.ptr=e.x.alk_enroller(r.ptr),this.ptr||e.fail("enroller")}ptr;push(e){return p(this.alk,_(this.alk,e,(r,t)=>this.alk.x.alk_enroller_push(o(this.ptr),r,t)))}result(){return p(this.alk,this.alk.x.alk_enroller_result(o(this.ptr)))}release(){this.ptr&&this.alk.x.alk_enroller_free(this.ptr),this.ptr=0}},N=class{constructor(e,r){this.alk=e;this.ptr=e.x.alk_timbre(r.ptr),this.ptr||e.fail("timbre")}ptr;get handle(){return this.ptr}windows(e){return p(this.alk,_(this.alk,e,(r,t)=>this.alk.x.alk_timbre_windows(o(this.ptr),r,t)))}embed(e,r){return b(this.alk,{source:r}),p(this.alk,_(this.alk,e,(t,n)=>this.alk.x.alk_timbre_embed(o(this.ptr),t,n)))}release(){this.ptr&&this.alk.x.alk_timbre_free(this.ptr),this.ptr=0}};function Q(s,e){return b(s,e),p(s,s.x.alk_voiceprint())}var M=class{constructor(e,r,t){this.alk=e;b(e,{source:r,start:t}),this.ptr=e.x.alk_learner(),this.ptr||e.fail("learner")}ptr;learn(e,r){return p(this.alk,_(this.alk,r,(t,n)=>this.alk.x.alk_learner_learn(o(this.ptr),e.handle,t,n)))}offer(e,r){return p(this.alk,_(this.alk,r,(t,n)=>this.alk.x.alk_learner_offer(o(this.ptr),e.handle,t,n)))}progress(){return p(this.alk,this.state()).progress}ready(){return p(this.alk,this.state()).ready}state(){return this.alk.x.alk_learner_state(o(this.ptr))}voiceprint(e=!1){return p(this.alk,this.alk.x.alk_learner_voiceprint(o(this.ptr),+e))}forget(){this.alk.x.alk_learner_forget(o(this.ptr))}release(){this.ptr&&this.alk.x.alk_learner_free(this.ptr),this.ptr=0}},V=class{constructor(e,r,t){this.alk=e;b(e,t),this.ptr=e.x.alk_pipeline(r.focus?.ptr??0,r.cue?.ptr??0,r.spot?.ptr??0),this.ptr||e.fail("pipeline")}ptr;out=new Float32Array(256);step(e,r){let{alk:t}=this,n=o(this.ptr);new Float32Array(t.heap,t.x.alk_pipeline_hop(n),256).set(e);let a=t.x.alk_pipeline_step(n,r);return this.out.set(new Float32Array(t.heap,a,256)),this.out}spot(e){this.alk.x.alk_pipeline_spot(o(this.ptr),e.ptr)}raw(e){new Float32Array(this.alk.heap,this.alk.x.alk_pipeline_hop(o(this.ptr)),256).set(e),this.alk.x.alk_pipeline_raw(o(this.ptr))}outs(){return this.alk.x.alk_pipeline_pending(o(this.ptr))?p(this.alk,this.alk.x.alk_pipeline_outs(o(this.ptr))):[]}verified(e,r,t){this.alk.x.alk_pipeline_verified(o(this.ptr),e,+r,t)}set(e){b(this.alk,e),p(this.alk,this.alk.x.alk_pipeline_set(o(this.ptr)))}record(e){p(this.alk,this.alk.x.alk_pipeline_record(o(this.ptr),e))}recent(){return p(this.alk,this.alk.x.alk_pipeline_recent(o(this.ptr)))}stats(){return p(this.alk,this.alk.x.alk_pipeline_stats(o(this.ptr)))}get time(){return this.alk.x.alk_pipeline_get(o(this.ptr),0)}get sleeping(){return this.alk.x.alk_pipeline_get(o(this.ptr),1)===1}get gainDb(){return this.alk.x.alk_pipeline_get(o(this.ptr),2)}get hops(){return this.alk.x.alk_pipeline_get(o(this.ptr),3)}get chained(){return this.alk.x.alk_pipeline_get(o(this.ptr),4)===1}release(){this.ptr&&this.alk.x.alk_pipeline_free(this.ptr),this.ptr=0}},E=class{constructor(e,r){this.alk=e;b(e,{reference:r}),this.ptr=e.x.alk_coach(),this.ptr||e.fail("coach")}ptr;learner(e){return b(this.alk,{start:e}),p(this.alk,this.alk.x.alk_coach_learner(o(this.ptr)))}forget(){this.alk.x.alk_coach_forget(o(this.ptr))}window(e,r,t,n){return p(this.alk,_(this.alk,r,(a,i)=>this.alk.x.alk_coach_window(o(this.ptr),e.handle,a,i,t,n)))}verify(e,r){return p(this.alk,_(this.alk,r,(t,n)=>this.alk.x.alk_coach_verify(o(this.ptr),e.handle,t,n)))}release(){this.ptr&&this.alk.x.alk_coach_free(this.ptr),this.ptr=0}};var T=class{constructor(e,r){this.alk=e;this.bytes=r}models=new Map;calls=new Map;lexer=null;voices=null;has(e){return this.models.has(e)}model(e){let r=this.models.get(e);return r||this.models.set(e,r=this.alk.model(this.bytes(e))),r}stream(e){return this.model(e).session()}chain(e){return new v(this.alk,this.model("focus"),this.model("cue"),e)}processor(e){let r=!!e.focus,t={focus:r?this.model("focus"):void 0,cue:r||e.cue?this.model("cue"):void 0,spot:e.spot?this.model("spot"):void 0};return new x(this.alk,t,e)}listener(e,r){return new A(this.alk,this.model("spot"),e,r)}enroller(e,r,t){return new F(this.alk,this.model("spot"),e,r,t)}lex(){return this.lexer??=new w(this.alk,this.model("lex"))}timbre(){return this.voices??=new N(this.alk,this.model("timbre"))}learner(e,r){return new M(this.alk,e,r)}pipeline(e){let r=!!e.chain,t=!!e.words?.length||!!e.enroll;return new V(this.alk,{focus:r?this.model("focus"):void 0,cue:r?this.model("cue"):void 0,spot:t?this.model("spot"):void 0},e)}coach(e){return new E(this.alk,e)}voiceprint(e){return Q(this.alk,e)}compile(e,r,t){return J(this.alk,/^\s*\/.+\/\s*$/.test(e)?null:this.lex(),e,r,t)}net(e,r){let t=`${e} ${r??""}`,n=this.calls.get(t);n||this.calls.set(t,n=this.model(e).session(r));let a=n;return{run:(i,l)=>a.call(i,l),reset:()=>a.reset()}}release(){this.lexer?.release(),this.voices?.release(),this.lexer=null,this.voices=null;for(let e of this.calls.values())e.release();for(let e of this.models.values())e.release();this.calls.clear(),this.models.clear()}};var g=class extends Error{},C=class s{constructor(e){this.x=e}static async create(e){let r=await WebAssembly.instantiate(e,{});return new s(r.exports)}get heap(){return this.x.memory.buffer}with(e,r){let t=this.x.alk_alloc(e.length);new Uint8Array(this.heap,t,e.length).set(e);try{return r(t,e.length)}finally{this.x.alk_free(t,e.length)}}fail(e){let t=this.x.alk_alloc(512);try{let n=this.x.alk_error(t,512);throw new g(`${e}: ${new TextDecoder().decode(new Uint8Array(this.heap,t,n))}`)}finally{this.x.alk_free(t,512)}}model(e){return new U(this,e)}},U=class{constructor(e,r){this.alk=e;let t=e.x;this.ptr=e.with(r,(i,l)=>t.alk_model(i,l)),this.ptr||e.fail("model");let n=t.alk_alloc(256),a=i=>new TextDecoder().decode(new Uint8Array(e.heap,n,i));try{this.plans=Array.from({length:t.alk_plans(this.ptr)},(i,l)=>{let m=c=>Array.from({length:t.alk_io_count(this.ptr,l,c)},(u,k)=>{let f=a(t.alk_io_name(this.ptr,l,c,k,n,256)),O=t.alk_io_shape(this.ptr,l,c,k,n,64),L=Array.from(new Uint32Array(e.heap,n,O));return{name:f,dims:L,size:L.reduce((q,K)=>q*K,1)}}),h=m(0);return{index:l,name:a(t.alk_plan_name(this.ptr,l,n,256)),inputs:h,outputs:m(1),variable:h[t.alk_variable(this.ptr,l)]?.name??null}})}finally{t.alk_free(n,256)}}ptr;plans;session(e){let r=e===void 0?this.plans[0]:this.plans.find(t=>t.name===e);if(!r)throw new g(`no plan named ${e}`);return new P(this,r)}release(){this.alk.x.alk_model_free(this.ptr)}},P=class{constructor(e,r){this.model=e;this.plan=r;let{alk:t}=e,n=t.x;this.ptr=n.alk_session(e.ptr,r.index),this.ptr||t.fail("session");let a=new TextEncoder;for(let[i,l]of[[r.inputs,n.alk_input],[r.outputs,n.alk_output]])for(let m of i)this.at.set(m.name,{...m,p:t.with(a.encode(m.name),(h,c)=>l(this.ptr,h,c))})}ptr;at=new Map;io(e){let r=this.at.get(e);if(!r)throw new g(`no tensor named ${e}`);return r}f32(e){let r=this.io(e);return new Float32Array(this.model.alk.heap,r.p,r.size)}run(){this.model.alk.x.alk_run(this.ptr)}reset(){this.model.alk.x.alk_state_clear(this.ptr)}call(e,r=this.plan.outputs.map(t=>t.name)){let{alk:t}=this.model,{variable:n}=this.plan;for(let i of this.plan.inputs){let l=e[i.name];if(!l)throw new g(`missing input ${i.name}`);if(i.name===n)t.x.alk_set_len(this.ptr,l.data.length)!==0&&t.fail(i.name);else if(l.data.length!==i.size)throw new g(`${i.name}: ${l.data.length} values for ${i.size}`);this.f32(i.name).set(l.data)}this.run();let a={};for(let i of r)a[i]={data:this.f32(i).slice(),dims:[...this.io(i).dims]};return a}release(){this.model.alk.x.alk_session_free(this.ptr),this.at.clear()}};function G(s){let e=new Map,r=new Map,t=(n,a,i)=>{let l=n.get(a);if(!l)throw new Error(`${i} ${a} is closed, or its worker was started again`);return l};return{embed:(n,a)=>s().timbre().embed(n,a??"raw"),learnerOpen:(n,a)=>{let i=s().learner("raw",a);return e.set(n,i),{progress:i.progress(),ready:i.ready(),voiceprint:i.voiceprint(!0)}},learnerOffer:(n,a)=>{let i=t(e,n,"learner");return{...i.offer(s().timbre(),a),voiceprint:i.voiceprint(!0)}},learnerForget:n=>(e.get(n)?.forget(),null),learnerClose:n=>(e.get(n)?.release(),e.delete(n),null),phonemize:(n,a)=>s().lex().phonemize(n,a),compile:(n,a)=>s().compile(n,a.lang===void 0?[]:[a.lang].flat(),a.chain).word,process:(n,a,i)=>{let l=H(s(),n,{...a,onProgress:i});return{audio:l.audio,raw:l.raw,speech:l.speech,wakes:l.wakes,diag:l.diag,trace:l.trace}},streamOpen:(n,a)=>(r.set(n,new W(s(),a)),null),streamPush:(n,a)=>t(r,n,"stream").push(a),streamEnd:n=>{let a=t(r,n,"stream");return r.delete(n),a.end()}}}var Y=class{engine=null;boot=null;bytes=new Map;calls=G(()=>this.engine);async ready(e){await(this.boot??=(async()=>{let r=await $(S,void 0,new URL("./alkove-engine.wasm",import.meta.url).href),t=await C.create(await WebAssembly.compile(r));this.engine=new T(t,n=>this.bytes.get(n))})().catch(r=>{throw this.boot=null,r}));for(let r of e)this.bytes.has(r)||this.bytes.set(r,await D(r));return this}};export{Y as NodeBackend};
|