MPD on the Pi

Rolands little music player... courtesy of a Raspberry Pi!

This will take a freshly installed raspbian sdcard to the point where you can upload music from your PC and control it from a simple web interface. I recommend you use a fresh sdcard for this and not use one that you've “tinkered” with already.

 

I will make a few assumptions.

 

  • You are installing this on a new blank SDCard (that doesn't have any data you want to keep on it!)

  • You have plugged this into a network cable (although wireless can be added later)

  • You have a network that just “works” - i.e. by plugging in a device into your network using a RJ-45 cable gives it full internet access.

  • You are using a Windows PC

  • The SDCard is at least 8Gb in size. It can be greater

Most of the install will be done from the comfort of your PC though initially you will need to plug this into some sort of screen. Once the initial bit is done you can unplug it from the screen. Obvoiusly it still needs to be plugged into some sort of speakers still!!!!

First we need to install rasbian.

I'll just steal the notes from the Raspberry Quick start guide if that is ok.

 


 

1. Download the Raspberry Pi operating system

The recommended OS is called Raspbian. Download it here:

 

http://downloads.raspberrypi.org/images/raspbian/2012-12-16-wheezy-raspbian/2012-12-16-wheezy-raspbian.zip

 

2. Unzip the file that you just downloaded

a) Right click on the file and choose “Extract all”.

b) Follow the instructions—you will end up with a file ending in .img

This .img file can only be written to your SD card by special disk imaging software, so...

 

 

3. Download the Win32DiskImager software

a) Download win32diskimager-binary.zip (currently version 0.6) from:

 

https://launchpad.net/win32-image-writer/+download

 

b) Unzip it in the same way you did the Raspbian .zip file

c) You now have a new folder called win32diskimager-binary

You are now ready to write the Raspbian image to your SD card.

 

4. Writing Raspbian to the SD card

 

a) Plug your SD card into your PC

b) In the folder you made in step 3(b), run the file named Win32DiskImager.exe

(in Windows Vista, 7 and 8 we recommend that you right-click this file and choose “Run as

administrator”). You will see something like this:

c) If the SD card (Device) you are using isnąt found automatically then click on the drop down box

and select it

3d) In the Image File box, choose the Raspbian .img file that you downloaded

e) Click Write

f) After a few minutes you will have an SD card that you can use in your Raspberry Pi

 

 


 

Boot the Pi up with your new image. Log in as the user “pi” with a password of “raspberry”.

 

The first time you start the pi up it will present you with a text based menu called “Raspi-config”.

From here you can set things like the timezone but I wouldn't worry. You can always go back in at any time by typing in “raspi-config”. Just scroll down to the bottom of this list with the arrow keys and hit the [tab] key twice till you get to Finish.

 

We'll be doing everything old school from now on – using the command line! This isn't as scarey as it sounds and as well as learning some useful Linux skills it has the benefit of being able to be done from anywhere on the internet if you set up your router right...

First up you will need to find out what the IP address of your Pi is currently. Run the following command

ifconfig eth0 (and hit return. Assume that from this point onwards!)

 

it should present a lot of information but what you are looking for is the line that starts

inet addr:...

Take a note of the number that immeadiately comes next. e.g I have

 

inet addr:192.168.0.2 Bcast:192.168.0.255 Mask:255.255.255.0

 

So the IP address in this case is “192.168.0.2”

 

OK lets now connect to this from the PC.

 

 

First of all download the SSH client program of choice for Windows. For most people that's a program called “Putty”. Get it from -

 

http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe

 

You can run the program straight away when you've downloaded it.

 

In the section that reads “Host name (or IP address)” type in the IP address you just got and select the “Open” button at the bottom.

 

The first thing it will do is ask you to accept a keyfile. Don't worry about this, just click yes. It will only do this once hopefully!

 

Now it will ask you who you want to login in as. Simply type pi and hit return. It will next ask you for the password. Just type raspberry. If you got in ok is will now present you with a command prompt. Time to start the real work!

 

All the rest of the install will be done with the administration account on this device. In Linux this is called the “root” user. To change to this user simply type

 

sudo -s

 

The prompt should have changed to start “root@”

 

