Class: Songbirdsh::Player

Inherits:
Object
  • Object
show all
Includes:
Queue
Defined in:
lib/songbirdsh/player.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Queue

#dequeue, #each, #enqueue

Constructor Details

#initialize(preferences) ⇒ Player

Returns a new instance of Player.



16
17
18
19
20
# File 'lib/songbirdsh/player.rb', line 16

def initialize preferences
  @scrobbler = Scrobbler.new preferences
  @scrobbling = true
  @library = Library.new preferences
end

Instance Attribute Details

#libraryObject (readonly)

Returns the value of attribute library.



13
14
15
# File 'lib/songbirdsh/player.rb', line 13

def library
  @library
end

#matchesObject

Returns the value of attribute matches.



14
15
16
# File 'lib/songbirdsh/player.rb', line 14

def matches
  @matches
end

#scrobblerObject (readonly)

Returns the value of attribute scrobbler.



13
14
15
# File 'lib/songbirdsh/player.rb', line 13

def scrobbler
  @scrobbler
end

#scrobblingObject

Returns the value of attribute scrobbling.



14
15
16
# File 'lib/songbirdsh/player.rb', line 14

def scrobbling
  @scrobbling
end

Instance Method Details

#c(text, colour) ⇒ Object



22
23
24
# File 'lib/songbirdsh/player.rb', line 22

def c text,colour
  text.to_s.foreground colour
end

#currentObject



37
38
39
# File 'lib/songbirdsh/player.rb', line 37

def current
  (@pid and File.exist?('current_song')) ? YAML.load_file('current_song') : nil
end

#register(track) ⇒ Object



41
42
43
44
# File 'lib/songbirdsh/player.rb', line 41

def register track
  track.started = Time.now.to_i
  File.open('current_song', 'w') {|f| f.print track.to_yaml }
end

#restartObject



88
89
90
91
# File 'lib/songbirdsh/player.rb', line 88

def restart
  stop
  start
end

#startObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/songbirdsh/player.rb', line 46

def start
  if @pid
    puts "Already started (pid #{@pid})"
    return
  end
  @pid = fork do
    player_pid = nil
    Signal.trap('TERM') do
      Process.kill 'TERM', player_pid if player_pid
      exit
    end
    total_tracks = @library.with_db {|db| db[:media_items].count }
    loop do
      id = dequeue || (rand * total_tracks).to_i
      row = @library.with_db {|db| db[:media_items][:media_item_id=>id] }
      unless row
        puts "track with id #{id} did not exist"
        next
      end
      path = CGI.unescape(row[:content_url].slice(7..-1)) if row[:content_url] =~ /^file/
      unless path and File.exist? path
        puts "track with id #{id} did not refer to a file"
        next
      end
      @library.with_track(id) do |track|
        @scrobbler.update track if @scrobbling
        register track
        player_pid = path.to_player
        Process.wait player_pid
        @scrobbler.scrobble track if @scrobbling
      end
    end
  end
  puts "Started (pid #{@pid})"
end

#statusObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/songbirdsh/player.rb', line 26

def status
  if @pid
    track = self.current
    puts "Since #{c Time.at(track.started), :cyan}\n\t#{track}"
    played = Time.now.to_i-track.started
    puts "#{c played, :yellow} seconds (#{c track.duration-played, :yellow} remaining)"
  else
    puts 'not playing'.foreground(:yellow)
  end
end

#stopObject



82
83
84
85
86
# File 'lib/songbirdsh/player.rb', line 82

def stop
  return unless @pid
  Process.kill 'TERM', @pid 
  @pid = nil
end