-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtranscription-service.js
45 lines (40 loc) · 1.27 KB
/
transcription-service.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
const { Deepgram } = require("@deepgram/sdk");
const EventEmitter = require("events");
class TranscriptionService extends EventEmitter {
constructor() {
super();
const deepgram = new Deepgram(process.env.DEEPGRAM_API_KEY);
this.deepgramLive = deepgram.transcription.live({
encoding: "mulaw",
sample_rate: "8000",
model: "nova",
punctuate: true,
interim_results: false,
});
this.deepgramLive.addListener("transcriptReceived", (transcriptionMessage) => {
const transcription = JSON.parse(transcriptionMessage);
const text = transcription.channel?.alternatives[0]?.transcript;
if (text) {
this.emit("transcription", text);
}
});
this.deepgramLive.addListener("error", (error) => {
console.error("deepgram error");
console.error(error);
});
this.deepgramLive.addListener("close", () => {
console.log("Deepgram connection closed");
});
}
/**
* Send the payload to Deepgram
* @param {String} payload A base64 MULAW/8000 audio stream
*/
send(payload) {
// TODO: Buffer up the media and then send
if (this.deepgramLive.getReadyState() === 1) {
this.deepgramLive.send(Buffer.from(payload, "base64"));
}
}
}
module.exports = { TranscriptionService }