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) 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 // TODO: token
this.connector.connect(`wss://${host}:${port}`, { this.connector.connect(`wss://${host}:${port}`, {
username: username, username: username,
password: password, password: password,
webrtc: { webrtc: {
enabled: true, enabled: true,
mic: micStream, mic: this._delayedMicNode.stream,
audioContext: audioContext() audioContext: ctx
} }
}).done(client => { }).done(client => {
log('Connected!') log('Connected!')
@ -695,6 +703,15 @@ class GlobalBindings {
voiceHandler.setMute(true) 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.client.setAudioQuality(
this.settings.audioBitrate, this.settings.audioBitrate,
this.settings.samplesPerPacket this.settings.samplesPerPacket
@ -976,7 +993,6 @@ function userToState () {
return flags.join(', ') return flags.join(', ')
} }
var micStream
var voiceHandler var voiceHandler
var testVoiceHandler var testVoiceHandler
@ -993,7 +1009,7 @@ var userMediaPromise = initVoice(data => {
voiceHandler.write(data) voiceHandler.write(data)
} }
}).then(userMedia => { }).then(userMedia => {
micStream = userMedia ui._micStream = userMedia
}, err => { }, err => {
window.alert('Failed to initialize user media\nRefresh page to retry.\n' + err) window.alert('Failed to initialize user media\nRefresh page to retry.\n' + err)
}) })