The mpd and lirc MINI HOWTO

Niklas Hofer


          
        

v0.09, 25 Jun 2003

This is a more or less detailed guide to configuration of the Music Player Daemon (MPD) written by Warren Dukes and LIRC (Linux Infrared Remote Control) by Christoph Bartelmus to work together in a home environment, for example as a music jukebox.

The Author is not a native English speaker so please be indulgent. If you find errors or you want to add something, please send him a diff to hofer[AT]informatik.hu-berlin.de .


Table of Contents
1. Introduction
1.1. Reasons
1.2. Updates
2. Quick Steps
2.1. Precautionary Preparations
2.2. For the Impatient
3. Infrared Hardware
3.1. Creating the infrared receiver
3.2. Remote control device
4. LIRC setup
4.1. Installation
4.2. LIRC Configuration
4.3. Testing the hardware configuration
4.4. Remote control configuration
5. MPD setup
6. LIRC - MPD interaction
6.1. mpc
6.2. mpc_playlist
6.3. irexec
7. Startup

1. Introduction

1.1. Reasons

You want to use MPD and LIRC together for (at least) one of following reasons:

  • You have got a hole bunch of music you want to listen to in a way that doesn't lack neiter comfort nor functionality.

  • You have got an old (or even new) PC you don't need anymore but your superduper hifi rack is unusable because you spent all your CDs to the local orphanage while just keeping the mp3 backups

  • ...


1.2. Updates

You may check out http://www.informatik.hu-berlin.de/~hofer for the most recent version of this HOWTO. If you find errors or you miss a theme, please add it and send the diff to the author.


2. Quick Steps

This section is written by Niklas Hofer (hofer[at]informatik.hu-berlin.de) (The latest version of this document is at "http://musicpd.sourceforge.net" . You may want to check there for changes). Mirror sites are at - "http://www.informatik.hu-berlin.de/~hofer" .


2.1. Precautionary Preparations

There are only some small Precautions:

  • save your old config files

  • prepare your neighbors for some future sleepless nights

  • ask your mum/girlfriend/wife for sanction and wishes!


2.2. For the Impatient

  • Create yourself an infrared receiver (hardware)

  • and don't forget to connect it (happened to me)

  • Find a unused remote control.

  • Download, configure and install LIRC

  • Download, install and configure MPD

  • Edit the LIRC config files as you need (this is the thing this Howto is all about)

  • Startup LIRC and MPD and have fun using the remote control

If you can do all that by yourself, you don't need this Howto.

See details of above steps in the following sections....


3. Infrared Hardware

3.1. Creating the infrared receiver

Please visit http://www.lirc.org/receivers.html and http://www.manoweb.com/alesan/lirc/cocoon/ for a detailed descripton for building an infrared receiver device. In Germany, you can get all the parts needed by CONRAD, a market dedicated to elektronic wares. The part IDs will be contained in the next revision of this HOWTO, if I find them.


3.2. Remote control device

