Build your own music database for demos and samples

I have a music database I have been using for years for demos because lets face it, music data is a lot more engaging than tired old Northwind. The database I was using had been hacked together with some crude code that read MP3 metadata tags through some Windows APIs and a web service client that pulled down album art from Amazon. However it was write once/run once sort of code and I never got around to cleaning it up so others could generate their own database and not be stuck with the few albums I had data for in mine.

My buddy Tim recently used that database for a demo VSTO app he wrote and is demoing this week at the Office System Developer Conference, and he prodded me to update that utility code, so I finally got off my butt and did so (sorry it wasn’t in time for your session buddy!). One decision I made was to not try to resurrect the code for using the Amazon Web Service directly. The problem with doing so is twofold. One problem is that unless you find an exact match on Artist and Album, it is hard to pull down the right album cover in an automated fashion without getting garbage. And unless the metadata is already up to date on your MP3 or WMV files, or you have been extremely disciplined with a folder convention matching the artist and album name, you are hosed for trying to do that matching. The other hitch is that to use the Amazon Web Service, you have to go register as a dev with them, get some IDs, and then make sure that you don’t make more than one call per second to their web service.

I decided a better approach was to leverage embedded album covers in the MP3/WMV file metadata. That of course requires that your music files have those. So how do you get them there? You do what it says here and here. MediaMonkey is a great little app for updating the metadata of your music files, and it will pull down the album cover for you – one at a time. So it did take me a few hours one day that I was extremely brain dead and lazy and not feeling like doing anything remotely challenging to go through my hundreds of albums and get their album covers updated into the file. But that benefits you for Zune, iPod, Windows Media Player, and any other decent software that plays those files, so I have already found it well worth doing.

Once your music files have the album art in them, updating my utility became more of a one-stop shop. I just needed a library that would pull those tags out of the metadata for me. I found a nice one in CSID3Lib (http://sourceforge.net/projects/csid3lib). This made it a snap to load an MP3 file and suck the metadata out of it. Then a little LINQ to SQL sprinkled on and I have a decent little database generating tool.

You can find the source code for the database builder here. I also have a little WPF data app that demonstrates using the database here. There is a SQL script in the MusicDBBuilder project that you will need to run to create the database, but there is a command line switch that will do that for you. If you choose this, you will need to run once with the -c and master as the database connection string, then run again without the -c and the connection string to the new database, which will be named MusicLibrary based on the SQL script.

The project is currently configured in the debug command line parameters so that if you just run it in the debugger, it will create the database for you (or blow it away if it already existed and was not up to date).

You will need to go into the project properties and change the file path command line parameter to the root folder of wherever your music files live and change the connection strinig to the new database after the first run that creates the DB. It will recursively probe for music files, but will only read MP3 files in the current version. There is also a command line switch to turn off recursion and you can remove the -c if you just want it to add what it finds to the database instead of starting from scratch.