Class: JimmyJukebox::Jukebox

Inherits:
Object
  • Object
show all
Defined in:
lib/jimmy_jukebox/jukebox.rb

Defined Under Namespace

Classes: NoCurrentSongException, NoNewSongException, NoPreviousSongException, NoSongsException

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_user_config = UserConfig.new, continuous_play = true) ⇒ Jukebox

Returns a new instance of Jukebox.

Raises:



37
38
39
40
41
42
# File 'lib/jimmy_jukebox/jukebox.rb', line 37

def initialize(new_user_config = UserConfig.new, continuous_play = true)
  disable_monitor_powerdown if JimmyJukebox::RUNNING_X_WINDOWS
  self.user_config = new_user_config
  self.continuous_play = continuous_play
  raise NoSongsException if downloaded_song_paths.empty?
end

Instance Attribute Details

#continuous_playObject

Returns the value of attribute continuous_play.



12
13
14
# File 'lib/jimmy_jukebox/jukebox.rb', line 12

def continuous_play
  @continuous_play
end

#current_songObject

Returns the value of attribute current_song.



12
13
14
# File 'lib/jimmy_jukebox/jukebox.rb', line 12

def current_song
  @current_song
end

#initial_dpms_stateObject

Returns the value of attribute initial_dpms_state.



12
13
14
# File 'lib/jimmy_jukebox/jukebox.rb', line 12

def initial_dpms_state
  @initial_dpms_state
end

#next_songObject



58
59
60
61
62
# File 'lib/jimmy_jukebox/jukebox.rb', line 58

def next_song
  # reset @next_song each time it's accessed
  current_next_song, @next_song = [@next_song || random_song, random_song]
  current_next_song
end

#playing=(value) ⇒ Object (writeonly)

Sets the attribute playing

Parameters:

  • value

    the value to set the attribute playing to.



13
14
15
# File 'lib/jimmy_jukebox/jukebox.rb', line 13

def playing=(value)
  @playing = value
end

#songs_playedObject

Returns the value of attribute songs_played.



12
13
14
# File 'lib/jimmy_jukebox/jukebox.rb', line 12

def songs_played
  @songs_played
end

#user_configObject



199
200
201
# File 'lib/jimmy_jukebox/jukebox.rb', line 199

def user_config
  @user_config ||= UserConfig.new
end

Instance Method Details

#disable_continuous_playObject



168
169
170
# File 'lib/jimmy_jukebox/jukebox.rb', line 168

def disable_continuous_play
  self.continuous_play = false
end

#disable_monitor_powerdownObject



29
30
31
32
33
34
35
# File 'lib/jimmy_jukebox/jukebox.rb', line 29

def disable_monitor_powerdown
  if JimmyJukebox::RUNNING_X_WINDOWS
    self.initial_dpms_state = dpms_state
    puts "*** Disabling DPMS. Will re-enable DPMS on shutdown ***" if initial_dpms_state == 'Enabled'
    `xset -dpms` #`; xset s off`
  end
end

#downloaded_song_pathsObject



143
144
145
# File 'lib/jimmy_jukebox/jukebox.rb', line 143

def downloaded_song_paths
  user_config.song_paths
end

#dpms_stateObject



15
16
17
18
19
20
# File 'lib/jimmy_jukebox/jukebox.rb', line 15

def dpms_state
  if JimmyJukebox::RUNNING_X_WINDOWS
    xsettings = `xset q`
    xsettings.match(/DPMS is (.*)/)[1]
  end
end

#enable_continuous_playObject



164
165
166
# File 'lib/jimmy_jukebox/jukebox.rb', line 164

def enable_continuous_play
  self.continuous_play = true
end

#erase_songObject



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/jimmy_jukebox/jukebox.rb', line 111

def erase_song
  if current_song
    song_to_erase = current_song
    skip_song
    File.delete(song_to_erase.music_file)
    puts "Erased #{song_to_erase.music_file}"
    user_config.generate_song_list
  else
    raise NoCurrentSongException, "No current_song"
  end
end

#next_song?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/jimmy_jukebox/jukebox.rb', line 54

def next_song?
  @next_song
end

#pause_current_songObject



135
136
137
# File 'lib/jimmy_jukebox/jukebox.rb', line 135

