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.
This commit is contained in:
Jonas Herzig 2019-02-06 14:21:13 +01:00
parent 6030aa8f2e
commit 41fa124b8f

View file

@ -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)
})