-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
61 lines (57 loc) · 2 KB
/
example.js
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
const fs = require('fs');
const { TranscribeStreamingClient, StartMedicalStreamTranscriptionCommand, StartStreamTranscriptionCommand, Specialty, Type } = require('@aws-sdk/client-transcribe-streaming');
function wait(time) {
return new Promise(resolve => {
setTimeout(resolve, time);
})
}
async function* audioSource() {
const chunkSize = 10 * 1000;
const fileBuf = fs.readFileSync('./testFile.wav');
let index = 0;
let i = 0;
while(index < fileBuf.length) {
// while(index < chunkSize * 60) {
const chunk = fileBuf.slice(index, Math.min(index + chunkSize, fileBuf.byteLength));
await wait(300);
yield chunk;
index += chunkSize;
}
}
(async () => {
console.log(`***start: [${new Date()}]`);
async function * audioStream() {
for await(const chunk of audioSource()) {
yield {AudioEvent: {AudioChunk: chunk}}
}
}
const client = new TranscribeStreamingClient({region: "us-west-2"});
const command = new StartStreamTranscriptionCommand({
LanguageCode: 'en-US',
MediaSampleRateHertz: 16000,
MediaEncoding: 'pcm',
AudioStream: audioStream(),
// Specialty: Specialty.CARDIOLOGY,
// Type: Type.CONVERSATION
});
try {
const data = await client.send(command, {sessionTimeout: 2000});
// console.log(data.TranscriptResultStream)
for await (const event of data.TranscriptResultStream) {
if(event.TranscriptEvent) {
const results = event.TranscriptEvent.Transcript.Results;
results.map(result => {
(result.Alternatives || []).map(alternative => {
const str = alternative.Items.map(item => item.Content).join(' ');
console.log(str)
})
})
}
}
console.log('DONE', data);
client.destroy();
} catch(e) {
console.log('ERROR: ', e);
process.exit(1);
}
})()