-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.rb
executable file
·110 lines (88 loc) · 2.22 KB
/
player.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
$LOAD_PATH << File.dirname(__FILE__)
require 'jukebox'
require 'track'
require 'mpg123_control'
class Player
SLEEP_DURATION = 1
PAUSED = 'pause'
PLAYING = 'play'
def initialize jukebox_url
@mpg123 = Mpg123Control.new
@jukebox = Jukebox.new jukebox_url
end
def run
loop do
handle_pause or handle_hammertime or handle_playlist or sleep(SLEEP_DURATION)
end
end
protected
def handle_pause
return false if @state == @jukebox.state
@state = @jukebox.state
if playing?
play current_track
else
current_track.start_time = @mpg123.current_track_time[:seconds_played]
@mpg123.toggle_pause
end
true
end
def handle_hammertime
if !hammertime_playing? && current_hammertime
current_track.start_time ||= @mpg123.current_track_time[:seconds_played]
play current_hammertime
return true
end
if hammertime_playing? && hammertime_done?
if @current_hammertime.pause_after?
@jukebox.pause
else
play current_track
end
@current_hammertime = nil
return true
end
false
end
def handle_playlist
return false unless playing? and (@jukebox.skip? or @mpg123.done_playing?)
@current_track = nil
play current_track
end
def current_track
@current_track ||= next_track
end
def current_hammertime
@current_hammertime ||= next_hammertime
end
def playing?
@state == PLAYING
end
def hammertime_playing?
!@current_hammertime.nil?
end
def hammertime_done?
@mpg123.current_track_time[:seconds_played] >= @current_hammertime.end_time
end
def play track
return unless track && track.file_location
@mpg123.play track.file_location, track.start_time
true
end
def next_track
file_location = @jukebox.next_track
return if file_location.nil? || file_location == ""
Track.new [file_location] rescue nil
end
def next_hammertime
track_attributes = @jukebox.next_hammertime
return if track_attributes.nil? || track_attributes.empty?
Track.new track_attributes rescue nil
end
end
jukebox_url = ARGV.last
pid = fork do
Signal.trap('HUP', 'IGNORE') # Don't die upon logout
Player.new(jukebox_url).run
end
Process.detach pid