Class: AudioInfo

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

Defined Under Namespace

Classes: Album

Constant Summary collapse

MUSICBRAINZ_FIELDS =
{ 
  "trmid" 	=> "TRM Id",
  "artistid" 	=> "Artist Id",
  "albumid" 	=> "Album Id",
  "albumtype"	=> "Album Type", 
  "albumstatus" => "Album Status",
  "albumartistid" => "Album Artist Id",
  "sortname" => "Sort Name",
  "trackid" => "Track Id"
}
SUPPORTED_EXTENSIONS =
%w{mp3 ogg mpc wma mp4 aac m4a flac}
VERSION =
"0.1.7"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, encoding = 'utf-8') ⇒ AudioInfo

open the file with path fn and convert all tags from/to specified encoding

Raises:



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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/audioinfo.rb', line 53

def initialize(filename, encoding = 'utf-8')
  raise(AudioInfoError, "path is nil") if filename.nil?
  @path = filename
  ext = File.extname(@path)
  raise(AudioInfoError, "cannot find extension") if ext.empty?
  @extension = ext[1..-1].downcase
  @musicbrainz_infos = {}
  @encoding = encoding

  begin
    case @extension
	when 'mp3'
 @info = Mp3Info.new(filename, :encoding => @encoding)
 default_tag_fill
 #"TXXX"=>
 #["MusicBrainz TRM Id\000",
 #"MusicBrainz Artist Id\000aba64937-3334-4c65-90a1-4e6b9d4d7ada",
 #"MusicBrainz Album Id\000e1a223c1-cbc2-427f-a192-5d22fefd7c4c",
 #"MusicBrainz Album Type\000album",
 #"MusicBrainz Album Status\000official",
 #"MusicBrainz Album Artist Id\000"]
        
 if (arr = @info.tag2["TXXX"]).is_a?(Array)
   fields = MUSICBRAINZ_FIELDS.invert
   arr.each do |val|
     if val =~ /^MusicBrainz (.+)\000(.*)$/
short_name = fields[$1]
       @musicbrainz_infos[short_name] = $2
     end
   end
 end
        @bitrate = @info.bitrate
 i = @info.tag.tracknum
 @tracknum = (i.is_a?(Array) ? i.last : i).to_i
 @length = @info.length.to_i
 @date = @info.tag["date"]
 @vbr = @info.vbr
 @info.close

	when 'ogg'
 @info = OggInfo.new(filename, @encoding)
 default_fill_musicbrainz_fields
 default_tag_fill
        @bitrate = @info.bitrate/1000
        @tracknum = @info.tag.tracknumber.to_i
 @length = @info.length.to_i
 @date = @info.tag["date"]
 @vbr = true
 @info.close
 
	when 'mpc'
        fill_ape_tag(filename)

 mpc_info = MpcInfo.new(filename)
        @bitrate = mpc_info.infos['bitrate']/1000
 @length = mpc_info.infos['length']

      when 'ape'
 fill_ape_tag(filename)

      when 'wma'
 @info = WmaInfo.new(filename, :encoding => @encoding)
 @artist = @info.tags["Author"]
 @album = @info.tags["AlbumTitle"]
 @title = @info.tags["Title"]
 @tracknum = @info.tags["TrackNumber"].to_i
 @date = @info.tags["Year"]
 @bitrate = @info.info["bitrate"]
 @length = @info.info["playtime_seconds"]
 MUSICBRAINZ_FIELDS.each do |key, original_key|
   @musicbrainz_infos[key] = 
            @info.info["MusicBrainz/" + original_key.tr(" ", "")] ||
            @info.info["MusicBrainz/" + original_key]
 end
        
	when 'aac', 'mp4', 'm4a'
 @info = MP4Info.open(filename)
 @artist = @info.ART
 @album = @info.ALB
 @title = @info.NAM
 @tracknum = ( t = @info.TRKN ) ? t.first : 0
 @date = @info.DAY
 @bitrate = @info.BITRATE
 @length = @info.SECS
 mapping = MUSICBRAINZ_FIELDS.invert

 faad_info(filename).match(/^MusicBrainz (.+)$/) do
   name, value = $1.split(/: /, 2)
   key = mapping[name]
   @musicbrainz_infos[key] = value
 end
	
	when 'flac'
 @info = FlacInfo.new(filename)
        tags = convert_tags_encoding(@info.tags, "UTF-8")
 @artist = tags["ARTIST"] || tags["artist"]
 @album = tags["ALBUM"] || tags["album"]
 @title = tags["TITLE"] || tags["title"]
 @tracknum = (tags["TRACKNUMBER"]||tags["tracknumber"]).to_i
 @date = tags["DATE"]||tags["date"]
 @length = @info.streaminfo["total_samples"] / @info.streaminfo["samplerate"].to_f
 @bitrate = File.size(filename).to_f*8/@length/1024
        tags.each do |tagname, tagvalue|
          next unless tagname =~ /^musicbrainz_(.+)$/
          @musicbrainz_infos[$1] = tags[tagname]
        end
        @musicbrainz_infos["trmid"] = tags["musicip_puid"]
 #default_fill_musicbrainz_fields

	else
 raise(AudioInfoError, "unsupported extension '.#{@extension}'")
    end

    if @tracknum == 0
      @tracknum = nil
    end

    @musicbrainz_infos.delete_if { |k, v| v.nil? }
    @hash = { "artist" => @artist,
	"album"  => @album,
	"title"  => @title,
	"tracknum" => @tracknum,
	"date" => @date,
	"length" => @length,
	"bitrate" => @bitrate,
    }

  rescue Exception, Mp3InfoError, OggInfoError, ApeTagError => e
    raise AudioInfoError, e.to_s, e.backtrace
  end

  @needs_commit = false

