From 41fa124b8f85ab3e41fef37b4f961b9bdbcc91e0 Mon Sep 17 00:00:00 2001 From: Jonas Herzig Date: Wed, 6 Feb 2019 14:21:13 +0100 Subject: [PATCH] Add artificial delay of 150ms to mic for VAD VAD can only tell whether the user is talking after ~150ms, so we need to delay the microphone stream for 150ms when it's in use. --- app/index.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/app/index.js b/app/index.js index 3094cc6..ca93067 100644 --- a/app/index.js +++ b/app/index.js @@ -337,14 +337,22 @@ class GlobalBindings { log('Connecting to server ', host) + let ctx = audioContext() + if (!this._delayedMicNode) { + this._micNode = ctx.createMediaStreamSource(this._micStream) + this._delayNode = ctx.createDelay() + this._delayNode.delayTime.value = 0.15 + this._delayedMicNode = ctx.createMediaStreamDestination() + } + // TODO: token this.connector.connect(`wss://${host}:${port}`, { username: username, password: password, webrtc: { enabled: true, - mic: micStream, - audioContext: audioContext() + mic: this._delayedMicNode.stream, + audioContext: ctx } }).done(client => { log('Connected!') @@ -695,6 +703,15 @@ class GlobalBindings { voiceHandler.setMute(true) } + this._micNode.disconnect() + this._delayNode.disconnect() + if (mode === 'vad') { + this._micNode.connect(this._delayNode) + this._delayNode.connect(this._delayedMicNode) + } else { + this._micNode.connect(this._delayedMicNode) + } + this.client.setAudioQuality( this.settings.audioBitrate, this.settings.samplesPerPacket @@ -976,7 +993,6 @@ function userToState () { return flags.join(', ') } -var micStream var voiceHandler var testVoiceHandler @@ -993,7 +1009,7 @@ var userMediaPromise = initVoice(data => { voiceHandler.write(data) } }).then(userMedia => { - micStream = userMedia + ui._micStream = userMedia }, err => { window.alert('Failed to initialize user media\nRefresh page to retry.\n' + err) })