Class: AudioInfo::Album

Inherits:
Object
  • Object
show all
Defined in:
lib/audioinfo/album.rb

Constant Summary collapse

IMAGE_EXTENSIONS =
%w{jpg jpeg gif png}
MULTICD_REGEXP =

a regexp to match the “multicd” suffix of a “multicd” string example: “toto (disc 1)” will match ‘ (disc 1)’

/\s*(\(|\[)?\s*(disc|cd):?-?\s*(\d+).*(\)|\])?\s*$/i

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, fast_lookup = false) ⇒ Album

open the Album with path. fast_lookup will only check first and last file of the directory



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
# File 'lib/audioinfo/album.rb', line 42

def initialize(path, fast_lookup = false)
  @path = path
  @multicd = false
  @basename = @path
  exts = AudioInfo::SUPPORTED_EXTENSIONS.join(",")

  # need to escape the glob path
  glob_escaped_path = @path.gsub(/([{}?*\[\]])/) { |s| '\\' << s }

  file_names = Dir.glob( File.join(glob_escaped_path, "*.{#{exts}}") , File::FNM_CASEFOLD).sort

  if fast_lookup
    file_names = [file_names.first, file_names.last]
  end

  @files_on_error = []

  @files = file_names.collect do |f|
    begin
      AudioInfo.new(f) 
    rescue AudioInfoError
      @files_on_error << f
	nil
    end
  end.compact

  if @files_on_error.empty?
    @files_on_error = nil
  end
  
  @infos = {}
  @infos["album"] = @files.collect { |i| i.album }.uniq
  @infos["album"] = @infos["album"].first if @infos["album"].size == 1
  artists = @files.collect { |i| i.artist }.uniq
  @infos["artist"] = artists.size > 1 ? "various" : artists.first
  @discnum = self.class.discnum(@infos["album"])

  if not @discnum.zero?
    @multicd = true
    @basename = self.class.basename(@infos["album"])
  end
end

Instance Attribute Details

#basenameObject (readonly)

Returns the value of attribute basename.



11
12
13
# File 'lib/audioinfo/album.rb', line 11

def basename
  @basename
end

#discnumObject (readonly)

Returns the value of attribute discnum.



11
12
13
# File 'lib/audioinfo/album.rb', line 11

def discnum
  @discnum
end

#filesObject (readonly)

Returns the value of attribute files.



11
12
13
# File 'lib/audioinfo/album.rb', line 11

def files
  @files
end

#files_on_errorObject (readonly)

Returns the value of attribute files_on_error.



11
12
13
# File 'lib/audioinfo/album.rb', line 11

def files_on_error
  @files_on_error
end

#infosObject (readonly)

Returns the value of attribute infos.



11
12
13
# File 'lib/audioinfo/album.rb', line 11

def infos
  @infos
end

#multicdObject (readonly)

Returns the value of attribute multicd.



11
12
13
# File 'lib/audioinfo/album.rb', line 11

def multicd
  @multicd
end

#pathObject (readonly)

Returns the value of attribute path.



11
12
13
# File 'lib/audioinfo/album.rb', line 11

def path
  @path
end

Class Method Details

.basename(name) ⇒ Object

strip the “multicd” string from the given name



27
28
29
# File 'lib/audioinfo/album.rb', line 27

def self.basename(name)
  name.sub(MULTICD_REGEXP, '')
end

.discnum(name) ⇒ Object

return the number of the disc in the box or 0



32
33
34
35
36
37
38
# File 'lib/audioinfo/album.rb', line 32

def self.discnum(name)
  if name =~ MULTICD_REGEXP
    $3.to_i
  else
    0
  end
end

.images(path) ⇒ Object

return the list of images in the album directory, with “folder.*” in first



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/audioinfo/album.rb', line 14

def self.images(path)
  arr = Dir.glob( File.join(path, "*.{#{IMAGE_EXTENSIONS.join(",")}}"), File::FNM_CASEFOLD).collect do |f| 
    File.expand_path(f)
  end
  # move "folder.*" image on top of the array
  if folder = arr.detect { |f| f =~ /folder\.[^.]+$/ }
    arr.delete(folder)
    arr.unshift(folder)
  end
  arr
end

Instance Method Details

#empty?Boolean

is the album empty?

Returns:

  • (Boolean)


86
87
88
# File 'lib/audioinfo/album.rb', line 86

def empty?
  @files.empty?
end

#imagesObject

return an array of images with “folder.*” in first



101
102
103
# File 'lib/audioinfo/album.rb', line 101

def images
  self.class.images(@path)
end

#mb_tagged?Boolean

are all the files of the album MusicBrainz tagged ?

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
# File 'lib/audioinfo/album.rb', line 91

def mb_tagged?
  return false if @files.empty?
  mb = true
  @files.each do |f|
    mb &&= f.mb_tagged?
  end
  mb
end

#mbidObject

mbid (MusicBrainz ID) of the album



116
117
118
119
# File 'lib/audioinfo/album.rb', line 116

def mbid
  return nil unless mb_tagged?
  @files.collect { |f| f.musicbrainz_infos["albumid"] }.uniq.first
end

#titleObject

title of the album



106
107
108
109
110
111
112
113
# File 'lib/audioinfo/album.rb', line 106

def title
  albums = @files.collect { |f| f.album }.uniq
  #if albums.size > 1
  #  "#{albums.first} others candidates: '" + albums[1..-1].join("', '") + "'"
  #else
    albums.first
  #end
end

#to_sObject

pretty print



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/audioinfo/album.rb', line 127

def to_s
  out = StringIO.new
  out.puts(@path)
  out.print "'#{title}'"
  
  unless va?
    out.print " by '#{@files.first.artist}' "
  end

  out.puts
  
  @files.sort_by { |f| f.tracknum }.each do |f|
    out.printf("%02d %s %3d %s", f.tracknum, f.extension, f.bitrate, f.title)
    if va?
	out.print(" "+f.artist)
    end
    out.puts
  end

  out.string
end

#va?Boolean

is the album multi-artist?

Returns:

  • (Boolean)


122
123
124
# File 'lib/audioinfo/album.rb', line 122

def va?
  @files.collect { |f| f.artist }.uniq.size > 1
end