Monday, March 28, 2005

iTunes script: Album Shuffle

Although normally I love iTunes Party Shuffle, today I'm suddenly sick of it. It's too choppy, like channel-surfing TV; today I want to listen at album length but I'm not sure what I want to listen to.

It strikes me that what I need is an Album Shuffle to choose for me. Here's a first cut: iTunesAlbumShuffle.py. (Requires: iTunes 4.5 or above; Python 2.4.x or above plus the pywin32 extensions -- or just get the ActivePython distribution.) Run once to populate an album's-worth of tracks into an Album Shuffle playlist, which it'll create if necessary.

Ideally it'd stick around and watch what's being played, removing played albums and adding new albums as needed; but those are version 2 features.

At the scripting level, you get a glimpse into iTunes internals. Sources, playlists and tracks are what iTunes really deals in at the database level. Albums are an abstraction which iTunes, and iTunes scripters, have to build by aggregating tracks with common Artist and Album attributes. That's why this script has a long startup time: it has to walk the entire iTunes library to determine what albums are present.

A quicker method might be to pick a random track from the library and then do an IITPlaylist::Search to find other tracks in the same album. Pitfalls await the unwary here, though. IITPlaylist::Search can match either on all track attributes, or on one and one only of the track attributes. In either case, you'd need to filter the search results to flush out any false positives. And selecting with this method skews the random choice by weighting albums with many tracks heavier than albums with few tracks. (The current code gives an equal weighting to all albums; possibly the fairest method would weight albums by the inverse of their length, allowing short-and-sweet singles a better chance than bulky doubles.)

And as I'm using Python 2.4, I get a chance to play with two new minor features: the sorted() builtin, and the key argument to list.sort() and sorted(). Nice. A lot of the sorting code I've written in the past could have be streamlined with these.

Oh and its first pick? Coldplay's A Rush Of Blood To The Head.

Comments:

Out of curiousity - I'm sure you're aware of it, but were unhappy with it, and I'm curious as to your reasons - what was wrong with iTunes own "Shuffle by Album" mode?

I use it quite a lot for exactly the same reason you mentioned - I'm not sure what I want to listen to, but chopping and changing every single song sounds far too random
Ah. Um. Well, I didn't know about that. (It's tucked away in Preferences / Advanced.)

It's not quite what I wanted — it affects the Shuffle play mode, not the Party Shuffle source — but close enough to make me feel a little sheepish.

Ah well, it made for a good escuse to play with the scripting interfaces...
*grin*

I only know about it because it's a feature that is much more obvious on an iPod; I found it, I liked it, I searched for it in iTunes..