ID3 Tags (#136)

The three rules of Ruby Q.:

  1. Please do not post any solutions or spoiler discussion for this quiz
    until
    48 hours have passed from the time on this message.

  2. Support Ruby Q. by submitting ideas as often as you can:

http://www.rubyquiz.com/

  1. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem helps
everyone
on Ruby T. follow the discussion. Please reply to the original quiz
message,
if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

The MP3 file format, didn’t provide any means for including metadata
about the
song. ID3 tags were invented to solve this problem.

You can tell if an MP3 file includes ID3 tags by examining the last 128
bytes of
the file. If they begin with the characters TAG, you have found an ID3
tag.
The format of the tag is as follows:

TAG song album artist comment year genre

The spaces above are just for us humans. The actual tags are
fixed-width fields
with no spacing between them. Song, album, artist, and comment are 30
bytes
each. The year is four bytes and the genre just gets one, which is an
index
into a list of predefined genres I’ll include at the end of this quiz.

A minor change was later made to ID3 tags to allow them to include track
numbers, creating ID3v1.1. In that format, if the 29th byte of a
comment is
null and the 30th is not, the 30th byte is an integer representing the
track
number.

Later changes evolved ID3v2 which is a scary beast we won’t worry about.

This week’s Ruby Q. is to write an ID3 tag parser. Using a library is
cheating. Roll up your sleeves and parse it yourself. It’s not hard at
all.

If you don’t have MP3 files to test your solution on, you can find some
free
files at:

http://www.mfiles.co.uk/mp3-files.htm

Here’s the official genre list with some extensions added by Winamp:

Blues
Classic Rock
Country
Dance
Disco
Funk
Grunge
Hip-Hop
Jazz
Metal
New Age
Oldies
Other
Pop
R&B
Rap
Reggae
Rock
Techno
Industrial
Alternative
Ska
Death Metal
Pranks
Soundtrack
Euro-Techno
Ambient
Trip-Hop
Vocal
Jazz+Funk
Fusion
Trance
Classical
Instrumental
Acid
House
Game
Sound Clip
Gospel
Noise
AlternRock
Bass
Soul
Punk
Space
Meditative
Instrumental Pop
Instrumental Rock
Ethnic
Gothic
Darkwave
Techno-Industrial
Electronic
Pop-Folk
Eurodance
Dream
Southern Rock
Comedy
Cult
Gangsta
Top 40
Christian Rap
Pop/Funk
Jungle
Native American
Cabaret
New Wave
Psychadelic
Rave
Showtunes
Trailer
Lo-Fi
Tribal
Acid Punk
Acid Jazz
Polka
Retro
Musical
Rock & Roll
Hard Rock
Folk
Folk-Rock
National Folk
Swing
Fast Fusion
Bebob
Latin
Revival
Celtic
Bluegrass
Avantgarde
Gothic Rock
Progressive Rock
Psychedelic Rock
Symphonic Rock
Slow Rock
Big Band
Chorus
Easy Listening
Acoustic
Humour
Speech
Chanson
Opera
Chamber Music
Sonata
Symphony
Booty Bass
Primus
Porn Groove
Satire
Slow Jam
Club
Tango
Samba
Folklore
Ballad
Power Ballad
Rhythmic Soul
Freestyle
Duet
Punk Rock
Drum Solo
A capella
Euro-House
Dance Hall

On 8/24/07, Ruby Q. [email protected] wrote:

The three rules of Ruby Q.:

The spaces above are just for us humans. The actual tags are fixed-width fields
with no spacing between them. Song, album, artist, and comment are 30 bytes
each. The year is four bytes and the genre just gets one, which is an index
into a list of predefined genres I’ll include at the end of this quiz.
zero based, I guess?

Cheers
Robert

On Aug 24, 2007, at 7:47 AM, Robert D. wrote:

quiz.
zero based, I guess?

It is, yes.

James Edward G. II

I think that the fields order is wrong.
I found this:
TAG song artist album year comment genre

Cédric

“Ruby Q.” [email protected] wrote in message
news:[email protected]

TAG song album artist comment year genre

You’ve misplaced year and comment.
http://www.id3.org/ID3v1

–EK

On Aug 24, 2007, at 10:03 AM, Cédric Finance wrote:

I think that the fields order is wrong.
I found this:
TAG song artist album year comment genre

You are right. Sorry about that. I’ve fixed it on the Ruby Q. site.

James Edward G. II

James G. wrote:

The format of the tag is as follows:

I assume that the song album artist and comment fields are NUL padded?

The 4 bytes of Year are 4 character and not a 32bit number?

John M.

On Aug 24, 2007, at 10:29 AM, John M. wrote:

James G. wrote:

The format of the tag is as follows:

I assume that the song album artist and comment fields are NUL padded?

The 4 bytes of Year are 4 character and not a 32bit number?

Yes and yes. :slight_smile:

James Edward G. II

On Aug 24, 2007, at 10:15 AM, James Edward G. II wrote:

On Aug 24, 2007, at 10:03 AM, Cédric Finance wrote:

I think that the fields order is wrong.
I found this:
TAG song artist album year comment genre

You are right. Sorry about that. I’ve fixed it on the Ruby Q.
site.

You fixed one problem, but artist and album are still flipped.

(this didn’t come through the first time, trying without the S/MIME
signature)

On Aug 25, 2007, at 5:04 PM, Brad E. wrote:

You fixed one problem, but artist and album are still flipped.

Egad. I must have had a massive dyslexia attack when I wrote that quiz.

It should be fixed now.

James Edward G. II

Here’s my solution. Should be pretty straightforward.
id3_tags.rb takes a list of filenames as arguments:

$ ./id3_tags.rb 04_Prepare_Yourself.mp3 05_Moonloop.mp3
04_Prepare_Yourself.mp3:
song: Prepare Yourself
track: 4
artist: Porcupine Tree
comment: some comment
year: 1995
album: The Sky Moves Sideways
genre: Progressive Rock

05_Moonloop.mp3:
song: Moonloop
track: 5
artist: Porcupine Tree
comment: test comment
year: 1995
album: The Sky Moves Sideways
genre: Progressive Rock

class NoID3Error < StandardError
end

class ID3
Genres=" Blues
Classic Rock
Country
Dance
Disco
Funk
Grunge
Hip-Hop
Jazz
Metal
New Age
Oldies
Other
Pop
R&B
Rap
Reggae
Rock
Techno
Industrial
Alternative
Ska
Death Metal
Pranks
Soundtrack
Euro-Techno
Ambient
Trip-Hop
Vocal
Jazz+Funk
Fusion
Trance
Classical
Instrumental
Acid
House
Game
Sound Clip
Gospel
Noise
AlternRock
Bass
Soul
Punk
Space
Meditative
Instrumental Pop
Instrumental Rock
Ethnic
Gothic
Darkwave
Techno-Industrial
Electronic
Pop-Folk
Eurodance
Dream
Southern Rock
Comedy
Cult
Gangsta
Top 40
Christian Rap
Pop/Funk
Jungle
Native American
Cabaret
New Wave
Psychadelic
Rave
Showtunes
Trailer
Lo-Fi
Tribal
Acid Punk
Acid Jazz
Polka
Retro
Musical
Rock & Roll
Hard Rock
Folk
Folk-Rock
National Folk
Swing
Fast Fusion
Bebob
Latin
Revival
Celtic
Bluegrass
Avantgarde
Gothic Rock
Progressive Rock
Psychedelic Rock
Symphonic Rock
Slow Rock
Big Band
Chorus
Easy Listening
Acoustic
Humour
Speech
Chanson
Opera
Chamber Music
Sonata
Symphony
Booty Bass
Primus
Porn Groove
Satire
Slow Jam
Club
Tango
Samba
Folklore
Ballad
Power Ballad
Rhythmic Soul
Freestyle
Duet
Punk Rock
Drum Solo
A capella
Euro-House
Dance Hall".split("\n").map{|x| x.gsub(/^\s+/,’’)}

attr_accessor :title, :artist, :album, :year, :comment, :genre, :track
def genre_name
Genres[@genre]
end

def initialize(filename)
rawdata=open(filename) do |f|
f.seek(f.lstat.size-128)
f.read
end
tag,@title,@artist,@album,@year,@comment,@genre=rawdata.unpack
“A3A30A30A30A4A30c”
if rawdata[3+30+30+30+4+28]==0
@track=rawdata[3+30+30+30+4+29]
@track=nil if @track==0
end
if tag!=“TAG”
raise NoID3Error
end
end
end

Hi,

Here is my solution :

require “delegate”

class ID3Tags < DelegateClass(Struct)
MP3_TYPE=%w(Blues Classic Rock Country Dance Disco Funk Grunge Hip-
Hop Jazz Metal New Age Oldies Other Pop R&B Rap Reggae Rock Techno
Industrial Alternative Ska Death Metal Pranks Soundtrack Euro-Techno
Ambient Trip-Hop Vocal Jazz+Funk Fusion Trance Classical Instrumental
Acid House Game Sound Clip Gospel Noise AlternRock Bass Soul Punk
Space Meditative Instrumental Pop Instrumental Rock Ethnic Gothic
Darkwave Techno-Industrial Electronic Pop-Folk Eurodance Dream
Southern Rock Comedy Cult Gangsta Top 40 Christian Rap Pop/Funk Jungle
Native American Cabaret New Wave Psychadelic Rave Showtunes Trailer Lo-
Fi Tribal Acid Punk Acid Jazz Polka Retro Musical Rock & Roll Hard
Rock Folk Folk-Rock National Folk Swing Fast Fusion Bebob Latin
Revival Celtic Bluegrass Avantgarde Gothic Rock Progressive Rock
Psychedelic Rock Symphonic Rock Slow Rock Big Band Chorus Easy
Listening Acoustic Humour Speech Chanson Opera Chamber Music Sonata
Symphony Booty Bass Primus Porn Groove Satire Slow Jam Club Tango
Samba Folklore Ballad Power Ballad Rhythmic Soul Freestyle Duet Punk
Rock Drum Solo A capella Euro-House Dance Hall)

Tag=Struct.new(:song,:album,:artist,:year,:comment,:track,:genre)

def initialize(file)
raise “No ID3 Tag detected” unless File.size(file) > 128
File.open(file,“r”) do |f|
f.seek(-128, IO::SEEK_END)
tag = f.read.unpack(‘A3A30A30A30A4A30C1’)
raise “No ID3 Tag detected” unless tag[0] == ‘TAG’
if tag[5][-2] == 0 and tag[5][-1] != 0
tag[5]=tag[5].unpack(‘A28A1C1’).values_at(0,2)
else
tag[5]=[tag[5],nil]
end
super(@tag=Tag.new(*tag.flatten[1…-1]))
end
end

def to_s
  members.each do |name|

puts “#{name} : #{send(name)}”
end
end

def genre
  MP3_TYPE[@tag.genre]
end

end

Come

On Sunday 26 August 2007, Jesse M. wrote:

Here’s my solution. Should be pretty straightforward.

Here’s a very slightly improved version of id3_tags.rb (which still
requires
the other two files I submitted, unchanged). The only change is less
ugly
use of String#[], and no more Null constant.

One of the biggest problems in software development is feature creep.
In the case of this Quiz, specification creep was the culprit, with
the spec being changed two times in two days. No offense intended,
JEG2 :wink:

Luckily, we can use the mighty power of Ruby to make our application
impervious to such changes, and save a couple heredocs to boot.


#!/usr/bin/env ruby -rubygems

%w(hpricot open-uri).each(&method(:require))

fields, genres = (Hpricot(open(“http://www.rubyquiz.com/
quiz136.html”)) / “p.example”).map{|e| e.inner_html}
fields = fields.split
genres = genres.split “

values = IO.read(ARGV.first)[-128…-1].unpack(“A3 A30 A30 A30 A4 A30 A”)

unless values.first == ‘TAG’
puts “No ID3 tag found”
exit 1
end

fields.zip(values).each do |field, value|
case field # this feels dirty
when ‘TAG’: # nada
when ‘genre’: puts “#{field}: #{genres[value[0]]}”
when ‘comment’
puts “#{field}: #{value}”
if value[28].to_i.zero? && !value[29].to_i.zero? # ID3v1.1
puts “track: #{value[29]}”
end
else puts “#{field}: #{value}”
end
end

On Aug 26, 2007, at 11:55 AM, come wrote:

Ambient Trip-Hop Vocal Jazz+Funk Fusion Trance Classical Instrumental
Listening Acoustic Humour Speech Chanson Opera Chamber Music Sonata
Symphony Booty Bass Primus Porn Groove Satire Slow Jam Club Tango
Samba Folklore Ballad Power Ballad Rhythmic Soul Freestyle Duet Punk
Rock Drum Solo A capella Euro-House Dance Hall)

