Friday, November 15, 2013

Recording audio streams on OSX and Linux

I want to time-shift (for my own personal use, blah, blah, blah) a particular stream from a radio station.  I can get Firefox and iHeart radio to play it, but capturing it is a little trickier.  Under OSX, things are pretty straightforward, as always.  Download and install the excellent Soundflower (http://code.google.com/p/soundflower/) to do the internal sound redirection.  Download and install Sox (http://sox.sourceforge.net/) to capture the sound redirection to a file.  Download and install the lame library (http://lame.sourceforge.net/) to allow the creation of mp3s.  Download and install mutagen (http://code.google.com/p/mutagen/) to label the mp3s that are created.  Setup up Soundflower in the System Preferences page to receive all sounds:
Then, create a script file to read from that stream, create an mp3, and add artist and album to the mp3:

d=`date +"%Y-%m-%d"`

sox -t coreaudio "Soundflower (2c" -q ~/$d.mp3 trim 0 3:00:0 channels 1

mid3v2 --artist="Lewis and Floorwax" --album=$d $d.mp3

Toss into cron and you're good to go.  I needed to move this process to a Linux box, due to domestic contention for computer resources.  This is a little trickier, and not well documented anywhere.  Make sure you have all of the above software, except Soundflower of course.  I'm averse to adding software that I don't need on any box (due to maintenance and security concerns). so wanted to use the built-in ALSA technology.  Fire up alsamixer, use the tab and arrow keys to select "Capture", and hit the space bar to get the red "CAPTURE" below it.  Arrow to the "Mix" and hit the space bar to put a red "CAPTURE" there too.

To control the volume, I didn't have to do anything with the capture device, but had to use the Master and PCM device volumes:


The Sox incantation then becomes:
sox -t alsa hw:0,0 -q ~/$d.mp3 trim 0 3:00:0 channels 1
Hope this helps someone. On yet another system, I use arecord and lame:
arecord -q -t wav -d 14400 -r 16000 -f S16_LE | lame - file.mp3