Recording audio streams

While most web radio stations want to prevent you from recording the audio streams they publish, they really can't. To record the stream just configure alsa to record a copy of what it plays in the speakers. Below is the configuration that should be in $HOME/.asoundrc for that to happen:

pcm.input_raw {
        type file
        slave.pcm "hw:0,0"
        file "/home/hans/input.raw"
}

pcm.output_raw {
        type file
        slave.pcm "hw:0,0"
        file "/home/hans/output.raw"
}

pcm.playback {
        type plug
        slave {
                pcm "output_raw"
#               format S16_LE
#               channels 2
#               rate 48000
        }
}

pcm.capture {
        type plug
        slave {
                pcm "input_raw"
#               format S16_LE
#               channels 2
#               rate 48000
        }
}

pcm.!default {
        type asym
        playback.pcm "playback"
        capture.pcm "capture"
}

Since you normally don't want to save a copy of the sound that is playing, you must temporary copy a file with this content to $HOME/.asoundrc before the application that plays sound is started, because it will read $HOME/.asoundrc only at startup.

(When you use a plugin to play sound in your browser, it should be enough that $HOME/.asoundrc exists when the plugin is started for the first time in the session).

This technique makes it possible to save songs from Spotify running under wine. Just be sure to use the alsa sound-driver for wine (in package libwine-alsa in Debian stable, use the command winecfg to set the sound-driver).

Here is a step-by-step howto save songs from spotify and encode them to ogg

Do this once

First, put the configuration code above in $HOME/.asoundrc.bak

Do this everytime you start spotify and want to record songs

Temporary copy $HOME/.asoundrc.bak to $HOME/.asoundrc

cp $HOME/.asoundrc.bak $HOME/.asoundrc

Start spotify

Clear output.raw

When started, spotify will make a sound, that will be recorded into output.raw. Clear output.raw by this command:

cat /dev/null > output.raw

Delete $HOME/.asoundrc

Now remove the file $HOME/.asoundrc, since Spotify (or the alsa-driver of wine) has already read it and will not read it again.

rm $HOME/.asoundrc

Do this for every song you record

First make a copy of output.raw, so you can record new songs, at the same time as encoding. Encode output.raw to ogg with oggenc, substitute the_title with the actual title. The second command clears output.raw so a new song can be recorded.

mv output.raw temp.raw
oggenc -r -o the_title.ogg -q 6 temp.raw

In order to ensure that spotify will not start playing a new song before you have had the time to move output.raw, you can always create a new, temporary, playlist for the song in question.

WAVE as target format

If you want to make an Audio CD, microsofts wave-format is a reliable format that will work with cd-recording software. Here is how to create a wave-file form the raw sound data in temp.raw.

sox -t raw -e signed-integer -b 16 -c 2 -r 44100 temp.raw foo.wav

comments powered by Disqus


Back to the index

Blog roll

R-bloggers, Debian Weekly
Valid XHTML 1.0 Strict [Valid RSS] Valid CSS! Emacs Muse Last modified: oktober 17, 2019