XMMS2 and Python

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()

About aballai

something about myself...hrmmm
This entry was posted in programming, python, xmms2. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>