Class: Id3Taginator::AudioFile

Inherits:
Object
  • Object
show all
Includes:
Extensions::Optionable
Defined in:
lib/id3taginator/audio_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Parameters:

  • file (StringIO, IO, File)

    the file stream

  • options (Options::Options) (defaults to: Options::Options.new)

    the options to use for this file

  • no_tag_parsing (Boolean) (defaults to: false)

    if true, no tag parsing is done (only use this for testing purposes, otherwise it renders the file literally useless)



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

def initialize(file, options = Options::Options.new, no_tag_parsing: false)
  @file = file
  @options = 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_tagObject (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_tagObject (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_bytesString

creates a byte array represented as a String (str.bytes) of the modified file data

Returns:

  • (String)

    the byte array of the file represented as a String



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_tagId3v1Tag

creates the id3v1 tag. If a tag already exists, it must be removed first.

Returns:

Raises:



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_tagId3v22Tag, ...

creates the id3v2.2 tag. If a tag already exists, it must be removed first.

Returns:

Raises:



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_tagId3v22Tag, ...

creates the id3v2.3 tag. If a tag already exists, it must be removed first.

Returns:

Raises:



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_tagId3v22Tag, ...

creates the id3v2.4 tag. If a tag already exists, it must be removed first.

Returns:

Raises:



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_dataString

reads the audio data part from the file

Returns:

  • (String)

    the byte array audio data part of the file represented as a String (str.bytes)



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_tagObject

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_tagObject

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

Parameters:

  • path (String)

    the file where to write the modified file too



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