Archive for the ‘Uncategorized’ Category

Visualising my Music Library

June 4, 2009

When you have a day off of work, the sun is shining, and everybody is on the beach, the ultimate thing to do is… visualising your music library. I wanted to have a two dimensional schema of artists, where similar artists are drawn  near each other, and unsimilar artists are drawn further apart. I made a small groovy script that does this in three steps:

  1. It reads the artists from my amarok database. Amarok is my favourite software for playing music.
  2. It calls a service to get to know which artists are similar
  3. It creates a (conceptual) graph of the artists in my database. Each artist is represented as a node. Similar artists are linked by an edge. The graph is written to a dot-file, which can easily be visualised by graphviz.
Part of music collection visualisation

Part of music collection visualisation

All of this happens in only 53 lines of Groovy script. Let’s look at some parts of it.

1. Reading the database

First put the jdbc driver jar into your path. Then, connect to the database using a groovy.sql.Sql object and query it. My script converts the returned rows into a set of Strings [“Venetian Snares”, “Max Richter”, “Murcof”, …].

def getArtists() {
    def sql = Sql.newInstance("jdbc:mysql://localhost:3306/amarok", "amarok", 
        "PASSWORDHERE", "com.mysql.jdbc.Driver")
    sql.rows("select name from artist").name as HashSet

2. Doing a web service call offers a RESTful webservice, that returns a list of artists similar to another. You can simply obtain it’s results by sending a get request to the
proper URL. Groovy simply let’s you type “http://url”.toUrl().text to do that!

def getSimilar(artist) {
    def url = "
    def text = url.toURL().text
    def slurper = new XmlSlurper().parseText(text)
    slurper.similarartists.artist.collect{ }

The text is returned as xml. Reading Xml is very easy if you use Groovy’s XmlSlurper. It is what happens in the last two lines. The names of all
similar artists are returned as a list of Strings [“Swod”, “Rachel’s”, …]

3. Visualising

We now obtained the information to draw the graph. We have a list of artists in the music collection, and for each artist a list of similar artists. By doing
some processing, we can convert this into a dot-file, that represents a nice graph. The code for doing this is trivial. By opening the dot file in graphviz
we get a nice graph.

And all this didn’t take me too long. So don’t worry. I still spent most of the day in the park on my day off.

See the full script here .