Class: MikePlayer::Song

Inherits:
Object
  • Object
show all
Defined in:
lib/mikeplayer/song.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Song

Returns a new instance of Song.



5
6
7
8
9
10
11
12
13
# File 'lib/mikeplayer/song.rb', line 5

def initialize(filename)
  @filename = filename

  Mp3Info.open(filename) do |mp3info|
    @artist = mp3info.tag.artist.to_s
    @title  = mp3info.tag.title.to_s
    @length = mp3info.length || 0
  end
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



3
4
5
# File 'lib/mikeplayer/song.rb', line 3

def filename
  @filename
end

Class Method Details

.as_duration_str(l, t = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mikeplayer/song.rb', line 42

def self.as_duration_str(l, t = nil)
  l_hr  = "%02d" % (l / 3600).floor
  l_min = "%02d" % ((l % 3600 )/ 60).floor
  l_sec = "%02d" % (l % 60)
  e_min = "%02d" % (t / 60).floor unless t.nil?
  e_sec = "%02d" % (t % 60) unless t.nil?

  result = "#{l_min}:#{l_sec}"
  result = "#{l_hr}:#{result}" if l >= 3600
  result = "#{e_min}:#{e_sec} [#{result}]" unless t.nil?

  return result
end

Instance Method Details

#infoObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mikeplayer/song.rb', line 15

def info
  artist = @artist
  title  = @title

  if (true == artist.empty?) && (true == title.empty?)
    return File.basename(@filename, '.mp3')
  elsif (true == artist.empty?)
    artist = "?????"
  elsif (true == title.empty?)
    title  = "?????"
  end

  return "#{artist} - #{title}"
end

#lengthObject



30
31
32
# File 'lib/mikeplayer/song.rb', line 30

def length
  return @length
end

#length_str(elapsed_time) ⇒ Object



34
35
36
# File 'lib/mikeplayer/song.rb', line 34

def length_str(elapsed_time)
  return Song.as_duration_str(self.length, elapsed_time)
end

#to_jsonObject



38
39
40
# File 'lib/mikeplayer/song.rb', line 38

def to_json
  return @filename.to_s
end