Raspberry Pi Audio Delay

On November 30, 2012, in Raspberry Pi, Tech, by tom

So it’s cricket season here and that means lots of sitting in front of the TV watching cricket and listening to the commentary by the clowns on Channel 9 when I would much rather be watching cricket and listening to the commentary by the clowns on ABC Grandstand.

The only problem is the audio delay. The digital TV broadcast lags behind the radio so that you hear the action before you see it, something like 10 seconds before. The delay is less with a digital radio but still in the order of 7 seconds or so. To make matters worse, the delay is dependent on the make of TV and digital radio you’re using.

But we are in luck! Because the radio precedes the video it should be possible to buffer up the audio from the radio, delay it by however long is needed and replay it back in sync with the video from the TV! Then all you need to do is mute the TV and you’re all set: watching cricket and listening to the commentary from the radio all in sync.

I bought a Raspberry Pi a few months back and decided this is something worth doing with the Pi. It is basically an embedded linux computer that runs at 700MHz+ with an ethernet port, 2 USB ports, a HDMI output and a stereo audio output.

The only problem is that the Raspberry Pi doesn’t have an an audio input. That’s where a USB audio interface comes into play! I happened to have an old Giffin iMic laying around, this has a line in and a line out and is perfect for the job.

Note that there are currently a few bugs in the Raspberry Pi kernel drivers for the USB interface that I found made it impractical to simultaneously run the audio input and output through the iMic over the USB interface. This resulted in lots of audio pops and breakups, so I resorted to using the audio input from the iMic over USB and the audio output from the built-in output on the Raspberry Pi.

The code should work with any USB input that is supported by the latest Raspbian “wheezy” image, but may need some fine tuning if you change the configuration of your Raspberry Pi.

I found that there was very little I needed to set up to get this to work, it uses perl, aplay and arecord from alsa utilities group which are all part of the standard Raspbian “wheezy” image. In fact it should work on any linux system fast enough to handle simultaneous input and output audio streams.

Download the code here:


The code is released under GPL V3 and comes with no warranty!

The code is sort of based on the idea I found here a perl script called “Sports Radio Delay”, but my code is different in that it uses separate threads for the input and output audio streams and a thread safe queue as a FIFO to pass the audio data between the input and output threads and also to implement the delay.

Usage and Troubleshooting
Having the latest version of the Raspberry Pi software helps:

$ sudo apt-get update && sudo apt-get upgrade

Having the latest firmware also helps with the USB performance, follow the instructions here.

Make the script executable:

$ chomd +x audiodelay

Run it, eg delay the audio input for 6.4 seconds

$ ./audiodelay 6.4

Now you’re all set, just hook your radio analogue output to the input on your audio source, hook the audio output from the Raspberry Pi to the audio input on your stereo and you’re all set.

Raspberry Pi Audio Delay Setup

The header of the script contains a few trouble shooting pointers on to how to modify the code to suit your audio interfaces and adjust the input and output levels.

Oh yeah, I am running my Raspberry Pi over clocked to 950MHz, with a headless command line only interface and as user “pi”, so your milage and permissions may vary.

I keep my Pi in an Adafruit Pi Box enclosure for safe keeping, and I’ve put some DRAM heat spreaders on the CPU and ethernet chip to try and keep them cooler.