Class: ID3::AudioFile

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

Overview

Class AudioFile may call this ID3File

reads and parses audio files for tags
writes audio files and attaches dumped tags to it..
revert feature would be nice to have..

If we query and AudioFile object, we query what's currently associated with it
e.g. we're not querying the file itself, but the perhaps modified tags
To query the file itself, use the module functions

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ AudioFile

e.g.: ID3::AudioFile.new(‘mp3/a.mp3’)



549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'lib/id3.rb', line 549

def initialize(filename)
    @filename     = filename      # similar to path method from class File, which is a mis-nomer!
    @pwd          = ENV["PWD"]
    @dirname      = File.dirname( "#{@pwd}/#{@filename}" )   # just sugar
    @basename     = File.basename( "#{@pwd}/#{@filename}" )  # just sugar
    
    @tagID3v1     = nil
    @tagID3v2     = nil
    
    audioStartX   = 0
    audioEndX     = File.size(filename)

    if ID3.hasID3v1tag?(@filename)
        @tagID3v1 = Tag1.new
        @tagID3v1.read(@filename)

        audioEndX -= ID3::ID3v1tagSize
    end
    if ID3.hasID3v2tag?(@filename) 
        @tagID3v2 = Tag2.new
        @tagID3v2.read(@filename)

        audioStartX = @tagID3v2.raw.size
    end
    
    # audioStartX audioEndX indices into the file need to be set
    @audioStartX = audioStartX 
    @audioEndX   = audioEndX
    
    # user may compute the MD5sum of the audio content later..
    # but we're only doing this if the user requests it..

    @audioMD5sum = nil
end

Instance Attribute Details

#audioEndXObject (readonly)

begin and end indices of audio data in file



533
534
535
# File 'lib/id3.rb', line 533

def audioEndX
  @audioEndX
end

#audioMD5sumObject (readonly)


audioMD5sum

if the user tries to access @audioMD5sum, it will be computed for him, 
unless it was previously computed. We try to calculate that only once 
and on demand, because it's a bit expensive to compute..


534
535
536
# File 'lib/id3.rb', line 534

def audioMD5sum
  @audioMD5sum
end

#audioStartXObject (readonly)

begin and end indices of audio data in file



533
534
535
# File 'lib/id3.rb', line 533

def audioStartX
  @audioStartX
end

#basenameObject (readonly)

absolute dirname and basename of the file (computed)



537
538
539
# File 'lib/id3.rb', line 537

def basename
  @basename
end

#dirnameObject (readonly)

absolute dirname and basename of the file (computed)



537
538
539
# File 'lib/id3.rb', line 537

def dirname
  @dirname
end

#filenameObject (readonly)

PWD and relative path/name how file was first referenced



536
537
538
# File 'lib/id3.rb', line 536

def filename
  @filename
end

#hasID3tagObject (readonly)

either false, or a string with all version numbers found



540
541
542
# File 'lib/id3.rb', line 540

def hasID3tag
  @hasID3tag
end

#pwdObject (readonly)

PWD and relative path/name how file was first referenced



536
537
538
# File 'lib/id3.rb', line 536

def pwd
  @pwd
end

#tagID3v1Object

Returns the value of attribute tagID3v1.



539
540
541
# File 'lib/id3.rb', line 539

def tagID3v1
  @tagID3v1
end

#tagID3v2Object

Returns the value of attribute tagID3v2.



539
540
541
# File 'lib/id3.rb', line 539

def tagID3v2
  @tagID3v2
end

Instance Method Details

#verifyMD5sumObject


verifyMD5sum

compare the audioMD5sum against a previously stored md5sum file
and returns boolean value of comparison

If no md5sum file existed, we create one and return true.

computes the @audioMD5sum, if it wasn't previously computed..


628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
# File 'lib/id3.rb', line 628

def verifyMD5sum

   oldMD5sum = ''
   
   self.audioMD5sum if ! @audioMD5sum  # compute MD5sum if it's not computed yet

   base = @basename.sub( /(.)\.[^.]+$/ , '\1')   # remove suffix from audio-file
   base += '.md5'                                # add new suffix .md5
   md5name = File.join(@dirname,base)
   
   # if a MD5-file doesn't exist, we should create one and return TRUE ...
   if File.exists?(md5name)
      File.open( md5name ,"r") { |f| 
         oldname,oldMD5sum = f.readline.split  # read old MD5-sum
      }
   else
      oldMD5sum = self.writeMD5sum        # create MD5-file and return true..
   end
   @audioMD5sum == oldMD5sum
   
end

#versionObject Also known as: versions




650
651
652
653
654
655
656
# File 'lib/id3.rb', line 650

def version
   a = Array.new
   a.push(@tagID3v1.version) if @tagID3v1
   a.push(@tagID3v2.version) if @tagID3v2
   return nil   if a == []
   a.join(' ') 
end

#writeMD5sumObject


writeMD5sum

write the filename and MD5sum of the audio portion into an ascii file 
in the same location as the audio file, but with suffix .md5

computes the @audioMD5sum, if it wasn't previously computed..


608
609
610
611
612
613
614
615
616
617
618
# File 'lib/id3.rb', line 608

def writeMD5sum

   self.audioMD5sum if ! @audioMD5sum  # compute MD5sum if it's not computed yet
   
   base = @basename.sub( /(.)\.[^.]+$/ , '\1')
   base += '.md5'
   File.open( File.join(@dirname,base) ,"w") { |f| 
      f.printf("%s   %s\n",  File.join(@dirname,@basename), @audioMD5sum)
   }
   @audioMD5sum
end