I am going to implement Audio Streaming using OpenAI TTS model. It will get audio data from OpenAI TTS model as streaming and send it to frontend via WebSocket. It plays on the Frontend. Frontend is React.js and backend is Node.js.
This is frontend code.
audio_response = await openai.audio.speech.create({
model: "tts-1",
voice: "nova",
input,
response_format: "mp3",
});
// Get audio chunks from the stream and send via websocket
const stream = audio_response.body;
// Pipe the audio stream to the WebSocket in small chunks
stream.on("data", (chunk) => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(chunk); // Send audio data as binary chunks
}
});
And this is backend:
const socket = new WebSocket(...);
socket.binaryType = "blob";
// Web Audio API setup
let audioContext;
let source;
let audioBufferQueue = []; // Queue for audio chunks
socket.addEventListener("message", async (event) => {
const audioChunk = event.data;
audioBufferQueue.push(audioChunk);
// Start playing audio if not already playing
if (!source) {
await playAudioQueue();
}
});
async function playAudioQueue() {
if (!audioContext) {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
}
while (audioBufferQueue.length > 0) {
const audioChunk = audioBufferQueue.shift();
// Decode audio data
const arrayBuffer = await audioChunk.arrayBuffer();
try {
const audioBuffer = await audioContext.decodeAudioData(arrayBuffer);
// Play the audio buffer
source = audioContext.createBufferSource();
source.buffer = audioBuffer;
source.connect(audioContext.destination);
// Wait for the audio to finish playing
await new Promise((resolve) => {
source.onended = resolve;
source.start();
});
source = null;
} catch (err) {
console.error("Error decoding audio data:", err);
}
}
}
Now this code has error like:
Error decoding audio data: EncodingError: Unable to decode audio data