Along with their conventional RF transmitters, nearly all major radio stations now broadcast their programs through the Internet (Streaming Media). Also, many web-radios are exclusively Internet-based.

Being cheap and small, transforming the Raspberry PI into an Internet radio player was really tempting. Adding a wireless adapter, plus a handful of cheap components, the RPI may easily be transformed into a standalone “receiver”.

This first article deals with the installation of some command-line programs: MPD, Music Player Daemon, MPC (Music Daemon control-program) and python-mpd. These programs can easily interact with any user-written programs.

The following setup was tested under a fresh installation of the latest Raspbian distro (do sudo apt-get update and sudo apt-get upgrade before starting).

Download and install the required packages from the repository:

sudo apt-get install mpd mpc python-mpd alsa-utils

Make sure that the sound module is enabled:

sudo cat /etc/modules

snd-bcm2835 must be present in the list. Otherwise add it.

The setup is done, so let’s test the “receiver” and make it play. Execute MPC:

sudo mpc

it should reply:

volume: 80%   repeat: off   random: off   single: off   consume: off

Good, it should work.

MPC holds the list of “stations” on a playlist. It’s time to add the first “radio” to the playlist, so enter:

sudo mpc add http://81.20.49.11:80/CapitalMP3

Inside the playlist, each station is identified by a number, starting from 1 and increasing as we enter the radios one after another. Having only one station, make it play entering:

sudo mpc play 1

MPC should reply with:

http://81.20.49.11:80/CapitalMP3
[playing] #1/1   0:00/0:00 (0%)
volume:100%   repeat: off   random: off   single: off   consume: off

and… if an headphone or the speakers are plugged into the RPI’s audio socket, we should now hear the radio playing.

Deleting an entry from the playlist, the list is shifted backwards (so, if the 4th was deleted, the 5th will become the 4th, the 6th will become the 5th, an so on).

The volume can be adjusted with the following command:

sudo mpc volume (followed by any number from 0 to 100)

and again MPC will reply with some useful informations:

volume:100%

Let’s now add another nice station,  then query the status of MPC:

sudo mpc add http://uwstream3.somafm.com:6200
sudo mpc

The reply should be something similar to:

http://uwstream3.somafm.com:6200
[playing] #2/2   0:00/0:00 (0%)
volume:100%   repeat: off   random: off   single: off   consume: off

Now we can save our playlist entering:

sudo mpc save mylist

The list, named “mylist” in the above example, is saved under /var/lib/mpd/playlists/ and the full-name is mylist.m3u. More than one list can be created and loaded when needed.

If the playlist exists, it must be deleted before saving it, using:

sudo mpc rm mylist

Among the many MPC commands, the most useful ones are:

sudo mpc load mylist
sudo mpc lsplaylists (show the available .m3u playlists)
sudo mpc playlist (show the playlist content)
sudo mpc clear (to empty the playlist)

To stop the player enter:

sudo mpc stop

Here is a very short and simple Python program that gets the Station Name and the Song Title from MPD and prints these informations onto the command-line:

#!/usr/bin/python
import mpd

client = mpd.MPDClient()
client.connect("localhost", 6600)

cs = client.currentsong()

if 'title' in cs:
   SongTitle = cs['title']
elif 'file' in cs:
   SongTitle = cs['file']

print ('Song Title  : ' + SongTitle)
print ('Station Name: ' + cs['name'])

Download and run the above program:

wget http://www.gmpa.it/resources/py_mpd.py
chmod +x py_mpd.py
./py_mpd.py

The program works only when MPD is tuned-in and playing (through mpc play).

Looks quite simple and promising, isn’t it ?

≡XXS≡