Showing posts with label AppleScripts. Show all posts
Showing posts with label AppleScripts. Show all posts

Wednesday, September 8, 2010

BPM graphs

Someone asked about how I make the BPM sparklines and frequency distributions that I post with each of my sets, so I thought I'd explain. It isn't a very elegant method: I export each setlist from my media player, copy it into Excel, and use a spreadsheet that I have set up to generate Google Chart API code. The code is embedded in the links I post, so the graph is generated when you click the link. To do this yourself, you need JRiver Media Center (or some other way of exporting setlists) and Excel.
  1. Create a customized view in your J-River Media Center. Under View->Customize View, select the following Columns to show, in this order: Name, Artist, Album, Duration, BPM. Click on Preset, save this view as "Setlist Export."
  2. Play a set, save it as a playlist. When viewing the playlist, select View->Customize View, then click on Preset and select "Setlist Export." Hit okay.
  3. Select File->Export Playlist, choose "Text File, delimited" and include fields "Only Visible Fields." Remember where the file is going to be saved, then hit okay.
  4. Download this excel file that I created, open it up, and select the tab called "set data."
  5. Find the set list file you just saved, open it in a text editor. Select all, hit Ctrl-C to copy it, and paste it into cell A1 of the excel sheet. It should automatically open the Text-to-Columns dialogue box. Enter "|" as the delimiter, hit okay. Your set should now appear in columns C through G of this sheet.
  6. Go to the "output" tab. Enter a title for your set into cell B1. Copy the contents of cell B4 and paste it into your internet browser. You should see a sparkline. Do the same with cell B5 to get the frequency distribution.
  7. Exclaim something nerdy. "Neato!" or "Mwaaaaaaahhhahaha!" would be appropriate.
  8. Go to column K of the tab "set data." Fill down to the end of your set, then copy-paste this into your blog entry to get the nicely formatted setlist.
  9. Leave a comment to this post and let me know how it works. Also let me know if you have improvements or alternative methods that work better.
By the way, back when I was using a Mac, I had a series of AppleScripts that did all this stuff. It was a bit cleaner than using Excel.

Monday, January 26, 2009

Disco XT to iTunes: another nifty little AppleScript

Here's yet another AppleScript that swing DJs might find useful. Disco XT is a software program (now available for Mac and PC) that I, and from what I hear, many other swing DJs, use to play our sets. It's my first choice because it:
  • reads my iTunes library,
  • works with dual audio output, so I can preview songs in my headphones while playing a different tune over the main speakers, and
  • automatically loads songs from a cue and transitions between songs.
It also keeps a log of every song played. I like to save all my DJ sets as iTunes playlists, so I wrote an AppleScript that will pull a Disco XT playlist back in to iTunes. The program is a little bit kluggy because you can't copy directly from a Disco XT log file--you have to export it as text first. So to use the script, you need to do the following in order:
  1. Play a set using Disco XT.
  2. When you're done, export your log to a text file.
  3. Open that file.
  4. Highlight the songs that you want to copy into iTunes and hit apple-C to copy.
  5. Open your iTunes and run this script.
  6. A new playlist called "Disco XT playlist" will be generated. Since Disco only allows you to export the title and artist of the tracks that you played, there is no way to uniquely identify a track if you happen to have multiple versions (for instance, your 6 different recordings of the Basie band playing One O'Clock Jump). The script pulls all of them, so you'll need to look through the playlist and delete the tracks that you didn't actually play.
If anyone has suggestions for improving the AppleScript code below, please feel free to share. Or if you have a better way of doing the same task that doesn't use an AppleScript at all, I'd love to hear it.

Here's the code:
tell application "Finder" to set theClipboard to the clipboard
set saveD to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set tempList to every text item of theClipboard
set theImportList to {}
repeat with lineItem in tempList
set AppleScript's text item delimiters to {" - "}
copy every text item of lineItem to end of theImportList
end repeat
set AppleScript's text item delimiters to saveD


tell application "iTunes"
-- create a new playlist
make new user playlist with properties {name:"Disco XT playlist"}
set newPlaylist to playlist "Disco XT playlist"
set the view of the front browser window to newPlaylist

-- add tracks to new playlist
repeat with trackItem in theImportList
set copy_these to (get every track of library playlist 1 whose artist is (first item of trackItem) and name is (second item of trackItem))
repeat with thisTrack in copy_these
duplicate thisTrack to newPlaylist
end repeat
end repeat
end tell

beep

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_
bpm
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

set
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
set
horiz_size to 600
end if


set range to first_non_zero * 10 & "," & (count of frequency_dist) * 10
set line_chart_string to "http://chart.apis.google.com/chart?cht=ls&
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 "http://chart.apis.google.com/chart?cht=bhs&
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

Friday, January 16, 2009

Nifty little AppleScript to publish a playlist

Those of you with copy-editorial leanings may have noticed that I recently changed how I publish my set lists on the blog.

A few weeks ago I wrote a little AppleScript that will take an iTunes playlist and copy the artist, title, album, length, and bpm of each track to the clipboard, in a format specified by the user. I just run this apple script on one of my set lists and wham-bam I can paste it into my blog.

If you're running a Mac, you can use it too. Open the Script editor to create a new script, then copy the code below into the file. Save the file under Library\iTunes\Scripts. The script should now be accessible from iTunes, from the little script drop-down menu (between Window and Help).

Here's the code:
set dialogResult to display dialog "Enter delimiter and click OK" default answer " - "
if button returned of dialogResult is "OK" then
set delimiter_character to text returned of dialogResult
tell application "iTunes"
set thePlaylist to the front window's view
set playlist_data to {}
tell thePlaylist
repeat with i from 1 to the count of tracks
tell track i
set the end of the playlist_data to {name, delimiter_character, artist, delimiter_character, album, delimiter_character, time as string, delimiter_character, bpm, return}
end tell
end repeat
end tell
end tell
tell application "Finder"
activate
display dialog "The playlist information has been copied to the clipboard"
set the clipboard to (playlist_data) as text
end tell
end if
FYI, this code is pretty rough-and-tumble--there's no error handling or anything like that, so it may not work for you. But it should be easy to modify, especially since the language is so easy to follow, almost English. I think the designers meant it to be the Apple equivalent of VBasic.

AppleScripts are pretty nifty. Don't know why I've never played with them before. I've got a few more in the works, so stay tuned.