That’s not going to work like you think it will:

%w(New Age)
=> [“New”, “Age”]

On Aug 26, 2007, at 8:45 AM, Jesse M. wrote:

Here’s my solution.

Here’s my own:

#!/usr/bin/env ruby -w

GENRES = %w[ Blues Classic\ Rock Country Dance Disco Funk Grunge Hip-
Hop Jazz
Metal New\ Age Oldies Other Pop R&B Rap Reggae Rock Techno
Industrial Alternative Ska Death\ Metal Pranks Soundtrack
Euro-Techno Ambient Trip-Hop Vocal Jazz+Funk Fusion Trance
Classical Instrumental Acid House Game Sound\ Clip
Gospel Noise
AlternRock Bass Soul Punk Space Meditative Instrumental
\ Pop
Instrumental\ Rock Ethnic Gothic Darkwave Techno-
Industrial
Electronic Pop-Folk Eurodance Dream Southern\ Rock
Comedy Cult
Gangsta Top\ 40 Christian\ Rap Pop/Funk Jungle Native
American
Cabaret New\ Wave Psychadelic Rave Showtunes Trailer Lo-
Fi Tribal
Acid\ Punk Acid\ Jazz Polka Retro Musical Rock\ &\ Roll
Hard\ Rock
Folk Folk-Rock National\ Folk Swing Fast\ Fusion Bebob
Latin
Revival Celtic Bluegrass Avantgarde Gothic\ Rock
Progressive\ Rock
Psychedelic\ Rock Symphonic\ Rock Slow\ Rock Big\ Band
Chorus
Easy\ Listening Acoustic Humour Speech Chanson Opera
Chamber\ Music
Sonata Symphony Booty\ Bass Primus Porn\ Groove Satire
Slow\ Jam
Club Tango Samba Folklore Ballad Power\ Ballad Rhythmic
\ Soul
Freestyle Duet Punk\ Rock Drum\ Solo A\ capella Euro-House
Dance\ Hall ]

