@k4ung/vad-web
Advanced tools
@@ -5,4 +5,5 @@ export declare enum Message { | ||
| VADMisfire = "VAD_MISFIRE", | ||
| SpeechEnd = "SPEECH_END" | ||
| SpeechEnd = "SPEECH_END", | ||
| SpeechStop = "SPEECH_STOP" | ||
| } | ||
| //# sourceMappingURL=messages.d.ts.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../../src/_common/messages.ts"],"names":[],"mappings":"AAAA,oBAAY,OAAO;IACjB,UAAU,gBAAgB;IAC1B,WAAW,iBAAiB;IAC5B,UAAU,gBAAgB;IAC1B,SAAS,eAAe;CACzB"} | ||
| {"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../../src/_common/messages.ts"],"names":[],"mappings":"AAAA,oBAAY,OAAO;IACjB,UAAU,gBAAgB;IAC1B,WAAW,iBAAiB;IAC5B,UAAU,gBAAgB;IAC1B,SAAS,eAAe;IACxB,UAAU,gBAAgB;CAC3B"} |
@@ -10,3 +10,4 @@ "use strict"; | ||
| Message["SpeechEnd"] = "SPEECH_END"; | ||
| Message["SpeechStop"] = "SPEECH_STOP"; | ||
| })(Message = exports.Message || (exports.Message = {})); | ||
| //# sourceMappingURL=messages.js.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"messages.js","sourceRoot":"","sources":["../../src/_common/messages.ts"],"names":[],"mappings":";;;AAAA,IAAY,OAKX;AALD,WAAY,OAAO;IACjB,qCAA0B,CAAA;IAC1B,uCAA4B,CAAA;IAC5B,qCAA0B,CAAA;IAC1B,mCAAwB,CAAA;AAC1B,CAAC,EALW,OAAO,GAAP,eAAO,KAAP,eAAO,QAKlB"} | ||
| {"version":3,"file":"messages.js","sourceRoot":"","sources":["../../src/_common/messages.ts"],"names":[],"mappings":";;;AAAA,IAAY,OAMX;AAND,WAAY,OAAO;IACjB,qCAA0B,CAAA;IAC1B,uCAA4B,CAAA;IAC5B,qCAA0B,CAAA;IAC1B,mCAAwB,CAAA;IACxB,qCAA0B,CAAA;AAC5B,CAAC,EANW,OAAO,GAAP,eAAO,KAAP,eAAO,QAMlB"} |
@@ -59,3 +59,3 @@ /* | ||
| eval("\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.Message = void 0;\nvar Message;\n(function (Message) {\n Message[\"AudioFrame\"] = \"AUDIO_FRAME\";\n Message[\"SpeechStart\"] = \"SPEECH_START\";\n Message[\"VADMisfire\"] = \"VAD_MISFIRE\";\n Message[\"SpeechEnd\"] = \"SPEECH_END\";\n})(Message = exports.Message || (exports.Message = {}));\n//# sourceMappingURL=messages.js.map\n\n//# sourceURL=webpack://vad/./dist/_common/messages.js?"); | ||
| eval("\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.Message = void 0;\nvar Message;\n(function (Message) {\n Message[\"AudioFrame\"] = \"AUDIO_FRAME\";\n Message[\"SpeechStart\"] = \"SPEECH_START\";\n Message[\"VADMisfire\"] = \"VAD_MISFIRE\";\n Message[\"SpeechEnd\"] = \"SPEECH_END\";\n Message[\"SpeechStop\"] = \"SPEECH_STOP\";\n})(Message = exports.Message || (exports.Message = {}));\n//# sourceMappingURL=messages.js.map\n\n//# sourceURL=webpack://vad/./dist/_common/messages.js?"); | ||
@@ -140,3 +140,3 @@ /***/ }), | ||
| eval("\nvar __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {\n if (k2 === undefined) k2 = k;\n var desc = Object.getOwnPropertyDescriptor(m, k);\n if (!desc || (\"get\" in desc ? !m.__esModule : desc.writable || desc.configurable)) {\n desc = { enumerable: true, get: function() { return m[k]; } };\n }\n Object.defineProperty(o, k2, desc);\n}) : (function(o, m, k, k2) {\n if (k2 === undefined) k2 = k;\n o[k2] = m[k];\n}));\nvar __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {\n Object.defineProperty(o, \"default\", { enumerable: true, value: v });\n}) : function(o, v) {\n o[\"default\"] = v;\n});\nvar __importStar = (this && this.__importStar) || function (mod) {\n if (mod && mod.__esModule) return mod;\n var result = {};\n if (mod != null) for (var k in mod) if (k !== \"default\" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);\n __setModuleDefault(result, mod);\n return result;\n};\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.AudioNodeVAD = exports.MicVAD = exports.defaultRealTimeVADOptions = void 0;\nconst ort = __importStar(__webpack_require__(/*! onnxruntime-web */ \"onnxruntime-web\"));\nconst _common_1 = __webpack_require__(/*! ./_common */ \"./dist/_common/index.js\");\nconst model_fetcher_1 = __webpack_require__(/*! ./model-fetcher */ \"./dist/model-fetcher.js\");\nconst asset_path_1 = __webpack_require__(/*! ./asset-path */ \"./dist/asset-path.js\");\nconst _getWorkletURL = () => {\n return (0, asset_path_1.assetPath)(\"vad.worklet.bundle.min.js\");\n};\nexports.defaultRealTimeVADOptions = {\n ..._common_1.defaultFrameProcessorOptions,\n onFrameProcessed: (probabilities) => { },\n onVADMisfire: () => {\n _common_1.log.debug(\"VAD misfire\");\n },\n onSpeechStart: () => {\n _common_1.log.debug(\"Detected speech start\");\n },\n onSpeechEnd: () => {\n _common_1.log.debug(\"Detected speech end\");\n },\n workletURL: _getWorkletURL(),\n stream: undefined,\n};\nclass MicVAD {\n static async new(options = {}) {\n const vad = new MicVAD({ ...exports.defaultRealTimeVADOptions, ...options });\n await vad.init();\n return vad;\n }\n constructor(options) {\n this.options = options;\n this.listening = false;\n this.init = async () => {\n if (this.options.stream === undefined)\n this.stream = await navigator.mediaDevices.getUserMedia({\n audio: {\n ...this.options.additionalAudioConstraints,\n channelCount: 1,\n echoCancellation: true,\n autoGainControl: true,\n noiseSuppression: true,\n },\n });\n else\n this.stream = this.options.stream;\n this.audioContext = new AudioContext();\n const source = new MediaStreamAudioSourceNode(this.audioContext, {\n mediaStream: this.stream,\n });\n this.audioNodeVAD = await AudioNodeVAD.new(this.audioContext, this.options);\n this.audioNodeVAD.receive(source);\n };\n this.pause = () => {\n this.audioNodeVAD.pause();\n this.listening = false;\n };\n this.start = () => {\n this.audioNodeVAD.start();\n this.listening = true;\n };\n this.destroy = () => {\n if (this.listening) {\n this.pause();\n }\n this.stream.getTracks().forEach((t) => t.stop());\n this.audioContext.close();\n };\n (0, _common_1.validateOptions)(options);\n }\n}\nexports.MicVAD = MicVAD;\nclass AudioNodeVAD {\n static async new(ctx, options = {}) {\n const vad = new AudioNodeVAD(ctx, {\n ...exports.defaultRealTimeVADOptions,\n ...options,\n });\n await vad.init();\n return vad;\n }\n constructor(ctx, options) {\n this.ctx = ctx;\n this.options = options;\n this.pause = () => {\n this.frameProcessor.pause();\n };\n this.start = () => {\n this.frameProcessor.resume();\n };\n this.receive = (node) => {\n node.connect(this.entryNode);\n };\n this.processFrame = async (frame) => {\n const { probs, msg, audio } = await this.frameProcessor.process(frame);\n if (probs !== undefined) {\n this.options.onFrameProcessed(probs);\n }\n switch (msg) {\n case _common_1.Message.SpeechStart:\n this.options.onSpeechStart();\n break;\n case _common_1.Message.VADMisfire:\n this.options.onVADMisfire();\n break;\n case _common_1.Message.SpeechEnd:\n // @ts-ignore\n this.options.onSpeechEnd(audio);\n break;\n default:\n break;\n }\n };\n this.init = async () => {\n await this.ctx.audioWorklet.addModule(this.options.workletURL);\n const vadNode = new AudioWorkletNode(this.ctx, \"vad-helper-worklet\", {\n processorOptions: {\n frameSamples: this.options.frameSamples,\n },\n });\n this.entryNode = vadNode;\n const model = await _common_1.Silero.new(ort, model_fetcher_1.modelFetcher);\n this.frameProcessor = new _common_1.FrameProcessor(model.process, model.reset_state, {\n frameSamples: this.options.frameSamples,\n positiveSpeechThreshold: this.options.positiveSpeechThreshold,\n negativeSpeechThreshold: this.options.negativeSpeechThreshold,\n redemptionFrames: this.options.redemptionFrames,\n preSpeechPadFrames: this.options.preSpeechPadFrames,\n minSpeechFrames: this.options.minSpeechFrames,\n });\n vadNode.port.onmessage = async (ev) => {\n switch (ev.data?.message) {\n case _common_1.Message.AudioFrame:\n const buffer = ev.data.data;\n const frame = new Float32Array(buffer);\n await this.processFrame(frame);\n break;\n default:\n break;\n }\n };\n };\n (0, _common_1.validateOptions)(options);\n }\n}\nexports.AudioNodeVAD = AudioNodeVAD;\n//# sourceMappingURL=real-time-vad.js.map\n\n//# sourceURL=webpack://vad/./dist/real-time-vad.js?"); | ||
| eval("\nvar __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {\n if (k2 === undefined) k2 = k;\n var desc = Object.getOwnPropertyDescriptor(m, k);\n if (!desc || (\"get\" in desc ? !m.__esModule : desc.writable || desc.configurable)) {\n desc = { enumerable: true, get: function() { return m[k]; } };\n }\n Object.defineProperty(o, k2, desc);\n}) : (function(o, m, k, k2) {\n if (k2 === undefined) k2 = k;\n o[k2] = m[k];\n}));\nvar __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {\n Object.defineProperty(o, \"default\", { enumerable: true, value: v });\n}) : function(o, v) {\n o[\"default\"] = v;\n});\nvar __importStar = (this && this.__importStar) || function (mod) {\n if (mod && mod.__esModule) return mod;\n var result = {};\n if (mod != null) for (var k in mod) if (k !== \"default\" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);\n __setModuleDefault(result, mod);\n return result;\n};\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.AudioNodeVAD = exports.MicVAD = exports.defaultRealTimeVADOptions = void 0;\nconst ort = __importStar(__webpack_require__(/*! onnxruntime-web */ \"onnxruntime-web\"));\nconst _common_1 = __webpack_require__(/*! ./_common */ \"./dist/_common/index.js\");\nconst model_fetcher_1 = __webpack_require__(/*! ./model-fetcher */ \"./dist/model-fetcher.js\");\nconst asset_path_1 = __webpack_require__(/*! ./asset-path */ \"./dist/asset-path.js\");\nconst _getWorkletURL = () => {\n return (0, asset_path_1.assetPath)(\"vad.worklet.bundle.min.js\");\n};\nexports.defaultRealTimeVADOptions = {\n ..._common_1.defaultFrameProcessorOptions,\n onFrameProcessed: (probabilities) => { },\n onVADMisfire: () => {\n _common_1.log.debug(\"VAD misfire\");\n },\n onSpeechStart: () => {\n _common_1.log.debug(\"Detected speech start\");\n },\n onSpeechEnd: () => {\n _common_1.log.debug(\"Detected speech end\");\n },\n workletURL: _getWorkletURL(),\n stream: undefined,\n};\nclass MicVAD {\n static async new(options = {}) {\n const vad = new MicVAD({ ...exports.defaultRealTimeVADOptions, ...options });\n await vad.init();\n return vad;\n }\n constructor(options) {\n this.options = options;\n this.listening = false;\n this.init = async () => {\n if (this.options.stream === undefined)\n this.stream = await navigator.mediaDevices.getUserMedia({\n audio: {\n ...this.options.additionalAudioConstraints,\n channelCount: 1,\n echoCancellation: true,\n autoGainControl: true,\n noiseSuppression: true,\n },\n });\n else\n this.stream = this.options.stream;\n this.audioContext = new AudioContext();\n const source = new MediaStreamAudioSourceNode(this.audioContext, {\n mediaStream: this.stream,\n });\n this.audioNodeVAD = await AudioNodeVAD.new(this.audioContext, this.options);\n this.audioNodeVAD.receive(source);\n };\n this.pause = () => {\n this.audioNodeVAD.pause();\n this.listening = false;\n };\n this.start = () => {\n this.audioNodeVAD.start();\n this.listening = true;\n };\n this.destroy = () => {\n if (this.listening) {\n this.pause();\n }\n this.stream.getTracks().forEach((t) => t.stop());\n this.audioContext.close();\n this.audioNodeVAD.entryNode.port.postMessage({\n message: _common_1.Message.SpeechStop,\n });\n };\n (0, _common_1.validateOptions)(options);\n }\n}\nexports.MicVAD = MicVAD;\nclass AudioNodeVAD {\n static async new(ctx, options = {}) {\n const vad = new AudioNodeVAD(ctx, {\n ...exports.defaultRealTimeVADOptions,\n ...options,\n });\n await vad.init();\n return vad;\n }\n constructor(ctx, options) {\n this.ctx = ctx;\n this.options = options;\n this.pause = () => {\n this.frameProcessor.pause();\n };\n this.start = () => {\n this.frameProcessor.resume();\n };\n this.receive = (node) => {\n node.connect(this.entryNode);\n };\n this.processFrame = async (frame) => {\n const { probs, msg, audio } = await this.frameProcessor.process(frame);\n if (probs !== undefined) {\n this.options.onFrameProcessed(probs);\n }\n switch (msg) {\n case _common_1.Message.SpeechStart:\n this.options.onSpeechStart();\n break;\n case _common_1.Message.VADMisfire:\n this.options.onVADMisfire();\n break;\n case _common_1.Message.SpeechEnd:\n // @ts-ignore\n this.options.onSpeechEnd(audio);\n break;\n default:\n break;\n }\n };\n this.init = async () => {\n await this.ctx.audioWorklet.addModule(this.options.workletURL);\n const vadNode = new AudioWorkletNode(this.ctx, \"vad-helper-worklet\", {\n processorOptions: {\n frameSamples: this.options.frameSamples,\n },\n });\n this.entryNode = vadNode;\n const model = await _common_1.Silero.new(ort, model_fetcher_1.modelFetcher);\n this.frameProcessor = new _common_1.FrameProcessor(model.process, model.reset_state, {\n frameSamples: this.options.frameSamples,\n positiveSpeechThreshold: this.options.positiveSpeechThreshold,\n negativeSpeechThreshold: this.options.negativeSpeechThreshold,\n redemptionFrames: this.options.redemptionFrames,\n preSpeechPadFrames: this.options.preSpeechPadFrames,\n minSpeechFrames: this.options.minSpeechFrames,\n });\n vadNode.port.onmessage = async (ev) => {\n switch (ev.data?.message) {\n case _common_1.Message.AudioFrame:\n const buffer = ev.data.data;\n const frame = new Float32Array(buffer);\n await this.processFrame(frame);\n break;\n default:\n break;\n }\n };\n };\n (0, _common_1.validateOptions)(options);\n }\n}\nexports.AudioNodeVAD = AudioNodeVAD;\n//# sourceMappingURL=real-time-vad.js.map\n\n//# sourceURL=webpack://vad/./dist/real-time-vad.js?"); | ||
@@ -143,0 +143,0 @@ /***/ }), |
@@ -1,1 +0,1 @@ | ||
| !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("onnxruntime-web")):"function"==typeof define&&define.amd?define(["onnxruntime-web"],t):"object"==typeof exports?exports.vad=t(require("onnxruntime-web")):e.vad=t(e.ort)}(self,(e=>(()=>{"use strict";var t={428:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.FrameProcessor=t.validateOptions=t.defaultFrameProcessorOptions=void 0;const i=s(294),o=s(842),r=[512,1024,1536];t.defaultFrameProcessorOptions={positiveSpeechThreshold:.5,negativeSpeechThreshold:.35,preSpeechPadFrames:1,redemptionFrames:8,frameSamples:1536,minSpeechFrames:3},t.validateOptions=function(e){r.includes(e.frameSamples)||o.log.warn("You are using an unusual frame size"),(e.positiveSpeechThreshold<0||e.negativeSpeechThreshold>1)&&o.log.error("postiveSpeechThreshold should be a number between 0 and 1"),(e.negativeSpeechThreshold<0||e.negativeSpeechThreshold>e.positiveSpeechThreshold)&&o.log.error("negativeSpeechThreshold should be between 0 and postiveSpeechThreshold"),e.preSpeechPadFrames<0&&o.log.error("preSpeechPadFrames should be positive"),e.redemptionFrames<0&&o.log.error("preSpeechPadFrames should be positive")};const n=e=>{const t=e.reduce(((e,t)=>(e.push(e.at(-1)+t.length),e)),[0]),s=new Float32Array(t.at(-1));return e.forEach(((e,i)=>{const o=t[i];s.set(e,o)})),s};t.FrameProcessor=class{constructor(e,t,s){this.modelProcessFunc=e,this.modelResetFunc=t,this.options=s,this.speaking=!1,this.redemptionCounter=0,this.active=!1,this.reset=()=>{this.speaking=!1,this.audioBuffer=[],this.modelResetFunc(),this.redemptionCounter=0},this.pause=()=>{this.active=!1,this.reset()},this.resume=()=>{this.active=!0},this.endSegment=()=>{const e=this.audioBuffer;this.audioBuffer=[];const t=this.speaking;this.reset();const s=e.reduce(((e,t)=>e+ +t.isSpeech),0);if(t){if(s>=this.options.minSpeechFrames){const t=n(e.map((e=>e.frame)));return{msg:i.Message.SpeechEnd,audio:t}}return{msg:i.Message.VADMisfire}}return{}},this.process=async e=>{if(!this.active)return{};const t=await this.modelProcessFunc(e);if(this.audioBuffer.push({frame:e,isSpeech:t.isSpeech>=this.options.positiveSpeechThreshold}),t.isSpeech>=this.options.positiveSpeechThreshold&&this.redemptionCounter&&(this.redemptionCounter=0),t.isSpeech>=this.options.positiveSpeechThreshold&&!this.speaking)return this.speaking=!0,{probs:t,msg:i.Message.SpeechStart};if(t.isSpeech<this.options.negativeSpeechThreshold&&this.speaking&&++this.redemptionCounter>=this.options.redemptionFrames){this.redemptionCounter=0,this.speaking=!1;const e=this.audioBuffer;if(this.audioBuffer=[],e.reduce(((e,t)=>e+ +t.isSpeech),0)>=this.options.minSpeechFrames){const s=n(e.map((e=>e.frame)));return{probs:t,msg:i.Message.SpeechEnd,audio:s}}return{probs:t,msg:i.Message.VADMisfire}}if(!this.speaking)for(;this.audioBuffer.length>this.options.preSpeechPadFrames;)this.audioBuffer.shift();return{probs:t}},this.audioBuffer=[],this.reset()}}},14:function(e,t,s){var i=this&&this.__createBinding||(Object.create?function(e,t,s,i){void 0===i&&(i=s);var o=Object.getOwnPropertyDescriptor(t,s);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[s]}}),Object.defineProperty(e,i,o)}:function(e,t,s,i){void 0===i&&(i=s),e[i]=t[s]}),o=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),r=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var s in e)"default"!==s&&Object.prototype.hasOwnProperty.call(e,s)&&i(t,e,s);return o(t,e),t},n=this&&this.__exportStar||function(e,t){for(var s in e)"default"===s||Object.prototype.hasOwnProperty.call(t,s)||i(t,e,s)};Object.defineProperty(t,"__esModule",{value:!0}),t.utils=void 0;const a=r(s(26));t.utils={minFramesForTargetMS:a.minFramesForTargetMS,arrayBufferToBase64:a.arrayBufferToBase64,encodeWAV:a.encodeWAV},n(s(405),t),n(s(428),t),n(s(294),t),n(s(842),t),n(s(260),t),n(s(724),t)},842:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.log=t.LOG_PREFIX=void 0,t.LOG_PREFIX="[VAD]";const s=["error","debug","warn"].reduce(((e,s)=>(e[s]=function(e){return(...s)=>{console[e](t.LOG_PREFIX,...s)}}(s),e)),{});t.log=s},294:(e,t)=>{var s;Object.defineProperty(t,"__esModule",{value:!0}),t.Message=void 0,(s=t.Message||(t.Message={})).AudioFrame="AUDIO_FRAME",s.SpeechStart="SPEECH_START",s.VADMisfire="VAD_MISFIRE",s.SpeechEnd="SPEECH_END"},260:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Silero=void 0;const i=s(842);class o{constructor(e,t){this.ort=e,this.modelFetcher=t,this.init=async()=>{i.log.debug("initializing vad");const e=await this.modelFetcher();this._session=await this.ort.InferenceSession.create(e),this._sr=new this.ort.Tensor("int64",[16000n]),this.reset_state(),i.log.debug("vad is initialized")},this.reset_state=()=>{const e=Array(128).fill(0);this._h=new this.ort.Tensor("float32",e,[2,1,64]),this._c=new this.ort.Tensor("float32",e,[2,1,64])},this.process=async e=>{const t={input:new this.ort.Tensor("float32",e,[1,e.length]),h:this._h,c:this._c,sr:this._sr},s=await this._session.run(t);this._h=s.hn,this._c=s.cn;const[i]=s.output.data;return{notSpeech:1-i,isSpeech:i}}}}t.Silero=o,o.new=async(e,t)=>{const s=new o(e,t);return await s.init(),s}},405:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.PlatformAgnosticNonRealTimeVAD=t.defaultNonRealTimeVADOptions=void 0;const i=s(428),o=s(294),r=s(260),n=s(724);t.defaultNonRealTimeVADOptions={...i.defaultFrameProcessorOptions},t.PlatformAgnosticNonRealTimeVAD=class{static async _new(e,s,i={}){const o=new this(e,s,{...t.defaultNonRealTimeVADOptions,...i});return await o.init(),o}constructor(e,t,s){this.modelFetcher=e,this.ort=t,this.options=s,this.init=async()=>{const e=await r.Silero.new(this.ort,this.modelFetcher);this.frameProcessor=new i.FrameProcessor(e.process,e.reset_state,{frameSamples:this.options.frameSamples,positiveSpeechThreshold:this.options.positiveSpeechThreshold,negativeSpeechThreshold:this.options.negativeSpeechThreshold,redemptionFrames:this.options.redemptionFrames,preSpeechPadFrames:this.options.preSpeechPadFrames,minSpeechFrames:this.options.minSpeechFrames}),this.frameProcessor.resume()},this.run=async function*(e,t){const s={nativeSampleRate:t,targetSampleRate:16e3,targetFrameSize:this.options.frameSamples},i=new n.Resampler(s).process(e);let r,a;for(const e of[...Array(i.length)].keys()){const t=i[e],{msg:s,audio:n}=await this.frameProcessor.process(t);switch(s){case o.Message.SpeechStart:r=e*this.options.frameSamples/16;break;case o.Message.SpeechEnd:a=(e+1)*this.options.frameSamples/16,yield{audio:n,start:r,end:a}}}const{msg:c,audio:h}=this.frameProcessor.endSegment();c==o.Message.SpeechEnd&&(yield{audio:h,start:r,end:i.length*this.options.frameSamples/16})},(0,i.validateOptions)(s)}}},724:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Resampler=void 0;const i=s(842);t.Resampler=class{constructor(e){this.options=e,this.process=e=>{const t=[];for(const t of e)this.inputBuffer.push(t);for(;this.inputBuffer.length*this.options.targetSampleRate/this.options.nativeSampleRate>this.options.targetFrameSize;){const e=new Float32Array(this.options.targetFrameSize);let s=0,i=0;for(;s<this.options.targetFrameSize;){let t=0,o=0;for(;i<Math.min(this.inputBuffer.length,(s+1)*this.options.nativeSampleRate/this.options.targetSampleRate);)t+=this.inputBuffer[i],o++,i++;e[s]=t/o,s++}this.inputBuffer=this.inputBuffer.slice(i),t.push(e)}return t},e.nativeSampleRate<16e3&&i.log.error("nativeSampleRate is too low. Should have 16000 = targetSampleRate <= nativeSampleRate"),this.inputBuffer=[]}}},26:(e,t)=>{function s(e,t,s){for(var i=0;i<s.length;i++)e.setUint8(t+i,s.charCodeAt(i))}Object.defineProperty(t,"__esModule",{value:!0}),t.encodeWAV=t.arrayBufferToBase64=t.minFramesForTargetMS=void 0,t.minFramesForTargetMS=function(e,t,s=16e3){return Math.ceil(e*s/1e3/t)},t.arrayBufferToBase64=function(e){for(var t="",s=new Uint8Array(e),i=s.byteLength,o=0;o<i;o++)t+=String.fromCharCode(s[o]);return btoa(t)},t.encodeWAV=function(e,t=3,i=16e3,o=1,r=32){var n=r/8,a=o*n,c=new ArrayBuffer(44+e.length*n),h=new DataView(c);return s(h,0,"RIFF"),h.setUint32(4,36+e.length*n,!0),s(h,8,"WAVE"),s(h,12,"fmt "),h.setUint32(16,16,!0),h.setUint16(20,t,!0),h.setUint16(22,o,!0),h.setUint32(24,i,!0),h.setUint32(28,i*a,!0),h.setUint16(32,a,!0),h.setUint16(34,r,!0),s(h,36,"data"),h.setUint32(40,e.length*n,!0),1===t?function(e,t,s){for(var i=0;i<s.length;i++,t+=2){var o=Math.max(-1,Math.min(1,s[i]));e.setInt16(t,o<0?32768*o:32767*o,!0)}}(h,44,e):function(e,t,s){for(var i=0;i<s.length;i++,t+=4)e.setFloat32(t,s[i],!0)}(h,44,e),c}},485:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.assetPath=void 0;const s=window.document.currentScript;let i="";s&&(i=s.src.replace(/#.*$/,"").replace(/\?.*$/,"").replace(/\/[^\/]+$/,"/")),t.assetPath=e=>i+e},590:function(e,t,s){var i=this&&this.__createBinding||(Object.create?function(e,t,s,i){void 0===i&&(i=s);var o=Object.getOwnPropertyDescriptor(t,s);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[s]}}),Object.defineProperty(e,i,o)}:function(e,t,s,i){void 0===i&&(i=s),e[i]=t[s]}),o=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),r=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var s in e)"default"!==s&&Object.prototype.hasOwnProperty.call(e,s)&&i(t,e,s);return o(t,e),t};Object.defineProperty(t,"__esModule",{value:!0}),t.defaultRealTimeVADOptions=t.AudioNodeVAD=t.MicVAD=t.NonRealTimeVAD=t.Message=t.FrameProcessor=t.utils=void 0;const n=r(s(656)),a=s(14);Object.defineProperty(t,"FrameProcessor",{enumerable:!0,get:function(){return a.FrameProcessor}}),Object.defineProperty(t,"Message",{enumerable:!0,get:function(){return a.Message}});const c=s(273),h=s(787);class u extends a.PlatformAgnosticNonRealTimeVAD{static async new(e={}){return await this._new(c.modelFetcher,n,e)}}t.NonRealTimeVAD=u,t.utils={audioFileToArray:h.audioFileToArray,...a.utils};var l=s(746);Object.defineProperty(t,"MicVAD",{enumerable:!0,get:function(){return l.MicVAD}}),Object.defineProperty(t,"AudioNodeVAD",{enumerable:!0,get:function(){return l.AudioNodeVAD}}),Object.defineProperty(t,"defaultRealTimeVADOptions",{enumerable:!0,get:function(){return l.defaultRealTimeVADOptions}})},273:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.modelFetcher=void 0;const i=s(485);t.modelFetcher=async()=>{const e=(0,i.assetPath)("silero_vad.onnx");return await fetch(e).then((e=>e.arrayBuffer()))}},746:function(e,t,s){var i=this&&this.__createBinding||(Object.create?function(e,t,s,i){void 0===i&&(i=s);var o=Object.getOwnPropertyDescriptor(t,s);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[s]}}),Object.defineProperty(e,i,o)}:function(e,t,s,i){void 0===i&&(i=s),e[i]=t[s]}),o=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),r=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var s in e)"default"!==s&&Object.prototype.hasOwnProperty.call(e,s)&&i(t,e,s);return o(t,e),t};Object.defineProperty(t,"__esModule",{value:!0}),t.AudioNodeVAD=t.MicVAD=t.defaultRealTimeVADOptions=void 0;const n=r(s(656)),a=s(14),c=s(273),h=s(485);t.defaultRealTimeVADOptions={...a.defaultFrameProcessorOptions,onFrameProcessed:e=>{},onVADMisfire:()=>{a.log.debug("VAD misfire")},onSpeechStart:()=>{a.log.debug("Detected speech start")},onSpeechEnd:()=>{a.log.debug("Detected speech end")},workletURL:(0,h.assetPath)("vad.worklet.bundle.min.js"),stream:void 0};class u{static async new(e={}){const s=new u({...t.defaultRealTimeVADOptions,...e});return await s.init(),s}constructor(e){this.options=e,this.listening=!1,this.init=async()=>{void 0===this.options.stream?this.stream=await navigator.mediaDevices.getUserMedia({audio:{...this.options.additionalAudioConstraints,channelCount:1,echoCancellation:!0,autoGainControl:!0,noiseSuppression:!0}}):this.stream=this.options.stream,this.audioContext=new AudioContext;const e=new MediaStreamAudioSourceNode(this.audioContext,{mediaStream:this.stream});this.audioNodeVAD=await l.new(this.audioContext,this.options),this.audioNodeVAD.receive(e)},this.pause=()=>{this.audioNodeVAD.pause(),this.listening=!1},this.start=()=>{this.audioNodeVAD.start(),this.listening=!0},this.destroy=()=>{this.listening&&this.pause(),this.stream.getTracks().forEach((e=>e.stop())),this.audioContext.close()},(0,a.validateOptions)(e)}}t.MicVAD=u;class l{static async new(e,s={}){const i=new l(e,{...t.defaultRealTimeVADOptions,...s});return await i.init(),i}constructor(e,t){this.ctx=e,this.options=t,this.pause=()=>{this.frameProcessor.pause()},this.start=()=>{this.frameProcessor.resume()},this.receive=e=>{e.connect(this.entryNode)},this.processFrame=async e=>{const{probs:t,msg:s,audio:i}=await this.frameProcessor.process(e);switch(void 0!==t&&this.options.onFrameProcessed(t),s){case a.Message.SpeechStart:this.options.onSpeechStart();break;case a.Message.VADMisfire:this.options.onVADMisfire();break;case a.Message.SpeechEnd:this.options.onSpeechEnd(i)}},this.init=async()=>{await this.ctx.audioWorklet.addModule(this.options.workletURL);const e=new AudioWorkletNode(this.ctx,"vad-helper-worklet",{processorOptions:{frameSamples:this.options.frameSamples}});this.entryNode=e;const t=await a.Silero.new(n,c.modelFetcher);this.frameProcessor=new a.FrameProcessor(t.process,t.reset_state,{frameSamples:this.options.frameSamples,positiveSpeechThreshold:this.options.positiveSpeechThreshold,negativeSpeechThreshold:this.options.negativeSpeechThreshold,redemptionFrames:this.options.redemptionFrames,preSpeechPadFrames:this.options.preSpeechPadFrames,minSpeechFrames:this.options.minSpeechFrames}),e.port.onmessage=async e=>{if(e.data?.message===a.Message.AudioFrame){const t=e.data.data,s=new Float32Array(t);await this.processFrame(s)}}},(0,a.validateOptions)(t)}}t.AudioNodeVAD=l},787:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.audioFileToArray=void 0,t.audioFileToArray=async function(e){const t=new OfflineAudioContext(1,1,44100),s=new FileReader;let i=null;if(await new Promise((o=>{s.addEventListener("loadend",(e=>{const r=s.result;t.decodeAudioData(r,(e=>{i=e,t.startRendering().then((e=>{console.log("Rendering completed successfully"),o()})).catch((e=>{console.error(`Rendering failed: ${e}`)}))}),(e=>{console.log(`Error with decoding audio data: ${e}`)}))})),s.readAsArrayBuffer(e)})),null===i)throw Error("some shit");let o=i,r=new Float32Array(o.length);for(let e=0;e<o.length;e++)for(let t=0;t<o.numberOfChannels;t++)r[e]+=o.getChannelData(t)[e];return{audio:r,sampleRate:o.sampleRate}}},656:t=>{t.exports=e}},s={};return function e(i){var o=s[i];if(void 0!==o)return o.exports;var r=s[i]={exports:{}};return t[i].call(r.exports,r,r.exports,e),r.exports}(590)})())); | ||
| !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("onnxruntime-web")):"function"==typeof define&&define.amd?define(["onnxruntime-web"],t):"object"==typeof exports?exports.vad=t(require("onnxruntime-web")):e.vad=t(e.ort)}(self,(e=>(()=>{"use strict";var t={428:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.FrameProcessor=t.validateOptions=t.defaultFrameProcessorOptions=void 0;const i=s(294),o=s(842),r=[512,1024,1536];t.defaultFrameProcessorOptions={positiveSpeechThreshold:.5,negativeSpeechThreshold:.35,preSpeechPadFrames:1,redemptionFrames:8,frameSamples:1536,minSpeechFrames:3},t.validateOptions=function(e){r.includes(e.frameSamples)||o.log.warn("You are using an unusual frame size"),(e.positiveSpeechThreshold<0||e.negativeSpeechThreshold>1)&&o.log.error("postiveSpeechThreshold should be a number between 0 and 1"),(e.negativeSpeechThreshold<0||e.negativeSpeechThreshold>e.positiveSpeechThreshold)&&o.log.error("negativeSpeechThreshold should be between 0 and postiveSpeechThreshold"),e.preSpeechPadFrames<0&&o.log.error("preSpeechPadFrames should be positive"),e.redemptionFrames<0&&o.log.error("preSpeechPadFrames should be positive")};const n=e=>{const t=e.reduce(((e,t)=>(e.push(e.at(-1)+t.length),e)),[0]),s=new Float32Array(t.at(-1));return e.forEach(((e,i)=>{const o=t[i];s.set(e,o)})),s};t.FrameProcessor=class{constructor(e,t,s){this.modelProcessFunc=e,this.modelResetFunc=t,this.options=s,this.speaking=!1,this.redemptionCounter=0,this.active=!1,this.reset=()=>{this.speaking=!1,this.audioBuffer=[],this.modelResetFunc(),this.redemptionCounter=0},this.pause=()=>{this.active=!1,this.reset()},this.resume=()=>{this.active=!0},this.endSegment=()=>{const e=this.audioBuffer;this.audioBuffer=[];const t=this.speaking;this.reset();const s=e.reduce(((e,t)=>e+ +t.isSpeech),0);if(t){if(s>=this.options.minSpeechFrames){const t=n(e.map((e=>e.frame)));return{msg:i.Message.SpeechEnd,audio:t}}return{msg:i.Message.VADMisfire}}return{}},this.process=async e=>{if(!this.active)return{};const t=await this.modelProcessFunc(e);if(this.audioBuffer.push({frame:e,isSpeech:t.isSpeech>=this.options.positiveSpeechThreshold}),t.isSpeech>=this.options.positiveSpeechThreshold&&this.redemptionCounter&&(this.redemptionCounter=0),t.isSpeech>=this.options.positiveSpeechThreshold&&!this.speaking)return this.speaking=!0,{probs:t,msg:i.Message.SpeechStart};if(t.isSpeech<this.options.negativeSpeechThreshold&&this.speaking&&++this.redemptionCounter>=this.options.redemptionFrames){this.redemptionCounter=0,this.speaking=!1;const e=this.audioBuffer;if(this.audioBuffer=[],e.reduce(((e,t)=>e+ +t.isSpeech),0)>=this.options.minSpeechFrames){const s=n(e.map((e=>e.frame)));return{probs:t,msg:i.Message.SpeechEnd,audio:s}}return{probs:t,msg:i.Message.VADMisfire}}if(!this.speaking)for(;this.audioBuffer.length>this.options.preSpeechPadFrames;)this.audioBuffer.shift();return{probs:t}},this.audioBuffer=[],this.reset()}}},14:function(e,t,s){var i=this&&this.__createBinding||(Object.create?function(e,t,s,i){void 0===i&&(i=s);var o=Object.getOwnPropertyDescriptor(t,s);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[s]}}),Object.defineProperty(e,i,o)}:function(e,t,s,i){void 0===i&&(i=s),e[i]=t[s]}),o=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),r=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var s in e)"default"!==s&&Object.prototype.hasOwnProperty.call(e,s)&&i(t,e,s);return o(t,e),t},n=this&&this.__exportStar||function(e,t){for(var s in e)"default"===s||Object.prototype.hasOwnProperty.call(t,s)||i(t,e,s)};Object.defineProperty(t,"__esModule",{value:!0}),t.utils=void 0;const a=r(s(26));t.utils={minFramesForTargetMS:a.minFramesForTargetMS,arrayBufferToBase64:a.arrayBufferToBase64,encodeWAV:a.encodeWAV},n(s(405),t),n(s(428),t),n(s(294),t),n(s(842),t),n(s(260),t),n(s(724),t)},842:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.log=t.LOG_PREFIX=void 0,t.LOG_PREFIX="[VAD]";const s=["error","debug","warn"].reduce(((e,s)=>(e[s]=function(e){return(...s)=>{console[e](t.LOG_PREFIX,...s)}}(s),e)),{});t.log=s},294:(e,t)=>{var s;Object.defineProperty(t,"__esModule",{value:!0}),t.Message=void 0,(s=t.Message||(t.Message={})).AudioFrame="AUDIO_FRAME",s.SpeechStart="SPEECH_START",s.VADMisfire="VAD_MISFIRE",s.SpeechEnd="SPEECH_END",s.SpeechStop="SPEECH_STOP"},260:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Silero=void 0;const i=s(842);class o{constructor(e,t){this.ort=e,this.modelFetcher=t,this.init=async()=>{i.log.debug("initializing vad");const e=await this.modelFetcher();this._session=await this.ort.InferenceSession.create(e),this._sr=new this.ort.Tensor("int64",[16000n]),this.reset_state(),i.log.debug("vad is initialized")},this.reset_state=()=>{const e=Array(128).fill(0);this._h=new this.ort.Tensor("float32",e,[2,1,64]),this._c=new this.ort.Tensor("float32",e,[2,1,64])},this.process=async e=>{const t={input:new this.ort.Tensor("float32",e,[1,e.length]),h:this._h,c:this._c,sr:this._sr},s=await this._session.run(t);this._h=s.hn,this._c=s.cn;const[i]=s.output.data;return{notSpeech:1-i,isSpeech:i}}}}t.Silero=o,o.new=async(e,t)=>{const s=new o(e,t);return await s.init(),s}},405:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.PlatformAgnosticNonRealTimeVAD=t.defaultNonRealTimeVADOptions=void 0;const i=s(428),o=s(294),r=s(260),n=s(724);t.defaultNonRealTimeVADOptions={...i.defaultFrameProcessorOptions},t.PlatformAgnosticNonRealTimeVAD=class{static async _new(e,s,i={}){const o=new this(e,s,{...t.defaultNonRealTimeVADOptions,...i});return await o.init(),o}constructor(e,t,s){this.modelFetcher=e,this.ort=t,this.options=s,this.init=async()=>{const e=await r.Silero.new(this.ort,this.modelFetcher);this.frameProcessor=new i.FrameProcessor(e.process,e.reset_state,{frameSamples:this.options.frameSamples,positiveSpeechThreshold:this.options.positiveSpeechThreshold,negativeSpeechThreshold:this.options.negativeSpeechThreshold,redemptionFrames:this.options.redemptionFrames,preSpeechPadFrames:this.options.preSpeechPadFrames,minSpeechFrames:this.options.minSpeechFrames}),this.frameProcessor.resume()},this.run=async function*(e,t){const s={nativeSampleRate:t,targetSampleRate:16e3,targetFrameSize:this.options.frameSamples},i=new n.Resampler(s).process(e);let r,a;for(const e of[...Array(i.length)].keys()){const t=i[e],{msg:s,audio:n}=await this.frameProcessor.process(t);switch(s){case o.Message.SpeechStart:r=e*this.options.frameSamples/16;break;case o.Message.SpeechEnd:a=(e+1)*this.options.frameSamples/16,yield{audio:n,start:r,end:a}}}const{msg:c,audio:h}=this.frameProcessor.endSegment();c==o.Message.SpeechEnd&&(yield{audio:h,start:r,end:i.length*this.options.frameSamples/16})},(0,i.validateOptions)(s)}}},724:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Resampler=void 0;const i=s(842);t.Resampler=class{constructor(e){this.options=e,this.process=e=>{const t=[];for(const t of e)this.inputBuffer.push(t);for(;this.inputBuffer.length*this.options.targetSampleRate/this.options.nativeSampleRate>this.options.targetFrameSize;){const e=new Float32Array(this.options.targetFrameSize);let s=0,i=0;for(;s<this.options.targetFrameSize;){let t=0,o=0;for(;i<Math.min(this.inputBuffer.length,(s+1)*this.options.nativeSampleRate/this.options.targetSampleRate);)t+=this.inputBuffer[i],o++,i++;e[s]=t/o,s++}this.inputBuffer=this.inputBuffer.slice(i),t.push(e)}return t},e.nativeSampleRate<16e3&&i.log.error("nativeSampleRate is too low. Should have 16000 = targetSampleRate <= nativeSampleRate"),this.inputBuffer=[]}}},26:(e,t)=>{function s(e,t,s){for(var i=0;i<s.length;i++)e.setUint8(t+i,s.charCodeAt(i))}Object.defineProperty(t,"__esModule",{value:!0}),t.encodeWAV=t.arrayBufferToBase64=t.minFramesForTargetMS=void 0,t.minFramesForTargetMS=function(e,t,s=16e3){return Math.ceil(e*s/1e3/t)},t.arrayBufferToBase64=function(e){for(var t="",s=new Uint8Array(e),i=s.byteLength,o=0;o<i;o++)t+=String.fromCharCode(s[o]);return btoa(t)},t.encodeWAV=function(e,t=3,i=16e3,o=1,r=32){var n=r/8,a=o*n,c=new ArrayBuffer(44+e.length*n),h=new DataView(c);return s(h,0,"RIFF"),h.setUint32(4,36+e.length*n,!0),s(h,8,"WAVE"),s(h,12,"fmt "),h.setUint32(16,16,!0),h.setUint16(20,t,!0),h.setUint16(22,o,!0),h.setUint32(24,i,!0),h.setUint32(28,i*a,!0),h.setUint16(32,a,!0),h.setUint16(34,r,!0),s(h,36,"data"),h.setUint32(40,e.length*n,!0),1===t?function(e,t,s){for(var i=0;i<s.length;i++,t+=2){var o=Math.max(-1,Math.min(1,s[i]));e.setInt16(t,o<0?32768*o:32767*o,!0)}}(h,44,e):function(e,t,s){for(var i=0;i<s.length;i++,t+=4)e.setFloat32(t,s[i],!0)}(h,44,e),c}},485:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.assetPath=void 0;const s=window.document.currentScript;let i="";s&&(i=s.src.replace(/#.*$/,"").replace(/\?.*$/,"").replace(/\/[^\/]+$/,"/")),t.assetPath=e=>i+e},590:function(e,t,s){var i=this&&this.__createBinding||(Object.create?function(e,t,s,i){void 0===i&&(i=s);var o=Object.getOwnPropertyDescriptor(t,s);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[s]}}),Object.defineProperty(e,i,o)}:function(e,t,s,i){void 0===i&&(i=s),e[i]=t[s]}),o=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),r=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var s in e)"default"!==s&&Object.prototype.hasOwnProperty.call(e,s)&&i(t,e,s);return o(t,e),t};Object.defineProperty(t,"__esModule",{value:!0}),t.defaultRealTimeVADOptions=t.AudioNodeVAD=t.MicVAD=t.NonRealTimeVAD=t.Message=t.FrameProcessor=t.utils=void 0;const n=r(s(656)),a=s(14);Object.defineProperty(t,"FrameProcessor",{enumerable:!0,get:function(){return a.FrameProcessor}}),Object.defineProperty(t,"Message",{enumerable:!0,get:function(){return a.Message}});const c=s(273),h=s(787);class u extends a.PlatformAgnosticNonRealTimeVAD{static async new(e={}){return await this._new(c.modelFetcher,n,e)}}t.NonRealTimeVAD=u,t.utils={audioFileToArray:h.audioFileToArray,...a.utils};var p=s(746);Object.defineProperty(t,"MicVAD",{enumerable:!0,get:function(){return p.MicVAD}}),Object.defineProperty(t,"AudioNodeVAD",{enumerable:!0,get:function(){return p.AudioNodeVAD}}),Object.defineProperty(t,"defaultRealTimeVADOptions",{enumerable:!0,get:function(){return p.defaultRealTimeVADOptions}})},273:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.modelFetcher=void 0;const i=s(485);t.modelFetcher=async()=>{const e=(0,i.assetPath)("silero_vad.onnx");return await fetch(e).then((e=>e.arrayBuffer()))}},746:function(e,t,s){var i=this&&this.__createBinding||(Object.create?function(e,t,s,i){void 0===i&&(i=s);var o=Object.getOwnPropertyDescriptor(t,s);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[s]}}),Object.defineProperty(e,i,o)}:function(e,t,s,i){void 0===i&&(i=s),e[i]=t[s]}),o=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),r=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var s in e)"default"!==s&&Object.prototype.hasOwnProperty.call(e,s)&&i(t,e,s);return o(t,e),t};Object.defineProperty(t,"__esModule",{value:!0}),t.AudioNodeVAD=t.MicVAD=t.defaultRealTimeVADOptions=void 0;const n=r(s(656)),a=s(14),c=s(273),h=s(485);t.defaultRealTimeVADOptions={...a.defaultFrameProcessorOptions,onFrameProcessed:e=>{},onVADMisfire:()=>{a.log.debug("VAD misfire")},onSpeechStart:()=>{a.log.debug("Detected speech start")},onSpeechEnd:()=>{a.log.debug("Detected speech end")},workletURL:(0,h.assetPath)("vad.worklet.bundle.min.js"),stream:void 0};class u{static async new(e={}){const s=new u({...t.defaultRealTimeVADOptions,...e});return await s.init(),s}constructor(e){this.options=e,this.listening=!1,this.init=async()=>{void 0===this.options.stream?this.stream=await navigator.mediaDevices.getUserMedia({audio:{...this.options.additionalAudioConstraints,channelCount:1,echoCancellation:!0,autoGainControl:!0,noiseSuppression:!0}}):this.stream=this.options.stream,this.audioContext=new AudioContext;const e=new MediaStreamAudioSourceNode(this.audioContext,{mediaStream:this.stream});this.audioNodeVAD=await p.new(this.audioContext,this.options),this.audioNodeVAD.receive(e)},this.pause=()=>{this.audioNodeVAD.pause(),this.listening=!1},this.start=()=>{this.audioNodeVAD.start(),this.listening=!0},this.destroy=()=>{this.listening&&this.pause(),this.stream.getTracks().forEach((e=>e.stop())),this.audioContext.close(),this.audioNodeVAD.entryNode.port.postMessage({message:a.Message.SpeechStop})},(0,a.validateOptions)(e)}}t.MicVAD=u;class p{static async new(e,s={}){const i=new p(e,{...t.defaultRealTimeVADOptions,...s});return await i.init(),i}constructor(e,t){this.ctx=e,this.options=t,this.pause=()=>{this.frameProcessor.pause()},this.start=()=>{this.frameProcessor.resume()},this.receive=e=>{e.connect(this.entryNode)},this.processFrame=async e=>{const{probs:t,msg:s,audio:i}=await this.frameProcessor.process(e);switch(void 0!==t&&this.options.onFrameProcessed(t),s){case a.Message.SpeechStart:this.options.onSpeechStart();break;case a.Message.VADMisfire:this.options.onVADMisfire();break;case a.Message.SpeechEnd:this.options.onSpeechEnd(i)}},this.init=async()=>{await this.ctx.audioWorklet.addModule(this.options.workletURL);const e=new AudioWorkletNode(this.ctx,"vad-helper-worklet",{processorOptions:{frameSamples:this.options.frameSamples}});this.entryNode=e;const t=await a.Silero.new(n,c.modelFetcher);this.frameProcessor=new a.FrameProcessor(t.process,t.reset_state,{frameSamples:this.options.frameSamples,positiveSpeechThreshold:this.options.positiveSpeechThreshold,negativeSpeechThreshold:this.options.negativeSpeechThreshold,redemptionFrames:this.options.redemptionFrames,preSpeechPadFrames:this.options.preSpeechPadFrames,minSpeechFrames:this.options.minSpeechFrames}),e.port.onmessage=async e=>{if(e.data?.message===a.Message.AudioFrame){const t=e.data.data,s=new Float32Array(t);await this.processFrame(s)}}},(0,a.validateOptions)(t)}}t.AudioNodeVAD=p},787:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.audioFileToArray=void 0,t.audioFileToArray=async function(e){const t=new OfflineAudioContext(1,1,44100),s=new FileReader;let i=null;if(await new Promise((o=>{s.addEventListener("loadend",(e=>{const r=s.result;t.decodeAudioData(r,(e=>{i=e,t.startRendering().then((e=>{console.log("Rendering completed successfully"),o()})).catch((e=>{console.error(`Rendering failed: ${e}`)}))}),(e=>{console.log(`Error with decoding audio data: ${e}`)}))})),s.readAsArrayBuffer(e)})),null===i)throw Error("some shit");let o=i,r=new Float32Array(o.length);for(let e=0;e<o.length;e++)for(let t=0;t<o.numberOfChannels;t++)r[e]+=o.getChannelData(t)[e];return{audio:r,sampleRate:o.sampleRate}}},656:t=>{t.exports=e}},s={};return function e(i){var o=s[i];if(void 0!==o)return o.exports;var r=s[i]={exports:{}};return t[i].call(r.exports,r,r.exports,e),r.exports}(590)})())); |
@@ -51,3 +51,3 @@ import { SpeechProbabilities, FrameProcessor, FrameProcessorOptions } from "./_common"; | ||
| frameProcessor: FrameProcessor; | ||
| entryNode: AudioNode; | ||
| entryNode: AudioWorkletNode; | ||
| static new(ctx: AudioContext, options?: Partial<RealTimeVADOptions>): Promise<AudioNodeVAD>; | ||
@@ -54,0 +54,0 @@ constructor(ctx: AudioContext, options: RealTimeVADOptions); |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"real-time-vad.d.ts","sourceRoot":"","sources":["../src/real-time-vad.ts"],"names":[],"mappings":"AACA,OAAO,EAIL,mBAAmB,EAEnB,cAAc,EACd,qBAAqB,EAEtB,MAAM,WAAW,CAAA;AAIlB,UAAU,oBAAoB;IAC5B,4GAA4G;IAC5G,gBAAgB,EAAE,CAAC,aAAa,EAAE,mBAAmB,KAAK,GAAG,CAAA;IAE7D;;OAEG;IACH,YAAY,EAAE,MAAM,GAAG,CAAA;IAEvB,oDAAoD;IACpD,aAAa,EAAE,MAAM,GAAG,CAAA;IAExB;;;;OAIG;IACH,WAAW,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,GAAG,CAAA;CAC1C;AAED;;;GAGG;AACH,KAAK,gBAAgB,GAAG,IAAI,CAC1B,qBAAqB,EACrB,cAAc,GAAG,kBAAkB,GAAG,iBAAiB,GAAG,kBAAkB,CAC7E,CAAA;AAED,UAAU,+BACR,SAAQ,qBAAqB,EAC3B,oBAAoB;IACtB,0BAA0B,CAAC,EAAE,gBAAgB,CAAA;IAC7C,UAA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E,OAAO,CAAC,kBAAkB,CAAM;gBAUxB,GAAG,EAAE,YAAY,EAAS,OAAO,EAAE,kBAAkB;IAIxE,KAAK,aAEJ;IAED,KAAK,aAEJ;IAED,OAAO,SAAU,SAAS,UAEzB;IAED,YAAY,UAAiB,YAAY,mBAsBxC;IAED,IAAI,sBAgCH;CACF"} |
@@ -92,2 +92,5 @@ "use strict"; | ||
| this.audioContext.close(); | ||
| this.audioNodeVAD.entryNode.port.postMessage({ | ||
| message: _common_1.Message.SpeechStop, | ||
| }); | ||
| }; | ||
@@ -94,0 +97,0 @@ (0, _common_1.validateOptions)(options); |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"real-time-vad.js","sourceRoot":"","sources":["../src/real-time-vad.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,qDAAsC;AACtC,uCASkB;AAClB,mDAA8C;AAC9C,6CAAwC;AAkDxC,MAAM,cAAc,GAAG,GAAG,EAAE;IAC1B,OAAO,IAAA,sBAAS,EAAC,2BAA2B,CAAC,CAAA;AAC/C,CAAC,CAAA;AAEY,QAAA,yBAAyB,GAAuB;IAC3D,GAAG,sCAA4B;IAC/B,gBAAgB,EAAE,CAAC,aAAa,EAAE,EAAE,GAAE,CAAC;IACvC,YAAY,EAAE,GAAG,EAAE;QACjB,aAAG,CAAC,KAAK,CAAC,aAAa,CAAC,CAAA;IAC1B,CAAC;IACD,aAAa,EAAE,GAAG,EAAE;QAClB,aAAG,CAAC,KAAK,CAAC,uBAAuB,CAAC,CAAA;IACpC,CAAC;IACD,WAAW,EAAE,GAAG,EAAE;QAChB,aAAG,CAAC,KAAK,CAAC,qBAAqB,CAAC,CAAA;IAClC,CAAC;IACD,UAAU,EAAE,cAAc,EAAE;IAC5B,MAAM,EAAE,SAAS;CAClB,CAAA;AAED,MAAa,MAAM;IASjB,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,UAAuC,EAAE;QACxD,MAAM,GAAG,GAAG,IAAI,MAAM,CA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,CAAC,YAAY;iBACxC;aACF,CAAC,CAAA;YACF,IAAI,CAAC,SAAS,GAAG,OAAO,CAAA;YAExB,MAAM,KAAK,GAAG,MAAM,gBAAM,CAAC,GAAG,CAAC,GAAG,EAAE,4BAAY,CAAC,CAAA;YAEjD,IAAI,CAAC,cAAc,GAAG,IAAI,wBAAc,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,WAAW,EAAE;gBACzE,YAAY,EAAE,IAAI,CAAC,OAAO,CAAC,YAAY;gBACvC,uBAAuB,EAAE,IAAI,CAAC,OAAO,CAAC,uBAAuB;gBAC7D,uBAAuB,EAAE,IAAI,CAAC,OAAO,CAAC,uBAAuB;gBAC7D,gBAAgB,EAAE,IAAI,CAAC,OAAO,CAAC,gBAAgB;gBAC/C,kBAAkB,EAAE,IAAI,CAAC,OAAO,CAAC,kBAAkB;gBACnD,eAAe,EAAE,IAAI,CAAC,OAAO,CAAC,eAAe;aAC9C,CAAC,CAAA;YAEF,OAAO,CAAC,IAAI,CAAC,SAAS,GAAG,KAAK,EAAE,EAAgB,EAAE,EAAE;gBAClD,QAAQ,EAAE,CAAC,IAAI,EAAE,OAAO,EAAE;oBACxB,KAAK,iBAAO,CAAC,UAAU;wBACrB,MAAM,MAAM,GAAgB,EAAE,CAAC,IAAI,CAAC,IAAI,CAAA;wBACxC,MAAM,KAAK,GAAG,IAAI,YAAY,CAAC,MAAM,CAAC,CAAA;wBACtC,MAAM,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC,CAAA;wBAC9B,MAAK;oBAEP;wBACE,MAAK;iBACR;YACH,CAAC,CAAA;QACH,CAAC,CAAA;QAvEC,IAAA,yBAAe,EAAC,OAAO,CAAC,CAAA;IAC1B,CAAC;CAuEF;AA3FD,oCA2FC"} |
@@ -49,3 +49,3 @@ /* | ||
| eval("\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.Message = void 0;\nvar Message;\n(function (Message) {\n Message[\"AudioFrame\"] = \"AUDIO_FRAME\";\n Message[\"SpeechStart\"] = \"SPEECH_START\";\n Message[\"VADMisfire\"] = \"VAD_MISFIRE\";\n Message[\"SpeechEnd\"] = \"SPEECH_END\";\n})(Message = exports.Message || (exports.Message = {}));\n//# sourceMappingURL=messages.js.map\n\n//# sourceURL=webpack://@k4ung/vad-web/./dist/_common/messages.js?"); | ||
| eval("\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.Message = void 0;\nvar Message;\n(function (Message) {\n Message[\"AudioFrame\"] = \"AUDIO_FRAME\";\n Message[\"SpeechStart\"] = \"SPEECH_START\";\n Message[\"VADMisfire\"] = \"VAD_MISFIRE\";\n Message[\"SpeechEnd\"] = \"SPEECH_END\";\n Message[\"SpeechStop\"] = \"SPEECH_STOP\";\n})(Message = exports.Message || (exports.Message = {}));\n//# sourceMappingURL=messages.js.map\n\n//# sourceURL=webpack://@k4ung/vad-web/./dist/_common/messages.js?"); | ||
@@ -100,3 +100,3 @@ /***/ }), | ||
| eval("\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nconst _common_1 = __webpack_require__(/*! ./_common */ \"./dist/_common/index.js\");\nclass Processor extends AudioWorkletProcessor {\n constructor(options) {\n super();\n this._initialized = false;\n this.init = async () => {\n _common_1.log.debug(\"initializing worklet\");\n this.resampler = new _common_1.Resampler({\n nativeSampleRate: sampleRate,\n targetSampleRate: 16000,\n targetFrameSize: this.options.frameSamples,\n });\n this._initialized = true;\n _common_1.log.debug(\"initialized worklet\");\n };\n this.options = options.processorOptions;\n this.init();\n }\n process(inputs, outputs, parameters) {\n // @ts-ignore\n const arr = inputs[0][0];\n if (this._initialized && arr instanceof Float32Array) {\n const frames = this.resampler.process(arr);\n for (const frame of frames) {\n this.port.postMessage({ message: _common_1.Message.AudioFrame, data: frame.buffer }, [frame.buffer]);\n }\n }\n return true;\n }\n}\nregisterProcessor(\"vad-helper-worklet\", Processor);\n//# sourceMappingURL=worklet.js.map\n\n//# sourceURL=webpack://@k4ung/vad-web/./dist/worklet.js?"); | ||
| eval("\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nconst _common_1 = __webpack_require__(/*! ./_common */ \"./dist/_common/index.js\");\nclass Processor extends AudioWorkletProcessor {\n constructor(options) {\n super();\n this._initialized = false;\n this._stopProcessing = false;\n this.init = async () => {\n _common_1.log.debug(\"initializing worklet\");\n this.resampler = new _common_1.Resampler({\n nativeSampleRate: sampleRate,\n targetSampleRate: 16000,\n targetFrameSize: this.options.frameSamples,\n });\n this._initialized = true;\n _common_1.log.debug(\"initialized worklet\");\n };\n this.options = options.processorOptions;\n this.init();\n this.port.onmessage = (ev) => {\n if (ev.data.message === _common_1.Message.SpeechStop) {\n this._stopProcessing = true;\n }\n };\n }\n process(inputs, outputs, parameters) {\n // @ts-ignore\n const arr = inputs[0][0];\n if (this._initialized && arr instanceof Float32Array) {\n const frames = this.resampler.process(arr);\n for (const frame of frames) {\n this.port.postMessage({ message: _common_1.Message.AudioFrame, data: frame.buffer }, [frame.buffer]);\n }\n }\n if (this._stopProcessing)\n return false;\n return true;\n }\n}\nregisterProcessor(\"vad-helper-worklet\", Processor);\n//# sourceMappingURL=worklet.js.map\n\n//# sourceURL=webpack://@k4ung/vad-web/./dist/worklet.js?"); | ||
@@ -103,0 +103,0 @@ /***/ }) |
@@ -1,1 +0,1 @@ | ||
| (()=>{"use strict";var e={428:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.FrameProcessor=t.validateOptions=t.defaultFrameProcessorOptions=void 0;const i=s(294),r=s(842),o=[512,1024,1536];t.defaultFrameProcessorOptions={positiveSpeechThreshold:.5,negativeSpeechThreshold:.35,preSpeechPadFrames:1,redemptionFrames:8,frameSamples:1536,minSpeechFrames:3},t.validateOptions=function(e){o.includes(e.frameSamples)||r.log.warn("You are using an unusual frame size"),(e.positiveSpeechThreshold<0||e.negativeSpeechThreshold>1)&&r.log.error("postiveSpeechThreshold should be a number between 0 and 1"),(e.negativeSpeechThreshold<0||e.negativeSpeechThreshold>e.positiveSpeechThreshold)&&r.log.error("negativeSpeechThreshold should be between 0 and postiveSpeechThreshold"),e.preSpeechPadFrames<0&&r.log.error("preSpeechPadFrames should be positive"),e.redemptionFrames<0&&r.log.error("preSpeechPadFrames should be positive")};const n=e=>{const t=e.reduce(((e,t)=>(e.push(e.at(-1)+t.length),e)),[0]),s=new Float32Array(t.at(-1));return e.forEach(((e,i)=>{const r=t[i];s.set(e,r)})),s};t.FrameProcessor=class{constructor(e,t,s){this.modelProcessFunc=e,this.modelResetFunc=t,this.options=s,this.speaking=!1,this.redemptionCounter=0,this.active=!1,this.reset=()=>{this.speaking=!1,this.audioBuffer=[],this.modelResetFunc(),this.redemptionCounter=0},this.pause=()=>{this.active=!1,this.reset()},this.resume=()=>{this.active=!0},this.endSegment=()=>{const e=this.audioBuffer;this.audioBuffer=[];const t=this.speaking;this.reset();const s=e.reduce(((e,t)=>e+ +t.isSpeech),0);if(t){if(s>=this.options.minSpeechFrames){const t=n(e.map((e=>e.frame)));return{msg:i.Message.SpeechEnd,audio:t}}return{msg:i.Message.VADMisfire}}return{}},this.process=async e=>{if(!this.active)return{};const t=await this.modelProcessFunc(e);if(this.audioBuffer.push({frame:e,isSpeech:t.isSpeech>=this.options.positiveSpeechThreshold}),t.isSpeech>=this.options.positiveSpeechThreshold&&this.redemptionCounter&&(this.redemptionCounter=0),t.isSpeech>=this.options.positiveSpeechThreshold&&!this.speaking)return this.speaking=!0,{probs:t,msg:i.Message.SpeechStart};if(t.isSpeech<this.options.negativeSpeechThreshold&&this.speaking&&++this.redemptionCounter>=this.options.redemptionFrames){this.redemptionCounter=0,this.speaking=!1;const e=this.audioBuffer;if(this.audioBuffer=[],e.reduce(((e,t)=>e+ +t.isSpeech),0)>=this.options.minSpeechFrames){const s=n(e.map((e=>e.frame)));return{probs:t,msg:i.Message.SpeechEnd,audio:s}}return{probs:t,msg:i.Message.VADMisfire}}if(!this.speaking)for(;this.audioBuffer.length>this.options.preSpeechPadFrames;)this.audioBuffer.shift();return{probs:t}},this.audioBuffer=[],this.reset()}}},14:function(e,t,s){var i=this&&this.__createBinding||(Object.create?function(e,t,s,i){void 0===i&&(i=s);var r=Object.getOwnPropertyDescriptor(t,s);r&&!("get"in r?!t.__esModule:r.writable||r.configurable)||(r={enumerable:!0,get:function(){return t[s]}}),Object.defineProperty(e,i,r)}:function(e,t,s,i){void 0===i&&(i=s),e[i]=t[s]}),r=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),o=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var s in e)"default"!==s&&Object.prototype.hasOwnProperty.call(e,s)&&i(t,e,s);return r(t,e),t},n=this&&this.__exportStar||function(e,t){for(var s in e)"default"===s||Object.prototype.hasOwnProperty.call(t,s)||i(t,e,s)};Object.defineProperty(t,"__esModule",{value:!0}),t.utils=void 0;const a=o(s(26));t.utils={minFramesForTargetMS:a.minFramesForTargetMS,arrayBufferToBase64:a.arrayBufferToBase64,encodeWAV:a.encodeWAV},n(s(405),t),n(s(428),t),n(s(294),t),n(s(842),t),n(s(260),t),n(s(724),t)},842:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.log=t.LOG_PREFIX=void 0,t.LOG_PREFIX="[VAD]";const s=["error","debug","warn"].reduce(((e,s)=>(e[s]=function(e){return(...s)=>{console[e](t.LOG_PREFIX,...s)}}(s),e)),{});t.log=s},294:(e,t)=>{var s;Object.defineProperty(t,"__esModule",{value:!0}),t.Message=void 0,(s=t.Message||(t.Message={})).AudioFrame="AUDIO_FRAME",s.SpeechStart="SPEECH_START",s.VADMisfire="VAD_MISFIRE",s.SpeechEnd="SPEECH_END"},260:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Silero=void 0;const i=s(842);class r{constructor(e,t){this.ort=e,this.modelFetcher=t,this.init=async()=>{i.log.debug("initializing vad");const e=await this.modelFetcher();this._session=await this.ort.InferenceSession.create(e),this._sr=new this.ort.Tensor("int64",[16000n]),this.reset_state(),i.log.debug("vad is initialized")},this.reset_state=()=>{const e=Array(128).fill(0);this._h=new this.ort.Tensor("float32",e,[2,1,64]),this._c=new this.ort.Tensor("float32",e,[2,1,64])},this.process=async e=>{const t={input:new this.ort.Tensor("float32",e,[1,e.length]),h:this._h,c:this._c,sr:this._sr},s=await this._session.run(t);this._h=s.hn,this._c=s.cn;const[i]=s.output.data;return{notSpeech:1-i,isSpeech:i}}}}t.Silero=r,r.new=async(e,t)=>{const s=new r(e,t);return await s.init(),s}},405:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.PlatformAgnosticNonRealTimeVAD=t.defaultNonRealTimeVADOptions=void 0;const i=s(428),r=s(294),o=s(260),n=s(724);t.defaultNonRealTimeVADOptions={...i.defaultFrameProcessorOptions},t.PlatformAgnosticNonRealTimeVAD=class{static async _new(e,s,i={}){const r=new this(e,s,{...t.defaultNonRealTimeVADOptions,...i});return await r.init(),r}constructor(e,t,s){this.modelFetcher=e,this.ort=t,this.options=s,this.init=async()=>{const e=await o.Silero.new(this.ort,this.modelFetcher);this.frameProcessor=new i.FrameProcessor(e.process,e.reset_state,{frameSamples:this.options.frameSamples,positiveSpeechThreshold:this.options.positiveSpeechThreshold,negativeSpeechThreshold:this.options.negativeSpeechThreshold,redemptionFrames:this.options.redemptionFrames,preSpeechPadFrames:this.options.preSpeechPadFrames,minSpeechFrames:this.options.minSpeechFrames}),this.frameProcessor.resume()},this.run=async function*(e,t){const s={nativeSampleRate:t,targetSampleRate:16e3,targetFrameSize:this.options.frameSamples},i=new n.Resampler(s).process(e);let o,a;for(const e of[...Array(i.length)].keys()){const t=i[e],{msg:s,audio:n}=await this.frameProcessor.process(t);switch(s){case r.Message.SpeechStart:o=e*this.options.frameSamples/16;break;case r.Message.SpeechEnd:a=(e+1)*this.options.frameSamples/16,yield{audio:n,start:o,end:a}}}const{msg:h,audio:p}=this.frameProcessor.endSegment();h==r.Message.SpeechEnd&&(yield{audio:p,start:o,end:i.length*this.options.frameSamples/16})},(0,i.validateOptions)(s)}}},724:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Resampler=void 0;const i=s(842);t.Resampler=class{constructor(e){this.options=e,this.process=e=>{const t=[];for(const t of e)this.inputBuffer.push(t);for(;this.inputBuffer.length*this.options.targetSampleRate/this.options.nativeSampleRate>this.options.targetFrameSize;){const e=new Float32Array(this.options.targetFrameSize);let s=0,i=0;for(;s<this.options.targetFrameSize;){let t=0,r=0;for(;i<Math.min(this.inputBuffer.length,(s+1)*this.options.nativeSampleRate/this.options.targetSampleRate);)t+=this.inputBuffer[i],r++,i++;e[s]=t/r,s++}this.inputBuffer=this.inputBuffer.slice(i),t.push(e)}return t},e.nativeSampleRate<16e3&&i.log.error("nativeSampleRate is too low. Should have 16000 = targetSampleRate <= nativeSampleRate"),this.inputBuffer=[]}}},26:(e,t)=>{function s(e,t,s){for(var i=0;i<s.length;i++)e.setUint8(t+i,s.charCodeAt(i))}Object.defineProperty(t,"__esModule",{value:!0}),t.encodeWAV=t.arrayBufferToBase64=t.minFramesForTargetMS=void 0,t.minFramesForTargetMS=function(e,t,s=16e3){return Math.ceil(e*s/1e3/t)},t.arrayBufferToBase64=function(e){for(var t="",s=new Uint8Array(e),i=s.byteLength,r=0;r<i;r++)t+=String.fromCharCode(s[r]);return btoa(t)},t.encodeWAV=function(e,t=3,i=16e3,r=1,o=32){var n=o/8,a=r*n,h=new ArrayBuffer(44+e.length*n),p=new DataView(h);return s(p,0,"RIFF"),p.setUint32(4,36+e.length*n,!0),s(p,8,"WAVE"),s(p,12,"fmt "),p.setUint32(16,16,!0),p.setUint16(20,t,!0),p.setUint16(22,r,!0),p.setUint32(24,i,!0),p.setUint32(28,i*a,!0),p.setUint16(32,a,!0),p.setUint16(34,o,!0),s(p,36,"data"),p.setUint32(40,e.length*n,!0),1===t?function(e,t,s){for(var i=0;i<s.length;i++,t+=2){var r=Math.max(-1,Math.min(1,s[i]));e.setInt16(t,r<0?32768*r:32767*r,!0)}}(p,44,e):function(e,t,s){for(var i=0;i<s.length;i++,t+=4)e.setFloat32(t,s[i],!0)}(p,44,e),h}}},t={};function s(i){var r=t[i];if(void 0!==r)return r.exports;var o=t[i]={exports:{}};return e[i].call(o.exports,o,o.exports,s),o.exports}(()=>{const e=s(14);class t extends AudioWorkletProcessor{constructor(t){super(),this._initialized=!1,this.init=async()=>{e.log.debug("initializing worklet"),this.resampler=new e.Resampler({nativeSampleRate:sampleRate,targetSampleRate:16e3,targetFrameSize:this.options.frameSamples}),this._initialized=!0,e.log.debug("initialized worklet")},this.options=t.processorOptions,this.init()}process(t,s,i){const r=t[0][0];if(this._initialized&&r instanceof Float32Array){const t=this.resampler.process(r);for(const s of t)this.port.postMessage({message:e.Message.AudioFrame,data:s.buffer},[s.buffer])}return!0}}registerProcessor("vad-helper-worklet",t)})()})(); | ||
| (()=>{"use strict";var e={428:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.FrameProcessor=t.validateOptions=t.defaultFrameProcessorOptions=void 0;const i=s(294),r=s(842),o=[512,1024,1536];t.defaultFrameProcessorOptions={positiveSpeechThreshold:.5,negativeSpeechThreshold:.35,preSpeechPadFrames:1,redemptionFrames:8,frameSamples:1536,minSpeechFrames:3},t.validateOptions=function(e){o.includes(e.frameSamples)||r.log.warn("You are using an unusual frame size"),(e.positiveSpeechThreshold<0||e.negativeSpeechThreshold>1)&&r.log.error("postiveSpeechThreshold should be a number between 0 and 1"),(e.negativeSpeechThreshold<0||e.negativeSpeechThreshold>e.positiveSpeechThreshold)&&r.log.error("negativeSpeechThreshold should be between 0 and postiveSpeechThreshold"),e.preSpeechPadFrames<0&&r.log.error("preSpeechPadFrames should be positive"),e.redemptionFrames<0&&r.log.error("preSpeechPadFrames should be positive")};const n=e=>{const t=e.reduce(((e,t)=>(e.push(e.at(-1)+t.length),e)),[0]),s=new Float32Array(t.at(-1));return e.forEach(((e,i)=>{const r=t[i];s.set(e,r)})),s};t.FrameProcessor=class{constructor(e,t,s){this.modelProcessFunc=e,this.modelResetFunc=t,this.options=s,this.speaking=!1,this.redemptionCounter=0,this.active=!1,this.reset=()=>{this.speaking=!1,this.audioBuffer=[],this.modelResetFunc(),this.redemptionCounter=0},this.pause=()=>{this.active=!1,this.reset()},this.resume=()=>{this.active=!0},this.endSegment=()=>{const e=this.audioBuffer;this.audioBuffer=[];const t=this.speaking;this.reset();const s=e.reduce(((e,t)=>e+ +t.isSpeech),0);if(t){if(s>=this.options.minSpeechFrames){const t=n(e.map((e=>e.frame)));return{msg:i.Message.SpeechEnd,audio:t}}return{msg:i.Message.VADMisfire}}return{}},this.process=async e=>{if(!this.active)return{};const t=await this.modelProcessFunc(e);if(this.audioBuffer.push({frame:e,isSpeech:t.isSpeech>=this.options.positiveSpeechThreshold}),t.isSpeech>=this.options.positiveSpeechThreshold&&this.redemptionCounter&&(this.redemptionCounter=0),t.isSpeech>=this.options.positiveSpeechThreshold&&!this.speaking)return this.speaking=!0,{probs:t,msg:i.Message.SpeechStart};if(t.isSpeech<this.options.negativeSpeechThreshold&&this.speaking&&++this.redemptionCounter>=this.options.redemptionFrames){this.redemptionCounter=0,this.speaking=!1;const e=this.audioBuffer;if(this.audioBuffer=[],e.reduce(((e,t)=>e+ +t.isSpeech),0)>=this.options.minSpeechFrames){const s=n(e.map((e=>e.frame)));return{probs:t,msg:i.Message.SpeechEnd,audio:s}}return{probs:t,msg:i.Message.VADMisfire}}if(!this.speaking)for(;this.audioBuffer.length>this.options.preSpeechPadFrames;)this.audioBuffer.shift();return{probs:t}},this.audioBuffer=[],this.reset()}}},14:function(e,t,s){var i=this&&this.__createBinding||(Object.create?function(e,t,s,i){void 0===i&&(i=s);var r=Object.getOwnPropertyDescriptor(t,s);r&&!("get"in r?!t.__esModule:r.writable||r.configurable)||(r={enumerable:!0,get:function(){return t[s]}}),Object.defineProperty(e,i,r)}:function(e,t,s,i){void 0===i&&(i=s),e[i]=t[s]}),r=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),o=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var s in e)"default"!==s&&Object.prototype.hasOwnProperty.call(e,s)&&i(t,e,s);return r(t,e),t},n=this&&this.__exportStar||function(e,t){for(var s in e)"default"===s||Object.prototype.hasOwnProperty.call(t,s)||i(t,e,s)};Object.defineProperty(t,"__esModule",{value:!0}),t.utils=void 0;const a=o(s(26));t.utils={minFramesForTargetMS:a.minFramesForTargetMS,arrayBufferToBase64:a.arrayBufferToBase64,encodeWAV:a.encodeWAV},n(s(405),t),n(s(428),t),n(s(294),t),n(s(842),t),n(s(260),t),n(s(724),t)},842:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.log=t.LOG_PREFIX=void 0,t.LOG_PREFIX="[VAD]";const s=["error","debug","warn"].reduce(((e,s)=>(e[s]=function(e){return(...s)=>{console[e](t.LOG_PREFIX,...s)}}(s),e)),{});t.log=s},294:(e,t)=>{var s;Object.defineProperty(t,"__esModule",{value:!0}),t.Message=void 0,(s=t.Message||(t.Message={})).AudioFrame="AUDIO_FRAME",s.SpeechStart="SPEECH_START",s.VADMisfire="VAD_MISFIRE",s.SpeechEnd="SPEECH_END",s.SpeechStop="SPEECH_STOP"},260:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Silero=void 0;const i=s(842);class r{constructor(e,t){this.ort=e,this.modelFetcher=t,this.init=async()=>{i.log.debug("initializing vad");const e=await this.modelFetcher();this._session=await this.ort.InferenceSession.create(e),this._sr=new this.ort.Tensor("int64",[16000n]),this.reset_state(),i.log.debug("vad is initialized")},this.reset_state=()=>{const e=Array(128).fill(0);this._h=new this.ort.Tensor("float32",e,[2,1,64]),this._c=new this.ort.Tensor("float32",e,[2,1,64])},this.process=async e=>{const t={input:new this.ort.Tensor("float32",e,[1,e.length]),h:this._h,c:this._c,sr:this._sr},s=await this._session.run(t);this._h=s.hn,this._c=s.cn;const[i]=s.output.data;return{notSpeech:1-i,isSpeech:i}}}}t.Silero=r,r.new=async(e,t)=>{const s=new r(e,t);return await s.init(),s}},405:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.PlatformAgnosticNonRealTimeVAD=t.defaultNonRealTimeVADOptions=void 0;const i=s(428),r=s(294),o=s(260),n=s(724);t.defaultNonRealTimeVADOptions={...i.defaultFrameProcessorOptions},t.PlatformAgnosticNonRealTimeVAD=class{static async _new(e,s,i={}){const r=new this(e,s,{...t.defaultNonRealTimeVADOptions,...i});return await r.init(),r}constructor(e,t,s){this.modelFetcher=e,this.ort=t,this.options=s,this.init=async()=>{const e=await o.Silero.new(this.ort,this.modelFetcher);this.frameProcessor=new i.FrameProcessor(e.process,e.reset_state,{frameSamples:this.options.frameSamples,positiveSpeechThreshold:this.options.positiveSpeechThreshold,negativeSpeechThreshold:this.options.negativeSpeechThreshold,redemptionFrames:this.options.redemptionFrames,preSpeechPadFrames:this.options.preSpeechPadFrames,minSpeechFrames:this.options.minSpeechFrames}),this.frameProcessor.resume()},this.run=async function*(e,t){const s={nativeSampleRate:t,targetSampleRate:16e3,targetFrameSize:this.options.frameSamples},i=new n.Resampler(s).process(e);let o,a;for(const e of[...Array(i.length)].keys()){const t=i[e],{msg:s,audio:n}=await this.frameProcessor.process(t);switch(s){case r.Message.SpeechStart:o=e*this.options.frameSamples/16;break;case r.Message.SpeechEnd:a=(e+1)*this.options.frameSamples/16,yield{audio:n,start:o,end:a}}}const{msg:h,audio:p}=this.frameProcessor.endSegment();h==r.Message.SpeechEnd&&(yield{audio:p,start:o,end:i.length*this.options.frameSamples/16})},(0,i.validateOptions)(s)}}},724:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Resampler=void 0;const i=s(842);t.Resampler=class{constructor(e){this.options=e,this.process=e=>{const t=[];for(const t of e)this.inputBuffer.push(t);for(;this.inputBuffer.length*this.options.targetSampleRate/this.options.nativeSampleRate>this.options.targetFrameSize;){const e=new Float32Array(this.options.targetFrameSize);let s=0,i=0;for(;s<this.options.targetFrameSize;){let t=0,r=0;for(;i<Math.min(this.inputBuffer.length,(s+1)*this.options.nativeSampleRate/this.options.targetSampleRate);)t+=this.inputBuffer[i],r++,i++;e[s]=t/r,s++}this.inputBuffer=this.inputBuffer.slice(i),t.push(e)}return t},e.nativeSampleRate<16e3&&i.log.error("nativeSampleRate is too low. Should have 16000 = targetSampleRate <= nativeSampleRate"),this.inputBuffer=[]}}},26:(e,t)=>{function s(e,t,s){for(var i=0;i<s.length;i++)e.setUint8(t+i,s.charCodeAt(i))}Object.defineProperty(t,"__esModule",{value:!0}),t.encodeWAV=t.arrayBufferToBase64=t.minFramesForTargetMS=void 0,t.minFramesForTargetMS=function(e,t,s=16e3){return Math.ceil(e*s/1e3/t)},t.arrayBufferToBase64=function(e){for(var t="",s=new Uint8Array(e),i=s.byteLength,r=0;r<i;r++)t+=String.fromCharCode(s[r]);return btoa(t)},t.encodeWAV=function(e,t=3,i=16e3,r=1,o=32){var n=o/8,a=r*n,h=new ArrayBuffer(44+e.length*n),p=new DataView(h);return s(p,0,"RIFF"),p.setUint32(4,36+e.length*n,!0),s(p,8,"WAVE"),s(p,12,"fmt "),p.setUint32(16,16,!0),p.setUint16(20,t,!0),p.setUint16(22,r,!0),p.setUint32(24,i,!0),p.setUint32(28,i*a,!0),p.setUint16(32,a,!0),p.setUint16(34,o,!0),s(p,36,"data"),p.setUint32(40,e.length*n,!0),1===t?function(e,t,s){for(var i=0;i<s.length;i++,t+=2){var r=Math.max(-1,Math.min(1,s[i]));e.setInt16(t,r<0?32768*r:32767*r,!0)}}(p,44,e):function(e,t,s){for(var i=0;i<s.length;i++,t+=4)e.setFloat32(t,s[i],!0)}(p,44,e),h}}},t={};function s(i){var r=t[i];if(void 0!==r)return r.exports;var o=t[i]={exports:{}};return e[i].call(o.exports,o,o.exports,s),o.exports}(()=>{const e=s(14);class t extends AudioWorkletProcessor{constructor(t){super(),this._initialized=!1,this._stopProcessing=!1,this.init=async()=>{e.log.debug("initializing worklet"),this.resampler=new e.Resampler({nativeSampleRate:sampleRate,targetSampleRate:16e3,targetFrameSize:this.options.frameSamples}),this._initialized=!0,e.log.debug("initialized worklet")},this.options=t.processorOptions,this.init(),this.port.onmessage=t=>{t.data.message===e.Message.SpeechStop&&(this._stopProcessing=!0)}}process(t,s,i){const r=t[0][0];if(this._initialized&&r instanceof Float32Array){const t=this.resampler.process(r);for(const s of t)this.port.postMessage({message:e.Message.AudioFrame,data:s.buffer},[s.buffer])}return!this._stopProcessing}}registerProcessor("vad-helper-worklet",t)})()})(); |
+8
-0
@@ -8,2 +8,3 @@ "use strict"; | ||
| this._initialized = false; | ||
| this._stopProcessing = false; | ||
| this.init = async () => { | ||
@@ -21,2 +22,7 @@ _common_1.log.debug("initializing worklet"); | ||
| this.init(); | ||
| this.port.onmessage = (ev) => { | ||
| if (ev.data.message === _common_1.Message.SpeechStop) { | ||
| this._stopProcessing = true; | ||
| } | ||
| }; | ||
| } | ||
@@ -32,2 +38,4 @@ process(inputs, outputs, parameters) { | ||
| } | ||
| if (this._stopProcessing) | ||
| return false; | ||
| return true; | ||
@@ -34,0 +42,0 @@ } |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"worklet.js","sourceRoot":"","sources":["../src/worklet.ts"],"names":[],"mappings":";;AAAA,uCAAmD;AAMnD,MAAM,SAAU,SAAQ,qBAAqB;IAM3C,YAAY,OAAO;QACjB,KAAK,EAAE,CAAA;QAJT,iBAAY,GAAG,KAAK,CAAA;QAQpB,SAAI,GAAG,KAAK,IAAI,EAAE;YAChB,aAAG,CAAC,KAAK,CAAC,sBAAsB,CAAC,CAAA;YACjC,IAAI,CAAC,SAAS,GAAG,IAAI,mBAAS,CAAC;gBAC7B,gBAAgB,EAAE,UAAU;gBAC5B,gBAAgB,EAAE,KAAK;gBACvB,eAAe,EAAE,IAAI,CAAC,OAAO,CAAC,YAAY;aAC3C,CAAC,CAAA;YACF,IAAI,CAAC,YAAY,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@ "main": "dist/index.js", |
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
1972495
0.08%1497
0.88%