diff options
author | dash <stanley_yip@brown.edu> | 2019-07-30 17:03:27 -0400 |
---|---|---|
committer | dash <stanley_yip@brown.edu> | 2019-07-30 17:03:27 -0400 |
commit | 5ec18c73ec04d0f50cedb220518be9c58e62997c (patch) | |
tree | e7530d6301d752da51c41629bdb94be52c83bed3 /src/client/util/DictationManager.ts | |
parent | 8ff901c58963aca8bbe5168d233e579c8aa0686c (diff) | |
parent | fd4760cc038ce36eb1974318cb867f8c2476c363 (diff) |
Merge branch 'master' of https://github.com/browngraphicslab/Dash-Web
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 |