Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

use id3 tags if available

  • Loading branch information...
commit 2cdf8328ff8b6981e9aa2faa4b3cb97d80ecce57 1 parent c0b60d4
Aaron Patterson authored
Showing with 32 additions and 12 deletions.
  1. +32 −12 lib/earworm/fingerprint.rb
44 lib/earworm/fingerprint.rb
View
@@ -8,13 +8,30 @@ def initialize(thing)
def to_hash
return @hash if @hash
info = nil
+ @hash = {
+ 'art' => 'unknown',
+ 'ttl' => 'unknown',
+ 'alb' => 'unknown',
+ 'tnm' => 0,
+ 'gnr' => 'unknown',
+ 'yrr' => 0,
+ 'brt' => 0,
+ 'fmt' => 'wav',
+ }
if @thing.is_a?(IO)
info = wav_info_for(@thing)
else
tmpfile = case @thing
when /mp3$/
+ @hash['fmt'] = 'mp3'
+ begin
+ require 'id3lib'
+ @hash = @hash.merge(id3_info_for(@thing))
+ rescue LoadError
+ end
decode_mp3(@thing)
when /ogg$/
+ @hash['fmt'] = 'ogg'
decode_ogg(@thing)
else # Assume its a wav file
@thing
@@ -23,18 +40,9 @@ def to_hash
info = wav_info_for(f)
}
end
- @hash = {
- 'fpt' => info[:fpt],
- 'art' => 'unknown',
- 'ttl' => 'unknown',
- 'alb' => 'unknown',
- 'tnm' => 0,
- 'gnr' => 'unknown',
- 'yrr' => 0,
- 'brt' => 0,
- 'fmt' => 'wav',
- 'dur' => info[:milliseconds],
- }
+ @hash['fpt'] = info[:fpt]
+ @hash['dur'] = info[:milliseconds]
+ @hash
end
def to_s
@@ -62,6 +70,18 @@ def wav_info_for(io)
info
end
+ def id3_info_for(filename)
+ tag = ID3Lib::Tag.new(filename)
+ {
+ 'art' => tag.artist,
+ 'ttl' => tag.title,
+ 'alb' => tag.album,
+ 'tnm' => tag.track[/^(\d+)/],
+ 'gnr' => tag.genre,
+ 'yrr' => tag.year,
+ }
+ end
+
{
'ogg' => Audio::OGG::Decoder,
'mp3' => Audio::MPEG::Decoder,
Please sign in to comment.
Something went wrong with that request. Please try again.