Creating a child-friendly audio player with the Raspberry Pi

Remote Control

After installing LIRC, you need to set up your remote, either by using irrecord to configure your controller of choice or by downloading its configuration file from the LIRC website  [4]. Irrecord records the signals from your remote and creates a configuration file for lircd.

I originally used a Logitech remote control for the prototype and then transferred the key assignments to the more compact Seki Slim remote  [9] – which only uses the Power, Next, Previous, Volume Up, Volume Down, and Play/Pause buttons – with the following command:

$ sudo irrecord /etc/lirc/Logi.conf

If this command causes difficulties with the remote model you are using, you can expand the call with the -f parameter to activate raw mode.

Next, you should integrate the configuration file Logi.conf in /etc/lirc/hardware.conf, the hardware configuration file (Listing  2). Then, configure irexec so you can execute other scripts with the touch of a button.

Listing 2



The irexec config file is /etc/lirc/lircrc (Listing  3) [10], which designates the default config value for each button. For example, the first button, KEY_POWER, calls the script with the argument power.

Listing 3


 prog = irexec
 button = KEY_POWER
 config = sudo -u pi /home/pi/ power
 prog = irexec
 button = KEY_NEXT
 config = sudo -u pi /home/pi/ next
 prog = irexec
 button = KEY_PREVIOUS
 config = sudo -u pi /home/pi/ prev
 prog = irexec
 button = KEY_VOLUMEUP
 config = sudo -u pi /home/pi/ volup
 prog = irexec
 config = sudo -u pi /home/pi/ voldown
 prog = irexec
 button = KEY_PLAY
 config = sudo -u pi /home/pi/ play

The Scripts

The script (Listing  4) accepts the power, next, prev, volup, voldown and play arguments. The events in the script result in various calls to XMMS2, making the music player go forward or backward to a title and increase or decrease the volume.

Listing 4

01 #!/bin/bash
02 AUDIODEV=hw:0
03 arg="$1"
04 case $arg in
05   "power")
06     zbarcam --nodisplay -Sdisable -Sqrcode.enable --prescale=320x240 /dev/video0 | /home/pi/
07     ;;
08   "next")
09     echo "Next Song"
10     xmms2 next
11     ;;
12  "prev")
13     echo "Previous Song"
14     xmms2 prev
15     ;;
16   "volup")
17     echo "volume 5 up"
18     xmms2 server volume +5
19     ;;
20   "voldown")
21     echo "volume 5 down"
22     xmms2 server volume -5
23     ;;
24   "play")
25     echo "toggle playback"
26     xmms2 toggle
27     status=$(xmms2 current)
28     if [[ "$status" =~ ^Paused ]]; then
29       play -q /home/pi/no.wav
30     fi
31     ;;
32 esac

The argument power starts the recognition process of a QR code (line  5) by calling the zbarcam program, which reads the data over the webcam and writes the recognized contents to standard output. This output is then processed by the script.

The --prescale=320x240 option reduces the resolution of the camera, because the default HD resolution would be much too detailed and compute intensive. The --nodisplay switch deactivates the output window because you do not need it in the script.

The script (Listing  5) then processes the recognized QR codes; you can see an example of the two-line text from the QR code in Listing  6. The first line contains the name of the performing artist, and the second contains the album title. Consequently, in lines  6 and  7 reads two lines of input, and lines  8 and  10 store the input in the variables $artist and $album. Then, the script stops recognition of the QR code with killall zbarcam in line  12.

Listing 5

01 #!/bin/bash
02 AUDIODEV=hw:0
03 while true; do
04   read qr
05   read qr2
06   if [[ "$qr" =~ ^QR ]]; then
07     artist=$(echo $qr | cut -d':' -f 3)
08     echo "artist: $artist"
09     album=$(echo $qr2 | cut -d':' -f 2-)
10     echo "album: $album"
11     if [[ "$album" =~ ^http ]]; then
12       killall zbarcam
13       xmms2 stop
14       xmms2 clear
15       xmms2 add $album
16       play -q /home/pi/source/rbar/ok.wav
17       xmms2 play
18       exit
19       s=""
20     else
21       s=$(xmms2 search album:"$album" | egrep -e "$album")
22     fi
23     if [[ -n "$s" ]]; then
24       killall zbarcam
25       xmms2 stop
26       xmms2 clear
27       xmms2 add album:"$album" -o "tracknr"
28       play -q /home/pi/source/rbar/ok.wav
29       xmms2 play
30       exit
31     else
32       xmms2 stop
33       play -q /home/pi/source/rbar/no.wav
34     fi
35     sleep 5
36   fi
37 done

Listing 6

QR Code Data

artist:Axel Scheffler
album:Der Gruffelo

If all goes well, the script will play a little jingle and start the playback. Otherwise, you will hear a different sound that indicates an error has occurred. The sound for the error message can also be fun, thereby helping to lower the frustration level of young users confronted with errors.

Buy this article as PDF

Express-Checkout as PDF

Pages: 8

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content