Playlist Randomization

I love WinAmp; however, I have always felt that it’s playlist randomization was a little on the weak side. Not really wanting to dive into writing a C++ winamp plugin, I took the alternate approach of writing a Groovy script to randomize playlist files.

// PlaylistRandomizer.groovy
import java.io.File
import java.util.ArrayList
import java.util.Collections
import java.security.SecureRandom

def songs = new ArrayList()
new File(args[0]).eachLine {
    if(!it.startsWith('#')){
        songs << it
    }
}

Collections.shuffle(songs,new SecureRandom())

def writer = new File("random_${args[0]}").newWriter()
songs.each {
    writer.writeLine(it)
}
writer.close()

println 'Done.'

You execute it with the file name of the playlist you want to shuffle.

groovy PlaylistRandomizer rock_n_roll.m3u

and it will generate a new, shuffled file, random_rock_n_roll.m3u.

It’s pretty simple and straight-forward. I am sure that I could spend a bit more time with it and pare it down a bit, but isn’t quick simplistic functionality one of the benefits of scripting langugages?

Note: I used SecureRandom instead of just the standard Random because it provides better shuffling, though the difference is not all that significant.

For some fun and practice, I should implement the same script in Ruby.

No votes yet