From 078fed55f5e98ac7390d3b34b101bdb0c0d26064 Mon Sep 17 00:00:00 2001 From: Giovanni Campagna Date: Tue, 8 Jun 2021 08:55:03 -0700 Subject: [PATCH] Fix VAD interface to support audio chunks of any length The webrtcvad library expects frames of fixed size, but Genie will pass the buffers as they come from nodejs streaming code without any slicing. Do the slicing and concatenation of frames here so we only call the library with frames of fixed size. --- service/platform/index.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/service/platform/index.js b/service/platform/index.js index bd0a5400..8ac72f8b 100644 --- a/service/platform/index.js +++ b/service/platform/index.js @@ -254,6 +254,7 @@ try { class VAD { constructor() { this._instance = null; + this._previousChunk = null; this.frameSize = 0; } @@ -275,10 +276,22 @@ class VAD { process(chunk) { if (!this._instance) return false; - let n = chunk.length % this.frameSize, r = 0; - for (let i = 0; i < n; i++) - r += this._instance.process(chunk.slice(i * this.frameSize, this.frameSize)); - return r; + + if (this._previousChunk) + chunk = Buffer.concat([this._previousChunk, chunk], this._previousChunk.length + chunk.length); + + let anySound = false; + let offset; + for (offset = 0; offset < chunk.length && offset + this.frameSize <= chunk.length; offset += this.frameSize) { + const sliced = chunk.slice(offset, offset + this.frameSize); + const sound = this._instance.process(sliced); + anySound = anySound || sound; + } + if (offset < chunk.length) + this._previousChunk = chunk.slice(offset); + else + this._previousChunk = null; + return anySound; } }