-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathreal-time-file-example.ts
72 lines (62 loc) · 2.2 KB
/
real-time-file-example.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
/**
* This file showcases the real-time-client package being used in NodeJS.
*
* It will connect to the real-time API and transcribe a file in real-time.
* To run this example, you will need to have a Speechmatics API key,
* which can be generated from the Speechmatics Portal: https://portal.speechmatics.com/api-keys
*
* NOTE: This script is run as an ES Module via tsx, letting us use top-level await.
* The library also works with CommonJS, but the code would need to be wrapped in an async function.
*/
import { RealtimeClient } from '@speechmatics/real-time-client';
import fs from 'node:fs';
import dotenv from 'dotenv';
import { createSpeechmaticsJWT } from '@speechmatics/auth';
dotenv.config();
const apiKey = process.env.API_KEY;
if (!apiKey) {
throw new Error('Please set the API_KEY environment variable');
}
const client = new RealtimeClient();
let finalText = '';
client.addEventListener('receiveMessage', ({ data }) => {
if (data.message === 'AddPartialTranscript') {
const partialText = data.results
.map((r) => r.alternatives?.[0].content)
.join(' ');
process.stdout.write(`\r${finalText} \x1b[3m${partialText}\x1b[0m`);
} else if (data.message === 'AddTranscript') {
const text = data.results.map((r) => r.alternatives?.[0].content).join(' ');
finalText += text;
process.stdout.write(`\r${finalText}`);
} else if (data.message === 'EndOfTranscript') {
process.stdout.write('\n');
process.exit(0);
}
});
const jwt = await createSpeechmaticsJWT({
type: 'rt',
apiKey,
ttl: 60, // 1 minute
});
const fileStream = fs.createReadStream('./example.wav', {
highWaterMark: 4096, // avoid sending too much data at once
});
await client.start(jwt, {
transcription_config: {
language: 'en',
enable_partials: true,
operating_point: 'enhanced',
},
});
//send it
fileStream.on('data', (sample) => {
client.sendAudio(sample);
});
//end the session
fileStream.on('end', () => {
// Send a stop message to the server when we're done sending audio.
// We set `noTimeout` because we are streaming faster than real-time,
// so we should wait for all the data to be processed before closing the connection.
client.stopRecognition({ noTimeout: true });
});