Class: Id3Taginator::Id3v1Tag
- Inherits:
-
Object
- Object
- Id3Taginator::Id3v1Tag
- 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
-
#album ⇒ Object
Returns the value of attribute album.
-
#artist ⇒ Object
Returns the value of attribute artist.
-
#comment ⇒ Object
Returns the value of attribute comment.
-
#genre ⇒ Object
Returns the value of attribute genre.
-
#title ⇒ Object
Returns the value of attribute title.
-
#track ⇒ Object
Returns the value of attribute track.
-
#year ⇒ Object
Returns the value of attribute year.
Class Method Summary collapse
-
.build_from_file(file) ⇒ Id3v1Tag
builds an id3tag from the given file stream.
-
.id3v1_tag?(file) ⇒ Boolean
checks if the given stream contains a id3v1 tag.
Instance Method Summary collapse
-
#initialize(title = nil, artist = nil, album = nil, year = nil, comment = nil, track = nil, genre = :INVALID) ⇒ Id3v1Tag
constructor
Constructor.
-
#to_bytes ⇒ String
dumps the id3v1 tag to a string representing the bytes.
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
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
#album ⇒ Object
Returns the value of attribute album.
10 11 12 |
# File 'lib/id3taginator/id3v1_tag.rb', line 10 def album @album end |
#artist ⇒ Object
Returns the value of attribute artist.
10 11 12 |
# File 'lib/id3taginator/id3v1_tag.rb', line 10 def artist @artist end |
#comment ⇒ Object
Returns the value of attribute comment.
10 11 12 |
# File 'lib/id3taginator/id3v1_tag.rb', line 10 def comment @comment end |
#genre ⇒ Object
Returns the value of attribute genre.
10 11 12 |
# File 'lib/id3taginator/id3v1_tag.rb', line 10 def genre @genre end |
#title ⇒ Object
Returns the value of attribute title.
10 11 12 |
# File 'lib/id3taginator/id3v1_tag.rb', line 10 def title @title end |
#track ⇒ Object
Returns the value of attribute track.
10 11 12 |
# File 'lib/id3taginator/id3v1_tag.rb', line 10 def track @track end |
#year ⇒ Object
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
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
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_bytes ⇒ String
dumps the id3v1 tag to a string representing the bytes
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 |