forked from hyoo-ru/mam_mol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
speech.ts
239 lines (181 loc) · 4.95 KB
/
speech.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
namespace $ {
interface SpeechResultsEvent extends Event {
resultIndex: number
results: SpeechRecognitionResultList
}
/**
* Web Speech API
* @see https://mol.hyoo.ru/#!section=demos/demo=mol_speech_demo
* @see https://developer.mozilla.org/en-US/docs/Web/API/Web_Speech_API
*/
export class $mol_speech extends $mol_plugin {
static speaker_make() {
return new Promise< SpeechSynthesis >( done => {
const API = $mol_dom_context.speechSynthesis
if( API.getVoices().length ) return done( API )
const on_voices = ( event : Event )=> {
if( !API.getVoices().length ) return
API.removeEventListener( 'voiceschanged' , on_voices )
done( API )
}
API.addEventListener( 'voiceschanged' , on_voices )
} )
}
@ $mol_mem
static speaker() {
return $mol_wire_sync( this ).speaker_make()
}
@ $mol_mem
static voices() {
const lang = this.$.$mol_locale.lang()
return this.speaker().getVoices().filter( voice => voice.lang.split('-')[0] === lang )
}
@ $mol_action
static say( text : string ) {
const speaker = this.speaker()
speaker.cancel()
speaker.resume()
const rate = 1
const voice = this.voices()[ this.voices().length - 1 ]
const pitch = 1
var utter = new SpeechSynthesisUtterance( text )
utter.voice = voice
utter.rate = rate
utter.pitch = pitch
speaker.speak( utter )
return null as null
}
@ $mol_mem
static speaking( next = true ) {
if( next ) this.speaker().resume()
else this.speaker().pause()
return next
}
@ $mol_mem
static hearer() {
$mol_wire_solid()
let Api
for (const prefix of ['', 'webkit', 'moz', 'ms']) {
if (Api = (window as any)[prefix + 'SpeechRecognition']) {
break
}
}
const api = new Api
api.interimResults = true
api.maxAlternatives = 1
api.continuous = true
api.lang = $mol_locale.lang()
api.onnomatch = ( event : any )=> {
api.stop()
return null
}
api.onresult = ( event: SpeechResultsEvent )=> {
this.recognition_index( [ ... event.results ].filter( res => res.isFinal ).length )
const recognition = event.results[ event.resultIndex ]
const index = event.resultIndex + this.recognition_offset()
this.recognition( index, recognition )
return null
}
api.onerror = ( event : ErrorEvent )=> {
if( event.error === 'no-speech' ) return null
console.log(event)
console.error( new Error( ( event as any ).error || event ) )
api.stop()
return null
}
api.onend = ( event : any )=> {
if( this.recognition_index() > 0 ) {
this.recognition_offset( this.recognition_offset() + this.recognition_index() )
}
this.recognition_index( -1 )
if( this.hearing() ) api.start()
}
api.onspeechend = ( event : any )=> {
api.stop()
}
return api;
}
@ $mol_mem
static hearing( next? : boolean ) {
if( next === undefined ) return false
if( next ) {
this.hearer().start()
} else {
this.hearer().stop()
}
return next
}
@ $mol_mem
static recognition_index( next = -1 ) {
$mol_wire_solid()
return next
}
@ $mol_mem
static recognition_offset( next = 0 ) {
$mol_wire_solid()
return next
}
@ $mol_mem_key
static recognition( index: number, next?: SpeechRecognitionResult ) {
$mol_wire_solid()
return next ?? null
}
@ $mol_mem
static recognitions() {
if( !this.hearing() ) return []
return $mol_range2(
index => this.recognition( index )!,
()=> Math.max( 0, this.recognition_index() + this.recognition_offset() ),
)
}
@ $mol_mem
static commands() {
return this.recognitions().map( result => result[0].transcript.toLowerCase().trim().replace( /[,\.]/g , '' ) )
}
@ $mol_mem
static text() {
return this.recognitions().map( result => result[0].transcript ).join( '' )
}
@ $mol_mem
commands_skip( next = 0 ) {
$mol_wire_solid()
$mol_speech.hearing()
return next
}
@ $mol_mem
render() : null {
const matchers = this.matchers()
const commands = $mol_speech.commands()
for( let i = this.commands_skip() ; i < commands.length ; ++ i ) {
for( let matcher of matchers ) {
const found = commands[i].match( matcher )
if( !found ) continue
new $mol_after_work( 16, ()=> {
this.commands_skip( i + 1 )
$mol_wire_async( this ).event_catch( found.slice( 1 ) )
} )
return null
}
}
return null
}
event_catch( found? : string[] ) {
return false
}
patterns() {
return [] as readonly string[]
}
@ $mol_mem
matchers() {
return this.patterns().map( pattern => {
return new RegExp( this.prefix() + pattern + this.suffix() , 'i' )
} )
}
prefix() {
return ''
}
suffix() {
return '[,\\s]+(?:please|would you kindly|пожалуйста|пожалуй 100|будь любезен|будь любезна|будь добра?|плиз)\.?$'
}
}
}