Raspberry Pi CODEC

STLs and Raspberry Pis

It’s a weird one. When it first came out, people were struggling to figure out what to do with it. A surprising feature for something that sold out very quickly – the Raspberry Pi.

Well, we’ve now come up with another use for them – it could replace your current STL codec. No, really. We’ve got a hospital radio station that’s looking to replace an old twisted pair line with an ADSL line for cost reasons. Admittedly the Barix Extreamer is very nice but a bit costly for our purposes. On top of that, we can configure the Pi to have some more complex fallback and alerting options with the LiquidSoap software. And that’s what I’m going to talk you through – setting up a Pi as a remote end codec.

So, without further ado:

  • Acquire a Raspberry Pi. Purchasing one from CPC/Farnell is much more appreciated than nicking one from your colleague.
  • Install Raspbian. It’s not that complicated and there’s a lot of guides out there.
  • However, when you get to the installer wizard bit, we want as little RAM as possible for the GPU (I don’t know of many codecs that need 3D graphics with fancy shaders), to turn off boot to desktop, to set a sensible password for the pi account and to enable SSH.
  • Fire up your Pi and install LiquidSoap. The command is literally apt-get install liquidsoap
  • If you’re pulling an MP3 stream or planning on using MP3 backup audio, you’ll need to install some of the LiquidSoap plugins.
    Create a file called /home/pi/stl.liq and bung in the following contents:

# STL Liquidsoap Config
# Logging

set(“log.file.path”, “/home/pi/log/liquidsoap.log”)

# Setup our sources

main_url = “http://yourserver:8000/stream_name”
emergency_audio = “/home/pi/emg.wav”

# Now our e-mail alert routine

def silenceHandler()

# Our main source is streaming with silence detection

radio_stream = input.http(main_url)
radio_stream = on_blank(silenceHandler, radio_stream)

# Fall back to an MP3 file
# Also worth noting, if we get this far, we ought to tell someone (if we can)

emg_file = single(emergency_audio)
radio_stream = fallback(track_sensitive = false, [radio_stream, emg_file])

# Finally, pump it out the soundcard


  • Add a line to /etc/rc.local before the exit command with the following:
    sudo -u pi liquidsoap /home/pi/stl.liq &
    That will start LiquidSoap on boot – awesome.
  • Copy a backup audio file to the Pi. On this system we called it emg.wav. You can call it whatever you want but you will need to change the above script.
  • Run alsamixer and bump the levels all the way up (for most installs).

Reboot and you’ll have a little codec. Try pulling the network lead out and seeing what happens.

So, that’s is all configured and running but what does it do. Well, by default it will pull a web stream from the URL of your choice and play it out of the sound card. If it goes silent, it runs a script called mailsend.pl. This is a shifty little script that we have to e-mail us when things go silent.

However, if the stream fails completely, we fall back to playing the emg.wav file on loop. Sounds good but it’s worth remembering that it will resume the backup file from wherever it stopped last on every subsequent failure. While that might sound odd to the ear, you don’t end up hearing “She Wolf” every three minutes when the ADSL line flaps.

*May not actually be Sony Award winning.

PS – I know there’s a phone number on the CODEC. However, it’s the public studio line so not that big of a deal. 😉

5 thoughts on “STLs and Raspberry Pis”

  1. Hi,

    thank you for this tuto !
    but when i launch the script i have the message:
    “char 12: the variable output.alsa used here has not been
    previously defined”

    a problem with this line: output.alsa(radio_stream)

    I have a raspberry pi and i did exactly what you explained.

    any suggestion ?


    1. Have a look and see if the liquidsoap-plugin-alsa package is installed. If it’s not, apt-get install liquidsoap-plugin-alsa should get it in place. There’s also a liquidsoap-plugin-all package that should install every available plugin.

      If it’s there and still doesn’t work, shout and I’ll have a go with a newer Pi installation to see if I can figure it out.

    1. Should be easy enough but you’d have to configure the USB stick to auto-mount. An appropriate entry in /etc/fstab combined with a new folder called /media/usb or similar would allow that to happen on boot.

      At that point, it’s possible to change the backup file to point at a file on the USB stick.

      Though, if you’re going down that route, you can get a lot more clever with LiquidSoap. For example, it’s possible to have it select from playlists of IDs and songs. See this page for some examples of what you can do with a bit of scripting.

Leave a Reply

Your email address will not be published. Required fields are marked *