How to Read Audio from Stdin in Ecasound
This article explores Ecasound's support for reading audio streams
through standard input using the stdin device. It details
how Ecasound handles piped audio data from the command line, outlines
the required syntax and formatting arguments, and provides practical
examples for integrating Ecasound into dynamic audio processing
workflows.
Does Ecasound Support Stdin?
Yes, Ecasound fully supports reading audio streams from standard
input. It designates stdin as a valid input audio object,
allowing users to pipe raw or formatted audio directly from other
utilities—such as FFmpeg, SoX, or ALSA capture tools—into Ecasound for
real-time processing, filtering, or routing.
Basic Syntax
To use standard input as an audio source in Ecasound, specify
stdin (or -i stdin) as the input argument:
ecasound -i stdin -o defaultOr using standard Unix pipes:
command_producing_audio | ecasound -i stdin -o alsaSpecifying Stream Formats
When reading from standard input, Ecasound often requires explicit
audio format settings unless the incoming data contains readable header
information (such as a RIFF WAV stream). If raw PCM data is piped into
stdin, you must define the stream parameters using the
-f flag before declaring the input:
cat raw_audio.pcm | ecasound -f:s16_le,2,44100 -i stdin -o defaultIn the format parameter
-f:format,channels,sample_rate:
- format: Specifies the sample encoding (e.g.,
s16_lefor 16-bit signed little-endian,f32_lefor 32-bit floating point). - channels: The number of audio channels (e.g.,
1for mono,2for stereo). - sample_rate: The sampling rate in Hertz (e.g.,
44100,48000).
Practical Example: Piped Decoding
A common use case involves decoding an unsupported or remote audio stream with FFmpeg and processing the audio in Ecasound:
ffmpeg -i input.mp3 -f wav - | ecasound -i stdin -o defaultBecause FFmpeg outputs a valid WAV stream containing header metadata, Ecasound automatically detects the sample rate, bit depth, and channel count without requiring manual format declarations.
Important Considerations
- Real-Time Latency: Streaming through standard input
introduces buffering overhead. For real-time applications requiring
minimal latency, configure Ecasound's buffer size using the
-b(buffer size in frames) or-B(buffer mode) options. - Stream Termination: Ecasound will process the input until the sending process closes the pipe (EOF), at which point Ecasound will stop execution unless configured to run interactively.