class AudioInfo::Album

Constants

IMAGE_EXTENSIONS
MULTICD_REGEXP

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

Attributes

basename[R]
discnum[R]
files[R]
infos[R]
multicd[R]
path[R]

Public Class Methods

basename(name) click to toggle source

strip the “multicd” string from the given name

# File lib/audioinfo/album.rb, line 30
def self.basename(name)
  name.sub(MULTICD_REGEXP, '')
end
discnum(name) click to toggle source

return the number of the disc in the box or 0

# File lib/audioinfo/album.rb, line 35
def self.discnum(name)
  if name =~ MULTICD_REGEXP
    Regexp.last_match(3).to_i
  else
    0
  end
end
images(path) click to toggle source

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

# File lib/audioinfo/album.rb, line 16
def self.images(path)
  path = path.dup.force_encoding('binary')
  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
new(path, fast_lookup = false) click to toggle source

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

# File lib/audioinfo/album.rb, line 45
def initialize(path, fast_lookup = false)
  @path = path
  @multicd = false
  @basename = @path
  exts = AudioInfo::SUPPORTED_EXTENSIONS.collect do |ext|
    ext.gsub(/[a-z]/) { |c| "[#{c.downcase}#{c.upcase}]" }
  end.join(',')

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

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

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

  @files = file_names.collect do |f|
    AudioInfo.new(f)
  end

  @infos = {}
  @infos['album'] = @files.collect(&:album).uniq
  @infos['album'] = @infos['album'].first if @infos['album'].size == 1
  artists = @files.collect(&:artist).uniq
  @infos['artist'] = artists.size > 1 ? 'various' : artists.first
  @discnum = self.class.discnum(@infos['album'])

  unless @discnum.zero?
    @multicd = true
    @basename = self.class.basename(@infos['album'])
  end
end

Public Instance Methods

empty?() click to toggle source

is the album empty?

# File lib/audioinfo/album.rb, line 79
def empty?
  @files.empty?
end
images() click to toggle source

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

# File lib/audioinfo/album.rb, line 95
def images
  self.class.images(@path)
end
inspect() click to toggle source
# File lib/audioinfo/album.rb, line 141
def inspect
  @infos.inspect
end
mb_tagged?() click to toggle source

are all the files of the album MusicBrainz tagged ?

# File lib/audioinfo/album.rb, line 84
def mb_tagged?
  return false if @files.empty?

  mb = true
  @files.each do |f|
    mb &&= f.mb_tagged?
  end
  mb
end
mbid() click to toggle source

mbid (MusicBrainz ID) of the album

# File lib/audioinfo/album.rb, line 111
def mbid
  return nil unless mb_tagged?

  @files.collect { |f| f.musicbrainz_infos['albumid'] }.uniq.first
end
title() click to toggle source

title of the album

# File lib/audioinfo/album.rb, line 100
def title
  # count the occurences of the title and take the one who has most
  hash_counted = files.collect(&:album).each_with_object(Hash.new(0)) { |album, hash| hash[album] += 1; }
  if hash_counted.empty?
    nil
  else
    hash_counted.max_by { |_k, v| v }[0]
  end
end
to_s() click to toggle source

pretty print

# File lib/audioinfo/album.rb, line 123
def to_s
  out = StringIO.new
  out.puts(@path)
  out.print "'#{title}'"

  out.print " by '#{@files.first.artist}' " unless va?

  out.puts

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

  out.string
end
va?() click to toggle source

is the album multi-artist?

# File lib/audioinfo/album.rb, line 118
def va?
  @files.collect(&:artist).uniq.size > 1
end