Thursday, December 27, 2012

My version of internet radio on the RPi

Setting up internet radio on the raspberry pi is not difficult but I think perhaps the way I do it is at least different.
It works very well for me, doesn't involve buying any additional hardware and is simple to set up.
Judge for yourself whether it's better or not for you.

Here's what I did.
Note that I did this primarily in Raspian Wheezy although it works just as well in Gentoo on the RPi (other than some problems ssh'ing into the Gentoo install which I haven't yet resolved).

1. Install both mpd and mpc
sudo apt-get install mpd mpc

2. The default version of mpd.conf works fine. However, I copied it to ~/.mpd/ but made no changes to the original mpd.conf.

3. Now we need to create scripts for all the radio stations we want in our menu.
I found the radio streaming files of type .m3u and .pls the easiest to download and play using mpc.
Note that to find from where to download tse files may require quite some searching around.
Perhaps your first port of call should be shoutcast.
Once you've selected the station you want, mouse over it and copy the link which will be of the form
http://yp.shoutcast.com/sbin/tunein-station.pls?id=111772
with only the id number changing for different stations.

I created a directory called radio in my home directory.
Now if you've decided on a shoutcast station, create within the radio directory an executable script of this form


#!/bin/bash
cd /home/paul/
rm tunein-station.pls*
wget http://yp.shoutcast.com/sbin/tunein-station.pls?id=27377
grep "File1=" tunein-station.pls?id=27377 > streamaddr
mpc clear
mpc add $(sed 's/File1=//g' streamaddr)
mpc play


Now, give it a name (save as to your ~/radio, I called mine "blues" as it's a blues music station), make it executable with
chmod a+x blues
and then copy it to /usr/bin/ with
sudo cp blues /usr/bin/

OK, now you've got your first radio station in your menu.
If all the others you want are available through shoutcast, you can very easily create an extensive menu.

But maybe you want some station available only as .pls files such as many of the BBC stations.
No problem but the script is a little different as shown here:


#!/bin/bash
cd /home/paul/
rm *.pls
wget http://www.bbc.co.uk/radio/listen/live/r4_aaclca.pls
grep "File1=" r4_aaclca.pls > streamaddr
mpc clear
mpc add $(sed 's/File1=//g' streamaddr)
mpc play


I copied this script from the third post of naicheben in this RPi forum thread.
You can get url's for most of the BBC radio stations here.
However, I was unable to get the BBC World Service to play in mpc with the url provided in this list.
Instead, after much searching, I finally stumbled upon a url which worked fine using this script:


#!/bin/bash
cd /home/paul/
rm eneuk.pls
wget http://www.bbc.co.uk/worldservice/meta/live/mp3/eneuk.pls
grep "File1=" eneuk.pls > streamaddr
mpc clear
mpc add $(sed 's/File1=//g' streamaddr)
mpc play

To handle radio station available as .m3u downloads, I used this script:


#!/bin/bash
cd /home/paul/
rm *.m3u*
wget http://av.rasset.ie/av/live/radio/rnag.m3u
mpc clear
cat rnag.m3u | grep http | mpc add
mpc play

You should at this stage have a range of scripts in ~/radio, one for each of the radio stations you want to have included in your menu.

Now, we need to make it easy to select the station we want.

4. To make the menu I used the marvellous script described in this great blog post.

Using this post, I made the following script which I called menu:


#!/bin/bash

# Displays a list of files in current directory and prompt for which
# file to edit
cd /home/paul/radio/
# Set the prompt for the select command
#echo -e '\E[1;34m ' 
PS3="Type a number or 'q' to quit: "
#echo -e '\E[0m ' 
# Create a list of files to display
#echo -e '\E[1;33m '
fileList=$(find . -maxdepth 1 -type f)
#echo -e '\E[0m ' 
# Show a menu and ask for input. If the user entered a valid choice,
# then invoke the editor on that file
select fileName in $fileList; do
    if [ -n "$fileName" ]; then
        ${fileName}
    fi
    break
done

I created another directory called "Commands" in ~ to store menu and some other scripts useful for easily stopping or rebooting the radio or even stopping or rebooting my RPi.
After creating the menu script, make it executabel and copy to /usr/bin/ as before.

Now when I open a terminal in Raspian and type menu, I'm presented with a menu of raio station options, each one of which has a unique identifying number.
Typing the number of the station you want and it'll start playing within less than 2 seconds.
Now this is starting to look superb.
But wait, there's more...........

5. I use the system described here for a "headless" internet radio with only a power cable, a micronet wifi dongle and a X-Mini speaker attached to my RPi.

The radio, including its menu, works wonderfully using this setup.
But if it's headless, how do you choose the station?
Well, you have an iPad, an iPhone or an iPod Touch don't you?
Then just use one of the many (and some are free) SSH apps for your iOS device to enable you to hook up to your headless radio. Use the menu script to choose your preferred station and stop or reboot either your radio or RPi.

See the photo for how my setup looks with the output available on my iPad (using SSH terminal).

I'm very happy with this setup and it works flawlessly as well as preserving the option to include as many radio stations as you want.
There's a lot of stuff on my desk so it might be a little confusing.
The small white box is my headless RPi (256 MB RAM) in a ModMyPi case.
The red item to it's right is the X-Mini speaker which I bought for €20 and can fully recommend.
Great sound for it size, has an on-off switch as well as a volume control.
In the foreground is my iPad showing the output when controlling the  radio and it includes the menu of radio stations I currently use.
The X-Mini speaker is not separately powered but I connect its USB outlet to the RPi which easily provides sufficient power to keep the X-Mini fully charged for as long as I've been using it (nearly two weeks, 15 hours per day).

I've posted some further enhancements to my radio here and here which are worth looking at.
















2 comments:

  1. Hi, i'm trying to achieve something similar to you by making a raspberry pi internet radio player which can deal with BBC .pls streams! This post is really useful and i'll try to follow it through soon to create a menu of stations.

    Would it be possible to configure the player to automatically start one of the stations on startup? I have tried to follow tutorials for getting scripts to execute on startup but no joy yet...

    Thanks, Alex

    ReplyDelete
  2. Hi, sorry for the delay in replying to your comment.
    Yes, getting scripts to launch on boot is a piece of cake.
    Obviously, the script must be made executable with 'chmod +x script'
    Next just add your script to /etc/xdg/lxsession/LXDE/autostart
    So, if your script is called script_1, then you add the line
    @script_1 to the autostart file and get ready to hear your radio next time you boot your Pi.
    Sometimes, it's advisable to add a 'sleep' command at the start of your script to make sure your radio doesn't start before everything it needs is running.

    ReplyDelete