Class: WahWah::ID3::V2Header

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

Overview

ID3v2/file identifier “ID3” ID3v2 version $03 00 ID3v2 flags %abc00000 ID3v2 size 4 * %0xxxxxxx

Constant Summary collapse

TAG_ID =
"ID3"
HEADER_SIZE =
10
HEADER_FORMAT =
"A3CxB8B*"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_io) ⇒ V2Header

Returns a new instance of V2Header.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/wahwah/id3/v2_header.rb', line 19

def initialize(file_io)
  header_content = file_io.read(HEADER_SIZE) || ""
  @id, @major_version, @flags, size_bits = header_content.unpack(HEADER_FORMAT) if header_content.size >= HEADER_SIZE

  return unless valid?

  # Tag size is the size excluding the header size,
  # so add header size back to get total size.
  @size = Helper.id3_size_caculate(size_bits) + HEADER_SIZE

  if has_extended_header?
    # Extended header structure:
    #
    # Extended header size   $xx xx xx xx
    # Extended Flags         $xx xx
    # Size of padding        $xx xx xx xx

    # Skip extended_header
    extended_header_size = Helper.id3_size_caculate(file_io.read(4).unpack1("B32"))
    file_io.seek(extended_header_size - 4, IO::SEEK_CUR)
  end
end

Instance Attribute Details

#major_versionObject (readonly)

Returns the value of attribute major_version.



17
18
19
# File 'lib/wahwah/id3/v2_header.rb', line 17

def major_version
  @major_version
end

#sizeObject (readonly)

Returns the value of attribute size.



17
18
19
# File 'lib/wahwah/id3/v2_header.rb', line 17

def size
  @size
end

Instance Method Details

#has_extended_header?Boolean

The second bit in flags byte indicates whether or not the header is followed by an extended header.

Returns:

  • (Boolean)


48
49
50
# File 'lib/wahwah/id3/v2_header.rb', line 48

def has_extended_header?
  @flags[1] == "1"
end

#valid?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/wahwah/id3/v2_header.rb', line 42

def valid?
  @id == TAG_ID
end