Skip to content

Commit

Permalink
Fix VAD interface to support audio chunks of any length (#218)
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 10, 2021
1 parent c70622b commit c21119e
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/service/platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,12 @@ export class SoundEffectsApi implements Tp.Capabilities.SoundEffectsApi {

class VAD implements Tp.Capabilities.VadApi {
private _instance : webrtcvad_|null;
private _previousChunk : Buffer|null;
frameSize : number;

constructor() {
this._instance = null;
this._previousChunk = null;
this.frameSize = 0;
}

Expand All @@ -270,11 +272,22 @@ class VAD implements Tp.Capabilities.VadApi {
process(chunk : Buffer) {
if (!this._instance)
return false;
const n = chunk.length % this.frameSize;
let r = false;
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 : number;
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 c21119e

Please sign in to comment.