-
Notifications
You must be signed in to change notification settings - Fork 9
/
aws.ts
340 lines (290 loc) · 10 KB
/
aws.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/* eslint-disable immutable/no-this */
import {EventStreamMarshaller} from '@aws-sdk/eventstream-marshaller'
import {fromUtf8, toUtf8} from '@aws-sdk/util-utf8-node'
import {Credentials} from 'aws-sdk'
import {ifElse, not, pathEq, pathOr, pathSatisfies, propSatisfies, tap, when} from 'ramda'
import {allPass, createPipe, prop} from 'remeda'
import crypto from 'webcrypto'
import {createPresignedURL, getCredentials} from '../lib/awsV4'
import Connection from '../lib/Connection'
import {CustomEventTarget} from '../lib/CustomEventTarget'
import MicStream from '../lib/MicStream'
import {convertAudioToBinaryMessage} from '../lib/audioUtils'
import {AWSSpeechRecognitionEvent, AWSTranscribeResponse, Config, ListenerCallback} from '../types/shared'
type requiredConfigs = Pick<Config, "region" | "IdentityPoolId">
type optionalConfigs = Omit<Config, "region" | "IdentityPoolId">
export type configArgs = requiredConfigs & Partial<optionalConfigs>
class AWSRecognizer extends CustomEventTarget implements SpeechRecognition {
/** in case future recognizers are built in the future (e.g. Azure) */
static type = 'AWS'
/** true if the library is supported by the currenly browser */
static isSupported = !!navigator?.mediaDevices?.getUserMedia
/** polyfill-specific config */
public config: Config
/** if the library is currently capturing/transcribing audio */
public listening = false
/** the langage (default en-US) */
public lang: Config['lang']
/** whether to continously transribe audio until .stop() is called */
public continuous: boolean
/** a proxy for new AWSRecognizer(config) */
static create(config: configArgs): typeof SpeechRecognition {
return class AWSRecognizerWithConfig extends AWSRecognizer {
constructor() {
super(config)
}
}
}
constructor(config: configArgs) {
super()
if (!config.IdentityPoolId || !config.region) throw new Error('Could not create AWS recognizer: missing configuration, see: https://github.com/ceuk/speech-recognition-aws-polyfill#configuration')
const defaults: optionalConfigs = {
sampleRate: 12000,
lang: 'en-US',
continuous: false
}
this.config = Object.assign(defaults, config)
this.lang = this.config.lang
this.continuous = this.config.continuous
}
/** start capturing/transcribing audio */
start() {
if (this.listening) return
this.dispatchEvent(new Event('start'))
navigator.mediaDevices.getUserMedia({audio: true, video: false})
.then(this.establishConnection.bind(this))
.catch(err => {
this.emitError(err)
});
}
/** stop capturing and return any final transcriptions */
public stop() {
MicStream.getInstance()?.end()
Connection.getInstance()?.close()
this.listening = false
this.dispatchEvent(new Event('audioend'))
}
/** stop capturing and don't emit any transcibed audio */
public abort() {
if (this.listening) {
MicStream.getInstance()?.end()
Connection.getInstance()?.close()
this.listening = false
this.dispatchEvent(new Event('audioend'))
}
}
/** dispatch transcription result */
private emitResult(transcript: string) {
if (!this.continuous && this.listening) {
this.stop()
}
if (transcript && transcript.length > 1) {
this.dispatchEvent(new AWSSpeechRecognitionEvent('result',
[{
0: {
transcript,
confidence: 1
},
isFinal: !this.listening
}]
))
} else {
this.dispatchEvent(new Event('nomatch'))
}
if (!this.listening) {
this.dispatchEvent(new Event('end'))
}
}
/** dispatch error event */
private emitError(error: Error) {
this.stop()
this.dispatchEvent(new ErrorEvent('error', error))
}
/** dispatch events related to sound start */
private emitSoundStart() {
this.dispatchEvent(new ErrorEvent('speechstart'))
this.dispatchEvent(new ErrorEvent('soundstart'))
}
/** dispatch events realated to sound end */
private emitSoundEnd() {
this.dispatchEvent(new ErrorEvent('speechend'))
this.dispatchEvent(new ErrorEvent('soundend'))
}
/** authenticate and connect to AWS Transcribe */
private async establishConnection(mediaStream: MediaStream) {
this.listening = true
this.dispatchEvent(new Event('audiostart'))
try {
const {IdentityPoolId, region} = this.config
const credentials = await getCredentials({IdentityPoolId, region}) as Credentials
Connection.setUrl(this.getSignedURL(credentials))
MicStream.setStream(mediaStream)
this.streamAudioToWebSocket()
} catch (err) {
if (err instanceof Error) {
this.emitError(err)
}
}
}
/** get a signed url using specified credentials */
private getSignedURL(credentials: Credentials) {
const endpoint = `transcribestreaming.${this.config.region}.amazonaws.com:8443`
return createPresignedURL(
'GET',
endpoint,
'/stream-transcription-websocket',
'transcribe',
crypto.createHash('sha256').update('', 'utf8').digest('hex'),
{
key: credentials.accessKeyId,
secret: credentials.secretAccessKey,
timestamp: Date.now(),
sessionToken: credentials.sessionToken,
protocol: 'wss',
expires: 15,
region: this.config.region,
query: `language-code=${this.lang}&media-encoding=pcm&sample-rate=${this.config.sampleRate}`
}
)
}
/** handle streaming received audio buffer to AWS transcribe */
private streamAudioToWebSocket() {
try {
// when we get audio data from the mic, send it to the WebSocket if possible
const connection = Connection.getInstance()
if (!connection) {
console.error('no usable connection')
return
}
connection.onopen = () => {
const micStream = MicStream.getInstance()
if (!micStream) {
console.error('no usable stream')
return
}
// emit sound start events
this.emitSoundStart()
// when audio is received from the mic stream, send it AWS
micStream.on('data', createPipe(
// emit the sound end if we are about to stop capturing
when(
() => !this.continuous,
tap(() => this.emitSoundEnd()),
),
// the audio stream is raw audio bytes. Transcribe expects PCM with additional metadata, encoded as binary
(audioChunk: Buffer) => convertAudioToBinaryMessage(audioChunk, this.config.sampleRate),
when(() => Connection.isActive(), connection.send.bind(connection))
))
// handle messages, errors, and close events
this.handleSocketMessages()
}
} catch (error) {
if (error instanceof Error) {
this.emitError(error)
}
}
}
/** handle websocket responses */
private handleSocketMessages() {
const eventStreamMarshaller = new EventStreamMarshaller(toUtf8, fromUtf8)
const stringEncode = (data: ArrayBufferLike) => new TextDecoder('utf-8').decode(data)
// convert the binary event stream message to JSON
type ParseMessageBody = (response: {body: ArrayBufferLike}) => AWSTranscribeResponse
const parseMessageBody: ParseMessageBody = createPipe(
prop('body'),
stringEncode,
JSON.parse.bind(JSON)
)
const connection = Connection.getInstance()
if (connection) {
connection.onmessage = createPipe(
prop('data'),
Buffer.from,
(buffer: Buffer) => eventStreamMarshaller.unmarshall(buffer) as MessageEvent,
ifElse(
pathEq(['headers', ':message-type', 'value'], 'event'),
// valid response
createPipe(
parseMessageBody,
pathOr([], ['Transcript', 'Results']),
when(
// validate the results
allPass([
propSatisfies((x: number) => x > 0, 'length'),
pathSatisfies((x: number) => x > 0, [0, 'Alternatives', 'length']),
pathSatisfies(not, [0, 'IsPartial'])
]),
// emit the transcription result
createPipe(
pathOr('', [0, 'Alternatives', 0, 'Transcript']),
decodeURIComponent,
this.emitResult.bind(this)
)
)
),
// error response
createPipe(
parseMessageBody,
prop('Message'),
console.error
)
)
)
}
}
// stub some unimplemented props/methods
set interimResults(_) {
console.warn('`continous` is not yet implemented in the AWS polyfill')
}
get interimResults() {
return false
}
set maxAlternatives(_) {
console.warn('`maxAlternatives` is not yet implemented in the AWS polyfill')
}
get maxAlternatives() {
return 1
}
set grammars(_) {
console.warn('`grammars` is not yet implemented in the AWS polyfill')
}
get grammars() {
console.warn('`grammars` is not yet implemented in the AWS polyfill')
return SpeechGrammar ? new SpeechGrammarList() : ([] as unknown as SpeechGrammarList)
}
// proxy event listeners
set onaudiostart(fn: ListenerCallback) {
this.addEventListener('audiostart', fn)
}
set onaudioend(fn: ListenerCallback) {
this.addEventListener('audioend', fn)
}
set onend(fn: ListenerCallback) {
this.addEventListener('end', fn)
}
set onerror(fn: ListenerCallback) {
this.addEventListener('error', fn)
}
set onnomatch(fn: ListenerCallback) {
this.addEventListener('nomatch', fn)
}
set onresult(fn: ListenerCallback) {
this.addEventListener('result', fn)
}
set onsoundstart(fn: ListenerCallback) {
this.addEventListener('soundstart', fn)
}
set onsoundend(fn: ListenerCallback) {
this.addEventListener('soundend', fn)
}
set onspeechstart(fn: ListenerCallback) {
this.addEventListener('speechstart', fn)
}
set onspeechend(fn: ListenerCallback) {
this.addEventListener('speechend', fn)
}
set onstart(fn: ListenerCallback) {
this.addEventListener('start', fn)
}
}
export default AWSRecognizer