the next thing we need to do is allocate the spare space on the sdcard to the music player. One way would be simply to expand the install partiton but a neater way is to create a new partition. That way if you fill all the disk space with music files you won't break the install. We will use a disk partition program called cfdisk.

 

 

First type the following EXACTLY

 

cfdisk /dev/mmcblk0

 

You should be presented with a new screen showing the partitions installed on the Sdcard.

 

Scroll down to where it says -

 

Pri/Log Free Space 6008.35

 

and select "New" from the bottom menu. When asked select "Primary", then accept the default size.

Now select "Write" from the menu and say "yes" when you are asked if you really want to write the changes. Finally select "quit"

 

 

The pi needs a reboot to pick up the changes so finally simply type in

 

reboot

 

After a few seconds you should be disconnected. You'll need to wait 20 seconds or so before you try and connect again. Simply follow the instuctions as before.

NOTE: sometimes the IP address will change when the device reboots. If you are not able to connect after a while you should plug the screen back in and check the IP address of the Pi as before.

 

Assuming you can log back in again type

sudo -s

 

to get back to a root prompt .

 

You have still got to format the new partition you created and you still need to tell the Pi where you want to put it (or mount it as we will refer to it more accurately)

 

First format the new parition with

 

mkfs.ext4 /dev/mmcblk0p3

(careful typing this as it could wipe out your install if you get it wrong!)

Now we need somewhere to mount this new partition but first we need to install MPD.

We are going to be installing all the software using the command line installer called “apt”. First of all lets update the device's list of software. Run

apt-get update

 

Hopefully you should see it downloading various bits of information. If it gives you errors then mabe your network isn't working the way you think???

Next run

 

apt-get install mpd mpc

 

This installs MPD and "mpc" which is a command line client.

It should also create a set of folders where it searches for music. This makes a good place to mount that new partition!

 

in order to make sure it mounts the partition each time that machine reboots you need to change the file “fstab” under /etc so we edit that file using "nano" - the command line file editor

nano /etc/fstab

 

scroll down to the bottom of this file and add the following line on its own

 

/dev/mmcblk0p3   /var/lib/mpd/music   ext4   defaults,noatime   0   1

 

To save the changes to the file in nano and exit you have to hit "Ctrl+o" (that is the Ctrl key and the letter o at the same time) and accept the default, then "Ctrl+x" (the same but the letter x)

If you made a mistake and don't want to save the changes simply hit “Ctrl+x” and say “no” to the changes.

 

Now mount the new partition by typing 

 

mount /var/lib/mpd/music

 

to check whether this has worked run the command

 

df -h

 

which shows what filesystems are mounted. You should see the line

 

/dev/mmcblk0p3 5.6G 140M 5.1G 3% /var/lib/mpd/music

