opus-codec-worker 1.0.0 → 1.0.1
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/package.json +1 -1
- package/worker.js +1 -1
package/package.json
CHANGED
package/worker.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{"use strict";var e,t,r,s={},i={};function a(e){var t=i[e];if(void 0!==t)return t.exports;var r=i[e]={exports:{}};return s[e](r,r.exports,a),r.exports}a.m=s,t=Object.getPrototypeOf?e=>Object.getPrototypeOf(e):e=>e.__proto__,a.t=function(r,s){if(1&s&&(r=this(r)),8&s)return r;if("object"==typeof r&&r){if(4&s&&r.__esModule)return r;if(16&s&&"function"==typeof r.then)return r}var i=Object.create(null);a.r(i);var n={};e=e||[null,t({}),t([]),t(t)];for(var o=2&s&&r;("object"==typeof o||"function"==typeof o)&&!~e.indexOf(o);o=t(o))Object.getOwnPropertyNames(o).forEach(e=>n[e]=()=>r[e]);return n.default=()=>r,a.d(i,n),i},a.d=(e,t)=>{for(var r in t)a.o(t,r)&&!a.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},a.f={},a.e=e=>Promise.all(Object.keys(a.f).reduce((t,r)=>(a.f[r](e,t),t),[])),a.u=e=>e+".js",a.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(e){if("object"==typeof window)return window}}(),a.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),a.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},(()=>{var e;a.g.importScripts&&(e=a.g.location+"");var t=a.g.document;if(!e&&t&&(t.currentScript&&"SCRIPT"===t.currentScript.tagName.toUpperCase()&&(e=t.currentScript.src),!e)){var r=t.getElementsByTagName("script");if(r.length)for(var s=r.length-1;s>-1&&(!e||!/^http(s?):/.test(e));)e=r[s--].src}if(!e)throw new Error("Automatic publicPath is not supported in this browser");e=e.replace(/^blob:/,"").replace(/#.*$/,"").replace(/\?.*$/,"").replace(/\/[^\/]+$/,"/"),a.p=e})(),(()=>{var e={941:1};a.f.i=(t,r)=>{e[t]||importScripts(a.p+a.u(t))};var t=self.webpackChunkopus_codec=self.webpackChunkopus_codec||[],r=t.push.bind(t);t.push=t=>{var[s,i,n]=t;for(var o in i)a.o(i,o)&&(a.m[o]=i[o]);for(n&&n(a);s.length;)e[s.pop()]=1;r(t)}})(),function(e){e[e.CreateEncoder=0]="CreateEncoder",e[e.CreateDecoder=1]="CreateDecoder",e[e.EncodeFloat=2]="EncodeFloat",e[e.DecodeFloat=3]="DecodeFloat",e[e.DestroyEncoder=4]="DestroyEncoder",e[e.InitializeWorker=5]="InitializeWorker",e[e.DestroyDecoder=6]="DestroyDecoder",e[e.OpusGetRequest=7]="OpusGetRequest",e[e.OpusSetRequest=8]="OpusSetRequest"}(r||(r={}));class n{#e;#t;constructor(e){this.#e=e,this.#t=e.malloc(e.originalRuntime().size_of_int())}value(){if(4!==this.#e.originalRuntime().size_of_int())throw new Error("invalid integer byte size");return this.#e.view().getInt32(this.#t,!0)}set(e){this.#e.view().setInt32(this.#t,e,!0)}size(){return this.#e.originalRuntime().size_of_int()}offset(){return this.#t}destroy(){this.#e.free(this.#t),this.#t=0}[Symbol.dispose](){this.destroy()}}class o{#e;constructor(e){this.#e=e}originalRuntime(){return this.#e}subarray(e,t){return new Uint8Array(this.#e.memory.buffer,e,t-e)}free(e){this.#e.free(e)}view(){return new DataView(this.#e.memory.buffer)}malloc(e){const t=this.#e.malloc(e);if(!t)throw new Error(`failed to allocate ${e} bytes`);return t}}class u{#t;#r;#e;constructor(e,t){this.#e=e,this.#r=t,this.#t=e.malloc(t)}offset(){return this.#t}data(){return this.#e.subarray(this.#t,this.#t+this.#r)}size(){return this.#r}destroy(){this.#e.free(this.#t),this.#t=0}}class d{#s=new Set;#i=!1;constructor(){}add(e){this.#s.add(e)}destroy(){if(this.#i)throw new Error("ResourcesHolder already destroyed");this.#i=!0;for(const e of this.#s)e.destroy()}[Symbol.dispose](){this.destroy()}}class l{#a;#n;#o;#e;#u;#d;#l=null;constructor(e,t,r,s){if(this.#d=s,this.#e=e,this.#o=new d,this.#a=new n(e),this.#o.add(this.#a),this.#n=e.originalRuntime().opus_decoder_create(t,r,this.#a.offset()),!this.#n||this.#a.value()<0)throw new Error("Failed to create decoder");this.#u=new u(e,this.#d*r*Float32Array.BYTES_PER_ELEMENT)}decodeFloat(e,t=0){let r=this.#l;r||(r=new u(this.#e,e.byteLength)),r.data().byteLength<e.byteLength&&(r.destroy(),r=new u(this.#e,e.byteLength)),r.data().set(e),this.#l=r;const s=this.#e.originalRuntime().opus_decode_float(this.#n,r.offset(),e.byteLength,this.#u.offset(),this.#d,t);if(s<0)throw new Error("Failed to decode float");return s}decoded(){const e=this.#u.data();return new Float32Array(e.buffer,e.byteOffset,e.byteLength/Float32Array.BYTES_PER_ELEMENT)}destroy(){this.#e.originalRuntime().opus_decoder_destroy(this.#n),this.#o.destroy(),this.#l?.destroy()}[Symbol.dispose](){this.destroy()}}const h=async function({wasmFileHref:e}={}){const t=new WebAssembly.Memory({initial:2}),r={env:{memory:t},wasi_snapshot_preview1:{args_get:()=>1,args_sizes_get:()=>1,fd_close:()=>1,fd_seek:()=>1,fd_write:()=>1,proc_exit:()=>0}};let s;if("process"in globalThis){const e=await a.e(306).then(a.t.bind(a,306,19)),t=(await a.e(178).then(a.t.bind(a,178,19))).resolve("/","index.wasm"),i=await e.promises.readFile(t);s=WebAssembly.instantiate(i,r)}else{if("string"!=typeof e&&(e=(await a.e(778).then(a.t.bind(a,778,17))).default),"string"!=typeof e)throw new Error("Invalid wasmFileHref");const t=await fetch(e);s=await WebAssembly.instantiateStreaming(t,r)}return{...(await s).instance.exports,memory:t}};var c;function f(e,t){let r;switch(t.type){case c.SetComplexity:r=e.setComplexity(t.value);break;case c.SetBitrate:r=e.setBitrate(t.value);break;case c.SetVbr:r=e.setVbr(t.value);break;case c.SetVbrConstraint:r=e.setVbrConstraint(t.value);break;case c.SetForceChannels:r=e.setForceChannels(t.value);break;case c.SetMaxBandwidth:r=e.setMaxBandwidth(t.value);break;case c.SetBandwidth:r=e.setBandwidth(t.value);break;case c.SetSignal:r=e.setSignal(t.value);break;case c.SetApplication:r=e.setApplication(t.value);break;case c.SetInbandFec:r=e.setInbandFec(t.value);break;case c.SetPacketLossperc:r=e.setPacketLossperc(t.value);break;case c.SetDtx:r=e.setDtx(t.value);break;case c.SetLsbDepth:r=e.setLsbDepth(t.value);break;case c.SetExpertFrameduration:r=e.setExpertFrameduration(t.value);break;case c.SetPredictionDisabled:r=e.setPredictionDisabled(t.value);break;case c.SetPhaseInversiondisabled:r=e.setPhaseInversiondisabled(t.value);break;case c.SetGain:r=e.setGain(t.value)}return r}function _(e,t){let r;switch(t.type){case c.GetComplexity:r=e.getComplexity();break;case c.GetBitrate:r=e.getBitrate();break;case c.GetVbr:r=e.getVbr();break;case c.GetVbrConstraint:r=e.getVbrConstraint();break;case c.GetForceChannels:r=e.getForceChannels();break;case c.GetMaxBandwidth:r=e.getMaxBandwidth();break;case c.GetSignal:r=e.getSignal();break;case c.GetApplication:r=e.getApplication();break;case c.GetLookahead:r=e.getLookahead();break;case c.GetInbandFec:r=e.getInbandFec();break;case c.GetPacketLossperc:r=e.getPacketLossperc();break;case c.GetDtx:r=e.getDtx();break;case c.GetLsbDepth:r=e.getLsbDepth();break;case c.GetExpertFrameduration:r=e.getExpertFrameduration();break;case c.GetPredictionDisabled:r=e.getPredictionDisabled();break;case c.GetBandwidth:r=e.getBandwidth();break;case c.GetSampleRate:r=e.getSampleRate();break;case c.GetPhaseInversiondisabled:r=e.getPhaseInversiondisabled();break;case c.GetInDtx:r=e.getInDtx();break;case c.GetGain:r=e.getGain();break;case c.GetLastPacketduration:r=e.getLastPacketduration();break;case c.GetPitch:r=e.getPitch()}return r}!function(e){e.SetComplexity="OPUS_SET_COMPLEXITY",e.GetComplexity="OPUS_GET_COMPLEXITY",e.SetBitrate="OPUS_SET_BITRATE",e.GetBitrate="OPUS_GET_BITRATE",e.SetVbr="OPUS_SET_VBR",e.GetVbr="OPUS_GET_VBR",e.SetVbrConstraint="OPUS_SET_VBR_CONSTRAINT",e.GetVbrConstraint="OPUS_GET_VBR_CONSTRAINT",e.SetForceChannels="OPUS_SET_FORCE_CHANNELS",e.GetForceChannels="OPUS_GET_FORCE_CHANNELS",e.SetMaxBandwidth="OPUS_SET_MAX_BANDWIDTH",e.GetMaxBandwidth="OPUS_GET_MAX_BANDWIDTH",e.SetBandwidth="OPUS_SET_BANDWIDTH",e.SetSignal="OPUS_SET_SIGNAL",e.GetSignal="OPUS_GET_SIGNAL",e.SetApplication="OPUS_SET_APPLICATION",e.GetApplication="OPUS_GET_APPLICATION",e.GetLookahead="OPUS_GET_LOOKAHEAD",e.SetInbandFec="OPUS_SET_INBAND_FEC",e.GetInbandFec="OPUS_GET_INBAND_FEC",e.SetPacketLossperc="OPUS_SET_PACKET_LOSS_PERC",e.GetPacketLossperc="OPUS_GET_PACKET_LOSS_PERC",e.SetDtx="OPUS_SET_DTX",e.GetDtx="OPUS_GET_DTX",e.SetLsbDepth="OPUS_SET_LSB_DEPTH",e.GetLsbDepth="OPUS_GET_LSB_DEPTH",e.SetExpertFrameduration="OPUS_SET_EXPERT_FRAME_DURATION",e.GetExpertFrameduration="OPUS_GET_EXPERT_FRAME_DURATION",e.SetPredictionDisabled="OPUS_SET_PREDICTION_DISABLED",e.GetPredictionDisabled="OPUS_GET_PREDICTION_DISABLED",e.GetBandwidth="OPUS_GET_BANDWIDTH",e.GetSampleRate="OPUS_GET_SAMPLE_RATE",e.SetPhaseInversiondisabled="OPUS_SET_PHASE_INVERSION_DISABLED",e.GetPhaseInversiondisabled="OPUS_GET_PHASE_INVERSION_DISABLED",e.GetInDtx="OPUS_GET_IN_DTX",e.SetGain="OPUS_SET_GAIN",e.GetGain="OPUS_GET_GAIN",e.GetLastPacketduration="OPUS_GET_LAST_PACKET_DURATION",e.GetPitch="OPUS_GET_PITCH"}(c||(c={}));const p=0;class m{#h;#e;#c;constructor(e,t){this.#e=e,this.#c=new n(e),this.#h=t}setComplexity(e){return this.#e.originalRuntime()._opus_set_complexity(this.#h,e)===p}getComplexity(){if(this.#e.originalRuntime()._opus_get_complexity(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_COMPLEXITY");return this.#c.value()}setBitrate(e){return this.#e.originalRuntime()._opus_set_bitrate(this.#h,e)===p}getBitrate(){if(this.#e.originalRuntime()._opus_get_bitrate(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_BITRATE");return this.#c.value()}setVbr(e){return this.#e.originalRuntime()._opus_set_vbr(this.#h,e)===p}getVbr(){if(this.#e.originalRuntime()._opus_get_vbr(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_VBR");return this.#c.value()}setVbrConstraint(e){return this.#e.originalRuntime()._opus_set_vbr_constraint(this.#h,e)===p}getVbrConstraint(){if(this.#e.originalRuntime()._opus_get_vbr_constraint(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_VBR_CONSTRAINT");return this.#c.value()}setForceChannels(e){return this.#e.originalRuntime()._opus_set_force_channels(this.#h,e)===p}getForceChannels(){if(this.#e.originalRuntime()._opus_get_force_channels(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_FORCE_CHANNELS");return this.#c.value()}setMaxBandwidth(e){return this.#e.originalRuntime()._opus_set_max_bandwidth(this.#h,e)===p}getMaxBandwidth(){if(this.#e.originalRuntime()._opus_get_max_bandwidth(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_MAX_BANDWIDTH");return this.#c.value()}setBandwidth(e){return this.#e.originalRuntime()._opus_set_bandwidth(this.#h,e)===p}setSignal(e){return this.#e.originalRuntime()._opus_set_signal(this.#h,e)===p}getSignal(){if(this.#e.originalRuntime()._opus_get_signal(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_SIGNAL");return this.#c.value()}setApplication(e){return this.#e.originalRuntime()._opus_set_application(this.#h,e)===p}getApplication(){if(this.#e.originalRuntime()._opus_get_application(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_APPLICATION");return this.#c.value()}getLookahead(){if(this.#e.originalRuntime()._opus_get_lookahead(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_LOOKAHEAD");return this.#c.value()}setInbandFec(e){return this.#e.originalRuntime()._opus_set_inband_fec(this.#h,e)===p}getInbandFec(){if(this.#e.originalRuntime()._opus_get_inband_fec(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_INBAND_FEC");return this.#c.value()}setPacketLossperc(e){return this.#e.originalRuntime()._opus_set_packet_loss_perc(this.#h,e)===p}getPacketLossperc(){if(this.#e.originalRuntime()._opus_get_packet_loss_perc(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_PACKET_LOSS_PERC");return this.#c.value()}setDtx(e){return this.#e.originalRuntime()._opus_set_dtx(this.#h,e)===p}getDtx(){if(this.#e.originalRuntime()._opus_get_dtx(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_DTX");return this.#c.value()}setLsbDepth(e){return this.#e.originalRuntime()._opus_set_lsb_depth(this.#h,e)===p}getLsbDepth(){if(this.#e.originalRuntime()._opus_get_lsb_depth(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_LSB_DEPTH");return this.#c.value()}setExpertFrameduration(e){return this.#e.originalRuntime()._opus_set_expert_frame_duration(this.#h,e)===p}getExpertFrameduration(){if(this.#e.originalRuntime()._opus_get_expert_frame_duration(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_EXPERT_FRAME_DURATION");return this.#c.value()}setPredictionDisabled(e){return this.#e.originalRuntime()._opus_set_prediction_disabled(this.#h,e)===p}getPredictionDisabled(){if(this.#e.originalRuntime()._opus_get_prediction_disabled(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_PREDICTION_DISABLED");return this.#c.value()}getBandwidth(){if(this.#e.originalRuntime()._opus_get_bandwidth(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_BANDWIDTH");return this.#c.value()}getSampleRate(){if(this.#e.originalRuntime()._opus_get_sample_rate(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_SAMPLE_RATE");return this.#c.value()}setPhaseInversiondisabled(e){return this.#e.originalRuntime()._opus_set_phase_inversion_disabled(this.#h,e)===p}getPhaseInversiondisabled(){if(this.#e.originalRuntime()._opus_get_phase_inversion_disabled(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_PHASE_INVERSION_DISABLED");return this.#c.value()}getInDtx(){if(this.#e.originalRuntime()._opus_get_in_dtx(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_IN_DTX");return this.#c.value()}setGain(e){return this.#e.originalRuntime()._opus_set_gain(this.#h,e)===p}getGain(){if(this.#e.originalRuntime()._opus_get_gain(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_GAIN");return this.#c.value()}getLastPacketduration(){if(this.#e.originalRuntime()._opus_get_last_packet_duration(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_LAST_PACKET_DURATION");return this.#c.value()}getPitch(){if(this.#e.originalRuntime()._opus_get_pitch(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_PITCH");return this.#c.value()}}class E extends m{#a;#e;#f;#_;#u;#o;constructor(e,t,r,s,i,a){const o=new n(e),l=e.originalRuntime().opus_encoder_create(t,r,s,o.offset());if(super(e,l),this.#o=new d,this.#a=o,this.#e=e,this.#u=new u(e,a),this.#_=new u(e,i),this.#o.add(this.#_),this.#o.add(this.#u),this.#o.add(this.#a),this.#f=l,o.value()<0)throw new Error("Failed to create encoder");if(!i)throw new Error("outBufferLength must be more than 0")}encoded(){return this.#_.data()}encodeFloat(e,t,r){if(r>this.#_.size())throw new Error(`encoded buffer length is ${this.#_.size()}, but maxDataBytes is ${r}`);this.#u.data().set(new Uint8Array(e.buffer,e.byteOffset,e.byteLength));const s=this.#e.originalRuntime().opus_encode_float(this.#f,this.#u.offset(),t,this.#_.offset(),r);if(s<0)throw new Error(`Failed to encode float: ${s}`);return s}destroy(){this.#o.destroy(),this.#e.originalRuntime().opus_encoder_destroy(this.#f)}[Symbol.dispose](){this.destroy()}}class g extends Error{constructor(e){super(e)}}function S({name:e,value:t,validations:r}){const s=new Array;if(r.integer&&!Number.isSafeInteger(t)&&s.push("It must be an integer."),t>r.max&&s.push(`It must be up to ${r.max}.`),t<r.min&&s.push(`It must be at least ${r.min}.`),s.length>0)throw new g([`"${t}" is not a valid value for ${e}. It failed to following validations:\n`,...s.map(e=>`\t- ${e}`)].join(""));return t}var b,w,v,O,T,P,y,I,R,G,A,D,F,k,C,B=function(e,t,r,s,i){if("m"===s)throw new TypeError("Private method is not writable");if("a"===s&&!i)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!i:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===s?i.call(e,r):i?i.value=r:t.set(e,r),r},L=function(e,t,r,s){if("a"===r&&!s)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!s:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?s:"a"===r?s.call(e):s?s.value:t.get(e)};const U=4294967295;class x{constructor({frameSize:e,frameCacheSize:t=0,preallocateFrameCount:r=10,TypedArrayConstructor:s}){if(b.add(this),w.set(this,void 0),v.set(this,void 0),O.set(this,void 0),T.set(this,void 0),P.set(this,void 0),y.set(this,void 0),I.set(this,void 0),B(this,T,S({value:e,name:"Frame Size",validations:{min:1,integer:!0,max:U}}),"f"),B(this,v,S({value:r,name:"Preallocate Frame Count",validations:{min:1,integer:!0,max:U}}),"f"),B(this,O,S({value:t,name:"Frame Cache Size",validations:{min:0,integer:!1,max:U}}),"f"),!("BYTES_PER_ELEMENT"in s))throw new g("TypedArrayConstructor must be a typed array");B(this,y,0,"f"),B(this,I,0,"f"),B(this,w,s,"f"),B(this,P,new ArrayBuffer(L(this,b,"m",C).call(this)),"f")}empty(){return L(this,b,"m",A).call(this)<L(this,T,"f")}peek(){return L(this,b,"m",R).call(this)}write(e){L(this,b,"m",k).call(this,e.length),L(this,b,"m",R).call(this).subarray(L(this,I,"f"),L(this,I,"f")+e.length).set(e),B(this,I,L(this,I,"f")+e.length,"f")}remainingFrames(){return Math.floor(L(this,b,"m",G).call(this))}drain(){return L(this,b,"m",F).call(this,L(this,b,"m",A).call(this))}read(){return L(this,b,"m",F).call(this,L(this,T,"f"))}get[(w=new WeakMap,v=new WeakMap,O=new WeakMap,T=new WeakMap,P=new WeakMap,y=new WeakMap,I=new WeakMap,b=new WeakSet,R=function(){return new(L(this,w,"f"))(L(this,P,"f"))},G=function(){return L(this,b,"m",A).call(this)/L(this,T,"f")},A=function(){return L(this,I,"f")-L(this,y,"f")},D=function(){return L(this,O,"f")>0&&L(this,b,"m",G).call(this)>=L(this,O,"f")},F=function(e){if(!e)return null;if(L(this,b,"m",A).call(this)<e)return null;let t=L(this,b,"m",R).call(this).subarray(L(this,y,"f"),L(this,y,"f")+e);if(B(this,y,L(this,y,"f")+e,"f"),L(this,y,"f")===L(this,I,"f")&&(B(this,I,0,"f"),B(this,y,0,"f")),L(this,b,"m",D).call(this)){const e=this.drain();e&&(t=t.slice(0),this.write(e))}return t},k=function(e){const t=e*L(this,w,"f").BYTES_PER_ELEMENT;if(L(this,b,"m",R).call(this).length-L(this,I,"f")<=e){const e=L(this,P,"f");B(this,P,new ArrayBuffer(e.byteLength+t+L(this,b,"m",C).call(this)),"f"),L(this,b,"m",R).call(this).set(new(L(this,w,"f"))(e))}return L(this,b,"m",R).call(this).subarray(L(this,y,"f"),L(this,y,"f")+L(this,T,"f"))},C=function(){return L(this,T,"f")*L(this,w,"f").BYTES_PER_ELEMENT*L(this,v,"f")},Symbol.toStringTag)](){return"RingBuffer"}[Symbol.iterator](){return this[Symbol.asyncIterator]()}*[Symbol.asyncIterator](){let e;do{e=this.read(),null!==e&&(yield e)}while(null!==e)}}class N extends x{constructor(e,t={}){super(Object.assign(Object.assign({},t),{frameSize:e,TypedArrayConstructor:Float32Array}))}get[Symbol.toStringTag](){return"RingBufferF32"}}Symbol.toStringTag,Symbol.toStringTag,Symbol.toStringTag,Symbol.toStringTag,Symbol.toStringTag;let q={queue:new Set};function M(){return crypto.getRandomValues(new Uint32Array(4)).join("-")}function V(e,t=[]){return postMessage(e,t)}const z=async e=>{if("queue"in q){switch(e.type){case r.InitializeWorker:{const t=q.queue,r={runtime:new o(await h({wasmFileHref:e.data.wasmFileHref})),encoders:new Map,decoders:new Map};q=r;for(const e of t)await z(e);t.clear(),V({requestId:e.requestId,value:null});break}default:q.queue.add(e)}return}const{runtime:t,encoders:s,decoders:i}=q;switch(e.type){case r.CreateEncoder:{const r=new E(t,e.data.sampleRate,e.data.channels,e.data.application,e.data.outBufferLength,e.data.pcmBufferLength),i=M();s.set(i,{sampleRate:e.data.sampleRate,ringBuffer:new N(e.data.pcmBufferLength/Float32Array.BYTES_PER_ELEMENT),encoder:r}),V({requestId:e.requestId,value:i});break}case r.CreateDecoder:{const r=new l(t,e.data.sampleRate,e.data.channels,e.data.frameSize),s=M();i.set(s,r),V({requestId:e.requestId,value:s});break}case r.DestroyEncoder:{const t=s.get(e.data);if(!t)throw new Error("Failed to get encoder");s.delete(e.data),t.encoder.destroy(),V(t?{requestId:e.requestId,value:null}:{requestId:e.requestId,failures:[`failed to find encoder with id: ${e.data}`]});break}case r.DestroyDecoder:{const t=i.get(e.data.decoderId);i.delete(e.data.decoderId),t&&t.destroy(),V(t?{requestId:e.requestId,value:null}:{requestId:e.requestId,failures:[`failed to find decoder with id: ${e.data.decoderId}`]});break}case r.EncodeFloat:{const t=s.get(e.data.encoderId);if(!t)throw new Error("Failed to get encoder");const r=null===e.data.input;e.data.input&&t.ringBuffer.write(e.data.input.pcm);const i=t.ringBuffer.read()??(r?t.ringBuffer.drain():null);if(null===i){V({requestId:e.requestId,value:{encoded:null}});break}const a=t.encoder.encodeFloat(i,i.length,e.data.maxDataBytes),n={buffer:new ArrayBuffer(a),duration:a/t.sampleRate};new Uint8Array(n.buffer).set(t.encoder.encoded().subarray(0,a)),V({requestId:e.requestId,value:{encoded:n}},[n.buffer]);break}case r.DecodeFloat:{const t=i.get(e.data.decoderId);let r;if(t){const s=t.decodeFloat(new Uint8Array(e.data.encoded),e.data.decodeFec);r=t.decoded().slice(0,s)}else r=null;V(r?{requestId:e.requestId,value:{decoded:r.buffer}}:{requestId:e.requestId,failures:[`no decoder found for decoder id: ${e.data.decoderId}`]},r?[r.buffer]:[]);break}case r.OpusSetRequest:{const t=s.get(e.data.encoderId);V(t?.encoder?{value:f(t.encoder,e.data),requestId:e.requestId}:{requestId:e.requestId,failures:[`no encoder found for: ${e.data.encoderId}`]});break}case r.OpusGetRequest:{const t=s.get(e.data.encoderId);V(t?.encoder?{value:_(t.encoder,e.data),requestId:e.requestId}:{requestId:e.requestId,failures:[`no encoder found for: ${e.data.encoderId}`]});break}}};onmessage=e=>{const t=e.data;z(t).catch(e=>{console.error("failed to process request: %o",{request:t,reason:e})})}})();
|
|
1
|
+
(()=>{"use strict";var e,t,r,s={},i={};function a(e){var t=i[e];if(void 0!==t)return t.exports;var r=i[e]={exports:{}};return s[e](r,r.exports,a),r.exports}a.m=s,t=Object.getPrototypeOf?e=>Object.getPrototypeOf(e):e=>e.__proto__,a.t=function(r,s){if(1&s&&(r=this(r)),8&s)return r;if("object"==typeof r&&r){if(4&s&&r.__esModule)return r;if(16&s&&"function"==typeof r.then)return r}var i=Object.create(null);a.r(i);var n={};e=e||[null,t({}),t([]),t(t)];for(var o=2&s&&r;("object"==typeof o||"function"==typeof o)&&!~e.indexOf(o);o=t(o))Object.getOwnPropertyNames(o).forEach(e=>n[e]=()=>r[e]);return n.default=()=>r,a.d(i,n),i},a.d=(e,t)=>{for(var r in t)a.o(t,r)&&!a.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},a.f={},a.e=e=>Promise.all(Object.keys(a.f).reduce((t,r)=>(a.f[r](e,t),t),[])),a.u=e=>e+".js",a.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(e){if("object"==typeof window)return window}}(),a.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),a.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},(()=>{var e;a.g.importScripts&&(e=a.g.location+"");var t=a.g.document;if(!e&&t&&(t.currentScript&&"SCRIPT"===t.currentScript.tagName.toUpperCase()&&(e=t.currentScript.src),!e)){var r=t.getElementsByTagName("script");if(r.length)for(var s=r.length-1;s>-1&&(!e||!/^http(s?):/.test(e));)e=r[s--].src}if(!e)throw new Error("Automatic publicPath is not supported in this browser");e=e.replace(/^blob:/,"").replace(/#.*$/,"").replace(/\?.*$/,"").replace(/\/[^\/]+$/,"/"),a.p=e})(),(()=>{var e={941:1};a.f.i=(t,r)=>{e[t]||importScripts(a.p+a.u(t))};var t=self.webpackChunkopus_codec=self.webpackChunkopus_codec||[],r=t.push.bind(t);t.push=t=>{var[s,i,n]=t;for(var o in i)a.o(i,o)&&(a.m[o]=i[o]);for(n&&n(a);s.length;)e[s.pop()]=1;r(t)}})(),function(e){e[e.CreateEncoder=0]="CreateEncoder",e[e.CreateDecoder=1]="CreateDecoder",e[e.EncodeFloat=2]="EncodeFloat",e[e.DecodeFloat=3]="DecodeFloat",e[e.DestroyEncoder=4]="DestroyEncoder",e[e.InitializeWorker=5]="InitializeWorker",e[e.DestroyDecoder=6]="DestroyDecoder",e[e.OpusGetRequest=7]="OpusGetRequest",e[e.OpusSetRequest=8]="OpusSetRequest"}(r||(r={}));class n{#e;#t;constructor(e){this.#e=e,this.#t=e.malloc(e.originalRuntime().size_of_int())}value(){if(4!==this.#e.originalRuntime().size_of_int())throw new Error("invalid integer byte size");return this.#e.view().getInt32(this.#t,!0)}set(e){this.#e.view().setInt32(this.#t,e,!0)}size(){return this.#e.originalRuntime().size_of_int()}offset(){return this.#t}destroy(){this.#e.free(this.#t),this.#t=0}[Symbol.dispose](){this.destroy()}}class o{#e;constructor(e){this.#e=e}originalRuntime(){return this.#e}subarray(e,t){return new Uint8Array(this.#e.memory.buffer,e,t-e)}free(e){this.#e.free(e)}view(){return new DataView(this.#e.memory.buffer)}malloc(e){const t=this.#e.malloc(e);if(!t)throw new Error(`failed to allocate ${e} bytes`);return t}}class u{#t;#r;#e;constructor(e,t){this.#e=e,this.#r=t,this.#t=e.malloc(t)}offset(){return this.#t}data(){return this.#e.subarray(this.#t,this.#t+this.#r)}size(){return this.#r}destroy(){this.#e.free(this.#t),this.#t=0}}class d{#s=new Set;#i=!1;constructor(){}add(e){this.#s.add(e)}destroy(){if(this.#i)throw new Error("ResourcesHolder already destroyed");this.#i=!0;for(const e of this.#s)e.destroy()}[Symbol.dispose](){this.destroy()}}class l{#a;#n;#o;#e;#u;#d;#l=null;constructor(e,t,r,s){if(this.#d=s,this.#e=e,this.#o=new d,this.#a=new n(e),this.#o.add(this.#a),this.#n=e.originalRuntime().opus_decoder_create(t,r,this.#a.offset()),!this.#n||this.#a.value()<0)throw new Error("Failed to create decoder");this.#u=new u(e,this.#d*r*Float32Array.BYTES_PER_ELEMENT)}decodeFloat(e,t=0){let r=this.#l;r||(r=new u(this.#e,e.byteLength)),r.data().byteLength<e.byteLength&&(r.destroy(),r=new u(this.#e,e.byteLength)),r.data().set(e),this.#l=r;const s=this.#e.originalRuntime().opus_decode_float(this.#n,r.offset(),e.byteLength,this.#u.offset(),this.#d,t);if(s<0)throw new Error("Failed to decode float");return s}decoded(){const e=this.#u.data();return new Float32Array(e.buffer,e.byteOffset,e.byteLength/Float32Array.BYTES_PER_ELEMENT)}destroy(){this.#e.originalRuntime().opus_decoder_destroy(this.#n),this.#o.destroy(),this.#l?.destroy()}[Symbol.dispose](){this.destroy()}}const h=async function({wasmFileHref:e}={}){const t=new WebAssembly.Memory({initial:2}),r={env:{memory:t},wasi_snapshot_preview1:{args_get:()=>1,args_sizes_get:()=>1,fd_close:()=>1,fd_seek:()=>1,fd_write:()=>1,proc_exit:()=>0}};let s;if("process"in globalThis){const e=await a.e(306).then(a.t.bind(a,306,19)),t=(await a.e(178).then(a.t.bind(a,178,19))).resolve("/","index.wasm"),i=await e.promises.readFile(t);s=WebAssembly.instantiate(i,r)}else{if("string"!=typeof e&&(e=(await a.e(778).then(a.t.bind(a,778,17))).default),"string"!=typeof e)throw new Error("Invalid wasmFileHref");const t=await fetch(e);s=await WebAssembly.instantiateStreaming(t,r)}return{...(await s).instance.exports,memory:t}};var c;function f(e,t){let r;switch(t.type){case c.SetComplexity:r=e.setComplexity(t.value);break;case c.SetBitrate:r=e.setBitrate(t.value);break;case c.SetVbr:r=e.setVbr(t.value);break;case c.SetVbrConstraint:r=e.setVbrConstraint(t.value);break;case c.SetForceChannels:r=e.setForceChannels(t.value);break;case c.SetMaxBandwidth:r=e.setMaxBandwidth(t.value);break;case c.SetBandwidth:r=e.setBandwidth(t.value);break;case c.SetSignal:r=e.setSignal(t.value);break;case c.SetApplication:r=e.setApplication(t.value);break;case c.SetInbandFec:r=e.setInbandFec(t.value);break;case c.SetPacketLossperc:r=e.setPacketLossperc(t.value);break;case c.SetDtx:r=e.setDtx(t.value);break;case c.SetLsbDepth:r=e.setLsbDepth(t.value);break;case c.SetExpertFrameduration:r=e.setExpertFrameduration(t.value);break;case c.SetPredictionDisabled:r=e.setPredictionDisabled(t.value);break;case c.SetPhaseInversiondisabled:r=e.setPhaseInversiondisabled(t.value);break;case c.SetGain:r=e.setGain(t.value)}return r}function _(e,t){let r;switch(t.type){case c.GetComplexity:r=e.getComplexity();break;case c.GetBitrate:r=e.getBitrate();break;case c.GetVbr:r=e.getVbr();break;case c.GetVbrConstraint:r=e.getVbrConstraint();break;case c.GetForceChannels:r=e.getForceChannels();break;case c.GetMaxBandwidth:r=e.getMaxBandwidth();break;case c.GetSignal:r=e.getSignal();break;case c.GetApplication:r=e.getApplication();break;case c.GetLookahead:r=e.getLookahead();break;case c.GetInbandFec:r=e.getInbandFec();break;case c.GetPacketLossperc:r=e.getPacketLossperc();break;case c.GetDtx:r=e.getDtx();break;case c.GetLsbDepth:r=e.getLsbDepth();break;case c.GetExpertFrameduration:r=e.getExpertFrameduration();break;case c.GetPredictionDisabled:r=e.getPredictionDisabled();break;case c.GetBandwidth:r=e.getBandwidth();break;case c.GetSampleRate:r=e.getSampleRate();break;case c.GetPhaseInversiondisabled:r=e.getPhaseInversiondisabled();break;case c.GetInDtx:r=e.getInDtx();break;case c.GetGain:r=e.getGain();break;case c.GetLastPacketduration:r=e.getLastPacketduration();break;case c.GetPitch:r=e.getPitch()}return r}!function(e){e.SetComplexity="OPUS_SET_COMPLEXITY",e.GetComplexity="OPUS_GET_COMPLEXITY",e.SetBitrate="OPUS_SET_BITRATE",e.GetBitrate="OPUS_GET_BITRATE",e.SetVbr="OPUS_SET_VBR",e.GetVbr="OPUS_GET_VBR",e.SetVbrConstraint="OPUS_SET_VBR_CONSTRAINT",e.GetVbrConstraint="OPUS_GET_VBR_CONSTRAINT",e.SetForceChannels="OPUS_SET_FORCE_CHANNELS",e.GetForceChannels="OPUS_GET_FORCE_CHANNELS",e.SetMaxBandwidth="OPUS_SET_MAX_BANDWIDTH",e.GetMaxBandwidth="OPUS_GET_MAX_BANDWIDTH",e.SetBandwidth="OPUS_SET_BANDWIDTH",e.SetSignal="OPUS_SET_SIGNAL",e.GetSignal="OPUS_GET_SIGNAL",e.SetApplication="OPUS_SET_APPLICATION",e.GetApplication="OPUS_GET_APPLICATION",e.GetLookahead="OPUS_GET_LOOKAHEAD",e.SetInbandFec="OPUS_SET_INBAND_FEC",e.GetInbandFec="OPUS_GET_INBAND_FEC",e.SetPacketLossperc="OPUS_SET_PACKET_LOSS_PERC",e.GetPacketLossperc="OPUS_GET_PACKET_LOSS_PERC",e.SetDtx="OPUS_SET_DTX",e.GetDtx="OPUS_GET_DTX",e.SetLsbDepth="OPUS_SET_LSB_DEPTH",e.GetLsbDepth="OPUS_GET_LSB_DEPTH",e.SetExpertFrameduration="OPUS_SET_EXPERT_FRAME_DURATION",e.GetExpertFrameduration="OPUS_GET_EXPERT_FRAME_DURATION",e.SetPredictionDisabled="OPUS_SET_PREDICTION_DISABLED",e.GetPredictionDisabled="OPUS_GET_PREDICTION_DISABLED",e.GetBandwidth="OPUS_GET_BANDWIDTH",e.GetSampleRate="OPUS_GET_SAMPLE_RATE",e.SetPhaseInversiondisabled="OPUS_SET_PHASE_INVERSION_DISABLED",e.GetPhaseInversiondisabled="OPUS_GET_PHASE_INVERSION_DISABLED",e.GetInDtx="OPUS_GET_IN_DTX",e.SetGain="OPUS_SET_GAIN",e.GetGain="OPUS_GET_GAIN",e.GetLastPacketduration="OPUS_GET_LAST_PACKET_DURATION",e.GetPitch="OPUS_GET_PITCH"}(c||(c={}));const p=0;class m{#h;#e;#c;constructor(e,t){this.#e=e,this.#c=new n(e),this.#h=t}setComplexity(e){return this.#e.originalRuntime().opus_set_complexity(this.#h,e)===p}getComplexity(){if(this.#e.originalRuntime().opus_get_complexity(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_COMPLEXITY");return this.#c.value()}setBitrate(e){return this.#e.originalRuntime().opus_set_bitrate(this.#h,e)===p}getBitrate(){if(this.#e.originalRuntime().opus_get_bitrate(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_BITRATE");return this.#c.value()}setVbr(e){return this.#e.originalRuntime().opus_set_vbr(this.#h,e)===p}getVbr(){if(this.#e.originalRuntime().opus_get_vbr(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_VBR");return this.#c.value()}setVbrConstraint(e){return this.#e.originalRuntime().opus_set_vbr_constraint(this.#h,e)===p}getVbrConstraint(){if(this.#e.originalRuntime().opus_get_vbr_constraint(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_VBR_CONSTRAINT");return this.#c.value()}setForceChannels(e){return this.#e.originalRuntime().opus_set_force_channels(this.#h,e)===p}getForceChannels(){if(this.#e.originalRuntime().opus_get_force_channels(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_FORCE_CHANNELS");return this.#c.value()}setMaxBandwidth(e){return this.#e.originalRuntime().opus_set_max_bandwidth(this.#h,e)===p}getMaxBandwidth(){if(this.#e.originalRuntime().opus_get_max_bandwidth(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_MAX_BANDWIDTH");return this.#c.value()}setBandwidth(e){return this.#e.originalRuntime().opus_set_bandwidth(this.#h,e)===p}setSignal(e){return this.#e.originalRuntime().opus_set_signal(this.#h,e)===p}getSignal(){if(this.#e.originalRuntime().opus_get_signal(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_SIGNAL");return this.#c.value()}setApplication(e){return this.#e.originalRuntime().opus_set_application(this.#h,e)===p}getApplication(){if(this.#e.originalRuntime().opus_get_application(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_APPLICATION");return this.#c.value()}getLookahead(){if(this.#e.originalRuntime().opus_get_lookahead(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_LOOKAHEAD");return this.#c.value()}setInbandFec(e){return this.#e.originalRuntime().opus_set_inband_fec(this.#h,e)===p}getInbandFec(){if(this.#e.originalRuntime().opus_get_inband_fec(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_INBAND_FEC");return this.#c.value()}setPacketLossperc(e){return this.#e.originalRuntime().opus_set_packet_loss_perc(this.#h,e)===p}getPacketLossperc(){if(this.#e.originalRuntime().opus_get_packet_loss_perc(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_PACKET_LOSS_PERC");return this.#c.value()}setDtx(e){return this.#e.originalRuntime().opus_set_dtx(this.#h,e)===p}getDtx(){if(this.#e.originalRuntime().opus_get_dtx(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_DTX");return this.#c.value()}setLsbDepth(e){return this.#e.originalRuntime().opus_set_lsb_depth(this.#h,e)===p}getLsbDepth(){if(this.#e.originalRuntime().opus_get_lsb_depth(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_LSB_DEPTH");return this.#c.value()}setExpertFrameduration(e){return this.#e.originalRuntime().opus_set_expert_frame_duration(this.#h,e)===p}getExpertFrameduration(){if(this.#e.originalRuntime().opus_get_expert_frame_duration(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_EXPERT_FRAME_DURATION");return this.#c.value()}setPredictionDisabled(e){return this.#e.originalRuntime().opus_set_prediction_disabled(this.#h,e)===p}getPredictionDisabled(){if(this.#e.originalRuntime().opus_get_prediction_disabled(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_PREDICTION_DISABLED");return this.#c.value()}getBandwidth(){if(this.#e.originalRuntime().opus_get_bandwidth(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_BANDWIDTH");return this.#c.value()}getSampleRate(){if(this.#e.originalRuntime().opus_get_sample_rate(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_SAMPLE_RATE");return this.#c.value()}setPhaseInversiondisabled(e){return this.#e.originalRuntime().opus_set_phase_inversion_disabled(this.#h,e)===p}getPhaseInversiondisabled(){if(this.#e.originalRuntime().opus_get_phase_inversion_disabled(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_PHASE_INVERSION_DISABLED");return this.#c.value()}getInDtx(){if(this.#e.originalRuntime().opus_get_in_dtx(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_IN_DTX");return this.#c.value()}setGain(e){return this.#e.originalRuntime().opus_set_gain(this.#h,e)===p}getGain(){if(this.#e.originalRuntime().opus_get_gain(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_GAIN");return this.#c.value()}getLastPacketduration(){if(this.#e.originalRuntime().opus_get_last_packet_duration(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_LAST_PACKET_DURATION");return this.#c.value()}getPitch(){if(this.#e.originalRuntime().opus_get_pitch(this.#h,this.#c.offset())!=p)throw new Error("Failed to set OPUS_GET_PITCH");return this.#c.value()}}class E extends m{#a;#e;#f;#_;#u;#o;constructor(e,t,r,s,i,a){const o=new n(e),l=e.originalRuntime().opus_encoder_create(t,r,s,o.offset());if(super(e,l),this.#o=new d,this.#a=o,this.#e=e,this.#u=new u(e,a),this.#_=new u(e,i),this.#o.add(this.#_),this.#o.add(this.#u),this.#o.add(this.#a),this.#f=l,o.value()<0)throw new Error("Failed to create encoder");if(!i)throw new Error("outBufferLength must be more than 0")}encoded(){return this.#_.data()}encodeFloat(e,t,r){if(r>this.#_.size())throw new Error(`encoded buffer length is ${this.#_.size()}, but maxDataBytes is ${r}`);this.#u.data().set(new Uint8Array(e.buffer,e.byteOffset,e.byteLength));const s=this.#e.originalRuntime().opus_encode_float(this.#f,this.#u.offset(),t,this.#_.offset(),r);if(s<0)throw new Error(`Failed to encode float: ${s}`);return s}destroy(){this.#o.destroy(),this.#e.originalRuntime().opus_encoder_destroy(this.#f)}[Symbol.dispose](){this.destroy()}}class g extends Error{constructor(e){super(e)}}function S({name:e,value:t,validations:r}){const s=new Array;if(r.integer&&!Number.isSafeInteger(t)&&s.push("It must be an integer."),t>r.max&&s.push(`It must be up to ${r.max}.`),t<r.min&&s.push(`It must be at least ${r.min}.`),s.length>0)throw new g([`"${t}" is not a valid value for ${e}. It failed to following validations:\n`,...s.map(e=>`\t- ${e}`)].join(""));return t}var b,w,v,O,T,P,y,I,R,G,A,D,F,k,C,B=function(e,t,r,s,i){if("m"===s)throw new TypeError("Private method is not writable");if("a"===s&&!i)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!i:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===s?i.call(e,r):i?i.value=r:t.set(e,r),r},L=function(e,t,r,s){if("a"===r&&!s)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!s:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?s:"a"===r?s.call(e):s?s.value:t.get(e)};const U=4294967295;class x{constructor({frameSize:e,frameCacheSize:t=0,preallocateFrameCount:r=10,TypedArrayConstructor:s}){if(b.add(this),w.set(this,void 0),v.set(this,void 0),O.set(this,void 0),T.set(this,void 0),P.set(this,void 0),y.set(this,void 0),I.set(this,void 0),B(this,T,S({value:e,name:"Frame Size",validations:{min:1,integer:!0,max:U}}),"f"),B(this,v,S({value:r,name:"Preallocate Frame Count",validations:{min:1,integer:!0,max:U}}),"f"),B(this,O,S({value:t,name:"Frame Cache Size",validations:{min:0,integer:!1,max:U}}),"f"),!("BYTES_PER_ELEMENT"in s))throw new g("TypedArrayConstructor must be a typed array");B(this,y,0,"f"),B(this,I,0,"f"),B(this,w,s,"f"),B(this,P,new ArrayBuffer(L(this,b,"m",C).call(this)),"f")}empty(){return L(this,b,"m",A).call(this)<L(this,T,"f")}peek(){return L(this,b,"m",R).call(this)}write(e){L(this,b,"m",k).call(this,e.length),L(this,b,"m",R).call(this).subarray(L(this,I,"f"),L(this,I,"f")+e.length).set(e),B(this,I,L(this,I,"f")+e.length,"f")}remainingFrames(){return Math.floor(L(this,b,"m",G).call(this))}drain(){return L(this,b,"m",F).call(this,L(this,b,"m",A).call(this))}read(){return L(this,b,"m",F).call(this,L(this,T,"f"))}get[(w=new WeakMap,v=new WeakMap,O=new WeakMap,T=new WeakMap,P=new WeakMap,y=new WeakMap,I=new WeakMap,b=new WeakSet,R=function(){return new(L(this,w,"f"))(L(this,P,"f"))},G=function(){return L(this,b,"m",A).call(this)/L(this,T,"f")},A=function(){return L(this,I,"f")-L(this,y,"f")},D=function(){return L(this,O,"f")>0&&L(this,b,"m",G).call(this)>=L(this,O,"f")},F=function(e){if(!e)return null;if(L(this,b,"m",A).call(this)<e)return null;let t=L(this,b,"m",R).call(this).subarray(L(this,y,"f"),L(this,y,"f")+e);if(B(this,y,L(this,y,"f")+e,"f"),L(this,y,"f")===L(this,I,"f")&&(B(this,I,0,"f"),B(this,y,0,"f")),L(this,b,"m",D).call(this)){const e=this.drain();e&&(t=t.slice(0),this.write(e))}return t},k=function(e){const t=e*L(this,w,"f").BYTES_PER_ELEMENT;if(L(this,b,"m",R).call(this).length-L(this,I,"f")<=e){const e=L(this,P,"f");B(this,P,new ArrayBuffer(e.byteLength+t+L(this,b,"m",C).call(this)),"f"),L(this,b,"m",R).call(this).set(new(L(this,w,"f"))(e))}return L(this,b,"m",R).call(this).subarray(L(this,y,"f"),L(this,y,"f")+L(this,T,"f"))},C=function(){return L(this,T,"f")*L(this,w,"f").BYTES_PER_ELEMENT*L(this,v,"f")},Symbol.toStringTag)](){return"RingBuffer"}[Symbol.iterator](){return this[Symbol.asyncIterator]()}*[Symbol.asyncIterator](){let e;do{e=this.read(),null!==e&&(yield e)}while(null!==e)}}class N extends x{constructor(e,t={}){super(Object.assign(Object.assign({},t),{frameSize:e,TypedArrayConstructor:Float32Array}))}get[Symbol.toStringTag](){return"RingBufferF32"}}Symbol.toStringTag,Symbol.toStringTag,Symbol.toStringTag,Symbol.toStringTag,Symbol.toStringTag;let q={queue:new Set};function M(){return crypto.getRandomValues(new Uint32Array(4)).join("-")}function V(e,t=[]){return postMessage(e,t)}const z=async e=>{if("queue"in q){switch(e.type){case r.InitializeWorker:{const t=q.queue,r={runtime:new o(await h({wasmFileHref:e.data.wasmFileHref})),encoders:new Map,decoders:new Map};q=r;for(const e of t)await z(e);t.clear(),V({requestId:e.requestId,value:null});break}default:q.queue.add(e)}return}const{runtime:t,encoders:s,decoders:i}=q;switch(e.type){case r.CreateEncoder:{const r=new E(t,e.data.sampleRate,e.data.channels,e.data.application,e.data.outBufferLength,e.data.pcmBufferLength),i=M();s.set(i,{sampleRate:e.data.sampleRate,ringBuffer:new N(e.data.pcmBufferLength/Float32Array.BYTES_PER_ELEMENT),encoder:r}),V({requestId:e.requestId,value:i});break}case r.CreateDecoder:{const r=new l(t,e.data.sampleRate,e.data.channels,e.data.frameSize),s=M();i.set(s,r),V({requestId:e.requestId,value:s});break}case r.DestroyEncoder:{const t=s.get(e.data);if(!t)throw new Error("Failed to get encoder");s.delete(e.data),t.encoder.destroy(),V(t?{requestId:e.requestId,value:null}:{requestId:e.requestId,failures:[`failed to find encoder with id: ${e.data}`]});break}case r.DestroyDecoder:{const t=i.get(e.data.decoderId);i.delete(e.data.decoderId),t&&t.destroy(),V(t?{requestId:e.requestId,value:null}:{requestId:e.requestId,failures:[`failed to find decoder with id: ${e.data.decoderId}`]});break}case r.EncodeFloat:{const t=s.get(e.data.encoderId);if(!t)throw new Error("Failed to get encoder");const r=null===e.data.input;e.data.input&&t.ringBuffer.write(e.data.input.pcm);const i=t.ringBuffer.read()??(r?t.ringBuffer.drain():null);if(null===i){V({requestId:e.requestId,value:{encoded:null}});break}const a=t.encoder.encodeFloat(i,i.length,e.data.maxDataBytes),n={buffer:new ArrayBuffer(a),duration:a/t.sampleRate};new Uint8Array(n.buffer).set(t.encoder.encoded().subarray(0,a)),V({requestId:e.requestId,value:{encoded:n}},[n.buffer]);break}case r.DecodeFloat:{const t=i.get(e.data.decoderId);let r;if(t){const s=t.decodeFloat(new Uint8Array(e.data.encoded),e.data.decodeFec);r=t.decoded().slice(0,s)}else r=null;V(r?{requestId:e.requestId,value:{decoded:r.buffer}}:{requestId:e.requestId,failures:[`no decoder found for decoder id: ${e.data.decoderId}`]},r?[r.buffer]:[]);break}case r.OpusSetRequest:{const t=s.get(e.data.encoderId);V(t?.encoder?{value:f(t.encoder,e.data),requestId:e.requestId}:{requestId:e.requestId,failures:[`no encoder found for: ${e.data.encoderId}`]});break}case r.OpusGetRequest:{const t=s.get(e.data.encoderId);V(t?.encoder?{value:_(t.encoder,e.data),requestId:e.requestId}:{requestId:e.requestId,failures:[`no encoder found for: ${e.data.encoderId}`]});break}}};onmessage=e=>{const t=e.data;z(t).catch(e=>{console.error("failed to process request: %o",{request:t,reason:e})})}})();
|