Daemon Diaries

cat /var/blog | grep posts | more

MPD OSD , 2 ways

with one comment

To play my music I use a program called MPD or Music Player Daemon … I put a bunch of keybindings in my Openbox config file to control MPD … but I never knew the name of the song that was playing currently without launching an MPD client ,
so I wrote a script to solve the problem … I mapped this to a key , it looks like this

There are two versions of the script , one in ruby and one in python , choose whichever floats your boat
for the ruby version , you need two gems


sudo gem install librmpd
sudo gem install libxosd-ruby

here’s the code

#!/usr/bin/env ruby
require 'rubygems'                              # Rubygems
require 'librmpd'                               # MPD Library
require 'xosd'                                  # OSD Library

mpd = MPD.new 'localhost', 6600                 # Init MPD
mpd.connect                                     # Connect to MPD Server
song = mpd.current_song                         # Get the current song

osd = XOSD::Xosd.new(5)                         # Init XOSD
osd.font = "-adobe-courier-*-r-*-*-24"          # Choose XOSD Font
osd.color = "#287D28"                           # Choose XOSD color (dark green)

if song.title then                              # Check to see if the song has a title
  osd.display_message(3, song.title)            # If so , display the title
  sleep(2)                                      # Display it for 2 seconds
else
  osd.display_message(3, song.file)             # if there is no title, display the filename
  sleep(2)                                      # Display it for 2 seconds
end

UPDATE: If you are running the latest version of Ubuntu (now Intrepid Ibex) you can skip these steps and just run the following command


sudo apt-get install python-osd python-mpd

If you’re not, follow on –

Then for the python version you need two modules:

http://ichi2.net/pyosd/pyosd-0.2.14.tar.gz
Extract that tarball and run


tar zxvf pyosd-0.2.14.tar.gz
sudo python setup.py install

then go to
http://pypi.python.org/pypi/python-mpd/
and Choose the format you want (zip,bzip,or gzip) , and decompress it
then run

sudo python setup.py install

ok , after all that…here’s the code

#!/usr/bin/env python
import mpd 					                        # Import MPD Library
import pyosd										            # Import OSD Library
import time											            # Import Internal Time Module

client = mpd.MPDClient()							      # Init MPD Client
client.connect("localhost", 6600)					  # Connect to local MPD Server

cs = client.currentsong()                   # Get the currentsong dict
if 'title' in cs:                           # Check to see if "title" title exists in the dict
 	songtitle = cs['title']                   # If it does set songtitle to the id3 title
elif 'file' in cs:                          # If it doesn't have a title use the filename
 	songtitle = cs['file']                    # Set songtitle to the filename

FONT = "-*-helvetica-*-r-normal--34-*-*"    # Set the font

COLOR = "#287D28"									          # Set the color (dark green)
p = pyosd.osd(font=FONT, colour=COLOR)		  # init pyosd with the font and color
p.display(songtitle)								        # Finally use Pyosd to display the song title

time.sleep(2)                               # Show the message for 2 seconsd

If you have any questions regarding this script or any other post of mine , please do not hesitate to contact me at
mike3.1459@mikedonghy.org minus pi

About these ads

Written by leachim6

June 22, 2008 at 4:30 pm

One Response

Subscribe to comments with RSS.

  1. awesome, thanks!

    slypheed

    November 27, 2008 at 7:39 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: