Streaming Audio Over Network Sockets with Ecasound

Ecasound is a versatile command-line multitrack audio processor capable of routing, recording, and applying real-time effects to sound. This article explains how Ecasound sends and receives audio across network sockets, detailing the use of standard input/output redirection, the native pipe subsystem, and network utilities like Netcat or Socat to establish reliable raw TCP and UDP audio streams.

The Underlying Mechanism: Standard Streams and Pipes

Ecasound handles network audio transmission by treating network connections as standard data streams rather than relying on heavy, built-in network protocol layers. By combining its flexible audio object syntax with POSIX-compliant standard I/O (stdin and stdout) or native command pipes (pipe:), Ecasound can interface directly with standard networking utilities like netcat (nc) or socat.

Because network streaming via sockets typically passes raw, uncompressed Pulse-Code Modulation (PCM) data without container metadata (like WAV headers), Ecasound requires explicit parameter definitions. Both sending and receiving endpoints must share matching parameters specifying the sample format, channel count, and sampling rate using the -f flag (e.g., -f:s16_le,2,44100).

Transmitting Audio to a Network Socket

To stream audio across a network, Ecasound captures sound from a local source—such as an ALSA hardware device, a JACK port, or an audio file—and routes the processed raw PCM data to a socket client.

Using Standard Output Redirection

You can pipe Ecasound's standard output directly into a networking tool:

ecasound -i alsahw:0,0 -f:s16_le,2,48000 -o stdout | nc remote_host 5000

In this setup, Ecasound processes input from the ALSA sound card, formats the stream as 16-bit little-endian stereo at 48 kHz, and streams the raw bytes to standard output. Netcat reads from the pipe and forwards the byte stream over a TCP connection to remote_host on port 5000.

Using the Internal Pipe Audio Object

Ecasound also allows embedding shell commands directly into its routing chain using the pipe: audio object:

ecasound -i audio_source.wav -f:s16_le,2,44100 -o pipe:"nc -u remote_host 5000"

This encapsulates the network transport inside Ecasound, transmitting the stream over UDP for low-latency transmission.

Receiving Audio from a Network Socket

Receiving audio over a socket operates symmetrically. A network utility listens on an open port and routes the incoming byte stream to Ecasound's input system.

Using Standard Input Redirection

A listening socket utility can forward incoming bytes into Ecasound's standard input:

nc -l -p 5000 | ecasound -f:s16_le,2,48000 -i stdin -o alsa

Netcat listens on TCP port 5000. When a sender connects and begins transmitting, Netcat pipes the incoming payload to Ecasound. Because the input stream lacks file headers, the -f:s16_le,2,48000 parameter instructs Ecasound how to interpret the byte stream before sending it to the ALSA playback device.

Using the Internal Pipe Audio Object for Input

Similarly, Ecasound can launch a listening socket directly as its designated input source:

ecasound -f:s16_le,2,44100 -i pipe:"nc -l -p 5000" -o alsahw:0,0

Decoupling Streams with Named Pipes (FIFOs)

For permanent setups or automated scripts, using standard UNIX named pipes (FIFOs) offers greater stability than standard shell pipelines. A FIFO prevents Ecasound from terminating unexpectedly if a network connection briefly drops:

  1. Create a named pipe on the filesystem:
    mkfifo /tmp/audio_stream.fifo
  2. Launch a network server that continuously writes incoming socket data to the pipe:
    socat TCP-LISTEN:5000,reuseaddr,fork OPEN:/tmp/audio_stream.fifo
  3. Run Ecasound using the named pipe as its audio input object:
    ecasound -f:s16_le,2,48000 -i /tmp/audio_stream.fifo -o alsa

Protocol Considerations: TCP vs. UDP