Tenderlove Making

Identifying unknown music with Ruby

Ben Bleything inspired me (or rather distracted me from my yak shaving) to get my music library cleaned up and remove duplicates. Unfortunately my duplicates don’t necessarily have ID3 tags, and they may be in different formats, so I wrote a gem called “earworm” which will identify unknown music. First I’ll give you a code sample, then explain how to get earworm working, and finally explain how earworm works.

Here is the code sample:

ew = Earworm::Client.new('MY Music DNS Key')
info = ew.identify(:file => '/home/aaron/unknown.wav')
puts "#{info.artist_name} - #{info.title}"

Earworm just needs a MusicDNS key and a file name to return information about your unknown audio.

Okay, now for getting the gem to work. Unfortunately this takes a little work right now. You need to install a few libraries before earworm will work. These libraries let you decode and identify mp3, ogg, and wav files.

For OS X (assuming you have MacPorts already installed) do this: $ sudo port install libogg libvorbis lame libofa

For Linux:
$ sudo yum install libogg-devel libvorbis-devel lame-devel libofa

Then just install the gem like so: $ sudo gem install earworm

The final thing you need is a key from <a href="http://www.musicip.com/dns/index.jsp">MusicDNS</a> which you can obtain <a href="http://www.musicip.com/dns/license.jsp">here</a>.  The key is free for non-commercial applications.  Now you should be set!  Earworm will automatically decode mp3 files and ogg files for you, so you don't have to do that yourself.

How does earworm work?  First, it relies on <a href="http://icanhasaudio.com/">icanhasaudio</a> for audio decoding.  Second, it uses <a href="http://ruby-doc.org/stdlib/libdoc/dl/rdoc/index.html">DL</a> to wrap <a href="http://code.google.com/p/musicip-libofa/">libofa</a>.  libofa takes between 10 and 135 seconds of audio and generates a hash.  After getting the hash for the audio snippet, earworm posts the hash (along with your key and some other info) to MusicDNS's web service.  The web services returns an XML document with information about the audio snippet.  Earworm parses the XML document and returns a nice object containing the information you requested.  Thats it!  No magic, and very simple (IMO) <a href="http://github.com/aaronp/earworm/tree/master">source code</a>.

« go back