diff --git a/app/index.js b/app/index.js index fa4e8b2..10005da 100644 --- a/app/index.js +++ b/app/index.js @@ -8,7 +8,7 @@ import Resampler from 'libsamplerate.js' import ko from 'knockout' import _dompurify from 'dompurify' -import { ContinuousVoiceHandler, initVoice } from './voice' +import { ContinuousVoiceHandler, PushToTalkVoiceHandler, initVoice } from './voice' const dompurify = _dompurify(window) @@ -309,7 +309,7 @@ class GlobalBindings { if (mode === 'cont') { voiceHandler = new ContinuousVoiceHandler(this.client) } else if (mode === 'ptt') { - + voiceHandler = new PushToTalkVoiceHandler(this.client, 'ctrl + shift') } else if (mode === 'vad') { } else { diff --git a/app/voice.js b/app/voice.js index e6fc026..73ac001 100644 --- a/app/voice.js +++ b/app/voice.js @@ -4,6 +4,7 @@ import audioContext from 'audio-context' import chunker from 'stream-chunker' import Resampler from 'libsamplerate.js' import getUserMedia from 'getusermedia' +import keyboardjs from 'keyboardjs' class VoiceHandler extends Writable { 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) { var resampler = new Resampler({ unsafe: true, diff --git a/package.json b/package.json index 53864d9..e6721ff 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "getusermedia": "^2.0.0", "html-loader": "^0.4.4", "json-loader": "^0.5.4", + "keyboardjs": "^2.3.4", "knockout": "^3.4.0", "lodash.assign": "^4.2.0", "microphone-stream": "^3.0.5",