Add push-to-talk voice handler

This commit is contained in:
Jonas Herzig 2017-09-19 20:57:08 +02:00
parent 9ad548e105
commit ef14f9b61f
3 changed files with 33 additions and 2 deletions

View file

@ -8,7 +8,7 @@ import Resampler from 'libsamplerate.js'
import ko from 'knockout' import ko from 'knockout'
import _dompurify from 'dompurify' import _dompurify from 'dompurify'
import { ContinuousVoiceHandler, initVoice } from './voice' import { ContinuousVoiceHandler, PushToTalkVoiceHandler, initVoice } from './voice'
const dompurify = _dompurify(window) const dompurify = _dompurify(window)
@ -309,7 +309,7 @@ class GlobalBindings {
if (mode === 'cont') { if (mode === 'cont') {
voiceHandler = new ContinuousVoiceHandler(this.client) voiceHandler = new ContinuousVoiceHandler(this.client)
} else if (mode === 'ptt') { } else if (mode === 'ptt') {
voiceHandler = new PushToTalkVoiceHandler(this.client, 'ctrl + shift')
} else if (mode === 'vad') { } else if (mode === 'vad') {
} else { } else {

View file

@ -4,6 +4,7 @@ import audioContext from 'audio-context'
import chunker from 'stream-chunker' import chunker from 'stream-chunker'
import Resampler from 'libsamplerate.js' import Resampler from 'libsamplerate.js'
import getUserMedia from 'getusermedia' import getUserMedia from 'getusermedia'
import keyboardjs from 'keyboardjs'
class VoiceHandler extends Writable { class VoiceHandler extends Writable {
constructor (client) { constructor (client) {
@ -44,6 +45,35 @@ export class ContinuousVoiceHandler extends VoiceHandler {
} }
} }
export class PushToTalkVoiceHandler extends VoiceHandler {
constructor (client, key) {
super(client)
this._key = key
this._pushed = false
this._keydown_handler = () => this._pushed = true
this._keyup_handler = () => {
this._stopOutbound()
this._pushed = false
}
keyboardjs.bind(this._key, this._keydown_handler, this._keyup_handler)
}
_write (data, _, callback) {
if (this._pushed) {
this._getOrCreateOutbound().write(data, callback)
} else {
callback()
}
}
_final (callback) {
super._final(e => {
keyboardjs.unbind(this._key, this._keydown_handler, this._keyup_handler)
callback(e)
})
}
}
export function initVoice (onData, onUserMediaError) { export function initVoice (onData, onUserMediaError) {
var resampler = new Resampler({ var resampler = new Resampler({
unsafe: true, unsafe: true,

View file

@ -31,6 +31,7 @@
"getusermedia": "^2.0.0", "getusermedia": "^2.0.0",
"html-loader": "^0.4.4", "html-loader": "^0.4.4",
"json-loader": "^0.5.4", "json-loader": "^0.5.4",
"keyboardjs": "^2.3.4",
"knockout": "^3.4.0", "knockout": "^3.4.0",
"lodash.assign": "^4.2.0", "lodash.assign": "^4.2.0",
"microphone-stream": "^3.0.5", "microphone-stream": "^3.0.5",