aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/DictationManager.ts
blob: b58bdb6c71510cd9bce7670e45b86c24efe4edaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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);
        });

    }

}