Extending the Raspberry Pi to a miniature music center

Configuring MPD

For this project, you will launch MPD as a system service on the Rasp Pi. The daemon reads its configuration from /etc/mpd.conf. If you call MPD directly as a user instead, it accesses the ~/.mpdconf configuration file; this is good information to have for testing purposes.

The most important point of the configuration involves telling MPD where to look for the music. Without any other clues, it uses the /var/lib/mpd/music/ directory for this purpose. MPD only accepts a single path for the music_directory option. If you want to spread your music over several directories, you need to link to the other directories in the default folder.

When first launched, MPD builds its database using title entries gleaned from the MP3 tags in your tracks. Large music collections can take quite some time: the Rasp Pi took about 20 minutes in my test with 10,000 tracks (see the box "Large Music Collections"). If MPD can't find your music, even though the files exist in the specified directory (mpc stats outputs current status information), it could be because permissions for the directory are not set correctly: MPD launches as the mpd user and needs read permission in the music folder. To set permissions, enter

Large Music Collections

When I attempted to read 10,136 music files from a Samba share in the test installation, the process stopped at 4,028 tracks. A manual update of the database using mpc update then brought the total up to 7,623 tracks. But, what had happened to the other tracks? This behavior seemed to be an isolated case, however, because a long search on the Internet brought no comparable problems to light.

Using a convenient workaround, I still managed to import all of the tracks. The individual subdirectories in my music collection each contained a maximum of only 15 directories with albums. Adding them to the MPD database didn't seem to cause any problems.

I thus used the script from Listing 1 to add just one subdirectory for each update and gradually worked my way through the entire collection. After doing this, all the tracks were available in the database.

Listing 1

Accessing All Tracks

fs=$IFS
IFS=$(echo -en "\n\b")
for f in $(ls <directory>);
       do echo "$f";
       mpc update --wait "$f";
done
IFS=$fs
sudo chmod -R o+r <music_directory>

and adjust <music_directory> according to your entry in mpd.conf.

After completing the basic configuration, you can set up MPD as a system service on the Rasp Pi. The following call handles this:

$ sudo update-rc.d mpd defaults

This command configures the service so that the system automatically loads it at boot time.

Pop and Crackle

In the default configuration, Debian uses the ALSA sound system for audio output on the Rasp Pi. After changing the default output channel from HDMI to analog output,

amixer cset numid = 3 1

where 1 stands for analog, the pops and crackles are unbearable. Blogs on the Internet report that money was saved on the Rasp Pi's audio hardware for obvious reasons.

The PulseAudio sound system works far more effectively on the Rasp Pi. To change the sound system to PulseAudio, add the following lines to your /etc/mpd.conf configuration file:

audio_output {
       type: "pulse"
       name: "MPD PulseAudio Output"
}

Adjusting the PulseAudio sound system for the Rasp Pi, however, proves to be somewhat tricky.

For PulseAudio ultimately to work as intended, you need tweaks in three different places. The first step is to ensure that the operating system loads pulseaudio at each system startup. To do this, you will need to add two entries to your /etc/default/pulseaudio file:

PULSEAUDIO_SYSTEM_START=1
DISALLOW_MODULE_LOADING=0

In the second step, you must prevent PulseAudio from entering power save mode when no sound is output, which makes for unpleasant clicking noises at the beginning and end of each track. To do this, you need to modify some entries in the /etc/pulse/system.pa file (Listing 2).

Listing 2

/etc/pulse/system.pa Changes

#load-module module-suspend-on-idle
load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1;192.168.0.0/24 auth-anonymous=1
load-module module-zeroconf-publish

Comment out the load-module module-suspend-on-idle line (by adding the hash as shown in the first line) and modify the network permissions for the local network (in this example, a home network on 192.168.0.0).

As a third step, convert the ALSA-configured libraries to PulseAudio by replacing the contents of the /etc/asound.conf file with:

pcm.pulse { type pulse }
ctl.pulse { type pulse }
pcm.!default { type pulse }
ctl.!default { type pulse }

You have now successfully navigated the necessary PulseAudio configuration tasks. Restart the daemons for PulseAudio and MPD with

$ sudo /etc/init.d/pulseaudio restart
$ sudo /etc/init.d/mpd restart

to ensure that the changes are applied.

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content