Class: Id3Taginator::AudioFile
- Inherits:
-
Object
- Object
- Id3Taginator::AudioFile
- Includes:
- Extensions::Optionable
- Defined in:
- lib/id3taginator/audio_file.rb
Instance Attribute Summary collapse
-
#id3v1_tag ⇒ Object
readonly
Returns the value of attribute id3v1_tag.
-
#id3v2_tag ⇒ Object
readonly
Returns the value of attribute id3v2_tag.
Instance Method Summary collapse
-
#audio_file_to_bytes ⇒ String
creates a byte array represented as a String (str.bytes) of the modified file data.
-
#create_id3v1_tag ⇒ Id3v1Tag
creates the id3v1 tag.
-
#create_id3v2_2_tag ⇒ Id3v22Tag, ...
creates the id3v2.2 tag.
-
#create_id3v2_3_tag ⇒ Id3v22Tag, ...
creates the id3v2.3 tag.
-
#create_id3v2_4_tag ⇒ Id3v22Tag, ...
creates the id3v2.4 tag.
-
#initialize(file, options = Options::Options.new, no_tag_parsing: false) ⇒ AudioFile
constructor
constructor.
-
#read_audio_data ⇒ String
reads the audio data part from the file.
-
#remove_id3v1_tag ⇒ Object
removes the id3v1 tag.
-
#remove_id3v2_tag ⇒ Object
removes the id3v1 tag.
-
#write_audio_file(path) ⇒ Object
writes the audio file to the specified path Note: This path can be the same path as the path that was used to read the file.
Methods included from Extensions::Optionable
#add_size_frame, #default_decode_for_destination, #default_encode_for_destination, #ignore_v23_frame_error, #ignore_v24_frame_error, #tag_padding
Constructor Details
#initialize(file, options = Options::Options.new, no_tag_parsing: false) ⇒ AudioFile
constructor
15 16 17 18 19 20 21 22 |
# File 'lib/id3taginator/audio_file.rb', line 15 def initialize(file, = Options::Options.new, no_tag_parsing: false) @file = file @options = parse_id3v2_tag unless no_tag_parsing parse_id3v1_tag unless no_tag_parsing read_audio_data unless no_tag_parsing end |
Instance Attribute Details
#id3v1_tag ⇒ Object (readonly)
Returns the value of attribute id3v1_tag.
7 8 9 |
# File 'lib/id3taginator/audio_file.rb', line 7 def id3v1_tag @id3v1_tag end |
#id3v2_tag ⇒ Object (readonly)
Returns the value of attribute id3v2_tag.
7 8 9 |
# File 'lib/id3taginator/audio_file.rb', line 7 def id3v2_tag @id3v2_tag end |
Instance Method Details
#audio_file_to_bytes ⇒ String
creates a byte array represented as a String (str.bytes) of the modified file data
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/id3taginator/audio_file.rb', line 105 def audio_file_to_bytes @id3v2_tag&.add_size_tag_if_not_present(audio_size_without_id3v2_tag) bytes = [] bytes << @id3v2_tag.to_bytes unless @id3v2_tag.nil? bytes << read_audio_data bytes << @id3v1_tag.to_bytes unless @id3v1_tag.nil? bytes.inject([]) { |sum, x| sum + x.bytes }.pack('C*') end |
#create_id3v1_tag ⇒ Id3v1Tag
creates the id3v1 tag. If a tag already exists, it must be removed first.
51 52 53 54 55 56 |
# File 'lib/id3taginator/audio_file.rb', line 51 def create_id3v1_tag raise Errors::Id3TagError, 'An ID3v1 tag already exists. Can\'t create a 2nd one.' unless @id3v1_tag.nil? @id3v1_tag = Id3v1Tag.new @id3v1_tag end |
#create_id3v2_2_tag ⇒ Id3v22Tag, ...
creates the id3v2.2 tag. If a tag already exists, it must be removed first.
66 67 68 69 70 71 |
# File 'lib/id3taginator/audio_file.rb', line 66 def create_id3v2_2_tag raise Errors::Id3TagError, 'An ID3v2.2 tag already exists. Can\'t create a 2nd one.' unless @id3v1_tag.nil? @id3v2_tag = Id3v2Tag.build_for_version(2, @options) @id3v2_tag end |
#create_id3v2_3_tag ⇒ Id3v22Tag, ...
creates the id3v2.3 tag. If a tag already exists, it must be removed first.
76 77 78 79 80 81 |
# File 'lib/id3taginator/audio_file.rb', line 76 def create_id3v2_3_tag raise Errors::Id3TagError, 'An ID3v2.3 tag already exists. Can\'t create a 2nd one.' unless @id3v1_tag.nil? @id3v2_tag = Id3v2Tag.build_for_version(3, @options) @id3v2_tag end |
#create_id3v2_4_tag ⇒ Id3v22Tag, ...
creates the id3v2.4 tag. If a tag already exists, it must be removed first.
86 87 88 89 90 91 |
# File 'lib/id3taginator/audio_file.rb', line 86 def create_id3v2_4_tag raise Errors::Id3TagError, 'An ID3v2.4 tag already exists. Can\'t create a 2nd one.' unless @id3v1_tag.nil? @id3v2_tag = Id3v2Tag.build_for_version(4, @options) @id3v2_tag end |
#read_audio_data ⇒ String
reads the audio data part from the file
119 120 121 122 123 124 125 |
# File 'lib/id3taginator/audio_file.rb', line 119 def read_audio_data return @audio_data unless @audio_data.nil? @file.seek(@audio_start_index) @audio_data = @file.read(@audio_end_index - @audio_start_index) @audio_data end |
#remove_id3v1_tag ⇒ Object
removes the id3v1 tag
44 45 46 |
# File 'lib/id3taginator/audio_file.rb', line 44 def remove_id3v1_tag @id3v1_tag = nil end |
#remove_id3v2_tag ⇒ Object
removes the id3v1 tag
59 60 61 |
# File 'lib/id3taginator/audio_file.rb', line 59 def remove_id3v2_tag @id3v2_tag = nil end |
#write_audio_file(path) ⇒ Object
writes the audio file to the specified path Note: This path can be the same path as the path that was used to read the file
97 98 99 100 |
# File 'lib/id3taginator/audio_file.rb', line 97 def write_audio_file(path) out_file = File.open(path, 'w') out_file.write(audio_file_to_bytes) end |