-
Notifications
You must be signed in to change notification settings - Fork 28
/
speech-to-text.service.ts
124 lines (110 loc) Β· 3.3 KB
/
speech-to-text.service.ts
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { Injectable, NgZone } from '@angular/core';
import { MatSnackBar } from '@angular/material/snack-bar';
import { Observable } from 'rxjs/internal/Observable';
import { NlpService } from '@app/core/nlp/nlp.service';
@Injectable({
providedIn: 'root'
})
export class SpeechToTextService {
recognizing = false;
recognition;
start_timestamp = 0;
onresult$: Observable<string>;
onend$: Observable<{}>;
onerror$: Observable<{}>;
onstart$: Observable<{}>;
constructor() {}
listen() {
if (!('webkitSpeechRecognition' in window)) {
console.error('Your device is not compatible with this easter egg. sorry!');
} else {
this.recognition = new window['webkitSpeechRecognition']();
this.recognition.continuous = true;
this.recognition.interimResults = true;
this.setupListeners();
}
if (this.recognizing) {
this.recognition.stop();
return;
}
this.recognition.lang = 'en-US';
this.recognition.start();
this.start_timestamp = Date.now();
}
private setupListeners() {
this.onstart$ = new Observable(observer => {
this.recognition.onstart = () => {
this.handleOnStart();
observer.next(event);
};
return () => (this.recognition.onstart = null);
});
this.onerror$ = new Observable(observer => {
this.recognition.onerror = event => {
this.handleOnError(event);
observer.next(event.error);
};
return () => (this.recognition.onerror = null);
});
this.onend$ = new Observable(observer => {
this.recognition.onend = event => {
this.handleOnEnd(event);
observer.next(event);
};
return () => (this.recognition.onend = null);
});
this.onresult$ = new Observable(observer => {
this.recognition.onresult = event => {
const transcript = this.handleOnResult(event);
observer.next(transcript);
};
return () => (this.recognition.onresult = null);
});
}
stop() {
this.recognition.stop();
}
start() {
this.recognition.start();
}
private handleOnStart() {
this.recognizing = true;
}
private handleOnResult(event) {
let interim_transcript = '';
let final_transcript = '';
if (typeof event.results === 'undefined') {
this.recognition.onend = null;
this.recognition.stop();
console.error(`Sorry, your device is not compatible with Speech-To-Text technology.`);
return;
}
for (let i = event.resultIndex; i < event.results.length; ++i) {
if (event.results[i].isFinal) {
final_transcript += event.results[i][0].transcript;
} else {
interim_transcript += event.results[i][0].transcript;
}
}
return final_transcript;
}
private handleOnEnd(event) {
this.recognizing = false;
}
private handleOnError(event) {
if (event.error === 'no-speech') {
console.log('info_no_speech');
console.error(`Sorry, I didn't hear anything.`);
}
if (event.error === 'audio-capture') {
console.error(`Sorry, your mic isn't available.`);
}
if (event.error === 'not-allowed') {
if (event.timeStamp - this.start_timestamp < 100) {
console.error(`Sorry, I was blocked from access your mic.`);
} else {
console.error(`Sorry, I couln't access your mic.`);
}
}
}
}