Skip to content

Commit

Permalink
Fix VAD interface to support audio chunks of any length
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gcampax committed Jun 8, 2021
1 parent ba2df3b commit 078fed5
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions service/platform/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ try {
class VAD {
constructor() {
this._instance = null;
this._previousChunk = null;
this.frameSize = 0;
}

Expand All @@ -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;
}
}

Expand Down

0 comments on commit 078fed5

Please sign in to comment.