class Mumbletune::HallonPlayer

Attributes

add_history[RW]
audio_queue[RW]
current_track[RW]
play_history[RW]
queue[RW]
ready[RW]

Public Class Methods

new() click to toggle source
# 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

Public Instance Methods

add_collection(col, now=false) click to toggle source

Queue Control

# 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?() click to toggle source
# 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_queue() click to toggle source
# File lib/mumbletune/hallon_player.rb, line 100
def clear_queue
        @queue.clear
        self.stop
end
connect() click to toggle source
# 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
disconnect() click to toggle source
# File lib/mumbletune/hallon_player.rb, line 46
def disconnect
        @logging_out = true
        @event_loop_thread.kill
        @session.logout!
end
empty?() click to toggle source
# File lib/mumbletune/hallon_player.rb, line 71
def empty?
        !any?
end
next() click to toggle source
# 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
pause() click to toggle source
# File lib/mumbletune/hallon_player.rb, line 114
def pause
        if playing?
                @player.pause
        elsif paused?
                @player.play
        end
end
paused?() click to toggle source
# File lib/mumbletune/hallon_player.rb, line 57
def paused?
        @player.status == :paused
end
play() click to toggle source

Playback Commands

# File lib/mumbletune/hallon_player.rb, line 106
def play
        if stopped?
                self.next
        elsif paused?
                @player.play
        end
end
play_token_lost() click to toggle source

Callback Handling

# 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?() click to toggle source

Status methods

# File lib/mumbletune/hallon_player.rb, line 53
def playing?
        @player.status == :playing
end
stop() click to toggle source
# File lib/mumbletune/hallon_player.rb, line 139
def stop
        @player.stop
end
stopped?() click to toggle source
# File lib/mumbletune/hallon_player.rb, line 61
def stopped?
        @player.status == :stopped
end
undo() click to toggle source
# 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

Private Instance Methods

start_event_loop() click to toggle source
# File lib/mumbletune/hallon_player.rb, line 152
def start_event_loop
        @event_loop_thread = Thread.new do
                loop do
                        @session.process_events unless @session.disconnected?
                        sleep 1
                end
        end
end