class Muzak::Song
Represents a single song for muzak.
Attributes
@return [String, nil] the album of the song, identified from metadata
@return [String, nil] the artist of the song, identified from metadata
@return [String, nil] any comments in the song's metadata
@return [String, nil] the genre of the song, identified from metadata
@return [Integer] the length of the song, in seconds
@return [String] the fully-qualified path to the song
@return [String] the title of the song, identified from metadata @note if metadata is missing, the basename of the path is used instead
@return [Integer, 0] the track number of the song, identified from metadata
@return [Integer, 0] the year of the song, identified from metadata
Public Class Methods
@param path [String] the path of the song to load
# File lib/muzak/song.rb, line 40 def initialize(path) @path = path TagLib::FileRef.open(path) do |ref| @title = ref&.tag&.title @artist = ref&.tag&.artist @album = ref&.tag&.album @year = ref&.tag&.year @track = ref&.tag&.track @genre = ref&.tag&.genre @comment = ref&.tag&.comment @length = ref&.audio_properties&.length end # provide some sane fallbacks @title ||= File.basename(path, File.extname(path)) rescue "" @track ||= 0 # we'll need to sort by track number end
Public Instance Methods
@return [Boolean] whether or not the given object is equal to this one @note compares song paths, not metadata
# File lib/muzak/song.rb, line 83 def ==(other) other.is_a?(Song) && path == other.path end
@return [String] A best guess path for the song's cover art @example
song.best_guess_album_art # => "/path/to/song/directory/cover.jpg"
# File lib/muzak/song.rb, line 62 def best_guess_album_art album_dir = File.dirname(path) art = Dir.entries(album_dir).find { |ent| Utils.album_art?(ent) } File.join(album_dir, art) unless art.nil? end
@return [String] the “full” title of the song, including artist and album
if available.
@example
song.full_title # => "Song by Artist on Album"
# File lib/muzak/song.rb, line 73 def full_title full = title.dup full << " by #{artist}" if artist full << " on #{album}" if album full end
@return [Hash] a hash representation of the song, including pathname
# File lib/muzak/song.rb, line 88 def to_h { path: path, title: title, artist: artist, album: album, year: year, track: track, genre: genre, comment: comment, length: length, } end
@param options [Array] options passed to the internal `to_json` call @return [String] a JSON-serialized representation of the song @note uses {to_h} to build the representation
# File lib/muzak/song.rb, line 105 def to_json(*options) to_h.to_json(*options) end