How to play raw PCM data coming through a websocket #281
Replies: 1 comment 2 replies
-
Hello 👋 You might want to take a look at our example, which uses openAI api. Starting in 0.4.2 (which should be published today), re-sampling (done in temporary function In order to use raw PCM data with the library, you have to convert the ArrayBuffer to array of float and create an For each chunk of the audio data, you have to create separate Simplified example: const aCtx = new AudioContext();
const dataSampleRate = 24000; // Sample rate in which deep gram is sending the PCM chunks.
/* ... */
const playPCMBuffer = async (data: Blob | ArrayBuffer) => {
try {
// Convert blob/buffer to Uint8Array if needed
const newChunk = new Float32Array(data);
const bufferData = [];
for (let i = 0; i < newChunk.length; i += 1) {
bufferData[i] = newChunk[i];
}
const audioBuffer = aCtx.createBuffer(1, newChunk.length, dataSampleRate);
audioBuffer.copyToChannel(0, bufferData);
const bufferSource = aCtx.createBufferSource();
bufferSource.buffer = audioBuffer;
bufferSource.connect(aCtx.destination);
bufferSource.play(lastChunkStopTime);
} catch (error) {
console.error("Error processing binary audio chunk:", error);
}
}; |
Beta Was this translation helpful? Give feedback.
-
Hi everyone,
I'm working on a conversational chatbot that leverages Deepgram Voice Agent, and I need low-latency, real-time audio streaming. Currently, I'm receiving raw PCM data via a WebSocket, converting it into a Uint8Array, and appending each new chunk to a buffer. Here's my current implementation:
At the end, I convert this aggregated buffer into a WAV file and play it using Expo AV. However, this approach is not optimal for real-time audio streaming due to latency concerns.
I've been exploring this library since it seems promising for achieving lower latency streaming. Unfortunately, the documentation is still under development, and I haven't been able to figure out how to best integrate it into my workflow.
Questions:
Beta Was this translation helpful? Give feedback.
All reactions