Class: PiFi::Player

Inherits:
Object
  • Object
show all
Defined in:
lib/pifi/lib/player.rb

Defined Under Namespace

Classes: VolNaError

Constant Summary collapse

CROSSFADE =
5
DEFAULT_TITLE_LOCAL =
"Music"
DEFAULT_TITLE_STREAM =
"Streaming"
DEFAULT_HOST =
"localhost"
DEFAULT_PORT =
"6600"

Instance Method Summary collapse

Constructor Details

#initialize(streams, host = DEFAULT_HOST, port = DEFAULT_PORT, password = nil) ⇒ Player

Returns a new instance of Player.



17
18
19
20
21
22
23
24
# File 'lib/pifi/lib/player.rb', line 17

def initialize(streams, host=DEFAULT_HOST, port=DEFAULT_PORT, password=nil)
  @streams = streams
  @mpd = MPD.new(host, port, { callbacks: true })
  @mpd.connect
  @mpd.password(password) unless password.to_s.empty?

  define_callbacks
end

Instance Method Details

#change_vol(delta) ⇒ Object

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
54
# File 'lib/pifi/lib/player.rb', line 47

def change_vol(delta)
  raise ArgumentError, "Invalid argument" unless delta =~ /^[+-]\d{1,2}$/
  raise VolNaError if @vol.nil?

  @mpd.send_command("volume", delta);
  # This is more up-to-date than @vol
  @mpd.volume
end

#playObject



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

def play
  # Assigning is quicker than callback
  @playing = true if @mpd.play
end

#play_radios(*names) ⇒ Object

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pifi/lib/player.rb', line 56

def play_radios(*names)
  raise ArgumentError, "Argument required" if names.empty?

  urls = []
  names.each do |name|
    url = @streams[name]
    # nil: key not found; empty: it's a radio category
    raise ArgumentError, "Invalid radio name: #{name}" if url.nil? || url.empty?

    urls << url
  end

  play_urls(*urls)
end

#play_randomObject



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/pifi/lib/player.rb', line 81

def play_random
  if @playing && @local
    @mpd.next
  else
    @mpd.clear
    @mpd.add("/")
    @mpd.random=(true)
    @mpd.crossfade=(CROSSFADE)
    @mpd.play
  end
end

#play_urls(*urls) ⇒ Object

Raises:

  • (ArgumentError)


71
72
73
74
75
76
77
78
79
# File 'lib/pifi/lib/player.rb', line 71

def play_urls(*urls)
  raise ArgumentError, "Argument required" if urls.empty?
  raise ArgumentError, "Empty url given" if urls.any?(&:empty?)

  @mpd.clear
  urls.each { |url| @mpd.add(url) }
  @mpd.random=(false)
  @mpd.play
end

#stateObject



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

def state
  { playing: @playing,
    title: @title,
    artist: @artist,
    local: @local,
    elapsed: @elapsed,
    length: @length,
    vol: @vol,
    con_mpd: @con_mpd }
end

#stopObject



42
43
44
45
# File 'lib/pifi/lib/player.rb', line 42

def stop
  # Assigning is quicker than callback
  @playing = false if @mpd.stop
end