Sunday 12 April 2015

Python, MPD and Volumio

I'm building a Raspberry Pi powered vintage radio, by stripping out all the gubbings and add a raspberry pi, IQAudio DAC & Amp.


I'm going to use Volumio to provide the music player and UI, but in order to get the existing buttons on the radio working I need to interact with MPD (Music Player Daemon) which is the server software which makes it all work.

There is a python library called python-mpd2 [code] which provides an api for communicating with MPD, its pretty easy to use and it allows you to do things such as change the volume, play, pause, stop, skip tracks, change playlists, search for music, etc.

Install python-mpd2

I found that I need to install python's "setup tools" when using the volumio image:
wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python
Once setup tools is installed you can download python-mpd2 and install it:
git clone git://github.com/Mic92/python-mpd2.git
cd python-mpd2
sudo python setup.py install
Usage

The python-mpd2 module is pretty easy to use, its effectively a python client for the MPD server. The first steps are to import it, create the MPDClient and connect to the server.
from mpd import MPDClient
client = MPDClient()
#to connect to MPD you need to know the IP address and port
client.connect("localhost", 6600)
Once you are connected you can use the client to issue commands to the MPD server, such as pulling back the current status and the current version.
print(client.status())
print(client.mpd_version)
Some useful other mpd-python2 functions are:
#set the volume between 0 and 100
client.setvol(100)

#play song at certain position
client.play(1)

#pause and resume playback
client.pause(0)
client.pause(1)

#skip to the next or previous track
client.next()
client.previous()

#clear current playlist and load a new one
client.playlistclear()
client.load("nameofyourplaylist")
Here is a complete list of mpd-python2 commands.

9 comments:

  1. I am building a similar vintage radio and plan on using almost the exact same configuration as you. I opted to use a hifiberry dac instead of the IAQ. I am just stumped on how to physically interface the pi with the rotary dials on my radio. I would love to have the rotary dials control volume and maybe change playlist when turned. How do you plan on physically interfacing your pi with the buttons on your radio?

    ReplyDelete
    Replies
    1. I have connected the original power switches and volume switch up using a MCP3008 analogue to digital converter. I have replaced the main tuning dial mechanism (a variable capacitor) with a rotary encoder.

      Delete
    2. Here is the setup and code for how I got the rotary encoder working.

      Delete
  2. Hello,
    very interesting project. Would it be possible to get the sourcecode? i'm just new to python programming and would love to make something like this...
    best regards

    paul

    ReplyDelete
    Replies
    1. Do you mean the code for my vintage radio build? If so ... the write up is here http://www.stuffaboutcode.com/2015/05/raspberry-pi-vintage-radio-build.html

      Delete
  3. I get the error when i type in "from mpd import MPDClient" then i get "from: can't read /var/mail/mpd" i am doing this on a raspberry pi running jessie (not lite) i am also just using the Terminal or i guess command prompt thing i REALLY need help this is my first time doing anything in the terminal so thank you so much

    ReplyDelete
    Replies
    1. What are you trying to do? Have you got MPD installed? Are you using a media player on your raspberry pi?

      Delete
  4. This comment has been removed by the author.

    ReplyDelete

Note: only a member of this blog may post a comment.