def pause_current_song
  current_song.pause
end

#play_loopObject



44
45
46
47
48
49
50
51
52
# File 'lib/jimmy_jukebox/jukebox.rb', line 44

def play_loop
  loop do
    if continuous_play && !playing?
      play_next_song
    else
      sleep 0.1
    end
  end
end

#play_next_songObject



64
65
66
# File 'lib/jimmy_jukebox/jukebox.rb', line 64

def play_next_song
  play_song(next_song)
end

#play_random_songObject

def play_random_undownloaded_song

play_song(random_undownloaded_song)

end



160
161
162
# File 'lib/jimmy_jukebox/jukebox.rb', line 160

def play_random_song
  play_song(random_song)
end

#play_song(song) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/jimmy_jukebox/jukebox.rb', line 172

def play_song(song)
  self.playing = true
  terminate_current_song(play_another: false) if current_song
  self.current_song = song
  self.songs_played << song
  current_song.play(user_config, self)
  puts "Finished playing"
  self.current_song = nil
  self.playing = false
rescue Song::SongTerminatedPrematurelyException
ensure
  puts "-------------------------------------"
end

#playing?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/jimmy_jukebox/jukebox.rb', line 73

def playing?
  @playing ||= false
end

#previous_songObject



77
78
79
80
81
82
83
# File 'lib/jimmy_jukebox/jukebox.rb', line 77

def previous_song
  if songs_played.length >= 2
    songs_played[songs_played.length-2]
  else
    nil
  end
end

#quitObject



68
69
70
71
# File 'lib/jimmy_jukebox/jukebox.rb', line 68

def quit
  disable_continuous_play
  terminate_current_song if current_song
end

#random_songObject

def undownloaded_songs

user_config.undownloaded_songs

end

Raises:



151
152
153
154
# File 'lib/jimmy_jukebox/jukebox.rb', line 151

def random_song
  raise NoNewSongException, "JimmyJukebox can't find any songs to play!" if downloaded_song_paths.length == 0
  Song.new( downloaded_song_paths[rand(downloaded_song_paths.length)] )
end

#replay_previous_songObject



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/jimmy_jukebox/jukebox.rb', line 98

def replay_previous_song
  if previous_song && current_song
    enable_continuous_play
    self.next_song = previous_song
    puts "Replaying #{previous_song.music_file}"
    current_song.terminate
    self.current_song = nil
    self.playing = false
  else
    raise NoPreviousSongException, "No previous song"
  end
end

#replay_songObject



89
90
91
92
93
94
95
96
# File 'lib/jimmy_jukebox/jukebox.rb', line 89

def replay_song
  enable_continuous_play
  self.next_song = current_song
  puts "Replaying #{current_song.music_file}"
  current_song.terminate
  self.current_song = nil
  self.playing = false
end

#restore_dpms_stateObject



22
23
24
25
26
27
# File 'lib/jimmy_jukebox/jukebox.rb', line 22

def restore_dpms_state
  if JimmyJukebox::RUNNING_X_WINDOWS
    puts "*** Restoring DPMS state to 'Enabled' ***"
    `xset +dpms` if initial_dpms_state == 'Enabled'
  end
end

#skip_songObject



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/jimmy_jukebox/jukebox.rb', line 123

def skip_song
  if current_song
    enable_continuous_play
    puts "Skipping #{current_song.music_file}"
    current_song.terminate
    self.current_song = nil
    self.playing = false
  else
    raise NoCurrentSongException, "No current_song"
  end
end

#terminate_current_song(opts = nil) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/jimmy_jukebox/jukebox.rb', line 186

def terminate_current_song(opts=nil)
  # By default, stops song and lets a new song play automatically
  # To prevent another song from playing automatically, pass "play_another: false"
  if current_song
    puts "Terminating #{current_song.music_file}"
    current_song.terminate
    self.current_song = nil
    self.playing = (opts && opts[:play_another]) ? !play_another : false
  else
    raise NoCurrentSongException, "No current_song"
  end
end

#unpause_current_songObject



139
140
141
# File 'lib/jimmy_jukebox/jukebox.rb', line 139

def unpause_current_song
  current_song.unpause
end