@livekit/agents-plugin-silero 0.4.5 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -0
- package/dist/index.cjs +31 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +5 -4
- package/dist/index.js.map +1 -1
- package/dist/onnx_model.cjs +95 -0
- package/dist/onnx_model.cjs.map +1 -0
- package/dist/onnx_model.d.ts.map +1 -1
- package/dist/onnx_model.js +64 -65
- package/dist/onnx_model.js.map +1 -1
- package/dist/onnxruntime.d.cjs +2 -0
- package/dist/onnxruntime.d.cjs.map +1 -0
- package/dist/onnxruntime.d.js +1 -0
- package/dist/onnxruntime.d.js.map +1 -0
- package/dist/vad.cjs +285 -0
- package/dist/vad.cjs.map +1 -0
- package/dist/vad.d.ts +1 -1
- package/dist/vad.d.ts.map +1 -1
- package/dist/vad.js +261 -244
- package/dist/vad.js.map +1 -1
- package/package.json +23 -7
- package/src/onnx_model.ts +2 -1
- package/src/vad.ts +4 -3
- package/.gitattributes +0 -5
- package/.turbo/turbo-build.log +0 -4
- package/CHANGELOG.md +0 -55
- package/api-extractor.json +0 -20
- package/tsconfig.json +0 -15
- package/tsconfig.tsbuildinfo +0 -1
package/dist/vad.cjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/vad.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport {\n ExpFilter,\n VADEventType,\n VADStream as baseStream,\n VAD as baseVAD,\n log,\n mergeFrames,\n} from '@livekit/agents';\nimport { AudioFrame, AudioResampler, AudioResamplerQuality } from '@livekit/rtc-node';\nimport type { InferenceSession } from 'onnxruntime-node';\nimport type { SampleRate } from './onnx_model.js';\nimport { OnnxModel, newInferenceSession } from './onnx_model.js';\n\nconst SLOW_INFERENCE_THRESHOLD = 200; // late by 200ms\n\nexport interface VADOptions {\n /** Minimum duration of speech to start a new speech chunk */\n minSpeechDuration: number;\n /** At the end of each speech, wait this duration before ending the speech */\n minSilenceDuration: number;\n /** Duration of padding to add to the beginning of each speech chunk */\n prefixPaddingDuration: number;\n /** Maximum duration of speech to keep in the buffer */\n maxBufferedSpeech: number;\n /** Maximum duration of speech to keep in the buffer*/\n activationThreshold: number;\n /** Sample rate for the inference (only 8KHz and 16KHz are supported) */\n sampleRate: SampleRate;\n /** Force the use of CPU for inference */\n forceCPU: boolean;\n}\n\nconst defaultVADOptions: VADOptions = {\n minSpeechDuration: 50,\n minSilenceDuration: 250,\n prefixPaddingDuration: 500,\n maxBufferedSpeech: 60000,\n activationThreshold: 0.5,\n sampleRate: 16000,\n forceCPU: true,\n};\n\nexport class VAD extends baseVAD {\n #session: InferenceSession;\n #opts: VADOptions;\n\n constructor(session: InferenceSession, opts: VADOptions) {\n super({ updateInterval: 32 });\n this.#session = session;\n this.#opts = opts;\n }\n\n /**\n * Load and initialize the Silero VAD model.\n *\n * This method loads the ONNX model and prepares it for inference. When options are not provided,\n * sane defaults are used.\n *\n * @remarks\n * This method may take time to load the model into memory.\n * It is recommended to call this method inside your prewarm mechanism.\n *\n * @example\n * ```ts\n * export default defineAgent({\n * prewarm: async (proc: JobProcess) => {\n * proc.userData.vad = await VAD.load();\n * },\n * entry: async (ctx: JobContext) => {\n * const vad = ctx.proc.userData.vad! as VAD;\n * // the rest of your agent logic\n * },\n * });\n * ```\n *\n * @param options -\n * @returns Promise\\<{@link VAD}\\>: An instance of the VAD class ready for streaming.\n */\n static async load(opts: Partial<VADOptions> = {}): Promise<VAD> {\n const mergedOpts: VADOptions = { ...defaultVADOptions, ...opts };\n const session = await newInferenceSession(mergedOpts.forceCPU);\n return new VAD(session, mergedOpts);\n }\n\n stream(): VADStream {\n return new VADStream(this.#opts, new OnnxModel(this.#session, this.#opts.sampleRate));\n }\n}\n\nexport class VADStream extends baseStream {\n #opts: VADOptions;\n #model: OnnxModel;\n #task: Promise<void>;\n #expFilter = new ExpFilter(0.35);\n #extraInferenceTime = 0;\n #logger = log();\n\n constructor(opts: VADOptions, model: OnnxModel) {\n super();\n this.#opts = opts;\n this.#model = model;\n\n this.#task = new Promise(async () => {\n let inferenceData = new Float32Array(this.#model.windowSizeSamples);\n\n // a copy is exposed to the user in END_OF_SPEECH\n let speechBuffer: Int16Array | null = null;\n let speechBufferMaxReached = false;\n let speechBufferIndex = 0;\n\n // \"pub\" means public, these values are exposed to the users through events\n let pubSpeaking = false;\n let pubSpeechDuration = 0;\n let pubSilenceDuration = 0;\n let pubCurrentSample = 0;\n let pubTimestamp = 0;\n let pubSampleRate = 0;\n let pubPrefixPaddingSamples = 0; // size in samples of padding data\n\n let speechThresholdDuration = 0;\n let silenceThresholdDuration = 0;\n\n let inputFrames = [];\n let inferenceFrames: AudioFrame[] = [];\n let resampler: AudioResampler | null = null;\n\n // used to avoid drift when the sampleRate ratio is not an integer\n let inputCopyRemainingFrac = 0.0;\n\n for await (const frame of this.input) {\n if (typeof frame === 'symbol') {\n continue; // ignore flush sentinel for now\n }\n\n if (!pubSampleRate || !speechBuffer) {\n pubSampleRate = frame.sampleRate;\n pubPrefixPaddingSamples = Math.trunc(\n (this.#opts.prefixPaddingDuration * pubSampleRate) / 1000,\n );\n\n speechBuffer = new Int16Array(\n this.#opts.maxBufferedSpeech * pubSampleRate + pubPrefixPaddingSamples,\n );\n\n if (this.#opts.sampleRate !== pubSampleRate) {\n // resampling needed: the input sample rate isn't the same as the model's\n // sample rate used for inference\n resampler = new AudioResampler(\n pubSampleRate,\n this.#opts.sampleRate,\n 1,\n AudioResamplerQuality.QUICK, // VAD doesn't need high quality\n );\n }\n } else if (frame.sampleRate !== pubSampleRate) {\n this.#logger.error('a frame with a different sample rate was already published');\n continue;\n }\n\n inputFrames.push(frame);\n if (resampler) {\n inferenceFrames.push(...resampler.push(frame));\n } else {\n inferenceFrames.push(frame);\n }\n\n while (true) {\n const startTime = process.hrtime.bigint();\n const availableInferenceSamples = inferenceFrames\n .map((x) => x.samplesPerChannel)\n .reduce((acc, x) => acc + x, 0);\n\n if (availableInferenceSamples < this.#model.windowSizeSamples) {\n break; // not enough samples to run inference\n }\n\n const inputFrame = mergeFrames(inputFrames);\n const inferenceFrame = mergeFrames(inferenceFrames);\n\n // convert data to f32\n inferenceData = Float32Array.from(\n inferenceFrame.data.subarray(0, this.#model.windowSizeSamples),\n (x) => x / 32767,\n );\n\n const p = await this.#model\n .run(inferenceData)\n .then((data) => this.#expFilter.apply(1, data));\n\n const windowDuration = (this.#model.windowSizeSamples / this.#opts.sampleRate) * 1000;\n pubCurrentSample += this.#model.windowSizeSamples;\n pubTimestamp += windowDuration;\n const resamplingRatio = pubSampleRate / this.#model.sampleRate;\n const toCopy = this.#model.windowSizeSamples * resamplingRatio + inputCopyRemainingFrac;\n const toCopyInt = Math.trunc(toCopy);\n inputCopyRemainingFrac = toCopy - toCopyInt;\n\n // copy the inference window to the speech buffer\n const availableSpace = speechBuffer.length - speechBufferIndex;\n const toCopyBuffer = Math.min(this.#model.windowSizeSamples, availableSpace);\n if (toCopyBuffer > 0) {\n speechBuffer.set(inputFrame.data.subarray(0, toCopyBuffer), speechBufferIndex);\n speechBufferIndex += toCopyBuffer;\n } else if (!speechBufferMaxReached) {\n speechBufferMaxReached = true;\n this.#logger.warn(\n 'maxBufferedSpeech reached, ignoring further data for the current speech input',\n );\n }\n\n const inferenceDuration = Number((process.hrtime.bigint() - startTime) / BigInt(1000000));\n this.#extraInferenceTime = Math.max(\n 0,\n this.#extraInferenceTime + inferenceDuration - windowDuration,\n );\n if (this.#extraInferenceTime > SLOW_INFERENCE_THRESHOLD) {\n this.#logger\n .child({ delay: this.#extraInferenceTime })\n .warn('inference is slower than realtime');\n }\n\n if (pubSpeaking) {\n pubSpeechDuration += inferenceDuration;\n } else {\n pubSilenceDuration += inferenceDuration;\n }\n\n this.queue.put({\n type: VADEventType.INFERENCE_DONE,\n samplesIndex: pubCurrentSample,\n timestamp: pubTimestamp,\n silenceDuration: pubSilenceDuration,\n speechDuration: pubSpeechDuration,\n probability: p,\n inferenceDuration,\n frames: [\n new AudioFrame(inputFrame.data.subarray(0, toCopyInt), pubSampleRate, 1, toCopyInt),\n ],\n speaking: pubSpeaking,\n });\n\n const resetWriteCursor = () => {\n if (!speechBuffer) throw new Error('speechBuffer is empty');\n if (speechBufferIndex <= pubPrefixPaddingSamples) {\n return;\n }\n\n const paddingData = speechBuffer.subarray(\n speechBufferIndex - pubPrefixPaddingSamples,\n speechBufferIndex,\n );\n speechBuffer.set(paddingData, 0);\n speechBufferIndex = pubPrefixPaddingSamples;\n speechBufferMaxReached = false;\n };\n\n const copySpeechBuffer = (): AudioFrame => {\n if (!speechBuffer) throw new Error('speechBuffer is empty');\n return new AudioFrame(\n speechBuffer.subarray(0, speechBufferIndex),\n pubSampleRate,\n 1,\n speechBufferIndex,\n );\n };\n\n if (p > this.#opts.activationThreshold) {\n speechThresholdDuration += windowDuration;\n silenceThresholdDuration = 0;\n if (!pubSpeaking && speechThresholdDuration >= this.#opts.minSpeechDuration) {\n pubSpeaking = true;\n pubSilenceDuration = 0;\n pubSpeechDuration = speechThresholdDuration;\n\n this.queue.put({\n type: VADEventType.START_OF_SPEECH,\n samplesIndex: pubCurrentSample,\n timestamp: pubTimestamp,\n silenceDuration: pubSilenceDuration,\n speechDuration: pubSpeechDuration,\n probability: p,\n inferenceDuration,\n frames: [copySpeechBuffer()],\n speaking: pubSpeaking,\n });\n }\n } else {\n silenceThresholdDuration += windowDuration;\n speechThresholdDuration = 0;\n\n if (!pubSpeaking) {\n resetWriteCursor();\n }\n\n if (pubSpeaking && silenceThresholdDuration > this.#opts.minSilenceDuration) {\n pubSpeaking = false;\n pubSpeechDuration = 0;\n pubSilenceDuration = silenceThresholdDuration;\n\n this.queue.put({\n type: VADEventType.END_OF_SPEECH,\n samplesIndex: pubCurrentSample,\n timestamp: pubTimestamp,\n silenceDuration: pubSilenceDuration,\n speechDuration: pubSpeechDuration,\n probability: p,\n inferenceDuration,\n frames: [copySpeechBuffer()],\n speaking: pubSpeaking,\n });\n\n resetWriteCursor();\n }\n }\n\n inputFrames = [];\n inferenceFrames = [];\n\n if (inputFrame.data.length > toCopyInt) {\n const data = inputFrame.data.subarray(toCopyInt);\n inputFrames.push(new AudioFrame(data, pubSampleRate, 1, Math.trunc(data.length / 2)));\n }\n if (inferenceFrame.data.length > this.#model.windowSizeSamples) {\n const data = inferenceFrame.data.subarray(this.#model.windowSizeSamples);\n inferenceFrames.push(\n new AudioFrame(data, this.#opts.sampleRate, 1, Math.trunc(data.length / 2)),\n );\n }\n }\n }\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,oBAOO;AACP,sBAAkE;AAGlE,wBAA+C;AAE/C,MAAM,2BAA2B;AAmBjC,MAAM,oBAAgC;AAAA,EACpC,mBAAmB;AAAA,EACnB,oBAAoB;AAAA,EACpB,uBAAuB;AAAA,EACvB,mBAAmB;AAAA,EACnB,qBAAqB;AAAA,EACrB,YAAY;AAAA,EACZ,UAAU;AACZ;AAEO,MAAM,YAAY,cAAAA,IAAQ;AAAA,EAC/B;AAAA,EACA;AAAA,EAEA,YAAY,SAA2B,MAAkB;AACvD,UAAM,EAAE,gBAAgB,GAAG,CAAC;AAC5B,SAAK,WAAW;AAChB,SAAK,QAAQ;AAAA,EACf;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EA4BA,aAAa,KAAK,OAA4B,CAAC,GAAiB;AAC9D,UAAM,aAAyB,EAAE,GAAG,mBAAmB,GAAG,KAAK;AAC/D,UAAM,UAAU,UAAM,uCAAoB,WAAW,QAAQ;AAC7D,WAAO,IAAI,IAAI,SAAS,UAAU;AAAA,EACpC;AAAA,EAEA,SAAoB;AACl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,KAAK,SAAS,CAAC,CAAC;AAAA,YAC5E;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH;AACF;","names":["baseVAD","baseStream"]}
|
package/dist/vad.d.ts
CHANGED
|
@@ -48,7 +48,7 @@ export declare class VAD extends baseVAD {
|
|
|
48
48
|
* @param options -
|
|
49
49
|
* @returns Promise\<{@link VAD}\>: An instance of the VAD class ready for streaming.
|
|
50
50
|
*/
|
|
51
|
-
static load(opts?: VADOptions): Promise<VAD>;
|
|
51
|
+
static load(opts?: Partial<VADOptions>): Promise<VAD>;
|
|
52
52
|
stream(): VADStream;
|
|
53
53
|
}
|
|
54
54
|
export declare class VADStream extends baseStream {
|
package/dist/vad.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vad.d.ts","sourceRoot":"","sources":["../src/vad.ts"],"names":[],"mappings":";AAGA,OAAO,EAGL,SAAS,IAAI,UAAU,EACvB,GAAG,IAAI,OAAO,EAGf,MAAM,iBAAiB,CAAC;AAEzB,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACzD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,EAAE,SAAS,EAAuB,MAAM,iBAAiB,CAAC;AAIjE,MAAM,WAAW,UAAU;IACzB,6DAA6D;IAC7D,iBAAiB,EAAE,MAAM,CAAC;IAC1B,6EAA6E;IAC7E,kBAAkB,EAAE,MAAM,CAAC;IAC3B,uEAAuE;IACvE,qBAAqB,EAAE,MAAM,CAAC;IAC9B,uDAAuD;IACvD,iBAAiB,EAAE,MAAM,CAAC;IAC1B,sDAAsD;IACtD,mBAAmB,EAAE,MAAM,CAAC;IAC5B,wEAAwE;IACxE,UAAU,EAAE,UAAU,CAAC;IACvB,yCAAyC;IACzC,QAAQ,EAAE,OAAO,CAAC;CACnB;AAYD,qBAAa,GAAI,SAAQ,OAAO;;gBAIlB,OAAO,EAA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
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import {
|
|
2
|
+
ExpFilter,
|
|
3
|
+
VADEventType,
|
|
4
|
+
VADStream as baseStream,
|
|
5
|
+
VAD as baseVAD,
|
|
6
|
+
log,
|
|
7
|
+
mergeFrames
|
|
8
|
+
} from "@livekit/agents";
|
|
9
|
+
import { AudioFrame, AudioResampler, AudioResamplerQuality } from "@livekit/rtc-node";
|
|
10
|
+
import { OnnxModel, newInferenceSession } from "./onnx_model.js";
|
|
11
|
+
const SLOW_INFERENCE_THRESHOLD = 200;
|
|
8
12
|
const defaultVADOptions = {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
minSpeechDuration: 50,
|
|
14
|
+
minSilenceDuration: 250,
|
|
15
|
+
prefixPaddingDuration: 500,
|
|
16
|
+
maxBufferedSpeech: 6e4,
|
|
17
|
+
activationThreshold: 0.5,
|
|
18
|
+
sampleRate: 16e3,
|
|
19
|
+
forceCPU: true
|
|
16
20
|
};
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
21
|
+
class VAD extends baseVAD {
|
|
22
|
+
#session;
|
|
23
|
+
#opts;
|
|
24
|
+
constructor(session, opts) {
|
|
25
|
+
super({ updateInterval: 32 });
|
|
26
|
+
this.#session = session;
|
|
27
|
+
this.#opts = opts;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Load and initialize the Silero VAD model.
|
|
31
|
+
*
|
|
32
|
+
* This method loads the ONNX model and prepares it for inference. When options are not provided,
|
|
33
|
+
* sane defaults are used.
|
|
34
|
+
*
|
|
35
|
+
* @remarks
|
|
36
|
+
* This method may take time to load the model into memory.
|
|
37
|
+
* It is recommended to call this method inside your prewarm mechanism.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* export default defineAgent({
|
|
42
|
+
* prewarm: async (proc: JobProcess) => {
|
|
43
|
+
* proc.userData.vad = await VAD.load();
|
|
44
|
+
* },
|
|
45
|
+
* entry: async (ctx: JobContext) => {
|
|
46
|
+
* const vad = ctx.proc.userData.vad! as VAD;
|
|
47
|
+
* // the rest of your agent logic
|
|
48
|
+
* },
|
|
49
|
+
* });
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @param options -
|
|
53
|
+
* @returns Promise\<{@link VAD}\>: An instance of the VAD class ready for streaming.
|
|
54
|
+
*/
|
|
55
|
+
static async load(opts = {}) {
|
|
56
|
+
const mergedOpts = { ...defaultVADOptions, ...opts };
|
|
57
|
+
const session = await newInferenceSession(mergedOpts.forceCPU);
|
|
58
|
+
return new VAD(session, mergedOpts);
|
|
59
|
+
}
|
|
60
|
+
stream() {
|
|
61
|
+
return new VADStream(this.#opts, new OnnxModel(this.#session, this.#opts.sampleRate));
|
|
62
|
+
}
|
|
58
63
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
}
|
|
181
|
-
const paddingData = speechBuffer.subarray(speechBufferIndex - pubPrefixPaddingSamples, speechBufferIndex);
|
|
182
|
-
speechBuffer.set(paddingData, 0);
|
|
183
|
-
speechBufferIndex = pubPrefixPaddingSamples;
|
|
184
|
-
speechBufferMaxReached = false;
|
|
185
|
-
};
|
|
186
|
-
const copySpeechBuffer = () => {
|
|
187
|
-
if (!speechBuffer)
|
|
188
|
-
throw new Error('speechBuffer is empty');
|
|
189
|
-
return new AudioFrame(speechBuffer.subarray(0, speechBufferIndex), pubSampleRate, 1, speechBufferIndex);
|
|
190
|
-
};
|
|
191
|
-
if (p > this.#opts.activationThreshold) {
|
|
192
|
-
speechThresholdDuration += windowDuration;
|
|
193
|
-
silenceThresholdDuration = 0;
|
|
194
|
-
if (!pubSpeaking && speechThresholdDuration >= this.#opts.minSpeechDuration) {
|
|
195
|
-
pubSpeaking = true;
|
|
196
|
-
pubSilenceDuration = 0;
|
|
197
|
-
pubSpeechDuration = speechThresholdDuration;
|
|
198
|
-
this.queue.put({
|
|
199
|
-
type: VADEventType.START_OF_SPEECH,
|
|
200
|
-
samplesIndex: pubCurrentSample,
|
|
201
|
-
timestamp: pubTimestamp,
|
|
202
|
-
silenceDuration: pubSilenceDuration,
|
|
203
|
-
speechDuration: pubSpeechDuration,
|
|
204
|
-
probability: p,
|
|
205
|
-
inferenceDuration,
|
|
206
|
-
frames: [copySpeechBuffer()],
|
|
207
|
-
speaking: pubSpeaking,
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
else {
|
|
212
|
-
silenceThresholdDuration += windowDuration;
|
|
213
|
-
speechThresholdDuration = 0;
|
|
214
|
-
if (!pubSpeaking) {
|
|
215
|
-
resetWriteCursor();
|
|
216
|
-
}
|
|
217
|
-
if (pubSpeaking && silenceThresholdDuration > this.#opts.minSilenceDuration) {
|
|
218
|
-
pubSpeaking = false;
|
|
219
|
-
pubSpeechDuration = 0;
|
|
220
|
-
pubSilenceDuration = silenceThresholdDuration;
|
|
221
|
-
this.queue.put({
|
|
222
|
-
type: VADEventType.END_OF_SPEECH,
|
|
223
|
-
samplesIndex: pubCurrentSample,
|
|
224
|
-
timestamp: pubTimestamp,
|
|
225
|
-
silenceDuration: pubSilenceDuration,
|
|
226
|
-
speechDuration: pubSpeechDuration,
|
|
227
|
-
probability: p,
|
|
228
|
-
inferenceDuration,
|
|
229
|
-
frames: [copySpeechBuffer()],
|
|
230
|
-
speaking: pubSpeaking,
|
|
231
|
-
});
|
|
232
|
-
resetWriteCursor();
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
inputFrames = [];
|
|
236
|
-
inferenceFrames = [];
|
|
237
|
-
if (inputFrame.data.length > toCopyInt) {
|
|
238
|
-
const data = inputFrame.data.subarray(toCopyInt);
|
|
239
|
-
inputFrames.push(new AudioFrame(data, pubSampleRate, 1, Math.trunc(data.length / 2)));
|
|
240
|
-
}
|
|
241
|
-
if (inferenceFrame.data.length > this.#model.windowSizeSamples) {
|
|
242
|
-
const data = inferenceFrame.data.subarray(this.#model.windowSizeSamples);
|
|
243
|
-
inferenceFrames.push(new AudioFrame(data, this.#opts.sampleRate, 1, Math.trunc(data.length / 2)));
|
|
244
|
-
}
|
|
245
|
-
}
|
|
64
|
+
class VADStream extends baseStream {
|
|
65
|
+
#opts;
|
|
66
|
+
#model;
|
|
67
|
+
#task;
|
|
68
|
+
#expFilter = new ExpFilter(0.35);
|
|
69
|
+
#extraInferenceTime = 0;
|
|
70
|
+
#logger = log();
|
|
71
|
+
constructor(opts, model) {
|
|
72
|
+
super();
|
|
73
|
+
this.#opts = opts;
|
|
74
|
+
this.#model = model;
|
|
75
|
+
this.#task = new Promise(async () => {
|
|
76
|
+
let inferenceData = new Float32Array(this.#model.windowSizeSamples);
|
|
77
|
+
let speechBuffer = null;
|
|
78
|
+
let speechBufferMaxReached = false;
|
|
79
|
+
let speechBufferIndex = 0;
|
|
80
|
+
let pubSpeaking = false;
|
|
81
|
+
let pubSpeechDuration = 0;
|
|
82
|
+
let pubSilenceDuration = 0;
|
|
83
|
+
let pubCurrentSample = 0;
|
|
84
|
+
let pubTimestamp = 0;
|
|
85
|
+
let pubSampleRate = 0;
|
|
86
|
+
let pubPrefixPaddingSamples = 0;
|
|
87
|
+
let speechThresholdDuration = 0;
|
|
88
|
+
let silenceThresholdDuration = 0;
|
|
89
|
+
let inputFrames = [];
|
|
90
|
+
let inferenceFrames = [];
|
|
91
|
+
let resampler = null;
|
|
92
|
+
let inputCopyRemainingFrac = 0;
|
|
93
|
+
for await (const frame of this.input) {
|
|
94
|
+
if (typeof frame === "symbol") {
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
if (!pubSampleRate || !speechBuffer) {
|
|
98
|
+
pubSampleRate = frame.sampleRate;
|
|
99
|
+
pubPrefixPaddingSamples = Math.trunc(
|
|
100
|
+
this.#opts.prefixPaddingDuration * pubSampleRate / 1e3
|
|
101
|
+
);
|
|
102
|
+
speechBuffer = new Int16Array(
|
|
103
|
+
this.#opts.maxBufferedSpeech * pubSampleRate + pubPrefixPaddingSamples
|
|
104
|
+
);
|
|
105
|
+
if (this.#opts.sampleRate !== pubSampleRate) {
|
|
106
|
+
resampler = new AudioResampler(
|
|
107
|
+
pubSampleRate,
|
|
108
|
+
this.#opts.sampleRate,
|
|
109
|
+
1,
|
|
110
|
+
AudioResamplerQuality.QUICK
|
|
111
|
+
// VAD doesn't need high quality
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
} else if (frame.sampleRate !== pubSampleRate) {
|
|
115
|
+
this.#logger.error("a frame with a different sample rate was already published");
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
inputFrames.push(frame);
|
|
119
|
+
if (resampler) {
|
|
120
|
+
inferenceFrames.push(...resampler.push(frame));
|
|
121
|
+
} else {
|
|
122
|
+
inferenceFrames.push(frame);
|
|
123
|
+
}
|
|
124
|
+
while (true) {
|
|
125
|
+
const startTime = process.hrtime.bigint();
|
|
126
|
+
const availableInferenceSamples = inferenceFrames.map((x) => x.samplesPerChannel).reduce((acc, x) => acc + x, 0);
|
|
127
|
+
if (availableInferenceSamples < this.#model.windowSizeSamples) {
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
const inputFrame = mergeFrames(inputFrames);
|
|
131
|
+
const inferenceFrame = mergeFrames(inferenceFrames);
|
|
132
|
+
inferenceData = Float32Array.from(
|
|
133
|
+
inferenceFrame.data.subarray(0, this.#model.windowSizeSamples),
|
|
134
|
+
(x) => x / 32767
|
|
135
|
+
);
|
|
136
|
+
const p = await this.#model.run(inferenceData).then((data) => this.#expFilter.apply(1, data));
|
|
137
|
+
const windowDuration = this.#model.windowSizeSamples / this.#opts.sampleRate * 1e3;
|
|
138
|
+
pubCurrentSample += this.#model.windowSizeSamples;
|
|
139
|
+
pubTimestamp += windowDuration;
|
|
140
|
+
const resamplingRatio = pubSampleRate / this.#model.sampleRate;
|
|
141
|
+
const toCopy = this.#model.windowSizeSamples * resamplingRatio + inputCopyRemainingFrac;
|
|
142
|
+
const toCopyInt = Math.trunc(toCopy);
|
|
143
|
+
inputCopyRemainingFrac = toCopy - toCopyInt;
|
|
144
|
+
const availableSpace = speechBuffer.length - speechBufferIndex;
|
|
145
|
+
const toCopyBuffer = Math.min(this.#model.windowSizeSamples, availableSpace);
|
|
146
|
+
if (toCopyBuffer > 0) {
|
|
147
|
+
speechBuffer.set(inputFrame.data.subarray(0, toCopyBuffer), speechBufferIndex);
|
|
148
|
+
speechBufferIndex += toCopyBuffer;
|
|
149
|
+
} else if (!speechBufferMaxReached) {
|
|
150
|
+
speechBufferMaxReached = true;
|
|
151
|
+
this.#logger.warn(
|
|
152
|
+
"maxBufferedSpeech reached, ignoring further data for the current speech input"
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
const inferenceDuration = Number((process.hrtime.bigint() - startTime) / BigInt(1e6));
|
|
156
|
+
this.#extraInferenceTime = Math.max(
|
|
157
|
+
0,
|
|
158
|
+
this.#extraInferenceTime + inferenceDuration - windowDuration
|
|
159
|
+
);
|
|
160
|
+
if (this.#extraInferenceTime > SLOW_INFERENCE_THRESHOLD) {
|
|
161
|
+
this.#logger.child({ delay: this.#extraInferenceTime }).warn("inference is slower than realtime");
|
|
162
|
+
}
|
|
163
|
+
if (pubSpeaking) {
|
|
164
|
+
pubSpeechDuration += inferenceDuration;
|
|
165
|
+
} else {
|
|
166
|
+
pubSilenceDuration += inferenceDuration;
|
|
167
|
+
}
|
|
168
|
+
this.queue.put({
|
|
169
|
+
type: VADEventType.INFERENCE_DONE,
|
|
170
|
+
samplesIndex: pubCurrentSample,
|
|
171
|
+
timestamp: pubTimestamp,
|
|
172
|
+
silenceDuration: pubSilenceDuration,
|
|
173
|
+
speechDuration: pubSpeechDuration,
|
|
174
|
+
probability: p,
|
|
175
|
+
inferenceDuration,
|
|
176
|
+
frames: [
|
|
177
|
+
new AudioFrame(inputFrame.data.subarray(0, toCopyInt), pubSampleRate, 1, toCopyInt)
|
|
178
|
+
],
|
|
179
|
+
speaking: pubSpeaking
|
|
180
|
+
});
|
|
181
|
+
const resetWriteCursor = () => {
|
|
182
|
+
if (!speechBuffer) throw new Error("speechBuffer is empty");
|
|
183
|
+
if (speechBufferIndex <= pubPrefixPaddingSamples) {
|
|
184
|
+
return;
|
|
246
185
|
}
|
|
247
|
-
|
|
248
|
-
|
|
186
|
+
const paddingData = speechBuffer.subarray(
|
|
187
|
+
speechBufferIndex - pubPrefixPaddingSamples,
|
|
188
|
+
speechBufferIndex
|
|
189
|
+
);
|
|
190
|
+
speechBuffer.set(paddingData, 0);
|
|
191
|
+
speechBufferIndex = pubPrefixPaddingSamples;
|
|
192
|
+
speechBufferMaxReached = false;
|
|
193
|
+
};
|
|
194
|
+
const copySpeechBuffer = () => {
|
|
195
|
+
if (!speechBuffer) throw new Error("speechBuffer is empty");
|
|
196
|
+
return new AudioFrame(
|
|
197
|
+
speechBuffer.subarray(0, speechBufferIndex),
|
|
198
|
+
pubSampleRate,
|
|
199
|
+
1,
|
|
200
|
+
speechBufferIndex
|
|
201
|
+
);
|
|
202
|
+
};
|
|
203
|
+
if (p > this.#opts.activationThreshold) {
|
|
204
|
+
speechThresholdDuration += windowDuration;
|
|
205
|
+
silenceThresholdDuration = 0;
|
|
206
|
+
if (!pubSpeaking && speechThresholdDuration >= this.#opts.minSpeechDuration) {
|
|
207
|
+
pubSpeaking = true;
|
|
208
|
+
pubSilenceDuration = 0;
|
|
209
|
+
pubSpeechDuration = speechThresholdDuration;
|
|
210
|
+
this.queue.put({
|
|
211
|
+
type: VADEventType.START_OF_SPEECH,
|
|
212
|
+
samplesIndex: pubCurrentSample,
|
|
213
|
+
timestamp: pubTimestamp,
|
|
214
|
+
silenceDuration: pubSilenceDuration,
|
|
215
|
+
speechDuration: pubSpeechDuration,
|
|
216
|
+
probability: p,
|
|
217
|
+
inferenceDuration,
|
|
218
|
+
frames: [copySpeechBuffer()],
|
|
219
|
+
speaking: pubSpeaking
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
} else {
|
|
223
|
+
silenceThresholdDuration += windowDuration;
|
|
224
|
+
speechThresholdDuration = 0;
|
|
225
|
+
if (!pubSpeaking) {
|
|
226
|
+
resetWriteCursor();
|
|
227
|
+
}
|
|
228
|
+
if (pubSpeaking && silenceThresholdDuration > this.#opts.minSilenceDuration) {
|
|
229
|
+
pubSpeaking = false;
|
|
230
|
+
pubSpeechDuration = 0;
|
|
231
|
+
pubSilenceDuration = silenceThresholdDuration;
|
|
232
|
+
this.queue.put({
|
|
233
|
+
type: VADEventType.END_OF_SPEECH,
|
|
234
|
+
samplesIndex: pubCurrentSample,
|
|
235
|
+
timestamp: pubTimestamp,
|
|
236
|
+
silenceDuration: pubSilenceDuration,
|
|
237
|
+
speechDuration: pubSpeechDuration,
|
|
238
|
+
probability: p,
|
|
239
|
+
inferenceDuration,
|
|
240
|
+
frames: [copySpeechBuffer()],
|
|
241
|
+
speaking: pubSpeaking
|
|
242
|
+
});
|
|
243
|
+
resetWriteCursor();
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
inputFrames = [];
|
|
247
|
+
inferenceFrames = [];
|
|
248
|
+
if (inputFrame.data.length > toCopyInt) {
|
|
249
|
+
const data = inputFrame.data.subarray(toCopyInt);
|
|
250
|
+
inputFrames.push(new AudioFrame(data, pubSampleRate, 1, Math.trunc(data.length / 2)));
|
|
251
|
+
}
|
|
252
|
+
if (inferenceFrame.data.length > this.#model.windowSizeSamples) {
|
|
253
|
+
const data = inferenceFrame.data.subarray(this.#model.windowSizeSamples);
|
|
254
|
+
inferenceFrames.push(
|
|
255
|
+
new AudioFrame(data, this.#opts.sampleRate, 1, Math.trunc(data.length / 2))
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
}
|
|
249
262
|
}
|
|
263
|
+
export {
|
|
264
|
+
VAD,
|
|
265
|
+
VADStream
|
|
266
|
+
};
|
|
250
267
|
//# sourceMappingURL=vad.js.map
|
package/dist/vad.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vad.js","sourceRoot":"","sources":["../src/vad.ts"],"names":[],"mappings":"AAAA,6CAA6C;AAC7C,EAAE;AACF,sCAAsC;AACtC,OAAO,EACL,SAAS,EACT,YAAY,EACZ,SAAS,IAAI,UAAU,EACvB,GAAG,IAAI,OAAO,EACd,GAAG,EACH,WAAW,GACZ,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAGtF,OAAO,EAAE,SAAS,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEjE,MAAM,wBAAwB,GAAG,GAAG,CAAC,CAAC,gBAAgB;AAmBtD,MAAM,iBAAiB,GAAe;IACpC,iBAAiB,EAAE,EAAE;IACrB,kBAAkB,EAAE,GAAG;IAC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IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;oBACxF,CAAC;oBACD,IAAI,cAAc,CAAC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,iBAAiB,EAAE,CAAC;wBAC/D,MAAM,IAAI,GAAG,cAAc,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,iBAAiB,CAAC,CAAC;wBACzE,eAAe,CAAC,IAAI,CAClB,IAAI,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,UAAU,EAAE,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAC5E,CAAC;oBACJ,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF"}
|
|
1
|
+
{"version":3,"sources":["../src/vad.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport {\n ExpFilter,\n VADEventType,\n VADStream as baseStream,\n VAD as baseVAD,\n log,\n mergeFrames,\n} from '@livekit/agents';\nimport { AudioFrame, AudioResampler, AudioResamplerQuality } from '@livekit/rtc-node';\nimport type { InferenceSession } from 'onnxruntime-node';\nimport type { SampleRate } from './onnx_model.js';\nimport { OnnxModel, newInferenceSession } from './onnx_model.js';\n\nconst SLOW_INFERENCE_THRESHOLD = 200; // late by 200ms\n\nexport interface VADOptions {\n /** Minimum duration of speech to start a new speech chunk */\n minSpeechDuration: number;\n /** At the end of each speech, wait this duration before ending the speech */\n minSilenceDuration: number;\n /** Duration of padding to add to the beginning of each speech chunk */\n prefixPaddingDuration: number;\n /** Maximum duration of speech to keep in the buffer */\n maxBufferedSpeech: number;\n /** Maximum duration of speech to keep in the buffer*/\n activationThreshold: number;\n /** Sample rate for the inference (only 8KHz and 16KHz are supported) */\n sampleRate: SampleRate;\n /** Force the use of CPU for inference */\n forceCPU: boolean;\n}\n\nconst defaultVADOptions: VADOptions = {\n minSpeechDuration: 50,\n minSilenceDuration: 250,\n prefixPaddingDuration: 500,\n maxBufferedSpeech: 60000,\n activationThreshold: 0.5,\n sampleRate: 16000,\n forceCPU: true,\n};\n\nexport class VAD extends baseVAD {\n #session: InferenceSession;\n #opts: VADOptions;\n\n constructor(session: InferenceSession, opts: VADOptions) {\n super({ updateInterval: 32 });\n this.#session = session;\n this.#opts = opts;\n }\n\n /**\n * Load and initialize the Silero VAD model.\n *\n * This method loads the ONNX model and prepares it for inference. When options are not provided,\n * sane defaults are used.\n *\n * @remarks\n * This method may take time to load the model into memory.\n * It is recommended to call this method inside your prewarm mechanism.\n *\n * @example\n * ```ts\n * export default defineAgent({\n * prewarm: async (proc: JobProcess) => {\n * proc.userData.vad = await VAD.load();\n * },\n * entry: async (ctx: JobContext) => {\n * const vad = ctx.proc.userData.vad! as VAD;\n * // the rest of your agent logic\n * },\n * });\n * ```\n *\n * @param options -\n * @returns Promise\\<{@link VAD}\\>: An instance of the VAD class ready for streaming.\n */\n static async load(opts: Partial<VADOptions> = {}): Promise<VAD> {\n const mergedOpts: VADOptions = { ...defaultVADOptions, ...opts };\n const session = await newInferenceSession(mergedOpts.forceCPU);\n return new VAD(session, mergedOpts);\n }\n\n stream(): VADStream {\n return new VADStream(this.#opts, new OnnxModel(this.#session, this.#opts.sampleRate));\n }\n}\n\nexport class VADStream extends baseStream {\n #opts: VADOptions;\n #model: OnnxModel;\n #task: Promise<void>;\n #expFilter = new ExpFilter(0.35);\n #extraInferenceTime = 0;\n #logger = log();\n\n constructor(opts: VADOptions, model: OnnxModel) {\n super();\n this.#opts = opts;\n this.#model = model;\n\n this.#task = new Promise(async () => {\n let inferenceData = new Float32Array(this.#model.windowSizeSamples);\n\n // a copy is exposed to the user in END_OF_SPEECH\n let speechBuffer: Int16Array | null = null;\n let speechBufferMaxReached = false;\n let speechBufferIndex = 0;\n\n // \"pub\" means public, these values are exposed to the users through events\n let pubSpeaking = false;\n let pubSpeechDuration = 0;\n let pubSilenceDuration = 0;\n let pubCurrentSample = 0;\n let pubTimestamp = 0;\n let pubSampleRate = 0;\n let pubPrefixPaddingSamples = 0; // size in samples of padding data\n\n let speechThresholdDuration = 0;\n let silenceThresholdDuration = 0;\n\n let inputFrames = [];\n let inferenceFrames: AudioFrame[] = [];\n let resampler: AudioResampler | null = null;\n\n // used to avoid drift when the sampleRate ratio is not an integer\n let inputCopyRemainingFrac = 0.0;\n\n for await (const frame of this.input) {\n if (typeof frame === 'symbol') {\n continue; // ignore flush sentinel for now\n }\n\n if (!pubSampleRate || !speechBuffer) {\n pubSampleRate = frame.sampleRate;\n pubPrefixPaddingSamples = Math.trunc(\n (this.#opts.prefixPaddingDuration * pubSampleRate) / 1000,\n );\n\n speechBuffer = new Int16Array(\n this.#opts.maxBufferedSpeech * pubSampleRate + pubPrefixPaddingSamples,\n );\n\n if (this.#opts.sampleRate !== pubSampleRate) {\n // resampling needed: the input sample rate isn't the same as the model's\n // sample rate used for inference\n resampler = new AudioResampler(\n pubSampleRate,\n this.#opts.sampleRate,\n 1,\n AudioResamplerQuality.QUICK, // VAD doesn't need high quality\n );\n }\n } else if (frame.sampleRate !== pubSampleRate) {\n this.#logger.error('a frame with a different sample rate was already published');\n continue;\n }\n\n inputFrames.push(frame);\n if (resampler) {\n inferenceFrames.push(...resampler.push(frame));\n } else {\n inferenceFrames.push(frame);\n }\n\n while (true) {\n const startTime = process.hrtime.bigint();\n const availableInferenceSamples = inferenceFrames\n .map((x) => x.samplesPerChannel)\n .reduce((acc, x) => acc + x, 0);\n\n if (availableInferenceSamples < this.#model.windowSizeSamples) {\n break; // not enough samples to run inference\n }\n\n const inputFrame = mergeFrames(inputFrames);\n const inferenceFrame = mergeFrames(inferenceFrames);\n\n // convert data to f32\n inferenceData = Float32Array.from(\n inferenceFrame.data.subarray(0, this.#model.windowSizeSamples),\n (x) => x / 32767,\n );\n\n const p = await this.#model\n .run(inferenceData)\n .then((data) => this.#expFilter.apply(1, data));\n\n const windowDuration = (this.#model.windowSizeSamples / this.#opts.sampleRate) * 1000;\n pubCurrentSample += this.#model.windowSizeSamples;\n pubTimestamp += windowDuration;\n const resamplingRatio = pubSampleRate / this.#model.sampleRate;\n const toCopy = this.#model.windowSizeSamples * resamplingRatio + inputCopyRemainingFrac;\n const toCopyInt = Math.trunc(toCopy);\n inputCopyRemainingFrac = toCopy - toCopyInt;\n\n // copy the inference window to the speech buffer\n const availableSpace = speechBuffer.length - speechBufferIndex;\n const toCopyBuffer = Math.min(this.#model.windowSizeSamples, availableSpace);\n if (toCopyBuffer > 0) {\n speechBuffer.set(inputFrame.data.subarray(0, toCopyBuffer), speechBufferIndex);\n speechBufferIndex += toCopyBuffer;\n } else if (!speechBufferMaxReached) {\n speechBufferMaxReached = true;\n this.#logger.warn(\n 'maxBufferedSpeech reached, ignoring further data for the current speech input',\n );\n }\n\n const inferenceDuration = Number((process.hrtime.bigint() - startTime) / BigInt(1000000));\n this.#extraInferenceTime = Math.max(\n 0,\n this.#extraInferenceTime + inferenceDuration - windowDuration,\n );\n if (this.#extraInferenceTime > SLOW_INFERENCE_THRESHOLD) {\n this.#logger\n .child({ delay: this.#extraInferenceTime })\n .warn('inference is slower than realtime');\n }\n\n if (pubSpeaking) {\n pubSpeechDuration += inferenceDuration;\n } else {\n pubSilenceDuration += inferenceDuration;\n }\n\n this.queue.put({\n type: VADEventType.INFERENCE_DONE,\n samplesIndex: pubCurrentSample,\n timestamp: pubTimestamp,\n silenceDuration: pubSilenceDuration,\n speechDuration: pubSpeechDuration,\n probability: p,\n inferenceDuration,\n frames: [\n new AudioFrame(inputFrame.data.subarray(0, toCopyInt), pubSampleRate, 1, toCopyInt),\n ],\n speaking: pubSpeaking,\n });\n\n const resetWriteCursor = () => {\n if (!speechBuffer) throw new Error('speechBuffer is empty');\n if (speechBufferIndex <= pubPrefixPaddingSamples) {\n return;\n }\n\n const paddingData = speechBuffer.subarray(\n speechBufferIndex - pubPrefixPaddingSamples,\n speechBufferIndex,\n );\n speechBuffer.set(paddingData, 0);\n speechBufferIndex = pubPrefixPaddingSamples;\n speechBufferMaxReached = false;\n };\n\n const copySpeechBuffer = (): AudioFrame => {\n if (!speechBuffer) throw new Error('speechBuffer is empty');\n return new AudioFrame(\n speechBuffer.subarray(0, speechBufferIndex),\n pubSampleRate,\n 1,\n speechBufferIndex,\n );\n };\n\n if (p > this.#opts.activationThreshold) {\n speechThresholdDuration += windowDuration;\n silenceThresholdDuration = 0;\n if (!pubSpeaking && speechThresholdDuration >= this.#opts.minSpeechDuration) {\n pubSpeaking = true;\n pubSilenceDuration = 0;\n pubSpeechDuration = speechThresholdDuration;\n\n this.queue.put({\n type: VADEventType.START_OF_SPEECH,\n samplesIndex: pubCurrentSample,\n timestamp: pubTimestamp,\n silenceDuration: pubSilenceDuration,\n speechDuration: pubSpeechDuration,\n probability: p,\n inferenceDuration,\n frames: [copySpeechBuffer()],\n speaking: pubSpeaking,\n });\n }\n } else {\n silenceThresholdDuration += windowDuration;\n speechThresholdDuration = 0;\n\n if (!pubSpeaking) {\n resetWriteCursor();\n }\n\n if (pubSpeaking && silenceThresholdDuration > this.#opts.minSilenceDuration) {\n pubSpeaking = false;\n pubSpeechDuration = 0;\n pubSilenceDuration = silenceThresholdDuration;\n\n this.queue.put({\n type: VADEventType.END_OF_SPEECH,\n samplesIndex: pubCurrentSample,\n timestamp: pubTimestamp,\n silenceDuration: pubSilenceDuration,\n speechDuration: pubSpeechDuration,\n probability: p,\n inferenceDuration,\n frames: [copySpeechBuffer()],\n speaking: pubSpeaking,\n });\n\n resetWriteCursor();\n }\n }\n\n inputFrames = [];\n inferenceFrames = [];\n\n if (inputFrame.data.length > toCopyInt) {\n const data = inputFrame.data.subarray(toCopyInt);\n inputFrames.push(new AudioFrame(data, pubSampleRate, 1, Math.trunc(data.length / 2)));\n }\n if (inferenceFrame.data.length > this.#model.windowSizeSamples) {\n const data = inferenceFrame.data.subarray(this.#model.windowSizeSamples);\n inferenceFrames.push(\n new AudioFrame(data, this.#opts.sampleRate, 1, Math.trunc(data.length / 2)),\n );\n }\n }\n }\n });\n }\n}\n"],"mappings":"AAGA;AAAA,EACE;AAAA,EACA;AAAA,EACA,aAAa;AAAA,EACb,OAAO;AAAA,EACP;AAAA,EACA;AAAA,OACK;AACP,SAAS,YAAY,gBAAgB,6BAA6B;AAGlE,SAAS,WAAW,2BAA2B;AAE/C,MAAM,2BAA2B;AAmBjC,MAAM,oBAAgC;AAAA,EACpC,mBAAmB;AAAA,EACnB,oBAAoB;AAAA,EACpB,uBAAuB;AAAA,EACvB,mBAAmB;AAAA,EACnB,qBAAqB;AAAA,EACrB,YAAY;AAAA,EACZ,UAAU;AACZ;AAEO,MAAM,YAAY,QAAQ;AAAA,EAC/B;AAAA,EACA;AAAA,EAEA,YAAY,SAA2B,MAAkB;AACvD,UAAM,EAAE,gBAAgB,GAAG,CAAC;AAC5B,SAAK,WAAW;AAChB,SAAK,QAAQ;AAAA,EACf;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EA4BA,aAAa,KAAK,OAA4B,CAAC,GAAiB;AAC9D,UAAM,aAAyB,EAAE,GAAG,mBAAmB,GAAG,KAAK;AAC/D,UAAM,UAAU,MAAM,oBAAoB,WAAW,QAAQ;AAC7D,WAAO,IAAI,IAAI,SAAS,UAAU;AAAA,EACpC;AAAA,EAEA,SAAoB;AAClB,WAAO,IAAI,UAAU,KAAK,OAAO,IAAI,UAAU,KAAK,UAAU,KAAK,MAAM,UAAU,CAAC;AAAA,EACtF;AACF;AAEO,MAAM,kBAAkB,WAAW;AAAA,EACxC;AAAA,EACA;AAAA,EACA;AAAA,EACA,aAAa,IAAI,UAAU,IAAI;AAAA,EAC/B,sBAAsB;AAAA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