Class: Mumbletune::HallonPlayer

Inherits:
Object
  • Object
show all
Defined in:
lib/mumbletune/hallon_player.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHallonPlayer

Returns a new instance of HallonPlayer.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/mumbletune/hallon_player.rb', line 11

def initialize
  conf = Mumbletune.config

  @play_history = Array.new
  @add_history  = Array.new
  @queue        = Array.new
  @prev_id      = 0
  @ready        = false
  @audio_queue  = Queue.new

  @ready = true
end

Instance Attribute Details

#add_historyObject

Returns the value of attribute add_history.



9
10
11
# File 'lib/mumbletune/hallon_player.rb', line 9

def add_history
  @add_history
end

#audio_queueObject

Returns the value of attribute audio_queue.



9
10
11
# File 'lib/mumbletune/hallon_player.rb', line 9

def audio_queue
  @audio_queue
end

#current_trackObject

Returns the value of attribute current_track.



9
10
11
# File 'lib/mumbletune/hallon_player.rb', line 9

def current_track
  @current_track
end

#play_historyObject

Returns the value of attribute play_history.



9
10
11
# File 'lib/mumbletune/hallon_player.rb', line 9

def play_history
  @play_history
end

#queueObject

Returns the value of attribute queue.



9
10
11
# File 'lib/mumbletune/hallon_player.rb', line 9

def queue
  @queue
end

#readyObject

Returns the value of attribute ready.



9
10
11
# File 'lib/mumbletune/hallon_player.rb', line 9

def ready
  @ready
end

Instance Method Details

#add_collection(col, now = false) ⇒ Object

Queue Control



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/mumbletune/hallon_player.rb', line 76

def add_collection(col, now=false)
  only_track = empty?

  # add to the queue
  if now
    @queue.unshift col
  else
    @queue.push col
  end

  # record in additions history
  @add_history.push col

  # play it, if this is the first track or if the user specified `now`
  self.next if now || only_track
end

#any?Boolean

Returns:

  • (Boolean)


65
66
67
68
69
# File 'lib/mumbletune/hallon_player.rb', line 65

def any?
  cols_with_more = @queue.map { |col| col.any? }
  cols_with_more.delete_if { |m| m == false }
  cols_with_more.any?
end

#clear_queueObject



100
101
102
103
# File 'lib/mumbletune/hallon_player.rb', line 100

def clear_queue
  @queue.clear
  self.stop
end

#connectObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mumbletune/hallon_player.rb', line 24

def connect
  conf = Mumbletune.config

  @session = Hallon::Session.initialize(IO.read(conf["spotify"]["appkey"]))
  @session.login!(conf["spotify"]["username"], conf["spotify"]["password"])

  @player = Hallon::Player.new(Hallon::QueueOutput) do
    # Set driver options
    @driver.queue = Mumbletune.player.audio_queue
    @driver.verbose = true if Mumbletune.verbose

    # Define callbacks
    on(:end_of_track) { Mumbletune.player.next }
    on(:streaming_error) { |s, e| raise "Spotify connection error: #{e}" }
    on(:play_token_lost) { Mumbletune.player.play_token_lost }
  end

  @ready = true

  start_event_loop
end

#disconnectObject



46
47
48
49
50
# File 'lib/mumbletune/hallon_player.rb', line 46

def disconnect
  @logging_out = true
  @event_loop_thread.kill
  @session.logout!
end

#empty?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/mumbletune/hallon_player.rb', line 71

def empty?
  !any?
end

#nextObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/mumbletune/hallon_player.rb', line 122

def next
  # move the collection to play_history if it has played all its tracks
  @play_history << @queue.shift if @queue.first && @queue.first.empty?

  return stop unless any?

  track = @queue.first.next

  return stop unless track

  # play that shit!
  @current_track = track
  @player.play track

  puts "\u266B  #{track.name} - #{track.artist.name}" unless Mumbletune.verbose
end

#pauseObject



114
115
116
117
118
119
120
# File 'lib/mumbletune/hallon_player.rb', line 114

def pause
  if playing?
    @player.pause
  elsif paused?
    @player.play
  end
end

#paused?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/mumbletune/hallon_player.rb', line 57

def paused?
  @player.status == :paused
end

#playObject

Playback Commands



106
107
108
109
110
111
112
# File 'lib/mumbletune/hallon_player.rb', line 106

def play
  if stopped?
    self.next
  elsif paused?
    @player.play
  end
end

#play_token_lostObject

Callback Handling



144
145
146
147
148
# File 'lib/mumbletune/hallon_player.rb', line 144

def play_token_lost
  Mumbletune.mumble.broadcast %w{Mumbletune was just paused because this
    Spotify account is being used elsewhere. Type <code>unpause
    </code> to regain control and keep playing.} * " "
end

#playing?Boolean

Status methods

Returns:

  • (Boolean)


53
54
55
# File 'lib/mumbletune/hallon_player.rb', line 53

def playing?
  @player.status == :playing
end

#stopObject



139
140
141
# File 'lib/mumbletune/hallon_player.rb', line 139

def stop
  @player.stop
end

#stopped?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/mumbletune/hallon_player.rb', line 61

def stopped?
  @player.status == :stopped
end

#undoObject



93
94
95
96
97
98
# File 'lib/mumbletune/hallon_player.rb', line 93

def undo
  removed = @add_history.pop
  @queue.delete_if { |col| col == removed }
  self.next if removed.current_track
  removed
end