-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
84 lines (71 loc) · 1.97 KB
/
app.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import "log-timestamp";
import WebSocket from "ws";
import mic from "mic";
import AudioPlayer from "./audio-player.js";
const INPUT_SAMPLE_RATE = 16000;
const OUTPUT_SAMPLE_RATE = 48000;
const CONFIG = {
type: "SettingsConfiguration",
audio: {
input: {
encoding: "linear16",
sample_rate: INPUT_SAMPLE_RATE
},
output: {
encoding: "linear16",
sample_rate: OUTPUT_SAMPLE_RATE,
container: "none",
}
},
agent: {
listen: {
model: "nova-2"
},
think: {
provider: {
type: "open_ai"
},
model: "gpt-3.5-turbo",
instructions: "You are a helpful assistant."
},
speak: {
model: "aura-asteria-en"
}
}
}
const audioPlayer = new AudioPlayer(OUTPUT_SAMPLE_RATE);
const ws = new WebSocket(`${getEnv("VOICE_AGENT_URL")}`, {
headers: { authorization: `token ${getEnv("DEEPGRAM_API_KEY")}` }
});
ws.on("open", function open() {
ws.send(JSON.stringify(CONFIG));
startStreamingFromMicrophone(ws);
});
ws.on("message", function message(data, isBinary) {
if (isBinary) {
audioPlayer.play(data);
} else {
console.log(`Got text message: ${data}`);
if (JSON.parse(data)["type"] === "UserStartedSpeaking") {
audioPlayer.stop();
}
}
});
function startStreamingFromMicrophone(websocket) {
var micInstance = mic({ rate: INPUT_SAMPLE_RATE, channels: 1 });
var micInputStream = micInstance.getAudioStream();
micInputStream.on('data', function (data) {
websocket.send(data);
});
micInputStream.on('error', function (err) {
console.log("Microphone error: " + err);
});
micInstance.start();
}
function getEnv(name) {
let val = process.env[name];
if ((val === undefined) || (val === null)) {
throw (`Please set the environment variable: ${name}`);
}
return val;
}