-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhackathon_practice.js
More file actions
73 lines (54 loc) · 1.54 KB
/
Copy pathhackathon_practice.js
File metadata and controls
73 lines (54 loc) · 1.54 KB
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
import fs from "node:fs"
import { Readable } from "node:stream"
import { AssemblyAI } from "assemblyai"
import recorder from "node-record-lpcm16"
const run = async () => {
const client = new AssemblyAI({
apiKey: "ff33fd8b26f14cb596840a33352ba7c8", // Replace with your chosen API key
})
const transcriber = client.streaming.transcriber({
sampleRate: 16_000,
formatTurns: true,
})
const transcriptionFile = "transcriptions.txt"
transcriber.on("open", ({ id }) => {
console.log(`Session opened with ID: ${id}`)
})
transcriber.on("error", error => {
console.error("Error:", error)
})
transcriber.on("close", (code, reason) =>
console.log("Session closed:", code, reason),
)
transcriber.on("turn", turn => {
if (!turn.transcript) {
return
}
console.log("Turn:", turn.transcript)
// Save the transcription to a file
fs.appendFileSync(transcriptionFile, `${turn.transcript}\n`)
})
try {
console.log("Connecting to streaming transcript service")
await transcriber.connect()
console.log("Starting recording")
const recording = recorder.record({
channels: 1,
sampleRate: 16_000,
audioType: "wav", // Linear PCM
})
Readable.toWeb(recording.stream()).pipeTo(transcriber.stream())
// Stop recording and close connection using Ctrl-C.
process.on("SIGINT", async () => {
console.log()
console.log("Stopping recording")
recording.stop()
console.log("Closing streaming transcript connection")
await transcriber.close()
process.exit()
})
} catch (error) {
console.error(error)
}
}
run()