diff options
author | Stanley Yip <33562077+yipstanley@users.noreply.github.com> | 2019-07-30 15:17:26 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-30 15:17:26 -0400 |
commit | cc271d2802cab295221dc4ea3d8c77a6328a8cfb (patch) | |
tree | b3868ce3d848e3a683102093dd41ac70cf2b40d8 /src/client/util/DictationManager.ts | |
parent | 2d0c2f56c5160f2796e5dfff31e391d5e64a806f (diff) | |
parent | 1aac1e8820c62a5f06d7e7630394e0bd58b19a94 (diff) |
Merge pull request #231 from browngraphicslab/speech-to-text
Speech to text
Diffstat (limited to 'src/client/util/DictationManager.ts')
-rw-r--r-- | src/client/util/DictationManager.ts | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/client/util/DictationManager.ts b/src/client/util/DictationManager.ts new file mode 100644 index 000000000..b58bdb6c7 --- /dev/null +++ b/src/client/util/DictationManager.ts @@ -0,0 +1,39 @@ +namespace CORE { + export interface IWindow extends Window { + webkitSpeechRecognition: any; + } +} + +const { webkitSpeechRecognition }: CORE.IWindow = window as CORE.IWindow; + +export default class DictationManager { + public static Instance = new DictationManager(); + private isListening = false; + private recognizer: any; + + constructor() { + this.recognizer = new webkitSpeechRecognition(); + this.recognizer.interimResults = false; + this.recognizer.continuous = true; + } + + finish = (handler: any, data: any) => { + handler(data); + this.isListening = false; + this.recognizer.stop(); + } + + listen = () => { + if (this.isListening) { + return undefined; + } + this.isListening = true; + this.recognizer.start(); + return new Promise<string>((resolve, reject) => { + this.recognizer.onresult = (e: any) => this.finish(resolve, e.results[0][0].transcript); + this.recognizer.onerror = (e: any) => this.finish(reject, e); + }); + + } + +}
\ No newline at end of file |