Shuffle Smarter, Not Harder
NOT ON THE CPAN
Today's article is about code not found on the CPAN. In fact, this code won't even work anymore on many new compiles of perl
. It's still an interesting idea, and maybe I will rewrite it to use the scripting bridge in some other way -- either via PerlObjCBridge or some non-Perl language with a working scripting bridge.
Albums as Atomic Units
Some recording artists are still refusing to be sold through the iTunes music store, in part because they can't restrict shoppers from buying single tracks. That is, some artists want to say, "I made an album. You can buy the whole album, or you can go buy something else. You can't just buy track nine." I can really get behind that. These guys are artists, and they don't want people to think they've experienced their work if they've really just experienced part of it, out of context.
Unfortunately, the idea that "music is organized into tracks, and tracks can be shuffled arbitrarily" is getting more and more popular with the rise of MP3 players. Years ago, I realized that because of the way I built my "smart playlists" in iTunes, I was very rarely hearing large parts of albums that I liked, and that I was almost never listening to whole albums. When I realized this, I spent a few days only listening to whole albums, and it was fantastic. After that, I felt sad that it was so hard to keep doing that daily.
iTunes didn't have a way to make smart playlists that included whole albums. The iPod had recently lost the ability to shuffle by albums, too -- making listening to classical music much harder.
After sulking for a while, I realized that this would be really easy to fix by stepping outside the smart playlist editor and reaching for Mac::Glue to analyze my library and build a playlist by hand.
The code that follows does this, and I will sprinkle annotations explaining how it all works.
albumen
1: | use strict; |
So far, we've prepared to run our program, and gotten our handle on the iTunes automation "glue."
1: | my $pl = $itunes->obj(playlist => whose(name => equals => 'Regularer'))->get; |
We're going to consider as our input tracks in the "Regularer" playlist, which is a smart playlists that filters out stuff that, more or less, isn't really music, like spoken word, podcasts, and jazz. When we build our playlist out, it will go into the playlist "Albumen."
1: | { |
We empty out the old playlist from Album and get our new set of tracks to consider adding.
1: | my %album; |
Here, we build up %album
, a hash in which keys are album/artist pairs (using the justly-maligned list-in-hash-subscript-means-join-with-$;
feature) and values are arrayrefs of tracks. Now that we've got a data structure suitable for building our playlist, we can really get to work.
1: | my $DEFAULT_TIME = time - 30 * 86_400; |
We start iterating through all the albums we've got to consider, and skip any album with fewer than four tracks available. Once, I skipped any album that didn't have all of its tracks available, but this ended up causing problems with albums with some tracks that were not "Regularer" music, like hip hop albums with "spoken word" tracks, or with albums that had no "total tracks" tags.
1: | my @lp_dates = map { undef $_ if $_ eq 'msng'; $_ || $DEFAULT_TIME } |
Next, we figure out the average "last listened to" of the album. If we've listened to the whole thing recently, or to a lot of its tracks recently, we remove it from the playlist. This works really well, because iPod and iTunes will update the "last played," so the next time you sync your iPod and run albumen, the albums you've listened to since your last sync will be replaced.
1: | my @ratings = grep { $_ > 0 } map { $_->{rating} } @tracks; |
After removing albums we've heard too recently, we remove albums we don't want to hear ever again. We get an average of ratings, skipping unrated tracks, and drop the album if it's got under a three and a quarter star rating. (The stars displayed in iTunes are counted as 20 "rating points" in the scripting interface.)
Now we've gotten down to just albums we'll consider putting in the playlist: nothing we've just listened to, and nothing we dislike. Now it's time to start building the playlist:
1: | my $total_size = 0; |
We'll use %seen_artist
to keep track of artists whom we've added to our playlist already. If we've already added a Negativland album, we won't add another one. I had to add this feature after I ended up, one day, with a playlist of nothing but Elvis Costello. It wasn't bad so much as overwhelming.
1: | if ($opt->{interactive}) { |
Sometimes, you just want to be able to veto something that would otherwise show up. Maybe it's time for me to listen to Tragic Kingdom again, but I'm just not ready to hear it again. Interactive mode lets me tell albumen to wait for next time.
1: | $seen_artist{ $artist }++; |
Finally, we add the tracks and make a record of adding them. If we've gotten our playlist up to a gig, we're done. Otherwise, we go ahead and look for another album.
The Gift of Music
I can honestly say that writing this program improved the quality of my life. Maybe not by leaps and bounds, but it made me remember how albums sounded. I found that many albums in my library were much better taken as a whole than as individual tracks, and that means that I was now enjoying my music collection much more than I had in years.
It's the ability to keep "last played" and "play count" data synced with my mobile device that's kept me hooked on iTunes, despite its numerous, crushing flaws. I encourage anybody who uses other music software to reproduce this program for his software, just to see how nice it can be.