Class: Id3Taginator::Id3v1Tag

Inherits:
Object
  • Object
show all
Includes:
Extensions::Encodable
Defined in:
lib/id3taginator/id3v1_tag.rb

Constant Summary collapse

IDENTIFIER =
'TAG'
TAG_SIZE =
128

Constants included from Extensions::Encodable

Extensions::Encodable::ISO8859_1, Extensions::Encodable::UNICODE_ZERO, Extensions::Encodable::UTF_16, Extensions::Encodable::UTF_16BE, Extensions::Encodable::UTF_8, Extensions::Encodable::ZERO

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Extensions::Encodable

#decode, #decode_using_encoding_byte, #default_encoding_destination_byte, #encode, #encode_and_add_encoding_byte, #encode_string, #find_encoding, #find_encoding_byte, included, #merge, #pad_left, #pad_right, #read_stream_until, #remove_trailing_zeros, #zero_byte

Constructor Details

#initialize(title = nil, artist = nil, album = nil, year = nil, comment = nil, track = nil, genre = :INVALID) ⇒ Id3v1Tag

Constructor

Parameters:

  • title (String, nil) (defaults to: nil)

    the title

  • artist (String, nil) (defaults to: nil)

    the artist

  • album (String, nil) (defaults to: nil)

    the album title

  • year (Integer, String, nil) (defaults to: nil)

    the year

  • comment (String, nil) (defaults to: nil)

    the comment

  • track (Integer, nil) (defaults to: nil)

    the track number

  • genre (Symbol, nil) (defaults to: :INVALID)

    the genre name as a Symbol, e.g. :ROCK



62
63
64
65
66
67
68
69
70
# File 'lib/id3taginator/id3v1_tag.rb', line 62

def initialize(title = nil, artist = nil, album = nil, year = nil, comment = nil, track = nil, genre = :INVALID)
  @title = title
  @artist = artist
  @album = album
  @year = year
  @comment = comment
  @track = track
  @genre = genre
end

Instance Attribute Details

#albumObject

Returns the value of attribute album.



10
11
12
# File 'lib/id3taginator/id3v1_tag.rb', line 10

def album
  @album
end

#artistObject

Returns the value of attribute artist.



10
11
12
# File 'lib/id3taginator/id3v1_tag.rb', line 10

def artist
  @artist
end

#commentObject

Returns the value of attribute comment.



10
11
12
# File 'lib/id3taginator/id3v1_tag.rb', line 10

def comment
  @comment
end

#genreObject

Returns the value of attribute genre.



10
11
12
# File 'lib/id3taginator/id3v1_tag.rb', line 10

def genre
  @genre
end

#titleObject

Returns the value of attribute title.



10
11
12
# File 'lib/id3taginator/id3v1_tag.rb', line 10

def title
  @title
end

#trackObject

Returns the value of attribute track.



10
11
12
# File 'lib/id3taginator/id3v1_tag.rb', line 10

def track
  @track
end

#yearObject

Returns the value of attribute year.



10
11
12
# File 'lib/id3taginator/id3v1_tag.rb', line 10

def year
  @year
end

Class Method Details

.build_from_file(file) ⇒ Id3v1Tag

builds an id3tag from the given file stream

Parameters:

  • file (StringIO, IO, File)

    the file stream

Returns:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/id3taginator/id3v1_tag.rb', line 29

def self.build_from_file(file)
  tag = file.read(3)
  unless tag == IDENTIFIER
    raise Errors::Id3TagError, "#{tag} is no valid Id3v1 identifier. The Tag seems to be corrupted."
  end

  title = file.read(30)&.strip
  artist = file.read(30)&.strip
  album = file.read(30)&.strip
  year = file.read(4)
  comment = file.read(28)
  track_flag = file.readbyte
  track = nil
  if track_flag.zero?
    track = file.readbyte
  else
    comment += track_flag.chr + file.readbyte.chr
  end
  comment = comment&.strip
  genre = Genres.genre(file.readbyte)

  new(title, artist, album, year, comment, track, genre)
end

.id3v1_tag?(file) ⇒ Boolean

checks if the given stream contains a id3v1 tag

Parameters:

  • file (StringIO, IO, File)

    the file stream to check

Returns:

  • (Boolean)

    true if contains an id3v1 tag, else false



17
18
19
20
21
22
# File 'lib/id3taginator/id3v1_tag.rb', line 17

def self.id3v1_tag?(file)
  tag = file.read(3)
  file.seek(-3, IO::SEEK_CUR)

  tag == IDENTIFIER
end

Instance Method Details

#to_bytesString

dumps the id3v1 tag to a string representing the bytes

Returns:

  • (String)

    id3v1 byte dump as a string. tag.bytes returns the bytes of the dump



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/id3taginator/id3v1_tag.rb', line 138

def to_bytes
  res = 'TAG'
  res += pad_left(@title.nil? ? '' : @title, 30)
  res += pad_left(@artist.nil? ? '' : @artist, 30)
  res += pad_left(@album.nil? ? '' : @album, 30)
  res += pad_left(@year.nil? ? '' : @year&.to_s, 4)
  if @track.nil?
    res += pad_left(@comment.nil? ? '' : @comment, 30)
  else
    res += pad_left(@comment.nil? ? '' : @comment, 28)
    res += 0.chr
    res += @track.chr
  end
  res += Genres.genre_by_name(@genre).chr

  res
end