Decoding MP3 Files in Ecasound with libmad

Ecasound can decode MP3 audio files using external helper libraries, most notably libmad. Although Ecasound does not implement an internal MP3 decoding algorithm from scratch, it integrates with libmad at compile time to provide native reading and processing of MP3 bitstreams. This article details how this integration works, the requirements for enabling it, and the difference between decoding and encoding in Ecasound workflows.

Direct MP3 Decoding with libmad

Ecasound includes source-level support for libmad (MPEG Audio Decoder), an open-source, high-precision, fixed-point MP3 decoding library. When built with libmad enabled, Ecasound treats MP3 files as native input audio objects.

To utilize this capability:

  1. The host system must have the libmad development files installed (often package-named libmad0-dev or libmad-devel).
  2. Ecasound's configuration script must detect the library during compilation. It is typically auto-detected, but can be explicitly ensured using the --enable-libmad flag during the ./configure step.

Once compiled with libmad support, decoding an MP3 file requires no special commands. Ecasound detects the .mp3 file extension automatically:

ecasound -i track.mp3 -o alsa

In this command, Ecasound uses the libmad backend to decompress track.mp3 directly into raw PCM audio before routing it to the specified output.

Limitations: Decoding vs. Encoding

While libmad handles decoding, it does not possess encoding capabilities. Consequently, Ecasound cannot output or save files directly to the MP3 format through libmad.

To encode audio to MP3, Ecasound relies on separate external utilities such as LAME. Encoding is typically handled by piping raw audio out of Ecasound into the encoder:

ecasound -i track.wav -f:s16_le,2,44100 -o stdout | lame - output.mp3

Alternative MP3 Decoding Methods

If Ecasound is compiled on a system without libmad, MP3 files can still be decoded using external helper command-line tools piped via standard streams. For example, mpg123 can act as the decoding helper:

mpg123 -s track.mp3 | ecasound -f:s16_le,2,44100 -i stdin -o alsa

Using libmad, however, remains the preferred method as it integrates directly into Ecasound's object engine, allowing seamless positioning, seeking, and multitrack mixing without managing external standard input/output pipes.