generated from GenesysCloudBlueprints/blueprint-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
asr-service.ts
75 lines (66 loc) · 2.15 KB
/
asr-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
import EventEmitter from 'events';
/*
* This class provides ASR support for the incoming audio from the Client.
* The following events are expected from the session:
*
* Name; error
* Parameters: Error message string or error object.
*
* Name: transcript
* Parameters: `Transcript` object.
*
* Name: final-transcript
* Parameters: `Transcript` object.
*
* The current usage of this class requires that a new instance be created once
* the final transcript has been received.
*/
export class ASRService {
private emitter = new EventEmitter();
private state = 'None';
private byteCount = 0;
on(event: string, listener: (...args: any[]) => void): ASRService {
this.emitter?.addListener(event, listener);
return this;
}
getState(): string {
return this.state;
}
/*
* For this implementation, we are just going to count the number of bytes received.
* Once we get "enough" bytes, we'll treat this as a completion. In a real-world
* scenario, an actual ASR engine should be invoked to process the audio bytes.
*/
processAudio(data: Uint8Array): ASRService {
if (this.state === 'Complete') {
this.emitter.emit('error', 'Speech recognition has already completed.');
return this;
}
this.byteCount += data.length;
/*
* If we get enough audio bytes, mark this instance as complete, send out the event,
* and reset the count to help prevent issues if this instance is attempted to be reused.
*
* 40k bytes equates to 5 seconds of 8khz PCMU audio.
*/
if (this.byteCount >= 40000) {
this.state = 'Complete';
this.emitter.emit('final-transcript', {
text: 'I would like to check my account balance.',
confidence: 1.0
});
this.byteCount = 0;
return this;
}
this.state = 'Processing';
return this;
}
}
export class Transcript {
text: string;
confidence: number;
constructor(text: string, confidence: number) {
this.text = text;
this.confidence = confidence;
}
}