Playlist Randomization

21 December 2007 ~ blog java groovy

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())

new File("random_${args[0]}").withWriter { writer->
    songs.each { line->
        writer.writeLine(line)
    }
}
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 languages?

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 figured I should implement the same script in Ruby. I was able to do it in
about ten minutes.

# rand_playlist.rb
lines = []
File.open("#{ARGV[0]}","r") do |file|
    while(line = file.gets)
        unless line[0..0] == '#'
            lines << line
        end
    end
end

lines.sort! { rand(3) - 1 }

out_file = File.new("random_#{ARGV[0]}","w");
lines.each do |line|
    out_file.puts line
end

You run this one the same as the last, except using ruby:

ruby rand_playlist.rb rock_n_roll.m3u

I wonder if there are other languages I should try implementing this in.


Creative Commons License CoffeaElectronica.com content is copyright © 2016 Christopher J. Stehno and available under a Creative Commons Attribution-ShareAlike 4.0 International License.