const ws = new WebSocket("wss://api.slng.ai/v1/tts/slng/deepgram/aura:2");
ws.onopen = () => {
// 1. Initialize session
ws.send(
JSON.stringify({
type: "init",
config: {
encoding: "linear16",
sample_rate: 24000,
},
}),
);
// 2. Send text to convert
ws.send(
JSON.stringify({
type: "text",
text: "Hello! This is a WebSocket TTS example.",
}),
);
// 3. Flush to get remaining audio
ws.send(
JSON.stringify({
type: "flush",
}),
);
};
ws.onmessage = (event) => {
if (event.data instanceof ArrayBuffer) {
// Binary audio data - play it!
playAudio(event.data);
} else {
// JSON control messages
const message = JSON.parse(event.data);
if (message.type === "ready") {
console.log("Session ready:", message.session_id);
} else if (message.type === "flushed") {
console.log("All audio sent");
}
}
};