abort “Usage: #{File.basename($PROGRAM_NAME)} MP3_FILE” unless
ARGV.size == 1

tag, song, artist, album, year, comment, genre =
ARGF.read[-128…-1].unpack(“A3A30A30A30A4A30C”)
if comment.size == 30 and comment[28] == ?\0
track = comment[29]
comment = comment[0…27].strip
else
track = nil
end

abort “ID3v1 tag not found.” unless tag == “TAG”

puts “Song: #{song}”
puts “Artist: #{artist}”
puts “Album: #{album}”
puts “Comment: #{comment}” unless comment.empty?
puts “Track: #{track}” unless track.nil?
puts “Year: #{year}”
puts “Genre: #{GENRES[genre] || ‘Unknown’}”

END

James Edward G. II

On Aug 26, 2007, at 12:32 PM, Brad E. wrote:

One of the biggest problems in software development is feature
creep. In the case of this Quiz, specification creep was the
culprit, with the spec being changed two times in two days. No
offense intended, JEG2 :wink:

I just did that to inspire you to such a clever solution. :wink:

James Edward G. II

Yes, you are right, I answered a little bit to fast :wink:

Hello,

My solution is straightforward. It seeks backwards from the end of file
to
read the ID3 tag, and then uses a regular expression to parse the tag. I
then manually extract the track number if found:

genres = [“Blues”,
… (snip) …
“Dance Hall”]

filename = ARGV[0]

if File.exists?(filename)
f = File.new(filename, “rb”)

Read ID3 tag from file

f.seek(-128, IO::SEEK_END)
data = f.read
f.close

Parse the ID3 tag, order is [TAG song artist album year comment

genre]
match_data =
/(TAG)(.{30})(.{30})(.{30})(.{4})(.{30})(.{1})/.match(data)

if match_data != nil

# If 29th byte of comment is 0, parse the field to obtain ID3 v1.1 

track
number
if match_data[6][28] == 0
comment = match_data[6].slice(0, 28)
track_num = match_data[6][29].to_i.to_s
else
comment = match_data[6]
track_num = “”
end

puts "   Song: #{match_data[2].strip}"
puts " Artist: #{match_data[3].strip}"
puts "  Album: #{match_data[4].strip}"
puts "   Year: #{match_data[5]}"
puts "Comment: #{comment.strip}"
puts "  Track: #{track_num}"
puts "  Genre: #{genres[match_data[7].to_i]}"

end
end

Here is a pastie of the complete program - Parked at Loopia
Thanks,

Justin