class Gramola::Player
Public Class Methods
new(argv)
click to toggle source
# File lib/gramola.rb, line 24 def initialize(argv) @shuffle = true #default parse_options(argv) @files = Dir[File.join(Dir.home,MUSIC_FOLDER,'**',MUSIC_EXTENSIONS)] @songs = filter_by_groups(@files,@groups) @length = get_total_length(@songs) end
Public Instance Methods
info()
click to toggle source
# File lib/gramola.rb, line 32 def info "Playing #{@songs.size} songs for about #{print_length(@length)} in #{shuffle_info}" end
play()
click to toggle source
# File lib/gramola.rb, line 36 def play (@shuffle ? @songs.shuffle : @songs).each{|s| `afplay "#{s}"`} #Main script function end
Private Instance Methods
filter_by_groups(files,groups)
click to toggle source
# File lib/gramola.rb, line 58 def filter_by_groups(files,groups) if groups.any? groups = groups.map &:downcase files.select{|f| f.downcase =~ Regexp.new("#{groups * '|' }") } else files end end
get_total_length(filenames)
click to toggle source
Returns total lenght of the files
in seconds.
# File lib/gramola.rb, line 68 def get_total_length(filenames) shell_formatted_filenames = Shellwords.join filenames res = `afinfo -b #{shell_formatted_filenames}` # total info length = 0 res.lines{|l| length = length + l.split.first.to_f if l.split.first.to_f} length end
parse_options(argv)
click to toggle source
# File lib/gramola.rb, line 42 def parse_options(argv) parser = OptionParser.new do |opts| opts.banner = "Usage: gramola.rb [-l] [file ...]. Shuffles all *.mp3 under ~/Music by default." opts.on('-l', '--list', "Play the files in order") {@shuffle = false} opts.on_tail("-h", "--help", "Show this message") do puts opts exit end @groups = opts.parse! end end
print_length(seconds)
click to toggle source
# File lib/gramola.rb, line 76 def print_length(seconds) m, s = seconds.divmod(60) h, m = m.divmod(60) d, h = h.divmod(24) [d > 0 ? ("%d day" % d) : nil, h > 0 ? ("%d hours" % h) : nil, m ? ("%d minutes" % m) : ("%d seconds... Orly?")].compact * ' ' end
shuffle_info()
click to toggle source
# File lib/gramola.rb, line 54 def shuffle_info @shuffle ? "shuffle mode" : "list mode" end