ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
now when i run this program as:
./a.out < 32a5.mp3
(where 32a5 is a short mp3 file)
i only get some white noise.
Your pcm device was opened as SND_PCM_STREAM_PLAYBACK, and you have set the parameters for SND_PCM_FORMAT_S16_LE (signed, 16-bit, little-endian PCM sound) at 44.1kHz. You can’t expect raw mp3 data to magically conform to this format. You will either need to decode the mp3 yourself or find some other sound files which are in signed, 16-bit, little-endian PCM format at 44.1kHz.
You’ll have two options if you want to decode the file: do it internally or external of the program. The easiest is to do it externally. For example,
Code:
madplay -owave:- 32a5.mp3 | ./a.out
would use the external mp3 decoder madplay (which interally uses libmad).
Alternatively, you could decode the stream yourself (in the program). This would most likely use an already-existing decoding library (such as libmad). If you don’t want to use an already-existing decoding library, you’ll have to read the MPEG specifications to figure out the format, and how it should convert to PCM. Then, you’ll have to write all the processing code yourself.
Btw, if you want to find the encoding of a file, most of them use magic bytes, and are suitable for use with file. The extension for MPEG, Layer-III audio encoding is usually “.mp3” (as you would expect). The extension for PCM audio varies, but is usually “.wav”, “.aiff”, “.au”, or “.raw”. Here’s what file tells me on my machine:
Code:
$ file foo.mp3
foo.mp3: Audio file with ID3 version 24.0 tag, MP3 encoding
$ file foo.wav
foo.wav: RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, stereo 44100 Hz
In my struggles with ALSA I was having the same problem as muskvar initially had, with "a.out: pcm.c:2171: snd_pcm_open: Assertion `pcmp && name' failed."
Once I finally looked at the call to snd_pcm_open in the example code, though, I noticed the second argument is argv[1] ! It's assuming the command-line argument to the program is the ALSA device name you want to use. So just run "./a.out default" (or something; "aplay -L" gives a list of device names available) instead of "./a.out" and it should work as expected.
dchou4u, snd_pcm_writei is defined in asoundlib.h, this can be found in /usr/include/alsa. To enable this for arm support the easiest way is to just move that alsa directory to your include location in gcc that you are using to compile for the arm target. Additionally you will need the alsa library, you can compile this yourself or if alsa is already on your arm board just search for libasound.so and copy it to the /lib directory you are using to compile.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.