XMMS2 and Python

November 30th, 2008

I rarely get to sit down at my home machine and make something for my own use these days. I’ve always been interested in working with xmms and now these days xmms2. I built a plugin years ago that would dump audio information via http. These days I’d do the same, but with a script instead of a compiled c module for xmms. So this brings me to experimenting with xmms2 and python. This could be the first in a series of me blogging about python integration and what is possible with linux libraries and apps that have native python libraries. Python lends itself to being a lightweight language with great performance and fast turnover. According to debian there is a package that contains xmms2 bindings for python.

python-xmmsclient - XMMS2 - Python bindings
apt-get install python-xmmsclient

I decided to port a display_track shell script found on a debian administration article

Instead of using osd-cat I went with aosd-cat since I like the options better and it uses the libaosd library. Whatever I build next is news to me =) See the code below if you are interested in forking it for yourself.

By the way, excuse the formatting, its a bug in my code highlighter!

  1. #!/usr/bin/env python
  2. import xmmsclient
  3. import os
  4. import sys
  5. import time
  6.  
  7. def xmms_connect(name):
  8. xmms = xmmsclient.XMMS(name)
  9. try:
  10. xmms.connect(os.getenv("XMMS_PATH"))
  11. except IOError, detail:
  12. print "Connection failed:", detail
  13. sys.exit(1)
  14.  
  15. return xmms
  16.  
  17. # retrieve current playing id
  18. def xmms_playback_id(xmms):
  19. result = xmms.playback_current_id()
  20. result.wait()
  21. if result.iserror():
  22. return 0
  23.  
  24. id = result.value()
  25.  
  26. return id
  27.  
  28.  
  29.  
  30. def get_current_track(xmms, playback_id):
  31. result = xmms.medialib_get_info(playback_id)
  32. result.wait()
  33.  
  34. if result.iserror():
  35. print "medialib get info returns error, %s" % result.get_error()
  36. return None
  37.  
  38. minfo = result.value()
  39. try:
  40. artist = minfo["artist"]
  41. except KeyError:
  42. artist = "No Artist"
  43.  
  44. try:
  45. title = minfo["title"]
  46. except KeyError:
  47. title = "No Title"
  48.  
  49. try:
  50. bitrate = minfo["bitrate"]
  51. except KeyError:
  52. bitrate = 0
  53.  
  54. return "%s - %s [%s]" % (artist, title, bitrate)
  55.  
  56. def main():
  57. last_playback_id = 0
  58. osd_options="\
  59. --font=‘-b&h-lucida-medium-normal-*-96-*-*-*-p-*-iso10646-1′ \
  60. -B black \
  61. -R white \
  62. -b 100 \
  63. --x-offset=50 \
  64. --y-offset=-50 \
  65. --width=900"
  66. xmms = xmms_connect("aosd_display")
  67.  
  68. while 1:
  69. playback_id = xmms_playback_id(xmms)
  70.  
  71. if playback_id != 0 and playback_id != last_playback_id:
  72. last_playback_id = playback_id
  73. track = get_current_track(xmms, playback_id)
  74. os.system("echo ‘%s’ | /usr/bin/aosd_cat %s" % (track, osd_options))
  75. time.sleep(2)
  76.  
  77. main()

Code Poetry, it’s an Art

November 21st, 2007

Gopal VTo my left is a kickass compiler poet named Gopal Vijay(shortened). I worked with Gopal when I just started to learn compilers and frameworks on the DotGNU Project. He understands what hard work is, as does Rhys Weatherly, the original author of Portable.NET. Both of them had mentored me to become a better programmer all around and not only understand what it means to do it correctly, always. After a couple years of working on DotGNU I moved on to constructive network protocol development which pushed me straight back into webservices where I stand today repeating the circle.

The reality is that we like to speak, walk, run around, and play with code. It’s our paintbrush, tinker toy, classical instrument, and pen. It can do things that people can take for granite or widen peoples’ eyes.

We all need to respect the programmer and give him/her a pat on the back. A raise might be nice too ;)

Yes, it’s true, we develop for ourselves, some more than others. If we want to build a god damn game engine for a calculator, by damn we will. You can’t stop us!

A toast for the digital poets!