Close

Turn On, Tune In, Drop Out

A project log for VoiceBox

Adding a Flask web interface (and other stuff) to the Google AIY Voice Kit.

tmTM 08/09/2018 at 15:270 Comments

I started out with Unix on vt100 terminals, so the thought of playing music from the Linux command line would never have occurred to me. If you wanted music on a dumb terminal you could only press ^G and use your imagination, just like ASCIIpr0n (NSFW if you're Amish). However, the Hackaday bots added Brad Arnett's Wolfgang project to the "Similar Projects" section below my first log entry (if only Google Assistant's AI was as good). "Wolfgang" introduced me to MPD, the Linux Music Player Daemon which makes adding basic music playing functionality almost painless. The first thing to do is to install MPD and a command line client, MPC.

sudo apt-get install mpd mpc

Then comes a bit of a problem: we're relying on the cooperation of two Lennart Poettering projects: PulseAudio and systemd. To get sound from PulseAudio, the mpd service needs to be run by a user (pi) rather than root. This screws up access control to the various log files used by the daemon. The simplest solution is to move MPD's files to pi's home directory.

mkdir ~/.mpd
mkdir ~/.mpd/playlists

Edit the file /lib/systemd/system/mpd.service (using sudo nano) to add a couple of entries under the [Service] heading:

[Service]
User=pi
Environment=DISPLAY=:0

(This causes systemd to run the MPD service as pi and gives the service access to PulseAudio in an X environment.)

We then edit the MPD configuration file /etc/mpd.conf changing pathnames to point to the new directories:

music_directory         "/home/pi/music"
playlist_directory      "~/.mpd/playlists"
db_file                 "~/.mpd/tag_cache"
log_file                "~/.mpd/mpd.log"
pid_file                "~/.mpd/pid"
state_file              "~/.mpd/state"
sticker_file            "~/.mpd/sticker.sql"

I also enabled PulseAudio output in mpd.conf (to enable music volume control):

audio_output {
    type    "pulse"
    name    "My Pulse Output"
}

After a reboot, the MPD service should be up and running ("journalctl -u mpd -f" should show the error log). We can now add some music. I used:

mpc add http://audio.wgbh.org:8108

to add WGBH Boston's Bach station to the (initially empty) playlist.

You can then test with commands like:

mpc play
mpc volume +10
mpc current
mpc playlist
mpc stop

 We can have the Assistant switch music on and off by adding the following to process_event():

def process_event(assistant, event):
...
    elif event.type == EventType.ON_RECOGNIZING_SPEECH_FINISHED and event.args:
...
        elif 'music on' in text or 'play music' in text:
            assistant.stop_conversation()
            subprocess.Popen(['mpc', 'play'])      
        elif 'music off' in text or 'stop music' in text:
            assistant.stop_conversation()
            subprocess.Popen(['mpc', 'stop'])      

And that's it, you should now be all Bached up:

Later we'll add some more streaming functionality (using the python-mpd2 library), but that's enough for now. 

Discussions