@ricky0123/vad-web
Advanced tools
Comparing version 0.0.12 to 0.0.13
@@ -10,3 +10,3 @@ "use strict"; | ||
Message["SpeechEnd"] = "SPEECH_END"; | ||
})(Message = exports.Message || (exports.Message = {})); | ||
})(Message || (exports.Message = Message = {})); | ||
//# sourceMappingURL=messages.js.map |
@@ -44,3 +44,3 @@ "use strict"; | ||
Silero.new = async (ort, modelFetcher) => { | ||
const model = new Silero(ort, modelFetcher); | ||
const model = new _a(ort, modelFetcher); | ||
await model.init(); | ||
@@ -47,0 +47,0 @@ return model; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.assetPath = void 0; | ||
const currentScript = window.document.currentScript; | ||
// nextjs@14 bundler may attempt to execute this during SSR and crash | ||
const isWeb = typeof window !== 'undefined' && typeof window.document !== 'undefined'; | ||
const currentScript = isWeb | ||
? window.document.currentScript | ||
: null; | ||
let basePath = ""; | ||
@@ -6,0 +10,0 @@ if (currentScript) { |
@@ -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 || (exports.Message = Message = {}));\n//# sourceMappingURL=messages.js.map\n\n//# sourceURL=webpack://vad/./dist/_common/messages.js?"); | ||
@@ -70,3 +70,3 @@ /***/ }), | ||
eval("\nvar _a;\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.Silero = void 0;\n// @ts-ignore\nconst logging_1 = __webpack_require__(/*! ./logging */ \"./dist/_common/logging.js\");\nclass Silero {\n constructor(ort, modelFetcher) {\n this.ort = ort;\n this.modelFetcher = modelFetcher;\n this.init = async () => {\n logging_1.log.debug(\"initializing vad\");\n const modelArrayBuffer = await this.modelFetcher();\n this._session = await this.ort.InferenceSession.create(modelArrayBuffer);\n this._sr = new this.ort.Tensor(\"int64\", [16000n]);\n this.reset_state();\n logging_1.log.debug(\"vad is initialized\");\n };\n this.reset_state = () => {\n const zeroes = Array(2 * 64).fill(0);\n this._h = new this.ort.Tensor(\"float32\", zeroes, [2, 1, 64]);\n this._c = new this.ort.Tensor(\"float32\", zeroes, [2, 1, 64]);\n };\n this.process = async (audioFrame) => {\n const t = new this.ort.Tensor(\"float32\", audioFrame, [1, audioFrame.length]);\n const inputs = {\n input: t,\n h: this._h,\n c: this._c,\n sr: this._sr,\n };\n const out = await this._session.run(inputs);\n this._h = out.hn;\n this._c = out.cn;\n const [isSpeech] = out.output.data;\n const notSpeech = 1 - isSpeech;\n return { notSpeech, isSpeech };\n };\n }\n}\nexports.Silero = Silero;\n_a = Silero;\nSilero.new = async (ort, modelFetcher) => {\n const model = new Silero(ort, modelFetcher);\n await model.init();\n return model;\n};\n//# sourceMappingURL=models.js.map\n\n//# sourceURL=webpack://vad/./dist/_common/models.js?"); | ||
eval("\nvar _a;\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.Silero = void 0;\n// @ts-ignore\nconst logging_1 = __webpack_require__(/*! ./logging */ \"./dist/_common/logging.js\");\nclass Silero {\n constructor(ort, modelFetcher) {\n this.ort = ort;\n this.modelFetcher = modelFetcher;\n this.init = async () => {\n logging_1.log.debug(\"initializing vad\");\n const modelArrayBuffer = await this.modelFetcher();\n this._session = await this.ort.InferenceSession.create(modelArrayBuffer);\n this._sr = new this.ort.Tensor(\"int64\", [16000n]);\n this.reset_state();\n logging_1.log.debug(\"vad is initialized\");\n };\n this.reset_state = () => {\n const zeroes = Array(2 * 64).fill(0);\n this._h = new this.ort.Tensor(\"float32\", zeroes, [2, 1, 64]);\n this._c = new this.ort.Tensor(\"float32\", zeroes, [2, 1, 64]);\n };\n this.process = async (audioFrame) => {\n const t = new this.ort.Tensor(\"float32\", audioFrame, [1, audioFrame.length]);\n const inputs = {\n input: t,\n h: this._h,\n c: this._c,\n sr: this._sr,\n };\n const out = await this._session.run(inputs);\n this._h = out.hn;\n this._c = out.cn;\n const [isSpeech] = out.output.data;\n const notSpeech = 1 - isSpeech;\n return { notSpeech, isSpeech };\n };\n }\n}\nexports.Silero = Silero;\n_a = Silero;\nSilero.new = async (ort, modelFetcher) => {\n const model = new _a(ort, modelFetcher);\n await model.init();\n return model;\n};\n//# sourceMappingURL=models.js.map\n\n//# sourceURL=webpack://vad/./dist/_common/models.js?"); | ||
@@ -111,6 +111,16 @@ /***/ }), | ||
eval("\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.assetPath = void 0;\nconst currentScript = window.document.currentScript;\nlet basePath = \"\";\nif (currentScript) {\n basePath = currentScript.src\n .replace(/#.*$/, \"\")\n .replace(/\\?.*$/, \"\")\n .replace(/\\/[^\\/]+$/, \"/\");\n}\nconst assetPath = (file) => {\n return basePath + file;\n};\nexports.assetPath = assetPath;\n//# sourceMappingURL=asset-path.js.map\n\n//# sourceURL=webpack://vad/./dist/asset-path.js?"); | ||
eval("\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.assetPath = void 0;\n// nextjs@14 bundler may attempt to execute this during SSR and crash\nconst isWeb = typeof window !== 'undefined' && typeof window.document !== 'undefined';\nconst currentScript = isWeb\n ? window.document.currentScript\n : null;\nlet basePath = \"\";\nif (currentScript) {\n basePath = currentScript.src\n .replace(/#.*$/, \"\")\n .replace(/\\?.*$/, \"\")\n .replace(/\\/[^\\/]+$/, \"/\");\n}\nconst assetPath = (file) => {\n return basePath + file;\n};\nexports.assetPath = assetPath;\n//# sourceMappingURL=asset-path.js.map\n\n//# sourceURL=webpack://vad/./dist/asset-path.js?"); | ||
/***/ }), | ||
/***/ "./dist/default-model-fetcher.js": | ||
/*!***************************************!*\ | ||
!*** ./dist/default-model-fetcher.js ***! | ||
\***************************************/ | ||
/***/ ((__unused_webpack_module, exports) => { | ||
eval("\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.defaultModelFetcher = void 0;\nconst defaultModelFetcher = (path) => {\n return fetch(path)\n .then(model => model.arrayBuffer());\n};\nexports.defaultModelFetcher = defaultModelFetcher;\n//# sourceMappingURL=default-model-fetcher.js.map\n\n//# sourceURL=webpack://vad/./dist/default-model-fetcher.js?"); | ||
/***/ }), | ||
/***/ "./dist/index.js": | ||
@@ -122,16 +132,6 @@ /*!***********************!*\ | ||
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.defaultRealTimeVADOptions = exports.AudioNodeVAD = exports.MicVAD = exports.NonRealTimeVAD = exports.Message = exports.FrameProcessor = exports.utils = void 0;\nconst ort = __importStar(__webpack_require__(/*! onnxruntime-web */ \"onnxruntime-web\"));\nconst _common_1 = __webpack_require__(/*! ./_common */ \"./dist/_common/index.js\");\nObject.defineProperty(exports, \"FrameProcessor\", ({ enumerable: true, get: function () { return _common_1.FrameProcessor; } }));\nObject.defineProperty(exports, \"Message\", ({ enumerable: true, get: function () { return _common_1.Message; } }));\nconst model_fetcher_1 = __webpack_require__(/*! ./model-fetcher */ \"./dist/model-fetcher.js\");\nconst utils_1 = __webpack_require__(/*! ./utils */ \"./dist/utils.js\");\nclass NonRealTimeVAD extends _common_1.PlatformAgnosticNonRealTimeVAD {\n static async new(options = {}) {\n return await this._new(model_fetcher_1.modelFetcher, ort, options);\n }\n}\nexports.NonRealTimeVAD = NonRealTimeVAD;\nexports.utils = { audioFileToArray: utils_1.audioFileToArray, ..._common_1.utils };\nvar real_time_vad_1 = __webpack_require__(/*! ./real-time-vad */ \"./dist/real-time-vad.js\");\nObject.defineProperty(exports, \"MicVAD\", ({ enumerable: true, get: function () { return real_time_vad_1.MicVAD; } }));\nObject.defineProperty(exports, \"AudioNodeVAD\", ({ enumerable: true, get: function () { return real_time_vad_1.AudioNodeVAD; } }));\nObject.defineProperty(exports, \"defaultRealTimeVADOptions\", ({ enumerable: true, get: function () { return real_time_vad_1.defaultRealTimeVADOptions; } }));\n//# sourceMappingURL=index.js.map\n\n//# sourceURL=webpack://vad/./dist/index.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.defaultRealTimeVADOptions = exports.AudioNodeVAD = exports.MicVAD = exports.NonRealTimeVAD = exports.Message = exports.FrameProcessor = exports.utils = exports.defaultNonRealTimeVADOptions = void 0;\nconst ort = __importStar(__webpack_require__(/*! onnxruntime-web */ \"onnxruntime-web\"));\nconst _common_1 = __webpack_require__(/*! ./_common */ \"./dist/_common/index.js\");\nObject.defineProperty(exports, \"FrameProcessor\", ({ enumerable: true, get: function () { return _common_1.FrameProcessor; } }));\nObject.defineProperty(exports, \"Message\", ({ enumerable: true, get: function () { return _common_1.Message; } }));\nconst utils_1 = __webpack_require__(/*! ./utils */ \"./dist/utils.js\");\nconst default_model_fetcher_1 = __webpack_require__(/*! ./default-model-fetcher */ \"./dist/default-model-fetcher.js\");\nconst asset_path_1 = __webpack_require__(/*! ./asset-path */ \"./dist/asset-path.js\");\nexports.defaultNonRealTimeVADOptions = {\n modelURL: (0, asset_path_1.assetPath)(\"silero_vad.onnx\"),\n modelFetcher: default_model_fetcher_1.defaultModelFetcher\n};\nclass NonRealTimeVAD extends _common_1.PlatformAgnosticNonRealTimeVAD {\n static async new(options = {}) {\n const { modelURL, modelFetcher } = { ...exports.defaultNonRealTimeVADOptions, ...options };\n return await this._new(() => modelFetcher(modelURL), ort, options);\n }\n}\nexports.NonRealTimeVAD = NonRealTimeVAD;\nexports.utils = { audioFileToArray: utils_1.audioFileToArray, ..._common_1.utils };\nvar real_time_vad_1 = __webpack_require__(/*! ./real-time-vad */ \"./dist/real-time-vad.js\");\nObject.defineProperty(exports, \"MicVAD\", ({ enumerable: true, get: function () { return real_time_vad_1.MicVAD; } }));\nObject.defineProperty(exports, \"AudioNodeVAD\", ({ enumerable: true, get: function () { return real_time_vad_1.AudioNodeVAD; } }));\nObject.defineProperty(exports, \"defaultRealTimeVADOptions\", ({ enumerable: true, get: function () { return real_time_vad_1.defaultRealTimeVADOptions; } }));\n//# sourceMappingURL=index.js.map\n\n//# sourceURL=webpack://vad/./dist/index.js?"); | ||
/***/ }), | ||
/***/ "./dist/model-fetcher.js": | ||
/*!*******************************!*\ | ||
!*** ./dist/model-fetcher.js ***! | ||
\*******************************/ | ||
/***/ ((__unused_webpack_module, exports, __webpack_require__) => { | ||
eval("\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.modelFetcher = void 0;\nconst asset_path_1 = __webpack_require__(/*! ./asset-path */ \"./dist/asset-path.js\");\nconst modelFetcher = async () => {\n const modelURL = (0, asset_path_1.assetPath)(\"silero_vad.onnx\");\n return await fetch(modelURL).then((r) => r.arrayBuffer());\n};\nexports.modelFetcher = modelFetcher;\n//# sourceMappingURL=model-fetcher.js.map\n\n//# sourceURL=webpack://vad/./dist/model-fetcher.js?"); | ||
/***/ }), | ||
/***/ "./dist/real-time-vad.js": | ||
@@ -143,3 +143,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 (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 asset_path_1 = __webpack_require__(/*! ./asset-path */ \"./dist/asset-path.js\");\nconst default_model_fetcher_1 = __webpack_require__(/*! ./default-model-fetcher */ \"./dist/default-model-fetcher.js\");\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: (0, asset_path_1.assetPath)(\"vad.worklet.bundle.min.js\"),\n modelURL: (0, asset_path_1.assetPath)(\"silero_vad.onnx\"),\n modelFetcher: default_model_fetcher_1.defaultModelFetcher,\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 (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, () => this.options.modelFetcher(this.options.modelURL));\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?"); | ||
@@ -146,0 +146,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},(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,function(e){e.AudioFrame="AUDIO_FRAME",e.SpeechStart="SPEECH_START",e.VADMisfire="VAD_MISFIRE",e.SpeechEnd="SPEECH_END"}(s||(t.Message=s={}))},260:(e,t,s)=>{var i;Object.defineProperty(t,"__esModule",{value:!0}),t.Silero=void 0;const o=s(842);class r{constructor(e,t){this.ort=e,this.modelFetcher=t,this.init=async()=>{o.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(),o.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,i=r,r.new=async(e,t)=>{const s=new i(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="undefined"!=typeof window&&void 0!==window.document?window.document.currentScript:null;let i="";s&&(i=s.src.replace(/#.*$/,"").replace(/\?.*$/,"").replace(/\/[^\/]+$/,"/")),t.assetPath=e=>i+e},973:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.defaultModelFetcher=void 0,t.defaultModelFetcher=e=>fetch(e).then((e=>e.arrayBuffer()))},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=t.defaultNonRealTimeVADOptions=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(787),h=s(973),d=s(485);t.defaultNonRealTimeVADOptions={modelURL:(0,d.assetPath)("silero_vad.onnx"),modelFetcher:h.defaultModelFetcher};class l extends a.PlatformAgnosticNonRealTimeVAD{static async new(e={}){const{modelURL:s,modelFetcher:i}={...t.defaultNonRealTimeVADOptions,...e};return await this._new((()=>i(s)),n,e)}}t.NonRealTimeVAD=l,t.utils={audioFileToArray:c.audioFileToArray,...a.utils};var u=s(746);Object.defineProperty(t,"MicVAD",{enumerable:!0,get:function(){return u.MicVAD}}),Object.defineProperty(t,"AudioNodeVAD",{enumerable:!0,get:function(){return u.AudioNodeVAD}}),Object.defineProperty(t,"defaultRealTimeVADOptions",{enumerable:!0,get:function(){return u.defaultRealTimeVADOptions}})},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(485),h=s(973);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,c.assetPath)("vad.worklet.bundle.min.js"),modelURL:(0,c.assetPath)("silero_vad.onnx"),modelFetcher:h.defaultModelFetcher,stream:void 0};class d{static async new(e={}){const s=new d({...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},(0,a.validateOptions)(e)}}t.MicVAD=d;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,(()=>this.options.modelFetcher(this.options.modelURL)));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)})())); |
import { PlatformAgnosticNonRealTimeVAD, FrameProcessor, FrameProcessorOptions, Message, NonRealTimeVADOptions } from "./_common"; | ||
import { audioFileToArray } from "./utils"; | ||
export interface NonRealTimeVADOptionsWeb extends NonRealTimeVADOptions { | ||
modelURL: string; | ||
modelFetcher: (path: string) => Promise<ArrayBuffer>; | ||
} | ||
export declare const defaultNonRealTimeVADOptions: { | ||
modelURL: string; | ||
modelFetcher: (path: string) => Promise<ArrayBuffer>; | ||
}; | ||
declare class NonRealTimeVAD extends PlatformAgnosticNonRealTimeVAD { | ||
static new(options?: Partial<NonRealTimeVADOptions>): Promise<NonRealTimeVAD>; | ||
static new(options?: Partial<NonRealTimeVADOptionsWeb>): Promise<NonRealTimeVAD>; | ||
} | ||
@@ -6,0 +14,0 @@ export declare const utils: { |
@@ -26,3 +26,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.defaultRealTimeVADOptions = exports.AudioNodeVAD = exports.MicVAD = exports.NonRealTimeVAD = exports.Message = exports.FrameProcessor = exports.utils = void 0; | ||
exports.defaultRealTimeVADOptions = exports.AudioNodeVAD = exports.MicVAD = exports.NonRealTimeVAD = exports.Message = exports.FrameProcessor = exports.utils = exports.defaultNonRealTimeVADOptions = void 0; | ||
const ort = __importStar(require("onnxruntime-web")); | ||
@@ -32,7 +32,13 @@ const _common_1 = require("./_common"); | ||
Object.defineProperty(exports, "Message", { enumerable: true, get: function () { return _common_1.Message; } }); | ||
const model_fetcher_1 = require("./model-fetcher"); | ||
const utils_1 = require("./utils"); | ||
const default_model_fetcher_1 = require("./default-model-fetcher"); | ||
const asset_path_1 = require("./asset-path"); | ||
exports.defaultNonRealTimeVADOptions = { | ||
modelURL: (0, asset_path_1.assetPath)("silero_vad.onnx"), | ||
modelFetcher: default_model_fetcher_1.defaultModelFetcher | ||
}; | ||
class NonRealTimeVAD extends _common_1.PlatformAgnosticNonRealTimeVAD { | ||
static async new(options = {}) { | ||
return await this._new(model_fetcher_1.modelFetcher, ort, options); | ||
const { modelURL, modelFetcher } = { ...exports.defaultNonRealTimeVADOptions, ...options }; | ||
return await this._new(() => modelFetcher(modelURL), ort, options); | ||
} | ||
@@ -39,0 +45,0 @@ } |
@@ -23,10 +23,13 @@ import { SpeechProbabilities, FrameProcessor, FrameProcessorOptions } from "./_common"; | ||
type AudioConstraints = Omit<MediaTrackConstraints, "channelCount" | "echoCancellation" | "autoGainControl" | "noiseSuppression">; | ||
interface RealTimeVADOptionsWithoutStream extends FrameProcessorOptions, RealTimeVADCallbacks { | ||
type AssetOptions = { | ||
workletURL: string; | ||
modelURL: string; | ||
modelFetcher: (path: string) => Promise<ArrayBuffer>; | ||
}; | ||
interface RealTimeVADOptionsWithoutStream extends FrameProcessorOptions, RealTimeVADCallbacks, AssetOptions { | ||
additionalAudioConstraints?: AudioConstraints; | ||
workletURL: string; | ||
stream: undefined; | ||
} | ||
interface RealTimeVADOptionsWithStream extends FrameProcessorOptions, RealTimeVADCallbacks { | ||
interface RealTimeVADOptionsWithStream extends FrameProcessorOptions, RealTimeVADCallbacks, AssetOptions { | ||
stream: MediaStream; | ||
workletURL: string; | ||
} | ||
@@ -33,0 +36,0 @@ export type RealTimeVADOptions = RealTimeVADOptionsWithStream | RealTimeVADOptionsWithoutStream; |
@@ -29,7 +29,4 @@ "use strict"; | ||
const _common_1 = require("./_common"); | ||
const model_fetcher_1 = require("./model-fetcher"); | ||
const asset_path_1 = require("./asset-path"); | ||
const _getWorkletURL = () => { | ||
return (0, asset_path_1.assetPath)("vad.worklet.bundle.min.js"); | ||
}; | ||
const default_model_fetcher_1 = require("./default-model-fetcher"); | ||
exports.defaultRealTimeVADOptions = { | ||
@@ -47,3 +44,5 @@ ..._common_1.defaultFrameProcessorOptions, | ||
}, | ||
workletURL: _getWorkletURL(), | ||
workletURL: (0, asset_path_1.assetPath)("vad.worklet.bundle.min.js"), | ||
modelURL: (0, asset_path_1.assetPath)("silero_vad.onnx"), | ||
modelFetcher: default_model_fetcher_1.defaultModelFetcher, | ||
stream: undefined, | ||
@@ -141,3 +140,3 @@ }; | ||
this.entryNode = vadNode; | ||
const model = await _common_1.Silero.new(ort, model_fetcher_1.modelFetcher); | ||
const model = await _common_1.Silero.new(ort, () => this.options.modelFetcher(this.options.modelURL)); | ||
this.frameProcessor = new _common_1.FrameProcessor(model.process, model.reset_state, { | ||
@@ -144,0 +143,0 @@ frameSamples: this.options.frameSamples, |
@@ -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://@ricky0123/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 || (exports.Message = Message = {}));\n//# sourceMappingURL=messages.js.map\n\n//# sourceURL=webpack://@ricky0123/vad-web/./dist/_common/messages.js?"); | ||
@@ -60,3 +60,3 @@ /***/ }), | ||
eval("\nvar _a;\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.Silero = void 0;\n// @ts-ignore\nconst logging_1 = __webpack_require__(/*! ./logging */ \"./dist/_common/logging.js\");\nclass Silero {\n constructor(ort, modelFetcher) {\n this.ort = ort;\n this.modelFetcher = modelFetcher;\n this.init = async () => {\n logging_1.log.debug(\"initializing vad\");\n const modelArrayBuffer = await this.modelFetcher();\n this._session = await this.ort.InferenceSession.create(modelArrayBuffer);\n this._sr = new this.ort.Tensor(\"int64\", [16000n]);\n this.reset_state();\n logging_1.log.debug(\"vad is initialized\");\n };\n this.reset_state = () => {\n const zeroes = Array(2 * 64).fill(0);\n this._h = new this.ort.Tensor(\"float32\", zeroes, [2, 1, 64]);\n this._c = new this.ort.Tensor(\"float32\", zeroes, [2, 1, 64]);\n };\n this.process = async (audioFrame) => {\n const t = new this.ort.Tensor(\"float32\", audioFrame, [1, audioFrame.length]);\n const inputs = {\n input: t,\n h: this._h,\n c: this._c,\n sr: this._sr,\n };\n const out = await this._session.run(inputs);\n this._h = out.hn;\n this._c = out.cn;\n const [isSpeech] = out.output.data;\n const notSpeech = 1 - isSpeech;\n return { notSpeech, isSpeech };\n };\n }\n}\nexports.Silero = Silero;\n_a = Silero;\nSilero.new = async (ort, modelFetcher) => {\n const model = new Silero(ort, modelFetcher);\n await model.init();\n return model;\n};\n//# sourceMappingURL=models.js.map\n\n//# sourceURL=webpack://@ricky0123/vad-web/./dist/_common/models.js?"); | ||
eval("\nvar _a;\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.Silero = void 0;\n// @ts-ignore\nconst logging_1 = __webpack_require__(/*! ./logging */ \"./dist/_common/logging.js\");\nclass Silero {\n constructor(ort, modelFetcher) {\n this.ort = ort;\n this.modelFetcher = modelFetcher;\n this.init = async () => {\n logging_1.log.debug(\"initializing vad\");\n const modelArrayBuffer = await this.modelFetcher();\n this._session = await this.ort.InferenceSession.create(modelArrayBuffer);\n this._sr = new this.ort.Tensor(\"int64\", [16000n]);\n this.reset_state();\n logging_1.log.debug(\"vad is initialized\");\n };\n this.reset_state = () => {\n const zeroes = Array(2 * 64).fill(0);\n this._h = new this.ort.Tensor(\"float32\", zeroes, [2, 1, 64]);\n this._c = new this.ort.Tensor(\"float32\", zeroes, [2, 1, 64]);\n };\n this.process = async (audioFrame) => {\n const t = new this.ort.Tensor(\"float32\", audioFrame, [1, audioFrame.length]);\n const inputs = {\n input: t,\n h: this._h,\n c: this._c,\n sr: this._sr,\n };\n const out = await this._session.run(inputs);\n this._h = out.hn;\n this._c = out.cn;\n const [isSpeech] = out.output.data;\n const notSpeech = 1 - isSpeech;\n return { notSpeech, isSpeech };\n };\n }\n}\nexports.Silero = Silero;\n_a = Silero;\nSilero.new = async (ort, modelFetcher) => {\n const model = new _a(ort, modelFetcher);\n await model.init();\n return model;\n};\n//# sourceMappingURL=models.js.map\n\n//# sourceURL=webpack://@ricky0123/vad-web/./dist/_common/models.js?"); | ||
@@ -63,0 +63,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,function(e){e.AudioFrame="AUDIO_FRAME",e.SpeechStart="SPEECH_START",e.VADMisfire="VAD_MISFIRE",e.SpeechEnd="SPEECH_END"}(s||(t.Message=s={}))},260:(e,t,s)=>{var i;Object.defineProperty(t,"__esModule",{value:!0}),t.Silero=void 0;const r=s(842);class o{constructor(e,t){this.ort=e,this.modelFetcher=t,this.init=async()=>{r.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(),r.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,i=o,o.new=async(e,t)=>{const s=new i(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)})()})(); |
@@ -15,3 +15,3 @@ { | ||
"homepage": "https://github.com/ricky0123/vad", | ||
"version": "0.0.12", | ||
"version": "0.0.13", | ||
"license": "ISC", | ||
@@ -18,0 +18,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
1972658
1498