-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathstt.ts
44 lines (37 loc) · 1.36 KB
/
stt.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
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
//
// SPDX-License-Identifier: Apache-2.0
import { type JobContext, WorkerOptions, cli, defineAgent, stt } from '@livekit/agents';
import { STT } from '@livekit/agents-plugin-deepgram';
import type { Track } from '@livekit/rtc-node';
import { AudioStream, RoomEvent, TrackKind } from '@livekit/rtc-node';
import { fileURLToPath } from 'node:url';
export default defineAgent({
entry: async (ctx: JobContext) => {
await ctx.connect();
console.log('starting STT example agent');
const transcribeTrack = async (track: Track) => {
const audioStream = new AudioStream(track);
const sttStream = new STT({ sampleRate: 48000 }).stream();
const sendTask = async () => {
for await (const event of audioStream) {
sttStream.pushFrame(event);
}
};
const recvTask = async () => {
for await (const event of sttStream) {
if (event.type === stt.SpeechEventType.FINAL_TRANSCRIPT) {
console.log(event.alternatives![0].text);
}
}
};
Promise.all([sendTask(), recvTask()]);
};
ctx.room.on(RoomEvent.TrackSubscribed, async (track: Track) => {
if (track.kind === TrackKind.KIND_AUDIO) {
transcribeTrack(track);
}
});
},
});
cli.runApp(new WorkerOptions({ agent: fileURLToPath(import.meta.url) }));