From 5ca332a151577422a01588aa8d0a1c71729c049a Mon Sep 17 00:00:00 2001 From: Jonas Herzig Date: Wed, 26 Sep 2018 13:06:37 +0200 Subject: [PATCH] Allow for configuration of audio bitrate and frames per packet --- app/index.js | 19 +++++++++++++++---- app/voice.js | 20 +++++++++++--------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/app/index.js b/app/index.js index 1eed3ad..60758e9 100644 --- a/app/index.js +++ b/app/index.js @@ -114,7 +114,9 @@ class SettingsDialog { if (this._testVad) { this._testVad.end() } - this._testVad = new VADVoiceHandler(null, this.vadLevel()) + let dummySettings = new Settings() + this.applyTo(dummySettings) + this._testVad = new VADVoiceHandler(null, dummySettings) this._testVad.on('started_talking', () => this.testVadActive(true)) .on('stopped_talking', () => this.testVadActive(false)) .on('level', level => this.testVadLevel(level)) @@ -162,6 +164,8 @@ class Settings { this.vadLevel = load('vadLevel') || 0.3 this.toolbarVertical = load('toolbarVertical') || false this.showAvatars = ko.observable(load('showAvatars') || 'always') + this.audioBitrate = Number(load('audioBitrate')) || 40000 + this.samplesPerPacket = Number(load('samplesPerPacket')) || 960 } save () { @@ -171,6 +175,8 @@ class Settings { save('vadLevel', this.vadLevel) save('toolbarVertical', this.toolbarVertical) save('showAvatars', this.showAvatars()) + save('audioBitrate', this.audioBitrate) + save('samplesPerPacket', this.samplesPerPacket) } } @@ -584,11 +590,11 @@ class GlobalBindings { } let mode = this.settings.voiceMode if (mode === 'cont') { - voiceHandler = new ContinuousVoiceHandler(this.client) + voiceHandler = new ContinuousVoiceHandler(this.client, this.settings) } else if (mode === 'ptt') { - voiceHandler = new PushToTalkVoiceHandler(this.client, this.settings.pttKey) + voiceHandler = new PushToTalkVoiceHandler(this.client, this.settings) } else if (mode === 'vad') { - voiceHandler = new VADVoiceHandler(this.client, this.settings.vadLevel) + voiceHandler = new VADVoiceHandler(this.client, this.settings) } else { log('Unknown voice mode:', mode) return @@ -606,6 +612,11 @@ class GlobalBindings { if (this.selfMute()) { voiceHandler.setMute(true) } + + this.client.setAudioQuality( + this.settings.audioBitrate, + this.settings.samplesPerPacket + ) } this.messageBoxHint = ko.pureComputed(() => { diff --git a/app/voice.js b/app/voice.js index 1703c36..b3bff80 100644 --- a/app/voice.js +++ b/app/voice.js @@ -9,9 +9,10 @@ import vad from 'voice-activity-detection' import DropStream from 'drop-stream' class VoiceHandler extends Writable { - constructor (client) { + constructor (client, settings) { super({ objectMode: true }) this._client = client + this._settings = settings this._outbound = null this._mute = false } @@ -47,7 +48,7 @@ class VoiceHandler extends Writable { }) this._outbound - .pipe(chunker(4 * 480)) + .pipe(chunker(4 * this._settings.samplesPerPacket)) .pipe(buffer2Float32Array) .pipe(this._client.createVoiceStream()) @@ -71,8 +72,8 @@ class VoiceHandler extends Writable { } export class ContinuousVoiceHandler extends VoiceHandler { - constructor (client) { - super(client) + constructor (client, settings) { + super(client, settings) } _write (data, _, callback) { @@ -85,9 +86,9 @@ export class ContinuousVoiceHandler extends VoiceHandler { } export class PushToTalkVoiceHandler extends VoiceHandler { - constructor (client, key) { - super(client) - this._key = key + constructor (client, settings) { + super(client, settings) + this._key = settings.pttKey this._pushed = false this._keydown_handler = () => this._pushed = true this._keyup_handler = () => { @@ -114,8 +115,9 @@ export class PushToTalkVoiceHandler extends VoiceHandler { } export class VADVoiceHandler extends VoiceHandler { - constructor (client, level) { - super(client) + constructor (client, settings) { + super(client, settings) + let level = settings.vadLevel const self = this this._vad = vad(audioContext, theUserMedia, { onVoiceStart () {