Sha256: feee3bf37e29734fb398a4359567b71bcccbf0f618878e0bf444b95e039537b8

Contents?: true

Size: 1.25 KB

Versions: 22

Compression:

Stored size: 1.25 KB

Contents

module FormatParser::MP3Parser::ID3V1
  PACKSPEC = [
    :tag, :a3,
    :song_name, :a30,
    :artist, :a30,
    :album, :a30,
    :year, :N1,
    :comment, :a30,
    :genre, :C,
  ]
  packspec_keys = PACKSPEC.select.with_index { |_, i| i.even? }
  TAG_SIZE_BYTES = 128

  class TagInformation < Struct.new(*packspec_keys)
  end

  def attempt_id3_v1_extraction(io)
    return if io.size < TAG_SIZE_BYTES # Won't fit the ID3v1 regardless

    io.seek(io.size - 128)
    trailer_bytes = io.read(128)

    return unless trailer_bytes && trailer_bytes.byteslice(0, 3) == 'TAG'

    id3_v1 = parse_id3_v1(trailer_bytes)

    # If all of the resulting strings are empty this ID3v1 tag is invalid and
    # we should ignore it.
    strings_from_id3v1 = id3_v1.values.select { |e| e.is_a?(String) && e != 'TAG' }
    return if strings_from_id3v1.all?(&:empty?)

    id3_v1
  end

  def parse_id3_v1(byte_str)
    _keys, values = PACKSPEC.partition.with_index { |_, i| i.even? }
    unpacked_values = byte_str.unpack(values.join)
    unpacked_values.map! { |e| e.is_a?(String) ? trim_id3v1_string(e) : e }
    TagInformation.new(unpacked_values)
  end

  # Remove trailing whitespace and trailing nullbytes
  def trim_id3v1_string(str)
    str.tr("\x00".b, '').strip
  end

  extend self
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
format_parser-0.12.4 lib/parsers/mp3_parser/id3_v1.rb
format_parser-0.12.2 lib/parsers/mp3_parser/id3_v1.rb
format_parser-0.12.1 lib/parsers/mp3_parser/id3_v1.rb
format_parser-0.12.0 lib/parsers/mp3_parser/id3_v1.rb
format_parser-0.11.0 lib/parsers/mp3_parser/id3_v1.rb
format_parser-0.10.0 lib/parsers/mp3_parser/id3_v1.rb
format_parser-0.9.4 lib/parsers/mp3_parser/id3_v1.rb
format_parser-0.9.3 lib/parsers/mp3_parser/id3_v1.rb
format_parser-0.9.0 lib/parsers/mp3_parser/id3_v1.rb
format_parser-0.8.0 lib/parsers/mp3_parser/id3_v1.rb
format_parser-0.7.0 lib/parsers/mp3_parser/id3_v1.rb
format_parser-0.6.0 lib/parsers/mp3_parser/id3_v1.rb
format_parser-0.5.2 lib/parsers/mp3_parser/id3_v1.rb
format_parser-0.5.1 lib/parsers/mp3_parser/id3_v1.rb
format_parser-0.5.0 lib/parsers/mp3_parser/id3_v1.rb
format_parser-0.4.0 lib/parsers/mp3_parser/id3_v1.rb
format_parser-0.3.5 lib/parsers/mp3_parser/id3_v1.rb
format_parser-0.3.4 lib/parsers/mp3_parser/id3_v1.rb
format_parser-0.3.3 lib/parsers/mp3_parser/id3_v1.rb
format_parser-0.3.2 lib/parsers/mp3_parser/id3_v1.rb