forked from Carter-Labs-Ltd/carter-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
143 lines (123 loc) · 4.08 KB
/
index.js
File metadata and controls
143 lines (123 loc) · 4.08 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// on document ready
document.addEventListener("DOMContentLoaded", function (event) {
const typewriterContainer = document.getElementById("typewriter-container");
const animationDuration = 1000; // Duration of the typing animation in milliseconds
const recordButton = document.getElementById("recordButton");
const urlParams = new URLSearchParams(window.location.search);
const apiKey = urlParams.get("key");
const stalls = [
"one second",
"hmm, let's see",
"uh, one moment",
"got it",
"ok",
"just a moment",
"gotchya",
"ah",
];
let recording = false;
let myvad = null;
function processAudio(audio) {
const audioDuration = audio.length / 16000;
if (audioDuration < 0.5) {
recordButton.innerText = "Start Recording";
return false;
}
if (audioDuration > 2) {
speakRandomStall();
}
return true;
}
function speakRandomStall() {
speak(stalls[Math.floor(Math.random() * stalls.length)]);
}
function postDataToAPI(audio) {
const wavBuffer = vad.utils.encodeWAV(audio);
const base64 = vad.utils.arrayBufferToBase64(wavBuffer);
fetch("https://api.carterlabs.ai/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
audio: base64,
key: apiKey,
playerId: "PLAYER1",
}),
})
.then((response) => response.json())
.then((data) => {
var outputText = data.output.text;
recordButton.innerText = "Start Recording";
// restart animation of output
const maxLength = 50;
const lines = splitText(outputText, maxLength);
addLinesSequentially(lines, animationDuration);
speak(data.output.audio);
})
.catch((error) => {
console.error(error);
});
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function addLinesSequentially(lines, animationDuration) {
typewriterContainer.innerHTML = "";
for (const line of lines) {
const typewriterLine = createTypewriterLine(line);
typewriterContainer.appendChild(typewriterLine);
await sleep(animationDuration);
}
}
function createTypewriterLine(content) {
const line = document.createElement("div");
line.classList.add("typewriter-text");
line.textContent = content;
return line;
}
function splitText(text, maxLength) {
const words = text.split(" ");
const lines = [];
let currentLine = "";
for (const word of words) {
if (currentLine.length + word.length <= maxLength) {
currentLine += word + " ";
} else {
lines.push(currentLine.trim());
currentLine = word + " ";
}
}
if (currentLine.trim()) {
lines.push(currentLine.trim());
}
return lines;
}
function speak(url) {
const audio = new Audio(url);
audio.play();
}
async function main() {
myvad = await vad.MicVAD.new({
positiveSpeechThreshold: 0.8,
negativeSpeechThreshold: 0.8 - 0.15,
minSpeechFrames: 1,
preSpeechPadFrames: 1,
redemptionFrames: 3,
onSpeechStart: () => {
recordButton.innerText = "Listening...";
},
onSpeechEnd: (audio) => {
myvad.pause();
recordButton.innerText = "Processing...";
if (processAudio(audio)) {
postDataToAPI(audio);
}
},
});
}
recordButton.addEventListener("click", () => {
recording = true;
recordButton.innerText = "Listening...";
myvad.start();
});
main();
});