5

I'm trying to figure out how to use libmad with C. The trouble is, libmad converts audio files to .pcm files and I'm unable to find a way to play these files. There is no straightforward .pcm player, and Audacity crashes every time I try to open the pcm file with it. Does any one have any experience with this? I'm open to converting these pcm files to wav files or some other format, but again there's no straightforward way to do this (at least I haven't found one).

3

Are you sure you're getting valid PCM data? What type of PCM are you getting?

Basic PCM is used on audio CDs and in WAV files ripped from CDs; it should be playable by just about anything. Specifically, it is 16-bit (per sample), 44.1 kHz (sample rate), stereo (2 channels). Playing this kind of PCM is as simple as cat file.pcm > /dev/audio (or /dev/dsp or another appropriate audio device).

The libmad homepage indicates that libmad may default to outputting 24-bit PCM, and your player software could be confused by it if expecting the standard 16-bit PCM. If you haven't already, check your program's libmad usage against the madlld (libmad low-level API tutorial) to make sure you're getting the correct output from the library.

Since PCM data doesn't include headers like a WAV file does, you may need to specify the PCM format for a program to load the file correctly. Audacity should be able to handle any standard PCM format.

It's also possible you're getting invalid PCM output from libmad, due to a bug in the library or due to improper use of the library by your code.

2
  • 1
    This lead me to the correct answer. Although Audacity crashed when I tried to open the PCM file, I was able to import it successfully. I just went to Project > Import Raw Data. I left it at the default settings (Signed 16-bit PCM, little-endian, stereo, at 44khz) and voila! Thank you. – Cookie Monster Nov 29 '09 at 21:03
  • /dev/audio may require extra packages, and /dev/sdn may not be very easy to use: unix.stackexchange.com/questions/13732/… – Ciro Santilli新疆棉花TRUMP BAN BAD Apr 8 '16 at 22:57
3

`ffplay

Tested on Ubuntu Linux 15.10:

sudo apt-get install ffmpeg
ffplay -autoexit -f u16be -ar 44100 -ac 1 in.raw

or:

sudo apt-get install play
play -b 16 --endian big -e unsigned -r 44100 in.raw

ffplay options:

  • -autoexit: exit player when stream ends
  • -f: format:
    • u: unsigned
    • 16: 16 bits per value
    • be: big-endian
  • -ar: sample frequency
  • -ac: number of channels

play options: compare the values ;-)

Example of how to generate your own .raw files to play with: https://stackoverflow.com/questions/732699/how-is-audio-represented-with-numbers/36510894#36510894

See also: https://stackoverflow.com/questions/20314739/how-to-play-pcm-sound-file-in-ubuntu

0

Perhaps you can use a tool like Sox

SoX is a cross-platform (Windows, Linux, MacOS X, etc.) command line utility that can convert various formats of computer audio files in to other formats. It can also apply various effects to these sound files, and, as an added bonus, SoX can play and record audio files on most platforms.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.