Class: ID3::Tag1

Inherits:
GenericTag show all
Defined in:
lib/id3/tag1.rb

Overview

Class Tag1 ID3 Version 1.x Tag

parses ID3v1 tags from a binary array
dumps  ID3v1 tags into a binary array
allows to modify tag's contents

Instance Attribute Summary

Attributes inherited from GenericTag

#raw, #version

Attributes inherited from RestrictedOrderedHash

#locked

Instance Method Summary collapse

Methods inherited from GenericTag

#[]=

Methods inherited from RestrictedOrderedHash

#[]=, #delete, #lock, #old_delete, #old_store

Constructor Details

#initializeTag1

Returns a new instance of Tag1.


12
13
14
15
# File 'lib/id3/tag1.rb', line 12

def initialize
  super
  @version = '1.1'
end

Instance Method Details

#dumpObject


dump version 1.1 ID3 Tag into a binary array

although we provide this method, it's stongly discouraged to use it, because ID3 version 1.x tags are inferior to version 2.x tags, as entries are often truncated and hence ID3 v1 tags are often useless..


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/id3/tag1.rb', line 105

def dump
  zeroes = ZEROBYTE * 32
  raw = ZEROBYTE * ID3::ID3v1tagSize
  raw[0..2] = 'TAG'

  self.each{ |key,value|

    range = ID3::Symbol2framename['1.1'][key]

    if range.class == Range 
      length = range.last - range.first + 1
      paddedstring = value + zeroes
      raw[range] = paddedstring[0..length-1]
    elsif range.class == Fixnum
      raw[range] = value.to_i.chr      # supposedly assigning a binary integer value to the location in the string
    else
      # this can't happen the way we defined the hash..
      next
    end
  }

  return raw
end

#parse!(raw) ⇒ Object


this routine modifies self, e.g. the Tag1 object

tag.parse!(raw) returns boolean value, showing if parsing was successful


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
# File 'lib/id3/tag1.rb', line 72

def parse!(raw)

  return false    if raw.size != ID3::ID3v1tagSize

  if (raw[ID3v1versionbyte] == 0) 
    @version = "1.0"
  else
    @version = "1.1"
  end

  self.clear    # remove all entries from Hash, we don't want left-overs..

  ID3::SUPPORTED_SYMBOLS[@version].each{ |key,val|
    if val.class == Range      #               self[key] = raw[val].squeeze(" \000").chomp(" ").chomp("\000")

      self[key] = raw[val].strip
    elsif val.class == Fixnum
      self[key] = raw[val].to_s
    else 
      # this can't happen the way we defined the hash..
      #              printf "unknown key/val : #{key} / #{val}  ; val-type: %s\n", val.class
    end       
  }
  @raw = raw
  return true
end

#read(filename) ⇒ Object


read reads a version 1.x ID3tag


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/id3/tag1.rb', line 21

def read(filename)
  f = File.open(filename, 'r')
  f.seek(-ID3::ID3v1tagSize, IO::SEEK_END)
  hastag = (f.read(3) == 'TAG')
  if hastag
    f.seek(-ID3::ID3v1tagSize, IO::SEEK_END)
    @raw = f.read(ID3::ID3v1tagSize)

    #           self.parse!(raw)    # we should use "parse!" instead of duplicating code!

    if (raw.getbyte(ID3v1versionbyte) == 0) 
      @version = "1.0"
    else
      @version = "1.1"
    end
  else
    @raw = @version = nil
  end
  f.close  #
  # now parse all the fields


  ID3::SUPPORTED_SYMBOLS[@version].each{ |key,val|
    if val.class == Range      #               self[key] = @raw[val].squeeze(" \000").chomp(" ").chomp("\000")

      self[key] = @raw[val].strip
    elsif val.class == Fixnum
      self[key] = @raw.getbyte(val).to_s
    else 
      # this can't happen the way we defined the hash..
      #              printf "unknown key/val : #{key} / #{val}  ; val-type: %s\n", val.type
    end
  }
  hastag
end