...making Linux just a little more fun!

2-Cent Tips

Recording ALSA PCM output as RAW data

Mulyadi Santosa [mulyadi.santosa at gmail.com]

Sun, 27 May 2007 15:18:48 +0700

Hi Gang...

I'd like to write a short tip here. Sometimes, you may want to record the PCM output stream, maybe for further sound analysis and such. How to do it?

You need to modify your ALSA setting e.g in /usr/share/alsa/alsa.conf and add below lines:

pcm.default {
        @args [ SLAVE FILE FORMAT ]
        @args.SLAVE {
                type string
                default "hw:0,0"
        @args.FILE {
                type string
                default "/mnt/linux/midi/test.raw"
        @args.FORMAT {
                type string
                default raw
        type file
        slave.pcm $SLAVE
        file $FILE
        format $FORMAT
The declaration is quite self explanatory. Pay attention to $FILE definition (where you want to put the RAW file). It is defined as 44100 HZ, 2 channel, signed linear sample data.

Later, you can convert this file into another audio format, such as WAV:

$ sox -V -w -c  2 -s -r 44100 ./copy.raw ./copy.wav
Notice that the above ALSA PCM definition is default definition, so it affects any ALSA based application that use default definition too. If you want, you can name it something like "pcm.tee" and later to use this definition, tell your application to do so, e.g:
aplay -D tee song.wav
NB: I read about this tip during my experiments with Linux Audio. There might be mistakes here, so please CMIIW.



Talkback: Discuss this article with The Answer Gang

Copyright © 2007, . Released under the Open Publication License unless otherwise noted in the body of the article. Linux Gazette is not produced, sponsored, or endorsed by its prior host, SSC, Inc.

Published in Issue 139 of Linux Gazette, June 2007