Find an old VRC remote control (grab the one from your parents, they don't need it anyway). A TV remote works, too, but we want to use the Play, Stop, Pause keys for playback control. Buying a universal remote control that combines multiple commom remotes is possibility, too. Have a look at eBay.


4. LIRC setup

LIRC consists of two parts:

Please look at the LIRC homepage or in the documentation delivered by the source package for a more detailed description.


4.1. Installation

  1. First, go to http://www.lirc.org and download the recent version of lirc. Alternatively you can install the lirc package provided by your distribution. I suggest to do it manually, because on my system the compilation of the kernel module with the destribution given packages didn't work (maybe my fault).

  2. Unpack the archive and follow its INSTALL instructions.

    
    $ tar xvfz lirc-version.tar.gz
        $ cd lirc-version
        $ less INSTALL
                            
    If you get problems, please read all the output the install script produces, it may contain information to help solving them. Notice to set the correct options for the hardware you built (serial ports, adresses etc). You will need the sources of your currently running kernel (or the kernel you want to use with lirc) in /usr/src/linux or it must be a symbolic link to the regarding directory. I suggest you download the current kernel source from http://www.kernel.org and read the Kernel-HOWTO by Al Dev if you never built a kernel by yourself.

Finished with that you should have installed a kernel module (ie. lirc-serial) , the LIRC deamon (often in /usr/sbin/lircd) and the lirc configuration (/etc/lirc). The directories may vary, but the install script is very verbose and should have told you all the relevant information.


4.2. LIRC Configuration

Use your favourite editor the edit the files in /etc/lirc

  1. /etc/lirc/hardware.conf should look this way (depending on your hardware setup. This is for a receiver connected to the serial port.

    
                           LIRCD_ARGS=""
    
                               START_LIRCMD=false
    
                               #Try to load appropriate kernel modules
                               LOAD_MODULES=true
    
                               DRIVER="default"
                               DEVICE="/dev/lirc"
                               MODULES="lirc_serial"
                            

  2. The file /etc/lirc/lirc-modules-source.conf was created by the lirc configuration script and should match the values you entered. Again, here is an example for a serial connected device. So only the SERIAL section contains useful data.

    
                           # Coma separated list of lirc kernel drivers to build
                               LIRC_MODULES="serial"
    
                               # Serial module configuration (these may vary!)
                               LIRC_SERIAL_PORT="0x3f8"
                               LIRC_SERIAL_IRQ="4"
                               LIRC_SERIAL_CFLAGS=" -DLIRC_SERIAL_SOFTCARRIER"
    
                               # Sir module configuration
                               LIRC_SIR_PORT="UNCONFIGURED"
                               LIRC_SIR_IRQ="UNCONFIGURED"
                               LIRC_SIR_CFLAGS="UNCONFIGURED"
    
                               # Parallel module configuration
                               LIRC_PARALLEL_PORT="UNCONFIGURED"
                               LIRC_PARALLEL_IRQ="UNCONFIGURED"
                               LIRC_PARALLEL_TIMER="UNCONFIGURED"
                            


4.3. Testing the hardware configuration

Plug in your receiver, run lircd by typing (as root)


   # /etc/init.d/lirc start
             
,run mode2 delivered by the lirc package (don't confuse with mode3), grab your remote control and start typing satanic patterns on your remote while pointing it to the receiver. LIRC must not run! You should see output like this:

                pulse 93
                space 4965
                pulse 108
                space 4969
                pulse 93
                space 7496
                pulse 93
                space 7489
                pulse 93
                space 47915
                pulse 138
                space 7475
                pulse 93
                space 7494
                pulse 93
             
If not, please read http://www.lirc.org/html/install.html . If yes, then be happy and continue with the next paragraph.


4.4. Remote control configuration

Here comes the first tricky part. If you got a common remote control (not universal) the go to http://lirc.sourceforge.net/remotes/ and get the config file for your remote. If your device is not listed there, you have to create a file for yourself. Don't worry, its very easy. Maybe more easy than seeking for the correct file in the archive. Please read the irrecord manpage.

If you have got a well documented universal remote control you can switch it exactly to a remote control you can find in the above archive. In the case you ain't got a good documentation ("Press 123 for getting THE Phillips remote") you have to try a bit. Or simply select your favourite manufacturer at the remote and create a config file yourself. Note: All the keys to get use of the best way this is, young jedi!

After all, you should have a config file for your remote. It is yours. Your own. Your precioussss. Hide it in the mountains and keep it at a safe place. Copy it to /etc/lirc/lircd.conf. A broad outline:


         # this config file was automatically generated
         # using lirc-0.6.4-CVS(hauppauge) on Sat May 26 21:15:45 2001
         #
         # contributed by G.P.M. van Dijnsen
         #
         # brand:             Philips
         # model:             RC 5260
         # supported devices: Unknown TV set and unknown VCR ( suspected pre 1987 )
         #

         begin remote

           name  Philips_5260
           bits            6
           flags RC5|CONST_LENGTH
           eps            30
           aeps          100

           one           889  889
           zero          889  889
           plead         889
           pre_data_bits   7
           pre_data       0x40
           gap          113792
           toggle_bit      2


               begin codes
                   1                        0x0000000000000001
                   2                        0x0000000000000002
                   3                        0x0000000000000003
                   4                        0x0000000000000004
                   5                        0x0000000000000005
                   6                        0x0000000000000006
                   7                        0x0000000000000007
                   8                        0x0000000000000008
                   9                        0x0000000000000009
                   0                        0x0000000000000000
                   1-                       0x000000000000000A
                   2-                       0x000000000000000B
                   brightness_down          0x0000000000000013
                   brightness_up            0x0000000000000012
                   contrast_down            0x0000000000000015
                   contrast_up              0x0000000000000014
                   volume_down              0x0000000000000011
                   volume_up                0x0000000000000010
                   trebble_down             0x0000000000000019
                   trebble_up               0x0000000000000018
                   bass_down                0x0000000000000017
                   bass_up                  0x0000000000000016
                   show_settings            0x000000000000000F
                   green                    0x000000000000000E
                   mute                     0x000000000000000D
                   skip_forward             0x0000000000000022
                   power                    0x000000000000000C
                   txt_off                  0x000000000000003F
                   txt_on                   0x000000000000003C
                   txt_clock                0x000000000000002A
                   txt_half                 0x000000000000002E
                   txt_question             0x000000000000002C
                   txt_cross                0x000000000000002D
                   txt_hold                 0x0000000000000029
                   txt_enlarg               0x000000000000002B
                   bell_on                  0x000000000000003B
                   bell_off                 0x000000000000002F
                   record                   0x0000000000000037
                   back                     0x0000000000000032
                   pause                    0x0000000000000036
                   play                     0x0000000000000035
                   forward                  0x0000000000000034
                   arrows                   0x0000000000000033
                   xmark                    0x0000000000000031
               end codes

         end remote
             
For testing you now should restart LIRC, run irw and type satanic pattrerns, again. The output should look like this:

                0000000000001010 00 volume_up Philips_5260
                0000000000001011 00 volume_down Philips_5260
                000000000000100d 00 mute Philips_5260
                000000000000103f 01 txt_off Philips_5260
                000000000000100d 00 mute Philips_5260
             
You see a code (like morse, but ir) you may ignor, the key name and the remote's name. This is a good way to procure the key mapping of your configuration.


5. MPD setup

Please download the package from http://musicpd.sourceforge.net or checkout the cvs like described there. Consult the INSTALL file to find alle the neccassary information to build and install your very own MPD.


6. LIRC - MPD interaction

6.1. mpc

You know should have a running lirc that listens to your remote control and an MPD accessible by telnet or by the webinterface phpMp.

Now we have to let both interact with each other. Since the remote control has an one-way interface we only have to associate the keypresses on the remote to MPD functions. It's as easy as clever: in the lirc package there is an executable named irexec, which executes prearranged executables when hitting keys. And this is exactly the feature we want. But which executable do we want to launch? Simply passing the command to telnet would be a way. I wrote a small perl script (mpc for MP client), which does exactly this and can be modified in a very simple way (for everyone having basic perl knowledge). Here it is:


             #!/usr/bin/perl

             # mpc
             # by Niklas Hofer , alzhaimer at gmx.net
             # connects to mpd and sends given command
             # can't get feedback yet - please contact me if you know a solution
             
             # Settings | please modify
             $host='127.0.0.1';
             $port='2323'; 

             # you don't have to set this if telnet is in PATH
             $mytelnet='/usr/bin/telnet';

             ## the fun begins here


             # using telnet is faster than using Net::Socket
             # not using strict for faster execution

             chomp ($telnet=`which telnet`);
             $telnet=$telnet ? $telnet : $mytelnet;

             $msg=join(' ', @ARGV );

             if ($msg) {
                open TN,"|$telnet $host $port" || die qq~can't pipe to $telnet: $!~;
                print TN $msg."\n";
                close TN;
             }
          


6.2. mpc_playlist

And I wrote another scipt - named mpc_playlist - that loads all playlists beginning with a specified number, letter or string. So you can press ie. the '1' key on your remote to add rock songs to the playlist, if you named the rock lists 1_metallica or 1_sepultura and so on, accoordingly to other numbers.


             #!/usr/bin/perl


             # mpc_playlist
             # by Niklas Hofer , alzhaimer at gmx.net
             # uses mpc to add playlists beginning with special char

             # Settings | please modify
             $playlistdir ='/data/playlists/' # has to end with slash (lazyness, speed)


             ## the fun begins here
             $first=shift || die "you must specify some of the first chars of a playlist\n";
             @files=sort <$playlistsdir/*.m3u>;
             foreach (@files) {
                  chomp;
                  s/^.+\///;
                  if (/^$first/) {
                         s/\.m3u$//;
                         system("mpc load $_");
                   }
             }
          
Don't forget to place mpc and mpc_playlist in your path - copy them to /usr/bin/ or /usr/local/bin.


6.3. irexec

The config file of irexec is /etc/lirc/lirc.lircrc . Here is an example:


            # using aumix to control volume
            begin
                    remote = Philips_5260
                    # replace this by the name of your remote control
                    
                    button = volume_up
                    # this is the name of the key (get it by irw)
                    
                    prog   = irexec
                    # irexec execs any desired executable, 
                    # specified by config (see below)
                    
                    repeat = 1
                    # is this key repeatable (bad for mpc commands)
                    
                    config = aumix -w +3 -v +3;aumix -S
                    # and here is the exec to run
                    # if you want multiple commands run in a row, join them
                    # with semicola

                    # if you want several alternating commands 
                    # simply add a new config line (and set repeat=0)
            end

            begin
                    remote = Philips_5260
                    button = volume_down
                    prog   = irexec
                    repeat = 1
                    config = aumix -w -4 -v -4;aumix -S
            end

            # mute (sound on/off)
            begin
                    remote = Philips_5260
                    button = mute
                    prog   = irexec
                    repeat = 0
                    config = aumix -S;aumix -w 0 -v 0
                    config = aumix -L
            end

            # open/close CDROM
            begin
                    remote = Philips_5260
                    button = txt_cross
                    prog   = irexec
                    repeat = 0
                    config = eject /cdrom
                    config = eject -t /cdrom
            end


            ########################################
            # mpd control
            ########################################
            begin
                    remote = Philips_5260
                    button = txt_hold
                    prog   = irexec
                    repeat = 0
                    config = mpc stop
            end
            begin
                    remote = Philips_5260
                    button = txt_clock
                    prog   = irexec
                    repeat = 0
                    config = mpc next
            end
            begin
                    remote = Philips_5260
                    button = txt_half
                    prog   = irexec
                    repeat = 0
                    config = mpc previous
            end
            begin
                    remote = Philips_5260
                    button = txt_enlarg
                    prog   = irexec
                    repeat = 0
                    config = mpc play 0
            end
            begin
                    remote = Philips_5260
                    button = green
                    prog   = irexec
                    repeat = 0
                    config = mpc pause
            end
            begin
                    remote = Philips_5260
                    button = txt_question
                    prog   = irexec
                    repeat = 0
                    config = mpc shuffle
            end

            ########################################
            # playlist loading
            ########################################
            begin
                    remote = Philips_5260
                    button = 1
                    prog   = irexec
                    repeat = 0
                    config = mpc_playlist 1
            end
            begin
                    remote = Philips_5260
                    button = 2
                    prog   = irexec
                    repeat = 0
                    config = mpc_playlist 2
            end
            begin
                    remote = Philips_5260
                    button = 3
                    prog   = irexec
                    repeat = 0
                    config = mpc_playlist 3
            end
            # ....
            begin
                    remote = Philips_5260
                    button = show_settings
                    prog   = irexec
                    repeat = 0
                    config = mpc clear
            end
        


7. Startup


          /etc/init.d/lircd start
          mpd /your/config/file
       
From now on you can use the web interface and your remote to control your music.