On my commute to and from work I listen to a lot of podcasts on my my phone (using Podkicker Pro) and more often than not I’m halfway through listening to one when I get home. While I want to carry on listening, I don’t want to have to keep the headphones on. What I want to do is pipe the audio to an internet radio. So this is the first use case for my Pi Audio Gateway project – get audio off a phone and onto an internet radio (there are other use cases involving various things with DNLA/UPNP or streaming DAB/DVB radio over the local network, but they can come later).
In order to get the audio off the phone, I’ll use Bluetooth’s A2DP protocol to stream the audio to a gateway that converts it to a stream that the internet radio can tune to. The Raspberry Pi has become my go to device for projects like this and I’ll be using one of the newer 512Mb models. Fortunately, Google has been my friend in getting this up and running (I’m not the first person think of doing things with a Raspberry Pi and Bluetooth audio or icecast streaming).
Step 1: Install Raspbian
I started with a basic Raspbian install with just “Standard system utilities” and “SSH server” and then modified it to use a static IP address.
Step 2: Get Bluetooth up and running
I’m using an old Bluetooth dongle that I got years ago at Tesco. I mostly followed KMonkey’s guide, but it didn’t quite work as described there (at a guess, this might be due to differences between using a “ready to go” image and installing Raspbian from the installer). To get the onboard sound to work (for testing, and who knows, it might come in handy down the road) I needed to
apt-get install alsa-utils
and I had I to load the Broadcom sound module even though from what I can tell, it should be enabled by default.
(I had to do both of these as root – the basic install doesn’t install sudo by default and I haven’t gotten around to installing it)
I had to do things slightly differently to get the Bluetooth side of things going as well. For some reason my phone wouldn’t see the Pi (neither would my Nexus 7). I found some slightly different instructions on the OpenELEC site, So I ran this
to get the MAC address of the phone and then
echo "0000" | bluez-simple-agent hci0 XX:XX:XX:XX:XX:XX
to do the pairing.
Step 3: Install icecast2
This bit is simple
apt-get install icecast2
I then had to edit /etc/default/icecast2 to change ENABLED to true and then
service icecast2 start
Step 4: Install gstreamer
I’ll use gstreamer to pipe audio out of PulseAudio and into icecast2, so I need to install gstreamer and it’s plugins.
apt-get install gstreamer-tools gstreamer0.10-plugins-base gstreamer0.10-plugins-good
Step 5: There be Dragons!
Sadly, my internet radio doesn’t support the Ogg Vorbis format, so I’ll need to use MP3. MP3 is patent encumbered (at least in some parts of the world) so I’ll need to install gstreamer-plugins-ugly, as that’s where you’ll find the more legally ambiguous parts of gstreamer (a quick check on http://mp3licensing.com/royalty/emd.html indicates that they’re not that bothered by private non-commercial activity, though you should look into whatever legal issues you may face for yourself).
Step 6: Setting up the gstreamer pipline
Everything is now installed, now it’s time to get the stream running. All I need to do is slightly modify the instructions found here so that it uses the Bluetooth PulseAudio device (instead of the local monitor) and a lower bitrate.
gst-launch pulsesrc device=bluez_source.C8_D1_5E_2B_6B_2B ! audioconvert ! lamemp3enc bitrate=32 ! shout2send ip=localhost port=8000 password=hackme mount=stream.mp3
I’m keeping the bitrate low to keep the processor demand low (at the moment the whole pipeline + PulseAudio is using around 60% of the CPU). Fortunately it doesn’t seem to make much difference with TWiT style podcasts.
Step 7: Tuning the radio
My radio uses a chipset from Frontier Silicon, which means that I need to go to http://www.wifiradio-frontier.com/ in order to add custom stations. I can’t say I’m overly comfortable with this, as I’ve already had hardware that has died because the company that made it went under and it was no longer able to phone home. But as that’s how my radio works then that’s what I have to do.
And that’s it – I’m listening to a podcast on my phone via my radio. However, there’s plenty of room for improvement. One of the first things I want to look at is getting it so that it doesn’t need to do so much decoding and encoding. The files on the phone are MP3s, the A2DP protocol supports streaming MP3s over it and the icecast steam is in MP3, so there must be some way to not touch the bitstream.