admin管理员组

文章数量:1335662

I've integrated VAPI.AI to handle inbound and outbound calling, and everything is working fine. There’s an option to listen to any inbound or outbound call through a WebSocket URL, which provides a PCM stream. Here’s my current setup:

I’m receiving PCM audio data through a WebSocket and saving it to a file (audio.pcm) on the server. Afterward, I use FFmpeg to convert it to an MP3 file, which I can then play without any issues. Below is my current code for receiving and saving the audio data:

const WebSocket = require('ws');
const fs = require('fs');

let pcmBuffer = Buffer.alloc(0);

const ws = new WebSocket("wss://aws-us-west-2-production1-phone-call-websocket.vapi.ai/7420f27a-30fd-4f49-a995-5549ae7cc00d/transport");

ws.on('open', () => console.log('WebSocket connection established'));

ws.on('message', (data, isBinary) => {
  if (isBinary) {
    pcmBuffer = Buffer.concat([pcmBuffer, data]);
    console.log(`Received PCM data, buffer size: ${pcmBuffer.length}`);
  } else {
    console.log('Received message:', JSON.parse(data.toString()));
  }
});

ws.on('close', () => {
  if (pcmBuffer.length > 0) {
    fs.writeFileSync('audio.pcm', pcmBuffer);
    console.log('Audio data saved to audio.pcm');
  }
});

ws.on('error', (error) => console.error('WebSocket error:', error));

Now, my question is:

Is there a way to play the PCM audio directly in the Next.js frontend (client-side) by using a WebSocket listener, without saving it to a file first? I want to stream the audio directly to the client as it is received from the WebSocket.

I’ve tried different solutions, but they result in noisy audio that is not clear. The playback sounds like noise, not the intended voice.

I’ve considered converting PCM to MP3 on the server and sending it to the frontend, but I’d prefer a more direct approach that avoids saving and converting the file if possible.

Any suggestions or solutions would be greatly appreciated!

本文标签: