longpipe 0.0.15 → 0.0.16
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/dist/audio.cjs +33 -0
- package/dist/audio.d.cts +37 -0
- package/dist/audio.d.ts +37 -0
- package/dist/audio.js +1 -0
- package/dist/chunk-JM6V2XLC.js +33 -0
- package/dist/chunk-O4OF2WRO.js +33 -0
- package/dist/index.cjs +42 -42
- package/dist/index.d.cts +3 -14
- package/dist/index.d.ts +3 -14
- package/dist/index.js +2 -34
- package/dist/messages-DjW-szBq.d.cts +14 -0
- package/dist/messages-DjW-szBq.d.ts +14 -0
- package/package.json +7 -2
package/dist/audio.cjs
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use strict';var a={rnnoise:{wasm:"rnnoise.wasm",weights:null,scaleIn:32768,scaleOut:30517578125e-15,needsSimd:false,exports:{malloc:"malloc",create:"rn_create",process:"rn_process"}},dfn:{wasm:"dfn.wasm",weights:"dfn_weights.pack",scaleIn:1,scaleOut:1,needsSimd:true,exports:{malloc:"df_lite_malloc",create:"df_lite_create",process:"df_lite_process",setBeta:"df_lite_set_beta",setGruLeak:"df_lite_set_gru_leak"}},dfnint8:{wasm:"dfn.wasm",weights:"dfn_weights_int8.pack",scaleIn:1,scaleOut:1,needsSimd:true,exports:{malloc:"df_lite_malloc",create:"df_lite_create_i8",process:"df_lite_process",setBeta:"df_lite_set_beta",setGruLeak:"df_lite_set_gru_leak"}}},_={high:"dfn",mid:"dfnint8",low:"rnnoise"},b=["high","mid","low"];function x(t){return b.includes(t)}function p(t){return x(t)?_[t]:t}function m(t,e){return `${t.replace(/\/$/,"")}/${e}`}async function f(t){let e=await fetch(t);if(!e.ok)throw new Error(`audio asset fetch failed: ${e.status} ${t}`);return e.arrayBuffer()}async function g(t,e){let r=a[t],[s,o]=await Promise.all([f(m(e,r.wasm)),r.weights?f(m(e,r.weights)):Promise.resolve(null)]);return {wasmBytes:s,weights:o}}function l(){try{return WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,5,1,96,0,1,123,3,2,1,0,10,22,1,20,0,253,12,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,11]))}catch{return false}}var M=1.3,k=64,S=3,A=`
|
|
2
|
+
self.onmessage = async (e) => {
|
|
3
|
+
const { wasmUrl, iters, rounds, budget } = e.data
|
|
4
|
+
try {
|
|
5
|
+
const res = await fetch(wasmUrl)
|
|
6
|
+
if (!res.ok) throw new Error(res.status + ' ' + wasmUrl)
|
|
7
|
+
const module = await WebAssembly.compile(await res.arrayBuffer())
|
|
8
|
+
const imports = {}
|
|
9
|
+
for (const im of WebAssembly.Module.imports(module)) {
|
|
10
|
+
;(imports[im.module] = imports[im.module] || {})
|
|
11
|
+
if (im.kind === 'function') imports[im.module][im.name] = () => 0
|
|
12
|
+
}
|
|
13
|
+
const ex = new WebAssembly.Instance(module, imports).exports
|
|
14
|
+
const best = (fn) => {
|
|
15
|
+
fn(iters) // warm up
|
|
16
|
+
let b = Infinity
|
|
17
|
+
for (let r = 0; r < rounds; r++) {
|
|
18
|
+
const t = performance.now()
|
|
19
|
+
fn(iters)
|
|
20
|
+
b = Math.min(b, (performance.now() - t) / iters)
|
|
21
|
+
}
|
|
22
|
+
return b
|
|
23
|
+
}
|
|
24
|
+
const dfnMs = best(ex.calibrate_f32)
|
|
25
|
+
const int8Ms = ex.calibrate_i8 ? best(ex.calibrate_i8) : Infinity
|
|
26
|
+
const model = dfnMs <= budget ? 'dfn' : int8Ms <= budget ? 'dfnint8' : 'rnnoise'
|
|
27
|
+
self.postMessage({ model, dfnMs, int8Ms })
|
|
28
|
+
} catch (err) {
|
|
29
|
+
self.postMessage({ error: (err && err.message) || String(err) })
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
`;async function w(t,e){if(!l())return "rnnoise";let r=`${t.replace(/\/$/,"")}/${a.dfn.wasm}`,s=null,o=null;try{s=URL.createObjectURL(new Blob([A],{type:"application/javascript"})),o=new Worker(s);let i=await new Promise((u,n)=>{o.onmessage=d=>u(d.data),o.onerror=d=>n(new Error(d.message||"probe worker error")),o.postMessage({wasmUrl:r,iters:k,rounds:S,budget:M});});if(i.error)throw new Error(i.error);return i.model??"rnnoise"}catch(i){return e?.(`audio tier probe failed (${i.message??String(i)}); using rnnoise`),"rnnoise"}finally{o?.terminate(),s&&URL.revokeObjectURL(s);}}var h='var b=Object.defineProperty;var y=(u,a,i)=>a in u?b(u,a,{enumerable:!0,configurable:!0,writable:!0,value:i}):u[a]=i;var r=(u,a,i)=>y(u,typeof a!="symbol"?a+"":a,i);var f={rnnoise:{wasm:"rnnoise.wasm",weights:null,scaleIn:32768,scaleOut:30517578125e-15,needsSimd:!1,exports:{malloc:"malloc",create:"rn_create",process:"rn_process"}},dfn:{wasm:"dfn.wasm",weights:"dfn_weights.pack",scaleIn:1,scaleOut:1,needsSimd:!0,exports:{malloc:"df_lite_malloc",create:"df_lite_create",process:"df_lite_process",setBeta:"df_lite_set_beta",setGruLeak:"df_lite_set_gru_leak"}},dfnint8:{wasm:"dfn.wasm",weights:"dfn_weights_int8.pack",scaleIn:1,scaleOut:1,needsSimd:!0,exports:{malloc:"df_lite_malloc",create:"df_lite_create_i8",process:"df_lite_process",setBeta:"df_lite_set_beta",setGruLeak:"df_lite_set_gru_leak"}}};var c=4096,m=class extends AudioWorkletProcessor{constructor(i){super();r(this,"cfg");r(this,"mem");r(this,"run");r(this,"state",0);r(this,"inPtr",0);r(this,"outPtr",0);r(this,"setBeta",null);r(this,"setGruLeak",null);r(this,"resample",!1);r(this,"rsPush",null);r(this,"inRs",0);r(this,"outRs",0);r(this,"rsDevInPtr",0);r(this,"rsOutPtr",0);r(this,"enabled");r(this,"model");r(this,"inAcc",new Float32Array(480));r(this,"inFill",0);r(this,"outRing",new Float32Array(4096));r(this,"outRead",0);r(this,"outWrite",0);r(this,"outCount",0);r(this,"primed",!1);r(this,"now");r(this,"times",new Float32Array(256));r(this,"tIdx",0);r(this,"hops",0);r(this,"_f32",null);let o=i.processorOptions;this.model=o.model,this.enabled=o.enabled,this.cfg=f[o.model];try{let t=this.instantiate(o.wasmBytes),e=n=>n?t[n]:void 0;this.mem=t.memory;let s=t[this.cfg.exports.malloc];if(this.run=t[this.cfg.exports.process],this.inPtr=s(480*4),this.outPtr=s(480*4),o.weights?this.state=t[this.cfg.exports.create](this.upload(s,o.weights),o.weights.byteLength):this.state=t[this.cfg.exports.create](),this.setBeta=e(this.cfg.exports.setBeta)??null,this.setGruLeak=e(this.cfg.exports.setGruLeak)??null,this.setBeta&&(o.postFilterBeta??0)>0&&this.setBeta(this.state,o.postFilterBeta),this.setGruLeak&&(o.gruLeak??1)<1&&this.setGruLeak(this.state,o.gruLeak),sampleRate!==48e3){let n=t.df_resampler_create;this.rsPush=t.df_resampler_push??null,n&&this.rsPush?(this.resample=!0,this.inRs=n(sampleRate,48e3),this.outRs=n(48e3,sampleRate),this.rsDevInPtr=s(256*4),this.rsOutPtr=s(c*4)):this.post({type:"error",message:`${o.model} can\'t resample ${sampleRate}\\u219248000 Hz; needs a 48 kHz AudioContext`})}}catch(t){let e=t;this.post({type:"error",message:`denoise init failed: ${e.message??String(t)}${e.stack?`\n`+e.stack:""}`})}this.now=typeof performance<"u"&&performance.now?()=>performance.now():null,this.port.onmessage=t=>{let e=t.data;e.type==="enabled"?this.enabled=e.value:e.type==="config"&&(this.setBeta&&e.postFilterBeta!=null&&this.setBeta(this.state,e.postFilterBeta),this.setGruLeak&&e.gruLeak!=null&&this.setGruLeak(this.state,e.gruLeak))},this.post({type:"ready"})}instantiate(i){var e;let o=new WebAssembly.Module(i),t={};for(let s of WebAssembly.Module.imports(o))t[e=s.module]??(t[e]={}),s.kind==="function"?t[s.module][s.name]=()=>0:s.kind==="memory"?t[s.module][s.name]=new WebAssembly.Memory({initial:256}):s.kind==="table"?t[s.module][s.name]=new WebAssembly.Table({initial:0,element:"anyfunc"}):s.kind==="global"&&(t[s.module][s.name]=new WebAssembly.Global({value:"i32",mutable:!1},0));return new WebAssembly.Instance(o,t).exports}upload(i,o){let t=new Uint8Array(o),e=i(t.length);return new Uint8Array(this.mem.buffer).set(t,e),e}heap(){return(!this._f32||this._f32.buffer!==this.mem.buffer)&&(this._f32=new Float32Array(this.mem.buffer)),this._f32}post(i){this.port.postMessage(i)}pushOut(i){this.outRing[this.outWrite]=i,this.outWrite=(this.outWrite+1)%this.outRing.length,this.outCount++}feed48k(i,o){for(let t=0;t<o;t++)this.inAcc[this.inFill++]=i[t],this.inFill===480&&(this.runHop(),this.inFill=0)}runHop(){let i=this.heap(),o=this.inPtr>>2;for(let e=0;e<480;e++)i[o+e]=this.inAcc[e]*this.cfg.scaleIn;let t=this.now?this.now():0;if(this.run(this.state,this.inPtr,this.outPtr),this.now&&(this.times[this.tIdx=this.tIdx+1&255]=this.now()-t),this.resample&&this.rsPush){let e=this.rsPush(this.outRs,this.outPtr,480,this.rsOutPtr,c);i=this.heap();let s=this.rsOutPtr>>2;for(let n=0;n<e;n++)this.pushOut(i[s+n])}else{let e=this.outPtr>>2;for(let s=0;s<480;s++)this.pushOut(i[e+s]*this.cfg.scaleOut)}++this.hops>=50&&(this.reportStats(),this.hops=0)}reportStats(){let i=Array.from(this.times).filter(e=>e>0).sort((e,s)=>e-s),o=e=>i.length?i[Math.min(i.length-1,Math.floor(e*i.length))]:null,t={model:this.model,p50Ms:this.now?o(.5):null,p95Ms:this.now?o(.95):null,latencyMs:this.outCount/sampleRate*1e3,active:this.enabled&&this.state!==0,sampleRate};this.post({type:"stats",stats:t})}process(i,o){let t=i[0]?.[0],e=o[0]?.[0];if(!e)return!0;let s=e.length;if(!t||!this.enabled||this.state===0)return t&&!this.enabled?e.set(t):e.fill(0),!0;if(this.resample&&this.rsPush){let n=this.heap(),g=this.rsDevInPtr>>2;for(let h=0;h<s;h++)n[g+h]=t[h];let d=this.rsPush(this.inRs,this.rsDevInPtr,s,this.rsOutPtr,c);n=this.heap(),this.feed48k(n.subarray(this.rsOutPtr>>2,(this.rsOutPtr>>2)+d),d)}else this.feed48k(t,s);if(!this.primed)if(this.outCount>=480)this.primed=!0;else return e.fill(0),!0;if(this.outCount>=s)for(let n=0;n<s;n++)e[n]=this.outRing[this.outRead],this.outRead=(this.outRead+1)%this.outRing.length,this.outCount--;else e.fill(0);return!0}};registerProcessor("denoise",m);\n';var y="https://cdn.longpipe.dev/models/v/0.0.5/";var E=.03,v=.995,c=class{constructor(e,r={}){this.opts=r;this.ctx=new AudioContext({sampleRate:48e3}),this.source=this.ctx.createMediaStreamSource(new MediaStream([e])),this.dest=this.ctx.createMediaStreamDestination(),this.outputTrack=this.dest.stream.getAudioTracks()[0],this.resume().catch(()=>{}),this.source.connect(this.dest),this.ready=this.init().then(()=>this.node!==null,s=>(this.opts.onError?.(`audio denoise init failed: ${s.message??String(s)}`),false));}opts;outputTrack;ready;ctx;source;dest;node=null;model=null;latestStats=null;destroyed=false;resolveExplicit(){let e=p(this.opts.model);return a[e].needsSimd&&!l()?(this.opts.onError?.(`${e} needs wasm SIMD; falling back to rnnoise`),"rnnoise"):e}async init(){let e=this.opts.weightsBaseUrl??y,r=(this.opts.model??"auto")==="auto"?await w(e,this.opts.onError):this.resolveExplicit();this.model=r;let s=await g(r,e);if(await this.ctx.audioWorklet.addModule(D()),this.destroyed)return;let o={model:r,wasmBytes:s.wasmBytes,weights:s.weights,enabled:this.opts.enabled??true,postFilterBeta:this.opts.postFilterBeta??E,gruLeak:this.opts.gruLeak??v},i=new AudioWorkletNode(this.ctx,"denoise",{numberOfInputs:1,numberOfOutputs:1,outputChannelCount:[1],processorOptions:o});i.port.onmessage=u=>{let n=u.data;n.type==="stats"?this.latestStats=n.stats:n.type==="error"?this.opts.onError?.(n.message):n.type==="ready"&&this.opts.onReady?.();},this.node=i,this.source.disconnect(this.dest),this.source.connect(i).connect(this.dest);}setEnabled(e){this.node?.port.postMessage({type:"enabled",value:e});}setConfig(e){this.node?.port.postMessage({type:"config",...e});}getStats(){return this.latestStats}async resume(){this.destroyed||this.ctx.state!=="suspended"||await this.ctx.resume();}destroy(){this.destroyed=true;try{this.node?.disconnect();}catch{}try{this.source.disconnect();}catch{}this.ctx.close();}};function D(){return URL.createObjectURL(new Blob([h],{type:"application/javascript"}))}
|
|
33
|
+
exports.AudioDenoiser=c;
|
package/dist/audio.d.cts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { D as DenoiseModelOption, A as AudioStats } from './messages-DjW-szBq.cjs';
|
|
2
|
+
export { a as DenoiseModel, b as DenoiseTier } from './messages-DjW-szBq.cjs';
|
|
3
|
+
|
|
4
|
+
interface AudioDenoiserOptions {
|
|
5
|
+
model?: DenoiseModelOption;
|
|
6
|
+
weightsBaseUrl?: string;
|
|
7
|
+
postFilterBeta?: number;
|
|
8
|
+
gruLeak?: number;
|
|
9
|
+
enabled?: boolean;
|
|
10
|
+
onReady?: () => void;
|
|
11
|
+
onError?: (message: string) => void;
|
|
12
|
+
}
|
|
13
|
+
declare class AudioDenoiser {
|
|
14
|
+
private opts;
|
|
15
|
+
readonly outputTrack: MediaStreamTrack;
|
|
16
|
+
readonly ready: Promise<boolean>;
|
|
17
|
+
private ctx;
|
|
18
|
+
private source;
|
|
19
|
+
private dest;
|
|
20
|
+
private node;
|
|
21
|
+
private model;
|
|
22
|
+
private latestStats;
|
|
23
|
+
private destroyed;
|
|
24
|
+
constructor(inputTrack: MediaStreamTrack, opts?: AudioDenoiserOptions);
|
|
25
|
+
private resolveExplicit;
|
|
26
|
+
private init;
|
|
27
|
+
setEnabled(on: boolean): void;
|
|
28
|
+
setConfig(cfg: {
|
|
29
|
+
postFilterBeta?: number;
|
|
30
|
+
gruLeak?: number;
|
|
31
|
+
}): void;
|
|
32
|
+
getStats(): AudioStats | null;
|
|
33
|
+
resume(): Promise<void>;
|
|
34
|
+
destroy(): void;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export { AudioDenoiser, type AudioDenoiserOptions, AudioStats, DenoiseModelOption };
|
package/dist/audio.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { D as DenoiseModelOption, A as AudioStats } from './messages-DjW-szBq.js';
|
|
2
|
+
export { a as DenoiseModel, b as DenoiseTier } from './messages-DjW-szBq.js';
|
|
3
|
+
|
|
4
|
+
interface AudioDenoiserOptions {
|
|
5
|
+
model?: DenoiseModelOption;
|
|
6
|
+
weightsBaseUrl?: string;
|
|
7
|
+
postFilterBeta?: number;
|
|
8
|
+
gruLeak?: number;
|
|
9
|
+
enabled?: boolean;
|
|
10
|
+
onReady?: () => void;
|
|
11
|
+
onError?: (message: string) => void;
|
|
12
|
+
}
|
|
13
|
+
declare class AudioDenoiser {
|
|
14
|
+
private opts;
|
|
15
|
+
readonly outputTrack: MediaStreamTrack;
|
|
16
|
+
readonly ready: Promise<boolean>;
|
|
17
|
+
private ctx;
|
|
18
|
+
private source;
|
|
19
|
+
private dest;
|
|
20
|
+
private node;
|
|
21
|
+
private model;
|
|
22
|
+
private latestStats;
|
|
23
|
+
private destroyed;
|
|
24
|
+
constructor(inputTrack: MediaStreamTrack, opts?: AudioDenoiserOptions);
|
|
25
|
+
private resolveExplicit;
|
|
26
|
+
private init;
|
|
27
|
+
setEnabled(on: boolean): void;
|
|
28
|
+
setConfig(cfg: {
|
|
29
|
+
postFilterBeta?: number;
|
|
30
|
+
gruLeak?: number;
|
|
31
|
+
}): void;
|
|
32
|
+
getStats(): AudioStats | null;
|
|
33
|
+
resume(): Promise<void>;
|
|
34
|
+
destroy(): void;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export { AudioDenoiser, type AudioDenoiserOptions, AudioStats, DenoiseModelOption };
|
package/dist/audio.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export{b as AudioDenoiser}from'./chunk-O4OF2WRO.js';
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
var a={rnnoise:{wasm:"rnnoise.wasm",weights:null,scaleIn:32768,scaleOut:30517578125e-15,needsSimd:false,exports:{malloc:"malloc",create:"rn_create",process:"rn_process"}},dfn:{wasm:"dfn.wasm",weights:"dfn_weights.pack",scaleIn:1,scaleOut:1,needsSimd:true,exports:{malloc:"df_lite_malloc",create:"df_lite_create",process:"df_lite_process",setBeta:"df_lite_set_beta",setGruLeak:"df_lite_set_gru_leak"}},dfnint8:{wasm:"dfn.wasm",weights:"dfn_weights_int8.pack",scaleIn:1,scaleOut:1,needsSimd:true,exports:{malloc:"df_lite_malloc",create:"df_lite_create_i8",process:"df_lite_process",setBeta:"df_lite_set_beta",setGruLeak:"df_lite_set_gru_leak"}}},_={high:"dfn",mid:"dfnint8",low:"rnnoise"},b=["high","mid","low"];function M(t){return b.includes(t)}function c(t){return M(t)?_[t]:t}function m(t,e){return `${t.replace(/\/$/,"")}/${e}`}async function p(t){let e=await fetch(t);if(!e.ok)throw new Error(`audio asset fetch failed: ${e.status} ${t}`);return e.arrayBuffer()}async function f(t,e){let r=a[t],[s,o]=await Promise.all([p(m(e,r.wasm)),r.weights?p(m(e,r.weights)):Promise.resolve(null)]);return {wasmBytes:s,weights:o}}function l(){try{return WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,5,1,96,0,1,123,3,2,1,0,10,22,1,20,0,253,12,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,11]))}catch{return false}}var k=1.3,x=64,S=3,R=`
|
|
2
|
+
self.onmessage = async (e) => {
|
|
3
|
+
const { wasmUrl, iters, rounds, budget } = e.data
|
|
4
|
+
try {
|
|
5
|
+
const res = await fetch(wasmUrl)
|
|
6
|
+
if (!res.ok) throw new Error(res.status + ' ' + wasmUrl)
|
|
7
|
+
const module = await WebAssembly.compile(await res.arrayBuffer())
|
|
8
|
+
const imports = {}
|
|
9
|
+
for (const im of WebAssembly.Module.imports(module)) {
|
|
10
|
+
;(imports[im.module] = imports[im.module] || {})
|
|
11
|
+
if (im.kind === 'function') imports[im.module][im.name] = () => 0
|
|
12
|
+
}
|
|
13
|
+
const ex = new WebAssembly.Instance(module, imports).exports
|
|
14
|
+
const best = (fn) => {
|
|
15
|
+
fn(iters) // warm up
|
|
16
|
+
let b = Infinity
|
|
17
|
+
for (let r = 0; r < rounds; r++) {
|
|
18
|
+
const t = performance.now()
|
|
19
|
+
fn(iters)
|
|
20
|
+
b = Math.min(b, (performance.now() - t) / iters)
|
|
21
|
+
}
|
|
22
|
+
return b
|
|
23
|
+
}
|
|
24
|
+
const dfnMs = best(ex.calibrate_f32)
|
|
25
|
+
const int8Ms = ex.calibrate_i8 ? best(ex.calibrate_i8) : Infinity
|
|
26
|
+
const model = dfnMs <= budget ? 'dfn' : int8Ms <= budget ? 'dfnint8' : 'rnnoise'
|
|
27
|
+
self.postMessage({ model, dfnMs, int8Ms })
|
|
28
|
+
} catch (err) {
|
|
29
|
+
self.postMessage({ error: (err && err.message) || String(err) })
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
`;async function g(t,e){if(!l())return "rnnoise";let r=`${t.replace(/\/$/,"")}/${a.dfn.wasm}`,s=null,o=null;try{s=URL.createObjectURL(new Blob([R],{type:"application/javascript"})),o=new Worker(s);let i=await new Promise((h,n)=>{o.onmessage=u=>h(u.data),o.onerror=u=>n(new Error(u.message||"probe worker error")),o.postMessage({wasmUrl:r,iters:x,rounds:S,budget:k});});if(i.error)throw new Error(i.error);return i.model??"rnnoise"}catch(i){return e?.(`audio tier probe failed (${i.message??String(i)}); using rnnoise`),"rnnoise"}finally{o?.terminate(),s&&URL.revokeObjectURL(s);}}var d='var b=Object.defineProperty;var y=(u,a,i)=>a in u?b(u,a,{enumerable:!0,configurable:!0,writable:!0,value:i}):u[a]=i;var r=(u,a,i)=>y(u,typeof a!="symbol"?a+"":a,i);var f={rnnoise:{wasm:"rnnoise.wasm",weights:null,scaleIn:32768,scaleOut:30517578125e-15,needsSimd:!1,exports:{malloc:"malloc",create:"rn_create",process:"rn_process"}},dfn:{wasm:"dfn.wasm",weights:"dfn_weights.pack",scaleIn:1,scaleOut:1,needsSimd:!0,exports:{malloc:"df_lite_malloc",create:"df_lite_create",process:"df_lite_process",setBeta:"df_lite_set_beta",setGruLeak:"df_lite_set_gru_leak"}},dfnint8:{wasm:"dfn.wasm",weights:"dfn_weights_int8.pack",scaleIn:1,scaleOut:1,needsSimd:!0,exports:{malloc:"df_lite_malloc",create:"df_lite_create_i8",process:"df_lite_process",setBeta:"df_lite_set_beta",setGruLeak:"df_lite_set_gru_leak"}}};var c=4096,m=class extends AudioWorkletProcessor{constructor(i){super();r(this,"cfg");r(this,"mem");r(this,"run");r(this,"state",0);r(this,"inPtr",0);r(this,"outPtr",0);r(this,"setBeta",null);r(this,"setGruLeak",null);r(this,"resample",!1);r(this,"rsPush",null);r(this,"inRs",0);r(this,"outRs",0);r(this,"rsDevInPtr",0);r(this,"rsOutPtr",0);r(this,"enabled");r(this,"model");r(this,"inAcc",new Float32Array(480));r(this,"inFill",0);r(this,"outRing",new Float32Array(4096));r(this,"outRead",0);r(this,"outWrite",0);r(this,"outCount",0);r(this,"primed",!1);r(this,"now");r(this,"times",new Float32Array(256));r(this,"tIdx",0);r(this,"hops",0);r(this,"_f32",null);let o=i.processorOptions;this.model=o.model,this.enabled=o.enabled,this.cfg=f[o.model];try{let t=this.instantiate(o.wasmBytes),e=n=>n?t[n]:void 0;this.mem=t.memory;let s=t[this.cfg.exports.malloc];if(this.run=t[this.cfg.exports.process],this.inPtr=s(480*4),this.outPtr=s(480*4),o.weights?this.state=t[this.cfg.exports.create](this.upload(s,o.weights),o.weights.byteLength):this.state=t[this.cfg.exports.create](),this.setBeta=e(this.cfg.exports.setBeta)??null,this.setGruLeak=e(this.cfg.exports.setGruLeak)??null,this.setBeta&&(o.postFilterBeta??0)>0&&this.setBeta(this.state,o.postFilterBeta),this.setGruLeak&&(o.gruLeak??1)<1&&this.setGruLeak(this.state,o.gruLeak),sampleRate!==48e3){let n=t.df_resampler_create;this.rsPush=t.df_resampler_push??null,n&&this.rsPush?(this.resample=!0,this.inRs=n(sampleRate,48e3),this.outRs=n(48e3,sampleRate),this.rsDevInPtr=s(256*4),this.rsOutPtr=s(c*4)):this.post({type:"error",message:`${o.model} can\'t resample ${sampleRate}\\u219248000 Hz; needs a 48 kHz AudioContext`})}}catch(t){let e=t;this.post({type:"error",message:`denoise init failed: ${e.message??String(t)}${e.stack?`\n`+e.stack:""}`})}this.now=typeof performance<"u"&&performance.now?()=>performance.now():null,this.port.onmessage=t=>{let e=t.data;e.type==="enabled"?this.enabled=e.value:e.type==="config"&&(this.setBeta&&e.postFilterBeta!=null&&this.setBeta(this.state,e.postFilterBeta),this.setGruLeak&&e.gruLeak!=null&&this.setGruLeak(this.state,e.gruLeak))},this.post({type:"ready"})}instantiate(i){var e;let o=new WebAssembly.Module(i),t={};for(let s of WebAssembly.Module.imports(o))t[e=s.module]??(t[e]={}),s.kind==="function"?t[s.module][s.name]=()=>0:s.kind==="memory"?t[s.module][s.name]=new WebAssembly.Memory({initial:256}):s.kind==="table"?t[s.module][s.name]=new WebAssembly.Table({initial:0,element:"anyfunc"}):s.kind==="global"&&(t[s.module][s.name]=new WebAssembly.Global({value:"i32",mutable:!1},0));return new WebAssembly.Instance(o,t).exports}upload(i,o){let t=new Uint8Array(o),e=i(t.length);return new Uint8Array(this.mem.buffer).set(t,e),e}heap(){return(!this._f32||this._f32.buffer!==this.mem.buffer)&&(this._f32=new Float32Array(this.mem.buffer)),this._f32}post(i){this.port.postMessage(i)}pushOut(i){this.outRing[this.outWrite]=i,this.outWrite=(this.outWrite+1)%this.outRing.length,this.outCount++}feed48k(i,o){for(let t=0;t<o;t++)this.inAcc[this.inFill++]=i[t],this.inFill===480&&(this.runHop(),this.inFill=0)}runHop(){let i=this.heap(),o=this.inPtr>>2;for(let e=0;e<480;e++)i[o+e]=this.inAcc[e]*this.cfg.scaleIn;let t=this.now?this.now():0;if(this.run(this.state,this.inPtr,this.outPtr),this.now&&(this.times[this.tIdx=this.tIdx+1&255]=this.now()-t),this.resample&&this.rsPush){let e=this.rsPush(this.outRs,this.outPtr,480,this.rsOutPtr,c);i=this.heap();let s=this.rsOutPtr>>2;for(let n=0;n<e;n++)this.pushOut(i[s+n])}else{let e=this.outPtr>>2;for(let s=0;s<480;s++)this.pushOut(i[e+s]*this.cfg.scaleOut)}++this.hops>=50&&(this.reportStats(),this.hops=0)}reportStats(){let i=Array.from(this.times).filter(e=>e>0).sort((e,s)=>e-s),o=e=>i.length?i[Math.min(i.length-1,Math.floor(e*i.length))]:null,t={model:this.model,p50Ms:this.now?o(.5):null,p95Ms:this.now?o(.95):null,latencyMs:this.outCount/sampleRate*1e3,active:this.enabled&&this.state!==0,sampleRate};this.post({type:"stats",stats:t})}process(i,o){let t=i[0]?.[0],e=o[0]?.[0];if(!e)return!0;let s=e.length;if(!t||!this.enabled||this.state===0)return t&&!this.enabled?e.set(t):e.fill(0),!0;if(this.resample&&this.rsPush){let n=this.heap(),g=this.rsDevInPtr>>2;for(let h=0;h<s;h++)n[g+h]=t[h];let d=this.rsPush(this.inRs,this.rsDevInPtr,s,this.rsOutPtr,c);n=this.heap(),this.feed48k(n.subarray(this.rsOutPtr>>2,(this.rsOutPtr>>2)+d),d)}else this.feed48k(t,s);if(!this.primed)if(this.outCount>=480)this.primed=!0;else return e.fill(0),!0;if(this.outCount>=s)for(let n=0;n<s;n++)e[n]=this.outRing[this.outRead],this.outRead=(this.outRead+1)%this.outRing.length,this.outCount--;else e.fill(0);return!0}};registerProcessor("denoise",m);\n';var w="https://cdn.longpipe.dev/models/v/0.0.5/";var E=.03,v=.995,y=class{constructor(e,r={}){this.opts=r;this.ctx=new AudioContext({sampleRate:48e3}),this.source=this.ctx.createMediaStreamSource(new MediaStream([e])),this.dest=this.ctx.createMediaStreamDestination(),this.outputTrack=this.dest.stream.getAudioTracks()[0],this.source.connect(this.dest),this.ready=this.init().then(()=>this.node!==null,s=>(this.opts.onError?.(`audio denoise init failed: ${s.message??String(s)}`),false));}opts;outputTrack;ready;ctx;source;dest;node=null;model=null;latestStats=null;destroyed=false;resolveExplicit(){let e=c(this.opts.model);return a[e].needsSimd&&!l()?(this.opts.onError?.(`${e} needs wasm SIMD; falling back to rnnoise`),"rnnoise"):e}async init(){let e=this.opts.weightsBaseUrl??w,r=(this.opts.model??"auto")==="auto"?await g(e,this.opts.onError):this.resolveExplicit();this.model=r;let s=await f(r,e);if(await this.ctx.audioWorklet.addModule(L()),this.destroyed)return;let o={model:r,wasmBytes:s.wasmBytes,weights:s.weights,enabled:this.opts.enabled??true,postFilterBeta:this.opts.postFilterBeta??E,gruLeak:this.opts.gruLeak??v},i=new AudioWorkletNode(this.ctx,"denoise",{numberOfInputs:1,numberOfOutputs:1,outputChannelCount:[1],processorOptions:o});i.port.onmessage=h=>{let n=h.data;n.type==="stats"?this.latestStats=n.stats:n.type==="error"?this.opts.onError?.(n.message):n.type==="ready"&&this.opts.onReady?.();},this.node=i,this.source.disconnect(this.dest),this.source.connect(i).connect(this.dest);}setEnabled(e){this.node?.port.postMessage({type:"enabled",value:e});}setConfig(e){this.node?.port.postMessage({type:"config",...e});}getStats(){return this.latestStats}destroy(){this.destroyed=true;try{this.node?.disconnect();}catch{}try{this.source.disconnect();}catch{}this.ctx.close();}};function L(){return URL.createObjectURL(new Blob([d],{type:"application/javascript"}))}
|
|
33
|
+
export{w as a,y as b};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
var a={rnnoise:{wasm:"rnnoise.wasm",weights:null,scaleIn:32768,scaleOut:30517578125e-15,needsSimd:false,exports:{malloc:"malloc",create:"rn_create",process:"rn_process"}},dfn:{wasm:"dfn.wasm",weights:"dfn_weights.pack",scaleIn:1,scaleOut:1,needsSimd:true,exports:{malloc:"df_lite_malloc",create:"df_lite_create",process:"df_lite_process",setBeta:"df_lite_set_beta",setGruLeak:"df_lite_set_gru_leak"}},dfnint8:{wasm:"dfn.wasm",weights:"dfn_weights_int8.pack",scaleIn:1,scaleOut:1,needsSimd:true,exports:{malloc:"df_lite_malloc",create:"df_lite_create_i8",process:"df_lite_process",setBeta:"df_lite_set_beta",setGruLeak:"df_lite_set_gru_leak"}}},_={high:"dfn",mid:"dfnint8",low:"rnnoise"},b=["high","mid","low"];function M(t){return b.includes(t)}function c(t){return M(t)?_[t]:t}function m(t,e){return `${t.replace(/\/$/,"")}/${e}`}async function p(t){let e=await fetch(t);if(!e.ok)throw new Error(`audio asset fetch failed: ${e.status} ${t}`);return e.arrayBuffer()}async function f(t,e){let r=a[t],[s,o]=await Promise.all([p(m(e,r.wasm)),r.weights?p(m(e,r.weights)):Promise.resolve(null)]);return {wasmBytes:s,weights:o}}function l(){try{return WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,5,1,96,0,1,123,3,2,1,0,10,22,1,20,0,253,12,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,11]))}catch{return false}}var k=1.3,x=64,S=3,R=`
|
|
2
|
+
self.onmessage = async (e) => {
|
|
3
|
+
const { wasmUrl, iters, rounds, budget } = e.data
|
|
4
|
+
try {
|
|
5
|
+
const res = await fetch(wasmUrl)
|
|
6
|
+
if (!res.ok) throw new Error(res.status + ' ' + wasmUrl)
|
|
7
|
+
const module = await WebAssembly.compile(await res.arrayBuffer())
|
|
8
|
+
const imports = {}
|
|
9
|
+
for (const im of WebAssembly.Module.imports(module)) {
|
|
10
|
+
;(imports[im.module] = imports[im.module] || {})
|
|
11
|
+
if (im.kind === 'function') imports[im.module][im.name] = () => 0
|
|
12
|
+
}
|
|
13
|
+
const ex = new WebAssembly.Instance(module, imports).exports
|
|
14
|
+
const best = (fn) => {
|
|
15
|
+
fn(iters) // warm up
|
|
16
|
+
let b = Infinity
|
|
17
|
+
for (let r = 0; r < rounds; r++) {
|
|
18
|
+
const t = performance.now()
|
|
19
|
+
fn(iters)
|
|
20
|
+
b = Math.min(b, (performance.now() - t) / iters)
|
|
21
|
+
}
|
|
22
|
+
return b
|
|
23
|
+
}
|
|
24
|
+
const dfnMs = best(ex.calibrate_f32)
|
|
25
|
+
const int8Ms = ex.calibrate_i8 ? best(ex.calibrate_i8) : Infinity
|
|
26
|
+
const model = dfnMs <= budget ? 'dfn' : int8Ms <= budget ? 'dfnint8' : 'rnnoise'
|
|
27
|
+
self.postMessage({ model, dfnMs, int8Ms })
|
|
28
|
+
} catch (err) {
|
|
29
|
+
self.postMessage({ error: (err && err.message) || String(err) })
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
`;async function g(t,e){if(!l())return "rnnoise";let r=`${t.replace(/\/$/,"")}/${a.dfn.wasm}`,s=null,o=null;try{s=URL.createObjectURL(new Blob([R],{type:"application/javascript"})),o=new Worker(s);let i=await new Promise((h,n)=>{o.onmessage=u=>h(u.data),o.onerror=u=>n(new Error(u.message||"probe worker error")),o.postMessage({wasmUrl:r,iters:x,rounds:S,budget:k});});if(i.error)throw new Error(i.error);return i.model??"rnnoise"}catch(i){return e?.(`audio tier probe failed (${i.message??String(i)}); using rnnoise`),"rnnoise"}finally{o?.terminate(),s&&URL.revokeObjectURL(s);}}var d='var b=Object.defineProperty;var y=(u,a,i)=>a in u?b(u,a,{enumerable:!0,configurable:!0,writable:!0,value:i}):u[a]=i;var r=(u,a,i)=>y(u,typeof a!="symbol"?a+"":a,i);var f={rnnoise:{wasm:"rnnoise.wasm",weights:null,scaleIn:32768,scaleOut:30517578125e-15,needsSimd:!1,exports:{malloc:"malloc",create:"rn_create",process:"rn_process"}},dfn:{wasm:"dfn.wasm",weights:"dfn_weights.pack",scaleIn:1,scaleOut:1,needsSimd:!0,exports:{malloc:"df_lite_malloc",create:"df_lite_create",process:"df_lite_process",setBeta:"df_lite_set_beta",setGruLeak:"df_lite_set_gru_leak"}},dfnint8:{wasm:"dfn.wasm",weights:"dfn_weights_int8.pack",scaleIn:1,scaleOut:1,needsSimd:!0,exports:{malloc:"df_lite_malloc",create:"df_lite_create_i8",process:"df_lite_process",setBeta:"df_lite_set_beta",setGruLeak:"df_lite_set_gru_leak"}}};var c=4096,m=class extends AudioWorkletProcessor{constructor(i){super();r(this,"cfg");r(this,"mem");r(this,"run");r(this,"state",0);r(this,"inPtr",0);r(this,"outPtr",0);r(this,"setBeta",null);r(this,"setGruLeak",null);r(this,"resample",!1);r(this,"rsPush",null);r(this,"inRs",0);r(this,"outRs",0);r(this,"rsDevInPtr",0);r(this,"rsOutPtr",0);r(this,"enabled");r(this,"model");r(this,"inAcc",new Float32Array(480));r(this,"inFill",0);r(this,"outRing",new Float32Array(4096));r(this,"outRead",0);r(this,"outWrite",0);r(this,"outCount",0);r(this,"primed",!1);r(this,"now");r(this,"times",new Float32Array(256));r(this,"tIdx",0);r(this,"hops",0);r(this,"_f32",null);let o=i.processorOptions;this.model=o.model,this.enabled=o.enabled,this.cfg=f[o.model];try{let t=this.instantiate(o.wasmBytes),e=n=>n?t[n]:void 0;this.mem=t.memory;let s=t[this.cfg.exports.malloc];if(this.run=t[this.cfg.exports.process],this.inPtr=s(480*4),this.outPtr=s(480*4),o.weights?this.state=t[this.cfg.exports.create](this.upload(s,o.weights),o.weights.byteLength):this.state=t[this.cfg.exports.create](),this.setBeta=e(this.cfg.exports.setBeta)??null,this.setGruLeak=e(this.cfg.exports.setGruLeak)??null,this.setBeta&&(o.postFilterBeta??0)>0&&this.setBeta(this.state,o.postFilterBeta),this.setGruLeak&&(o.gruLeak??1)<1&&this.setGruLeak(this.state,o.gruLeak),sampleRate!==48e3){let n=t.df_resampler_create;this.rsPush=t.df_resampler_push??null,n&&this.rsPush?(this.resample=!0,this.inRs=n(sampleRate,48e3),this.outRs=n(48e3,sampleRate),this.rsDevInPtr=s(256*4),this.rsOutPtr=s(c*4)):this.post({type:"error",message:`${o.model} can\'t resample ${sampleRate}\\u219248000 Hz; needs a 48 kHz AudioContext`})}}catch(t){let e=t;this.post({type:"error",message:`denoise init failed: ${e.message??String(t)}${e.stack?`\n`+e.stack:""}`})}this.now=typeof performance<"u"&&performance.now?()=>performance.now():null,this.port.onmessage=t=>{let e=t.data;e.type==="enabled"?this.enabled=e.value:e.type==="config"&&(this.setBeta&&e.postFilterBeta!=null&&this.setBeta(this.state,e.postFilterBeta),this.setGruLeak&&e.gruLeak!=null&&this.setGruLeak(this.state,e.gruLeak))},this.post({type:"ready"})}instantiate(i){var e;let o=new WebAssembly.Module(i),t={};for(let s of WebAssembly.Module.imports(o))t[e=s.module]??(t[e]={}),s.kind==="function"?t[s.module][s.name]=()=>0:s.kind==="memory"?t[s.module][s.name]=new WebAssembly.Memory({initial:256}):s.kind==="table"?t[s.module][s.name]=new WebAssembly.Table({initial:0,element:"anyfunc"}):s.kind==="global"&&(t[s.module][s.name]=new WebAssembly.Global({value:"i32",mutable:!1},0));return new WebAssembly.Instance(o,t).exports}upload(i,o){let t=new Uint8Array(o),e=i(t.length);return new Uint8Array(this.mem.buffer).set(t,e),e}heap(){return(!this._f32||this._f32.buffer!==this.mem.buffer)&&(this._f32=new Float32Array(this.mem.buffer)),this._f32}post(i){this.port.postMessage(i)}pushOut(i){this.outRing[this.outWrite]=i,this.outWrite=(this.outWrite+1)%this.outRing.length,this.outCount++}feed48k(i,o){for(let t=0;t<o;t++)this.inAcc[this.inFill++]=i[t],this.inFill===480&&(this.runHop(),this.inFill=0)}runHop(){let i=this.heap(),o=this.inPtr>>2;for(let e=0;e<480;e++)i[o+e]=this.inAcc[e]*this.cfg.scaleIn;let t=this.now?this.now():0;if(this.run(this.state,this.inPtr,this.outPtr),this.now&&(this.times[this.tIdx=this.tIdx+1&255]=this.now()-t),this.resample&&this.rsPush){let e=this.rsPush(this.outRs,this.outPtr,480,this.rsOutPtr,c);i=this.heap();let s=this.rsOutPtr>>2;for(let n=0;n<e;n++)this.pushOut(i[s+n])}else{let e=this.outPtr>>2;for(let s=0;s<480;s++)this.pushOut(i[e+s]*this.cfg.scaleOut)}++this.hops>=50&&(this.reportStats(),this.hops=0)}reportStats(){let i=Array.from(this.times).filter(e=>e>0).sort((e,s)=>e-s),o=e=>i.length?i[Math.min(i.length-1,Math.floor(e*i.length))]:null,t={model:this.model,p50Ms:this.now?o(.5):null,p95Ms:this.now?o(.95):null,latencyMs:this.outCount/sampleRate*1e3,active:this.enabled&&this.state!==0,sampleRate};this.post({type:"stats",stats:t})}process(i,o){let t=i[0]?.[0],e=o[0]?.[0];if(!e)return!0;let s=e.length;if(!t||!this.enabled||this.state===0)return t&&!this.enabled?e.set(t):e.fill(0),!0;if(this.resample&&this.rsPush){let n=this.heap(),g=this.rsDevInPtr>>2;for(let h=0;h<s;h++)n[g+h]=t[h];let d=this.rsPush(this.inRs,this.rsDevInPtr,s,this.rsOutPtr,c);n=this.heap(),this.feed48k(n.subarray(this.rsOutPtr>>2,(this.rsOutPtr>>2)+d),d)}else this.feed48k(t,s);if(!this.primed)if(this.outCount>=480)this.primed=!0;else return e.fill(0),!0;if(this.outCount>=s)for(let n=0;n<s;n++)e[n]=this.outRing[this.outRead],this.outRead=(this.outRead+1)%this.outRing.length,this.outCount--;else e.fill(0);return!0}};registerProcessor("denoise",m);\n';var w="https://cdn.longpipe.dev/models/v/0.0.5/";var E=.03,v=.995,y=class{constructor(e,r={}){this.opts=r;this.ctx=new AudioContext({sampleRate:48e3}),this.source=this.ctx.createMediaStreamSource(new MediaStream([e])),this.dest=this.ctx.createMediaStreamDestination(),this.outputTrack=this.dest.stream.getAudioTracks()[0],this.resume().catch(()=>{}),this.source.connect(this.dest),this.ready=this.init().then(()=>this.node!==null,s=>(this.opts.onError?.(`audio denoise init failed: ${s.message??String(s)}`),false));}opts;outputTrack;ready;ctx;source;dest;node=null;model=null;latestStats=null;destroyed=false;resolveExplicit(){let e=c(this.opts.model);return a[e].needsSimd&&!l()?(this.opts.onError?.(`${e} needs wasm SIMD; falling back to rnnoise`),"rnnoise"):e}async init(){let e=this.opts.weightsBaseUrl??w,r=(this.opts.model??"auto")==="auto"?await g(e,this.opts.onError):this.resolveExplicit();this.model=r;let s=await f(r,e);if(await this.ctx.audioWorklet.addModule(L()),this.destroyed)return;let o={model:r,wasmBytes:s.wasmBytes,weights:s.weights,enabled:this.opts.enabled??true,postFilterBeta:this.opts.postFilterBeta??E,gruLeak:this.opts.gruLeak??v},i=new AudioWorkletNode(this.ctx,"denoise",{numberOfInputs:1,numberOfOutputs:1,outputChannelCount:[1],processorOptions:o});i.port.onmessage=h=>{let n=h.data;n.type==="stats"?this.latestStats=n.stats:n.type==="error"?this.opts.onError?.(n.message):n.type==="ready"&&this.opts.onReady?.();},this.node=i,this.source.disconnect(this.dest),this.source.connect(i).connect(this.dest);}setEnabled(e){this.node?.port.postMessage({type:"enabled",value:e});}setConfig(e){this.node?.port.postMessage({type:"config",...e});}getStats(){return this.latestStats}async resume(){this.destroyed||this.ctx.state!=="suspended"||await this.ctx.resume();}destroy(){this.destroyed=true;try{this.node?.disconnect();}catch{}try{this.source.disconnect();}catch{}this.ctx.close();}};function L(){return URL.createObjectURL(new Blob([d],{type:"application/javascript"}))}
|
|
33
|
+
export{w as a,y as b};
|