end

Instance Attribute Details

#albumObject

Returns the value of attribute album.



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

def album
  @album
end

#artistObject

Returns the value of attribute artist.



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

def artist
  @artist
end

#bitrateObject (readonly)

Returns the value of attribute bitrate.



33
34
35
# File 'lib/audioinfo.rb', line 33

def bitrate
  @bitrate
end

#dateObject (readonly)

Returns the value of attribute date.



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

def date
  @date
end

#extensionObject (readonly)

Returns the value of attribute extension.



33
34
35
# File 'lib/audioinfo.rb', line 33

def extension
  @extension
end

#lengthObject (readonly)

Returns the value of attribute length.



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

def length
  @length
end

#musicbrainz_infosObject (readonly)

Returns the value of attribute musicbrainz_infos.



33
34
35
# File 'lib/audioinfo.rb', line 33

def musicbrainz_infos
  @musicbrainz_infos
end

#pathObject (readonly)

Returns the value of attribute path.



33
34
35
# File 'lib/audioinfo.rb', line 33

def path
  @path
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

#tracknumObject

Returns the value of attribute tracknum.



33
34
35
# File 'lib/audioinfo.rb', line 33

def tracknum
  @tracknum
end

#vbrObject (readonly)

Returns the value of attribute vbr.



33
34
35
# File 'lib/audioinfo.rb', line 33

def vbr
  @vbr
end

Class Method Details

.open(*args) ⇒ Object

“block version” of #new()



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/audioinfo.rb', line 37

def self.open(*args)
  audio_info = self.new(*args)
  ret = nil
  if block_given?
    begin
      ret = yield(audio_info)
    ensure
      audio_info.close
    end
  else
    ret = audio_info
  end
  ret
end

Instance Method Details

#[](key) ⇒ Object

hash-like access to tag



222
223
224
# File 'lib/audioinfo.rb', line 222

def [](key)
  @hash[key]
end

#closeObject

close the file and commits changes to disk



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/audioinfo.rb', line 232

def close
  if @needs_commit
    case @info
      when Mp3Info
 Mp3Info.open(@path, :encoding => @encoding) do |info|
   info.tag.artist = @artist
   info.tag.title = @title
   info.tag.album = @album
   info.tag.tracknum = @tracknum
 end
	when OggInfo
 OggInfo.open(@path, @encoding) do |ogg|
          { "artist" => @artist,
     "album"  => @album,
     "title"  => @title,
            "tracknumber" => @tracknum}.each do |k,v|
     ogg.tag[k] = v.to_s
   end
 end

	else
 raise(AudioInfoError, "implement me")
    end
    
  end
  @needs_commit
end

#mb_tagged?Boolean

check if the file is correctly tagged by MusicBrainz

Returns:

  • (Boolean)


277
278
279
# File 'lib/audioinfo.rb', line 277

def mb_tagged?
  ! @musicbrainz_infos.empty?
end

#to_hObject

convert tags to hash



227
228
229
# File 'lib/audioinfo.rb', line 227

def to_h
  @hash
end