Spaces:
Running
Running
Download assets/buffer.worker-iRGVlhZN.js from ysdede/keet-streaming: direct link, hf CLI and curl.
- Browser
- Download file 6.47 kB
-
https://huggingface.co/spaces/ysdede/keet-streaming/resolve/main/assets/buffer.worker-iRGVlhZN.js
- Command line
-
hf download hf://spaces/ysdede/keet-streaming/assets/buffer.worker-iRGVlhZN.js
-
curl -L -o buffer.worker-iRGVlhZN.js https://huggingface.co/spaces/ysdede/keet-streaming/resolve/main/assets/buffer.worker-iRGVlhZN.js
6.47 kB
| class E{hopSamples;entryDimension;maxEntries;buffer;globalWriteIndex=0;constructor(e,n){this.hopSamples=e.hopSamples,this.entryDimension=e.entryDimension,this.maxEntries=Math.ceil(n*e.maxDurationSec/e.hopSamples),this.buffer=new Float32Array(this.maxEntries*this.entryDimension)}write(e){const n=this.globalWriteIndex%this.maxEntries*this.entryDimension;if(this.entryDimension===1){this.buffer[n]=e[0]??0,this.globalWriteIndex++;return}const s=e instanceof Float32Array?e:Float32Array.from(e);this.buffer.set(s.subarray(0,this.entryDimension),n),this.globalWriteIndex++}writeBatch(e,n){const s=n??Math.floor(e.length/this.entryDimension);for(let i=0;i<s;i++){const l=this.globalWriteIndex%this.maxEntries*this.entryDimension,r=i*this.entryDimension;this.buffer.set(e.subarray(r,r+this.entryDimension),l),this.globalWriteIndex++}}setGlobalWriteIndex(e){this.globalWriteIndex=e}sampleToEntry(e){return Math.floor(e/this.hopSamples)}entryToSample(e){return e*this.hopSamples}getBaseEntry(){return Math.max(0,this.globalWriteIndex-this.maxEntries)}getCurrentSample(){return this.globalWriteIndex*this.hopSamples}getOldestSample(){return this.getBaseEntry()*this.hopSamples}readRange(e,n){if(n<=e)return null;const s=this.sampleToEntry(e),i=Math.ceil(n/this.hopSamples),l=this.getBaseEntry(),r=Math.max(s,l),c=Math.min(i,this.globalWriteIndex);if(c<=r)return null;const m=c-r,p=new Float32Array(m*this.entryDimension);for(let d=0;d<m;d++){const h=(r+d)%this.maxEntries*this.entryDimension,S=d*this.entryDimension;this.entryDimension===1?p[S]=this.buffer[h]:p.set(this.buffer.subarray(h,h+this.entryDimension),S)}return{data:p,entryCount:m,entryDimension:this.entryDimension,firstEntrySample:r*this.hopSamples,hopSamples:this.hopSamples}}hasSpeechInRange(e,n,s){if(this.entryDimension!==1)return{hasSpeech:!1,maxProb:0,entriesChecked:0};const i=this.sampleToEntry(e),l=Math.ceil(n/this.hopSamples),r=this.getBaseEntry(),c=Math.max(i,r),m=Math.min(l,this.globalWriteIndex);if(m<=c)return{hasSpeech:!1,maxProb:0,entriesChecked:0};let p=0;const d=m-c;for(let h=0;h<d;h++){const S=(c+h)%this.maxEntries,y=this.buffer[S];if(y>p&&(p=y),y>=s)return{hasSpeech:!0,maxProb:y,entriesChecked:h+1}}return{hasSpeech:!1,maxProb:p,entriesChecked:d}}getSilenceTailDuration(e,n){if(this.entryDimension!==1||this.globalWriteIndex===0)return 0;const s=this.getBaseEntry();let i=0;for(let l=this.globalWriteIndex-1;l>=s;l--){const r=l%this.maxEntries;if(this.buffer[r]>=e)break;i++}return i*this.hopSamples/n}getState(){return{globalWriteIndex:this.globalWriteIndex,currentSample:this.getCurrentSample(),oldestSample:this.getOldestSample(),fillCount:Math.min(this.globalWriteIndex,this.maxEntries),maxEntries:this.maxEntries,hopSamples:this.hopSamples,entryDimension:this.entryDimension}}reset(){this.globalWriteIndex=0,this.buffer.fill(0)}}let f=null,a=null;self.onmessage=t=>{const e=t.data;try{switch(e.type){case"INIT":g(e.id,e.payload);break;case"WRITE":b(e.payload);break;case"WRITE_BATCH":x(e.payload);break;case"HAS_SPEECH":I(e.id,e.payload);break;case"GET_VAD_SUMMARY":T(e.id,e.payload);break;case"GET_SILENCE_TAIL":R(e.id,e.payload);break;case"QUERY_RANGE":D(e.id,e.payload);break;case"GET_STATE":A(e.id);break;case"RESET":W(e.id);break;default:o({type:"ERROR",id:e.id??0,payload:`Unknown message type: ${e.type}`})}}catch(n){o({type:"ERROR",id:e.id??0,payload:String(n)})}};function g(t,e){f=e;const n=["audio","mel","energyVad","inferenceVad"];a={};for(const l of n){const r=e.layers[l];a[l]=new E(r,e.sampleRate)}const s=(a.audio.getState().maxEntries*4/(1024*1024)).toFixed(1),i=(a.mel.getState().maxEntries*a.mel.getState().entryDimension*4/(1024*1024)).toFixed(1);console.log(`[BufferWorker] Initialized: sr=${e.sampleRate}, audio=${s}MB, mel=${i}MB, energyVad hop=${e.layers.energyVad.hopSamples}, inferenceVad hop=${e.layers.inferenceVad.hopSamples}`),o({type:"INIT",id:t,payload:{success:!0}})}function b(t){if(!a)return;const e=a[t.layer];if(!e)return;if(t.globalSampleOffset!==void 0){const s=e.sampleToEntry(t.globalSampleOffset);e.setGlobalWriteIndex(s)}const n=t.data instanceof Float32Array?t.data:new Float32Array(t.data);e.write(n)}function x(t){if(!a)return;const e=a[t.layer];if(e){if(t.globalSampleOffset!==void 0){const n=e.sampleToEntry(t.globalSampleOffset);e.setGlobalWriteIndex(n)}e.writeBatch(t.data)}}function I(t,e){if(!a){o({type:"HAS_SPEECH",id:t,payload:{hasSpeech:!1,maxProb:0,entriesChecked:0}});return}const n=a[e.layer];if(!n){o({type:"HAS_SPEECH",id:t,payload:{hasSpeech:!1,maxProb:0,entriesChecked:0}});return}const s=n.hasSpeechInRange(e.startSample,e.endSample,e.threshold);o({type:"HAS_SPEECH",id:t,payload:s})}function u(){return{hasSpeech:!1,maxProb:0,entriesChecked:0}}function T(t,e){if(!a||!f){const h=u(),S={energy:h,inference:e.requireInference?h:null,hasSpeech:!1,silenceTailDurationSec:0};o({type:"GET_VAD_SUMMARY",id:t,payload:S});return}const n=Math.min(e.startSample,e.endSample),s=Math.max(e.startSample,e.endSample),i=a.energyVad,l=a.inferenceVad,r=i?i.hasSpeechInRange(n,s,e.energyThreshold):u(),c=e.requireInference?l?l.hasSpeechInRange(n,s,e.inferenceThreshold):u():null,m=e.requireInference?r.hasSpeech&&!!c?.hasSpeech:r.hasSpeech,p=i?i.getSilenceTailDuration(e.energyThreshold,f.sampleRate):0;o({type:"GET_VAD_SUMMARY",id:t,payload:{energy:r,inference:c,hasSpeech:m,silenceTailDurationSec:p}})}function R(t,e){if(!a||!f){o({type:"GET_SILENCE_TAIL",id:t,payload:{durationSec:0}});return}const n=a[e.layer];if(!n){o({type:"GET_SILENCE_TAIL",id:t,payload:{durationSec:0}});return}const s=n.getSilenceTailDuration(e.threshold,f.sampleRate);o({type:"GET_SILENCE_TAIL",id:t,payload:{durationSec:s}})}function D(t,e){if(!a){o({type:"QUERY_RANGE",id:t,payload:{startSample:e.startSample,endSample:e.endSample,layers:{}}});return}const n={startSample:e.startSample,endSample:e.endSample,layers:{}},s=[];for(const i of e.layers){const l=a[i];if(!l)continue;const r=l.readRange(e.startSample,e.endSample);r&&(n.layers[i]=r,s.push(r.data.buffer))}self.postMessage({type:"QUERY_RANGE",id:t,payload:n},s)}function A(t){if(!a||!f){o({type:"ERROR",id:t,payload:"BufferWorker not initialized"});return}const e={sampleRate:f.sampleRate,layers:{audio:a.audio.getState(),mel:a.mel.getState(),energyVad:a.energyVad.getState(),inferenceVad:a.inferenceVad.getState()}};o({type:"GET_STATE",id:t,payload:e})}function W(t){if(a)for(const e of Object.values(a))e.reset();o({type:"RESET",id:t,payload:{success:!0}})}function o(t,e){self.postMessage(t)} | |