@livekit/agents-plugin-silero 0.4.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/dist/vad.js ADDED
@@ -0,0 +1,240 @@
1
+ // SPDX-FileCopyrightText: 2024 LiveKit, Inc.
2
+ //
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ import { ExpFilter, VADEventType, VADStream as baseStream, VAD as baseVAD, log, mergeFrames, } from '@livekit/agents';
5
+ import { AudioFrame, AudioResampler, AudioResamplerQuality } from '@livekit/rtc-node';
6
+ import { OnnxModel, newInferenceSession } from './onnx_model.js';
7
+ const SLOW_INFERENCE_THRESHOLD = 200; // late by 200ms
8
+ const defaultVADOptions = {
9
+ minSpeechDuration: 50,
10
+ minSilenceDuration: 250,
11
+ prefixPaddingDuration: 100,
12
+ maxBufferedSpeech: 60000,
13
+ activationThreshold: 0.5,
14
+ sampleRate: 16000,
15
+ forceCPU: true,
16
+ };
17
+ export class VAD extends baseVAD {
18
+ #session;
19
+ #opts;
20
+ constructor(session, opts) {
21
+ super({ updateInterval: 32 });
22
+ this.#session = session;
23
+ this.#opts = opts;
24
+ }
25
+ /**
26
+ * Load and initialize the Silero VAD model.
27
+ *
28
+ * This method loads the ONNX model and prepares it for inference. When options are not provided,
29
+ * sane defaults are used.
30
+ *
31
+ * @remarks
32
+ * This method may take time to load the model into memory.
33
+ * It is recommended to call this method inside your prewarm mechanism.
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * export default defineAgent({
38
+ * prewarm: async (proc: JobProcess) => {
39
+ * proc.userData.vad = await VAD.load();
40
+ * },
41
+ * entry: async (ctx: JobContext) => {
42
+ * const vad = ctx.proc.userData.vad! as VAD;
43
+ * // the rest of your agent logic
44
+ * },
45
+ * });
46
+ * ```
47
+ *
48
+ * @param options -
49
+ * @returns Promise\<{@link VAD}\>: An instance of the VAD class ready for streaming.
50
+ */
51
+ static async load(opts = defaultVADOptions) {
52
+ const session = await newInferenceSession(opts.forceCPU);
53
+ return new VAD(session, opts);
54
+ }
55
+ stream() {
56
+ return new VADStream(this.#opts, new OnnxModel(this.#session, this.#opts.sampleRate));
57
+ }
58
+ }
59
+ export class VADStream extends baseStream {
60
+ #opts;
61
+ #model;
62
+ #task;
63
+ #expFilter = new ExpFilter(0.35);
64
+ #extraInferenceTime = 0;
65
+ #logger = log();
66
+ constructor(opts, model) {
67
+ super();
68
+ this.#opts = opts;
69
+ this.#model = model;
70
+ this.#task = new Promise(async () => {
71
+ let inferenceData = new Float32Array(this.#model.windowSizeSamples);
72
+ // a copy is exposed to the user in END_OF_SPEECH
73
+ let speechBuffer = null;
74
+ let speechBufferMaxReached = false;
75
+ let speechBufferIndex = 0;
76
+ // "pub" means public, these values are exposed to the users through events
77
+ let pubSpeaking = false;
78
+ let pubSpeechDuration = 0;
79
+ let pubSilenceDuration = 0;
80
+ let pubCurrentSample = 0;
81
+ let pubTimestamp = 0;
82
+ let pubSampleRate = 0;
83
+ let pubPrefixPaddingSamples = 0; // size in samples of padding data
84
+ let speechThresholdDuration = 0;
85
+ let silenceThresholdDuration = 0;
86
+ let inputFrames = [];
87
+ let inferenceFrames = [];
88
+ let resampler = null;
89
+ // used to avoid drift when the sampleRate ratio is not an integer
90
+ let inputCopyRemainingFrac = 0.0;
91
+ for await (const frame of this.input) {
92
+ if (typeof frame === 'symbol') {
93
+ continue; // ignore flush sentinel for now
94
+ }
95
+ if (!pubSampleRate || !speechBuffer) {
96
+ pubSampleRate = frame.sampleRate;
97
+ pubPrefixPaddingSamples = Math.ceil(this.#opts.prefixPaddingDuration * pubSampleRate);
98
+ speechBuffer = new Int16Array((this.#opts.maxBufferedSpeech + this.#opts.prefixPaddingDuration) * pubSampleRate);
99
+ if (this.#opts.sampleRate !== pubSampleRate) {
100
+ // resampling needed: the input sample rate isn't the same as the model's
101
+ // sample rate used for inference
102
+ resampler = new AudioResampler(pubSampleRate, this.#opts.sampleRate, 1, AudioResamplerQuality.QUICK);
103
+ }
104
+ }
105
+ else if (frame.sampleRate !== pubSampleRate) {
106
+ this.#logger.error('a frame with a different sample rate was already published');
107
+ continue;
108
+ }
109
+ inputFrames.push(frame);
110
+ if (resampler) {
111
+ inferenceFrames.push(...resampler.push(frame));
112
+ }
113
+ else {
114
+ inferenceFrames.push(frame);
115
+ }
116
+ while (true) {
117
+ const startTime = process.hrtime.bigint();
118
+ const availableInferenceSamples = inferenceFrames
119
+ .map((x) => x.samplesPerChannel)
120
+ .reduce((acc, x) => acc + x, 0);
121
+ if (availableInferenceSamples < this.#model.windowSizeSamples) {
122
+ break; // not enough samples to run inference
123
+ }
124
+ const inputFrame = mergeFrames(inputFrames);
125
+ const inferenceFrame = mergeFrames(inferenceFrames);
126
+ // convert data to f32
127
+ inferenceData = Float32Array.from(inferenceFrame.data.subarray(0, this.#model.windowSizeSamples), (x) => x / 32767);
128
+ const p = await this.#model
129
+ .run(inferenceData)
130
+ .then((data) => this.#expFilter.apply(1, data));
131
+ const windowDuration = (this.#model.windowSizeSamples / this.#opts.sampleRate) * 1000;
132
+ pubCurrentSample += this.#model.windowSizeSamples;
133
+ pubTimestamp += windowDuration;
134
+ const resamplingRatio = pubSampleRate / this.#model.sampleRate;
135
+ const toCopy = this.#model.windowSizeSamples * resamplingRatio + inputCopyRemainingFrac;
136
+ const toCopyInt = Math.trunc(toCopy);
137
+ inputCopyRemainingFrac = toCopy - toCopyInt;
138
+ // copy the inference window to the speech buffer
139
+ const availableSpace = speechBuffer.length - speechBufferIndex;
140
+ const toCopyBuffer = Math.min(this.#model.windowSizeSamples, availableSpace);
141
+ if (toCopyBuffer > 0) {
142
+ speechBuffer.set(inputFrame.data.subarray(0, toCopyBuffer), speechBufferIndex);
143
+ }
144
+ else if (!speechBufferMaxReached) {
145
+ speechBufferMaxReached = true;
146
+ this.#logger.warn('maxBufferedSpeech reached, ignoring further data for the current speech input');
147
+ }
148
+ const inferenceDuration = Number((process.hrtime.bigint() - startTime) / BigInt(1000000));
149
+ this.#extraInferenceTime = Math.max(0, this.#extraInferenceTime + inferenceDuration - windowDuration);
150
+ if (this.#extraInferenceTime > SLOW_INFERENCE_THRESHOLD) {
151
+ this.#logger
152
+ .child({ delay: this.#extraInferenceTime })
153
+ .warn('inference is slower than realtime');
154
+ }
155
+ if (pubSpeaking) {
156
+ pubSpeechDuration += inferenceDuration;
157
+ }
158
+ else {
159
+ pubSilenceDuration += inferenceDuration;
160
+ }
161
+ this.queue.put({
162
+ type: VADEventType.INFERENCE_DONE,
163
+ samplesIndex: pubCurrentSample,
164
+ timestamp: pubTimestamp,
165
+ silenceDuration: pubSilenceDuration,
166
+ speechDuration: pubSpeechDuration,
167
+ probability: p,
168
+ inferenceDuration,
169
+ frames: [
170
+ new AudioFrame(new Int16Array(inputFrame.data.subarray(0, toCopyInt)), pubSampleRate, 1, toCopyInt),
171
+ ],
172
+ speaking: pubSpeaking,
173
+ });
174
+ const copySpeechBuffer = () => {
175
+ if (!speechBuffer)
176
+ throw new Error('speechBuffer is empty');
177
+ return new AudioFrame(new Int16Array(speechBuffer.subarray(0, speechBufferIndex)), pubSampleRate, 1, speechBufferIndex);
178
+ };
179
+ if (p > this.#opts.activationThreshold) {
180
+ speechThresholdDuration += windowDuration;
181
+ silenceThresholdDuration = 0;
182
+ if (!pubSpeaking && speechThresholdDuration >= this.#opts.minSpeechDuration) {
183
+ pubSpeaking = true;
184
+ pubSilenceDuration = 0;
185
+ pubSpeechDuration = speechThresholdDuration;
186
+ this.queue.put({
187
+ type: VADEventType.START_OF_SPEECH,
188
+ samplesIndex: pubCurrentSample,
189
+ timestamp: pubTimestamp,
190
+ silenceDuration: pubSilenceDuration,
191
+ speechDuration: pubSpeechDuration,
192
+ probability: p,
193
+ inferenceDuration,
194
+ frames: [copySpeechBuffer()],
195
+ speaking: pubSpeaking,
196
+ });
197
+ }
198
+ }
199
+ else {
200
+ silenceThresholdDuration += windowDuration;
201
+ speechThresholdDuration = 0;
202
+ if (!pubSpeaking && speechBufferIndex <= pubPrefixPaddingSamples) {
203
+ const paddingData = speechBuffer.subarray(speechBufferIndex - pubPrefixPaddingSamples, speechBufferIndex);
204
+ speechBuffer.set(paddingData, 0);
205
+ speechBufferIndex = pubPrefixPaddingSamples;
206
+ speechBufferMaxReached = false;
207
+ }
208
+ if (pubSpeaking && silenceThresholdDuration > this.#opts.minSilenceDuration) {
209
+ pubSpeaking = false;
210
+ pubSpeechDuration = 0;
211
+ pubSilenceDuration = silenceThresholdDuration;
212
+ this.queue.put({
213
+ type: VADEventType.END_OF_SPEECH,
214
+ samplesIndex: pubCurrentSample,
215
+ timestamp: pubTimestamp,
216
+ silenceDuration: pubSilenceDuration,
217
+ speechDuration: pubSpeechDuration,
218
+ probability: p,
219
+ inferenceDuration,
220
+ frames: [copySpeechBuffer()],
221
+ speaking: pubSpeaking,
222
+ });
223
+ }
224
+ }
225
+ inputFrames = [];
226
+ inferenceFrames = [];
227
+ if (inputFrame.data.length > toCopyInt) {
228
+ const data = new Int16Array(inputFrame.data.subarray(toCopyInt));
229
+ inputFrames.push(new AudioFrame(data, pubSampleRate, 1, Math.trunc(data.length / 2)));
230
+ }
231
+ if (inferenceFrame.data.length > this.#model.windowSizeSamples) {
232
+ const data = new Int16Array(inferenceFrame.data.subarray(this.#model.windowSizeSamples));
233
+ inferenceFrames.push(new AudioFrame(data, this.#opts.sampleRate, 1, Math.trunc(data.length / 2)));
234
+ }
235
+ }
236
+ }
237
+ });
238
+ }
239
+ }
240
+ //# sourceMappingURL=vad.js.map
@@ -0,0 +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,CA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E,CAAC,EAAE,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,IAAI,UAAU,CACzB,cAAc,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,iBAAiB,CAAC,CAC5D,CAAC;wBACF,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"}
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@livekit/agents-plugin-silero",
3
+ "version": "0.4.0",
4
+ "description": "Silero voice activity detection LiveKit Node Agents",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "author": "LiveKit",
8
+ "type": "module",
9
+ "devDependencies": {
10
+ "@microsoft/api-extractor": "^7.35.0",
11
+ "@types/ws": "^8.5.10",
12
+ "onnxruntime-common": "^1.19.2",
13
+ "typescript": "^5.0.0"
14
+ },
15
+ "dependencies": {
16
+ "@livekit/rtc-node": "^0.11.1",
17
+ "onnxruntime-node": "^1.19.2",
18
+ "ws": "^8.16.0",
19
+ "@livekit/agents": "0.4.0"
20
+ },
21
+ "scripts": {
22
+ "build": "tsc && cp src/*.onnx dist/",
23
+ "clean": "rm -rf dist",
24
+ "clean:build": "pnpm clean && pnpm build",
25
+ "lint": "eslint -f unix \"src/**/*.{ts,js}\"",
26
+ "api:check": "api-extractor run --typescript-compiler-folder ../../node_modules/typescript",
27
+ "api:update": "api-extractor run --local --typescript-compiler-folder ../../node_modules/typescript --verbose"
28
+ }
29
+ }
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ // SPDX-FileCopyrightText: 2024 LiveKit, Inc.
2
+ //
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ export { VAD, VADStream } from './vad.js';
@@ -0,0 +1,80 @@
1
+ // SPDX-FileCopyrightText: 2024 LiveKit, Inc.
2
+ //
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ import { InferenceSession, Tensor } from 'onnxruntime-node';
5
+
6
+ export type SampleRate = 8000 | 16000;
7
+
8
+ export const newInferenceSession = (forceCPU: boolean) => {
9
+ return InferenceSession.create(new URL('silero_vad.onnx', import.meta.url).pathname, {
10
+ interOpNumThreads: 1,
11
+ intraOpNumThreads: 1,
12
+ executionMode: 'sequential',
13
+ executionProviders: forceCPU ? [{ name: 'cpu' }] : undefined,
14
+ });
15
+ };
16
+
17
+ export class OnnxModel {
18
+ #session: InferenceSession;
19
+ #sampleRate: number;
20
+ #windowSizeSamples: number;
21
+ #contextSize: number;
22
+ #sampleRateNd: BigInt64Array;
23
+ #context: Float32Array;
24
+ // #state: Float32Array;
25
+ #rnnState: Float32Array;
26
+ #inputBuffer: Float32Array;
27
+
28
+ constructor(session: InferenceSession, sampleRate: SampleRate) {
29
+ this.#session = session;
30
+ this.#sampleRate = sampleRate;
31
+
32
+ switch (sampleRate) {
33
+ case 8000:
34
+ this.#windowSizeSamples = 256;
35
+ this.#contextSize = 32;
36
+ break;
37
+ case 16000:
38
+ this.#windowSizeSamples = 512;
39
+ this.#contextSize = 64;
40
+ break;
41
+ }
42
+
43
+ this.#sampleRateNd = BigInt64Array.from([BigInt(sampleRate)]);
44
+ this.#context = new Float32Array(this.#contextSize);
45
+ this.#rnnState = new Float32Array(2 * 1 * 128);
46
+ this.#inputBuffer = new Float32Array(this.#contextSize + this.#windowSizeSamples);
47
+ }
48
+
49
+ get sampleRate(): number {
50
+ return this.#sampleRate;
51
+ }
52
+
53
+ get windowSizeSamples(): number {
54
+ return this.#windowSizeSamples;
55
+ }
56
+
57
+ get contextSize(): number {
58
+ return this.#contextSize;
59
+ }
60
+
61
+ async run(x: Float32Array): Promise<number> {
62
+ this.#inputBuffer.set(this.#context, 0);
63
+ this.#inputBuffer.set(x, this.#contextSize);
64
+
65
+ return await this.#session
66
+ .run({
67
+ input: new Tensor('float32', this.#inputBuffer, [
68
+ 1,
69
+ this.#contextSize + this.#windowSizeSamples,
70
+ ]),
71
+ state: new Tensor('float32', this.#rnnState, [2, 1, 128]),
72
+ sr: new Tensor('int64', this.#sampleRateNd),
73
+ })
74
+ .then((result) => {
75
+ // this.#state = result.output.data as Float32Array,
76
+ this.#context = this.#inputBuffer.subarray(0, this.#contextSize);
77
+ return (result.output.data as Float32Array).at(0)!;
78
+ });
79
+ }
80
+ }
@@ -0,0 +1,8 @@
1
+ // SPDX-FileCopyrightText: 2024 LiveKit, Inc.
2
+ //
3
+ // SPDX-License-Identifier: Apache-2.0
4
+
5
+ // https://github.com/microsoft/onnxruntime/issues/17979
6
+ declare module 'onnxruntime-node' {
7
+ export * from 'onnxruntime-common';
8
+ }
@@ -0,0 +1,3 @@
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6b99cbfd39246b6706f98ec13c7c50c6b299181f2474fa05cbc8046acc274396
3
+ size 2313101