Pipe audio stream to another application
# support
a
Hello. Is it possible to take the audio stream in a web session and send that on to another application? My hope is that I can take the audio stream from the agent and send it into another application we are building. Format could be byte stream, or anything. Any thoughts, questions, or guidance would be appreciated, thanks.
v
Yes, you can pipe audio from a web session to another application by capturing audio in the browser and sending it as a byte stream over a WebSocket. Here’s a simple example using JavaScript:
Copy code
js
navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
  const audioContext = new AudioContext();
  const source = audioContext.createMediaStreamSource(stream);
  const processor = audioContext.createScriptProcessor(1024, 1, 1);
  const socket = new WebSocket('wss://your-server-url');
  processor.onaudioprocess = (event) => {
    const pcmData = event.inputBuffer.getChannelData(0);
    const int16Data = new Int16Array(pcmData.length);
    for (let i = 0; i < pcmData.length; i++) {
      int16Data[i] = Math.max(-32768, Math.min(32767, pcmData[i] * 32768));
    }
    if (socket.readyState === WebSocket.OPEN) socket.send(int16Data.buffer);
  };
  source.connect(processor);
  processor.connect(audioContext.destination);
});
This streams raw PCM audio to your WebSocket server, where you can forward or process it as needed. For best results, ensure your receiving application can handle the incoming audio buffer format. [See more details in the documentation.](https://docs.vapi.ai/calls/websocket-transport) Source: - [WebSocket Transport Vapi Documentation](https://docs.vapi.ai/calls/websocket-transport)
a
@Vapi Support Bot can this be used to take the audio from the agent, the example looks like it takes the users microphone and sends it to another application. it doesn't seem like it would take vapi's audio output (the agent speaking)
v
Thank you for clarifying your goal. To help you best: Are you looking to capture the agent's audio output in real time during a live call (e.g., via WebSocket), or do you need access to the audio files after the call is finished? This will help me suggest the most suitable approach for your use case. Sources: - [WebSocket Transport Vapi Documentation](https://docs.vapi.ai/calls/websocket-transport) - [Call Features Documentation](https://docs.vapi.ai/calls/call-features) - [Custom TTS Integration](https://docs.vapi.ai/customization/custom-voices/custom-tts)
a
@Vapi Support Bot I'm wanting to take the real time stream of vapi's agent audio and send it to another application.
v
To capture the agent's audio output in real time and send it to another application, set up a custom TTS endpoint in Vapi. When your assistant needs to speak, Vapi will POST the text to your endpoint, and you can generate or proxy the audio, then forward the raw PCM bytes wherever you need. Example (Node.js Express):
Copy code
js
app.post('/api/synthesize', async (req, res) => {
  // Generate or fetch audioBuffer (PCM) for req.body.message.text
  // Forward audioBuffer to your other application here
  res.setHeader('Content-Type', 'application/octet-stream');
  res.write(audioBuffer);
  res.end();
});
This gives you full control over the agent's audio stream before it's sent to the user. See: [Custom TTS Integration Guide](https://docs.vapi.ai/customization/custom-voices/custom-tts) Source: - [Custom TTS Integration Guide](https://docs.vapi.ai/customization/custom-voices/custom-tts)
2 Views