When it comes to the right way to rate songs in your iTunes library, there’s been tons of discussion over the years. For me, though, the situation is much more basic: why would I bother to rate my songs when iTunes is buried under a plethora of terminal sessions, Xcode windows, and other stuff?
It’s inconvenient, and there has to be a solution to this problem!
Such was my thinking a couple of weeks ago. I’ve been having a rather serious urge to hack on hardware lately, and this seemed like a nice, simple project to keep me busy. I also had a spare Arduino sitting around, so what could be easier? I mean, it’s only five or ten minutes worth of hacking!
The concept was simple enough. Take six lighted pushbuttons, label one with a zero and the other five with stars, and line ‘em all up in a row. When iTunes plays a track, have it light up the correct number of stars based on the song’s rating – and if the song isn’t rated, flash the zero button. If someone wants to rate a tune, they can just hit the corresponding star button.
Of course, there are a thousand different apps out there that will do more or less the same thing up in the menu bar without the extra hardware, so why bother? Truth is, I ignore that side of my menu bar. I want something to tell me when something needs to be done, and it needs to be noticeable, yet subtle enough that I can ignore it if I don’t care at the moment.
Besides, a software solution would just be boring!
A quick dig through the parts bin reveals that I have plenty of LEDs, but no buttons. Oh well, a proof of concept then; I’ll deal with finding the right parts later. I already have everything else, and so I embark on–
The Great Arduino Experiment
I’m mostly a PIC guy, but the Arduino has gotten enough press lately that I got curious and bought one. It’s rather well done: slap some hardware together, write some code without having to know much about the underlying platform, upload, and off you go. Great concept, pretty good execution, even runs natively on the Mac.
Kudos to the Arduino folks.
So I pull out my shiny new Arduino Uno, a box of jumpers, some resistors, and some LEDs. The hardware build is trivial: connect the LEDs to the Arduino’s digital I/O ports, connect the Arduino to the Mac, whip up a little sketch, and add a dash of Cocoa on the Mac to talk to it. Five minute hack. In the end I even managed to salvage a single pushbutton that I could use to send some input as a test.
For the Mac side, Scripting Bridge provides a convenient method of talking to iTunes from any Cocoa app, and the rest is stock code to deal with the serial port – something I’ve done a fair few times before. The test app worked out to about 80 lines of code, all told.
The way it works is that the Cocoa app uses NSDistributedNotificationCenter to listen for a com.apple.iTunes.playerInfo message. When such a message is received, it grabs the current track rating, divides by twenty (since iTunes stores it as 0-100 instead of 0-5), and sends the resulting digit down the serial link in ASCII. If it gets a digit back from the link at any point, it reverses the process, multiplying the provided number by 20 and setting the track rating appropriately.
Not exactly rocket science. Took all of half an hour to put the whole thing together from start to finish, hardware included, so I was happy. All I needed were some appropriate lighted pushbuttons, and I’d have my toy!
And then I rebooted my Mac.
Now, the last time I used USB serial was way back in the dark ages, before it was common. It was a complete hassle back then; not only did you have to find the exact drivers for your adapter somewhere out there on the ‘net, but you also had to figure out which COM port was associated with it. It wasn’t all that fun since some of the drivers would randomly add sixteen or so COM ports to the system, even though there was only one physical device connected.
Initially, the Mac was much better about this, to my pleasant surprise. Yes, you have to go find the port, but it turns up in /dev under a prefix that there’s typically only one of. Not hard to find. Setting this once when configuring an application, while not ideal, isn’t a showstopper by any means for a one-off hack.
But OS X apparently doesn’t write down what it’s calling things, and has a tendency to forget on reboot.
Well, that just annoys me. I mean, if I’m gonna go to the trouble of building a device that connects over USB, it should be plug-and-play! Hell, even Windows ’95 (*snort*) was plug-n-play, right? It’s a must!
So I was left with the choice of either (a) putting together an old-skool device autodetection routine, or (b) building something from the ground up that can act like a proper, non-faux-serial, USB device.
Guess which one I chose…