(don't worry if the numbers are slightly different!)

 

Now restart MPD with -

 

/etc/init.d/mpd restart

 

We're going to add an easy way to upload files and a web interface but for now lets just test what we have.

 

First of all change to the correct directory

 

cd /var/lib/mpd/music/

 

Now download a royalty free MP3 using the "wget" command like this -

 

wget -O face_to_face.mp3 "http://freeplaymusic.com/search/download_file.php?id=2779&dur=0&type=mp3"

(That's a capital o not a zero)

 

Check that it has downloaded ok by typing Linux's list command -

 

ls

 

You should see one file called face_to_face.mp3

Finally we are going to use the command line client for MPD - mpc - to update our music database with this Mp3.

 

mpc update

 

Is you had a lot of music this may take a long time. However in this case it should only take a second or so. List the music in the database by typing

 

mpc ls

 

Finally play the only tune you have at the moment fist by adding it to the current playlist with -

 

mpc add face_to_face.mp3

 

then playing the current playlist with

 

mpc play

 

you should hear some music. It may be quite quiet. Don't worry we can put the volume up later on but for now just adjust the speakers.

 

Ok so now we have a command line program for playing our music. Not very convenient. Lets create a very simple web based interface.

 

First of all we must install a web server. Normally on Linux this would be a program called apache. However on a device like the pi this is a bit overkill. There's a cut down web server called “lighttpd” which uses less resouces. Install it as so -

 

apt-get install lighttpd php5-cgi

 

We also installed an additional web module called “PHP” there which we need to enable too. Run the following commands in succession -

 

lighty-enable-mod fastcgi

lighty-enable-mod fastcgi-php

service lighttpd force-reload

 

Hopefully this all worked ok. Now we've got to download the web pages themselves. First of all go to the right folder on the pi

 

cd /var/www/

 

I've got a copy of the pages on my server which i've customised slightly to work with this install. Firstly type

 

wget http://www.primitive.co.uk/files/phpmp.tar.gz

 

This should download an archive file called phpmp.tar.gz . You will need to uncompress this file before we can use it. To do this use the Linux archive too called “tar” as so -

 

tar -xvf phpmp.tar.gz

 

Now because you are doing this all as the root user you need to change the user permissions on all the files you have downloaded. The easiest way to do this is by simply running the following (slightly complicated looking) command

 

chown -R www-data:www-data /var/www

 

Ok you are all done with putty for the moment. You'll need to come back to it however so I would suggest you simply minimise the window.

 

Now fire up a web browser and go to the IP address of the Pi.

E.g. if your IP address is 192.168.0.12 got to

 

http://192.168.0.12/

 

This gives you a simple web interface to MPD. It's not elegant but easy to use. Note: Sometimes it doesn't refresh the status of the playing tune - At the top right you'll see a link that reads "(refresh)". Just click on that while you are playing an MP3 and it will show you what it's doing!

 

You should see your one file on the left hand side and a playlst on the right. You can add a file to the playlist by hitting the [add] button next to the tune. Start playing the playlist by hitting the [Play] link on the right hand side. I'm sure you can work out the rest!

 

Right so now you have a little device with a simple but effective web interface but you only have one tune. Time to upload more. For this we will use a very important service called Samba. This is the basis for most storage servers out there and simply lets you share files to Windows (and Mac) computers. We will share out the new partition so you can upload some of your music to it.

 

Back to Putty. Connect back in if you disconnected. If you did disconnect make sure you are root again by typing “sudo -s” again.

 

To install samba simply type

 

apt-get install samba

 

First of all just get rid of the default configuration that it comes with. We are going to create our own. To remove the existing file type

 

rm /etc/samba/smb.conf

 

Now fire up nano to create the new configuration by typing

 

nano /etc/samba/smb.conf

 

And put the following in the file. Rather than typing all this in i'd suggest copying and pasting!!! To paste text in Putty simply right click on the screen. Make sure you have selected the correct text first of all though! If you make a big mess of this bit remember that unless you save your changes (Ctrl+o) you can just exit and start again...

 

workgroup = HOMENET

security = share

guest account = nobody

netbios name = MPD

[MUSIC]

writable = yes

path = /var/lib/mpd/music

public = yes

guest ok = yes

guest only = yes

guest account = nobody

browsable = yes

 

Save it (Ctrl+o) and close it (Ctrl+x) then restart samba by typing

 

/etc/init.d/samba restart

 

One more thing we have to do. In order to let you upload files to the pi we need to set looser permissions on the folder that stores all the music. Run the following command

 

chmod -R 777 /var/lib/mpd/music

 

Now assuming you are using a Windows machine, go to your "Run" icon and type in

 

\\MPD

 

If everything worked right you should now see your new file share called MUSIC. Open it up. If you want you could map the drive permanently but you don't need to. You should now be able to drag some MP3's into that folder. I'd suggest you use a proper hierarchy too (something like Artist/Album). Also remember you've only got a certain amount of storage. I'm using a 8Gb SDCard and so the share only can take 5Gb of music. Also for now this will probably only work with MP3's, not AAC or WMA tracks...

 

Once you've finished copying the files over go back to the web interface and click on the link at the top left that reads "(db update)". Depending on how much music you have copied over this might take a little while! If you click on that (refresh) link in the top right it will update the screen. Just wait till the "updating..." message has gone.

 

Hopefully now your new music will be displayed!

PHPMP is not hard to use, just fiddle about with it till you figure it all out.

Ok there's a couple of extra things we can do now, including using more storage and changing the volume but I'll leave that till next time...