Wednesday, January 21, 2009

Nifty little AppleScript to chart the BPM of your set

Here's another AppleScript that might be useful to Swing DJs out there. This one will pull the BPM of an iTune playlist and graph it two ways:
  • as a line chart, so you can see how your BPM flowed over the course of the night
  • as a frequency distribution, so you can see how you covered the tempo range

AppleScript doesn't make it all that easy to crunch numbers, so the code is a bit clunky. I use the Google Chart API to turn the data into graphics, which is a nifty tool though it has some quirks of its own.

I'd love to have a couple more tools like this, things that I could use for post-game analytics. It'd be great to have one that would tell me how often I spin the songs in my set (e.g., half my set is stuff that I've played 10 times in the past six months, 4 songs I'd never spun before, etc.). But it's tricky to get that info out of iTunes. What else would be useful to see as a chart?

Just like the last script, I make no warranty that this little program will actually work for you. Here's the code:
set frequency_dist to {}
set bpm_over_time to {}

tell application "iTunes"
set thePlaylist to the front window's view

set playlistName to ""
set nTracks to count of tracks of thePlaylist
set p to name of thePlaylist
repeat with w in every word of p
set playlistName to playlistName & "+" & w as text
end repeat
set playlistName to text 2 through (length of playlistName) of playlistName

repeat with i from 1 to nTracks
set end of bpm_over_time to bpm of track i of thePlaylist
set track_bpm to round (((bpm of track i of thePlaylist) - 1) / 10)
if track_bpm > (count of frequency_dist) then
repeat with j from ((count of frequency_dist) + 1) to track_
set end of frequency_dist to 0
end repeat
end if
if track_
bpm = 0 then
set track_
bpm to 1
end if
set (item track_
bpm of frequency_dist) to (item track_bpm of frequency_dist) + 1
end repeat
end tell

set first_non_zero to 1
repeat while item first_non_zero of frequency_dist is 0
set first_non_zero to first_non_zero + 1
end repeat

bpm_data_string to first item of bpm_over_time as text
repeat with i in items 2
thru (count of bpm_over_time) of bpm_over_time
if i <> max then
set max to f
end if
end repeat
set max to 3 * (round (max / 3) rounding up)

set vert_size to 50 + 28 * ((count of frequency_dist) - first_non_zero + 1)

set horiz_size to 15 * nTracks
if horiz_size > 600 then
horiz_size to 600
end if

set range to first_non_zero * 10 & "," & (count of frequency_dist) * 10
set line_chart_string to "
chxt=y&chxs=0,555555,12,0,t&chs=" & horiz_size & "x200&chd=t:" & bpm_data_string & "&chds=" & range & "&chxr=0," & range & "&chtt=BPM+by+song|" & playlistName

set freq_dist_string to "
chxt=x,y&chxs=0,555555,12,0,t|1,555555,12,-1,t&chd=t:" & freq_data_string & "&chds=0," & max & "&chs=300x" & vert_size & "&chxr=0,0," & max & "|1," & range & "&chtt=BPM+Frequency+Distribution|" & playlistName

tell application "Finder"
open location line_chart_string
open location freq_dist_string
end tell

1 comment: