How to save audio stream from Asterisk to a file via WebSocket

To save an audio stream from Asterisk to a file via WebSocket, you need to capture the audio being streamed from Asterisk and write it to an audio file format (such as WAV or PCM). Here’s a step-by-step guide to accomplish this: create web socket and save data in a text file

Requirements:

  1. Asterisk: To generate and stream the audio.
  2. WebSocket server: To receive the audio stream from Asterisk.
  3. Python with websockets and other necessary libraries to handle the WebSocket connection and save the audio file.

General Steps:

  1. Asterisk Configuration: Set up Asterisk to stream the audio (e.g., using EAGI).
  2. WebSocket Server: Set up a WebSocket server to receive the audio stream.
  3. Capture the Stream and Save: Write the received audio stream into a file format like WAV or raw PCM.

Step 1: Set Up Asterisk to Stream Audio

You can use an EAGI (Enhanced AGI) script to capture raw audio from Asterisk. EAGI allows you to capture audio using a file descriptor (/dev/fd/3), and this can be streamed to a WebSocket.

Step 2: Create a WebSocket Server to Receive Audio

You need to create a WebSocket server that will receive the audio stream from Asterisk. Here’s how you can implement the WebSocket server using Python and the websockets library.

Now, create a WebSocket server that listens for audio streams and saves them to a file.

#!/usr/bin/env python3

import asyncio
import websockets

async def save_audio(websocket, path):
    # Open a file to write the incoming audio stream
    with open("/tmp/received_audio.raw", "wb") as audio_file:
        try:
            while True:
                # Receive audio data in bytes from the WebSocket
                audio_chunk = await websocket.recv()

                # Write the audio chunk to the file
                audio_file.write(audio_chunk)
                print(f"Received {len(audio_chunk)} bytes of audio")

        except websockets.ConnectionClosed:
            print("WebSocket connection closed")
        except Exception as e:
            print(f"An error occurred: {e}")

# Run the WebSocket server
start_server = websockets.serve(save_audio, "localhost", 8765)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

This script will:

  • Start a WebSocket server on localhost:8765.
  • Receive audio data (in byte format) sent from Asterisk.
  • Save the received audio data to /tmp/received_audio.raw as raw PCM.

Step 4: Convert Raw PCM to WAV (Optional)

The audio is streamed as raw PCM by default, which may not be useful in its raw form. You can convert the raw PCM data into a WAV file using a utility like SoX.

Here’s an example to convert the raw PCM file into a WAV file:

sox -t raw -r 8000 -e signed-integer -b 16 -c 1 /tmp/received_audio.raw /tmp/received_audio.wav

This assumes:

  • Sample rate: 8000 Hz
  • Encoding: signed-integer
  • Bit-depth: 16-bit
  • Channels: 1 (mono)

You can adjust the parameters (-r, -b, etc.) according to the format of the audio data coming from Asterisk.

Step 5: Test the Setup

  1. Start the WebSocket server:
python websocket_server.py

Call the extension in Asterisk: Dial the extension (e.g., 1000) from an Asterisk softphone or another system.

Check the Saved Audio: Once the call ends, check the /tmp/received_audio.raw file or convert it to WAV using SoX, as shown above.

Summary

  • Asterisk (EAGI) streams audio as raw PCM through file descriptor 3.
  • The EAGI script sends this audio stream to a WebSocket server.
  • The WebSocket server captures the stream and saves it to a file.
  • Optionally, the raw PCM data can be converted to WAV or another audio format for easier playback.

By following this approach, you can capture audio streams from Asterisk and save them over WebSocket in real time.

Leave a Reply