Class: EasyTag::Attributes::MP3Attribute

Inherits:
BaseAttribute show all
Defined in:
lib/easytag/attributes/mp3.rb

Constant Summary

Constants inherited from BaseAttribute

BaseAttribute::Utilities

Instance Attribute Summary collapse

Attributes inherited from BaseAttribute

#aliases

Instance Method Summary collapse

Methods inherited from BaseAttribute

#call, can_clone?, deep_copy, #default, default_for_type, name_to_ivar, #post_process, #read_audio_property, #read_default, #type_cast, #user_info_lookup

Constructor Details

#initialize(args) ⇒ MP3Attribute

Returns a new instance of MP3Attribute.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/easytag/attributes/mp3.rb', line 11

def initialize(args)
  super(args)
  @id3v2_frames = args[:id3v2_frames] || []
  @id3v1_tag    = args[:id3v1_tag] || nil

  # fill default options

  # ID3 stores boolean values as numeric strings
  #   set to true to enable type casting  (post process)
  @options[:is_flag]    ||= false
  # return entire field list instead of first item in field list
  @options[:field_list] ||= false
end

Instance Attribute Details

#ivarObject (readonly)

Returns the value of attribute ivar.



9
10
11
# File 'lib/easytag/attributes/mp3.rb', line 9

def ivar
  @ivar
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/easytag/attributes/mp3.rb', line 9

def name
  @name
end

Instance Method Details

#data_from_frame(frame) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/easytag/attributes/mp3.rb', line 34

def data_from_frame(frame)
  data = nil
  if frame.is_a?(TagLib::ID3v2::TextIdentificationFrame)
    field_list = frame.field_list
    data = @options[:field_list] ? field_list : field_list.first
  elsif frame.is_a?(TagLib::ID3v2::UnsynchronizedLyricsFrame)
    data = frame.text
  elsif frame.is_a?(TagLib::ID3v2::CommentsFrame)
    data = frame.text
  elsif frame.is_a?(TagLib::ID3v2::AttachedPictureFrame)
    data = EasyTag::Image.new(frame.picture)
    data.desc = frame.description
    data.type = frame.type
    data.mime_type = frame.mime_type
  elsif frame.is_a?(TagLib::ID3v2::UnknownFrame)
    nil
  else
    warn 'no defined frames match the given frame'
  end

  data
end

#first_frame_for_id(id, iface) ⇒ Object



30
31
32
# File 'lib/easytag/attributes/mp3.rb', line 30

def first_frame_for_id(id, iface)
  frames_for_id(id, iface).first
end

#frames_for_id(id, iface) ⇒ Object



26
27
28
# File 'lib/easytag/attributes/mp3.rb', line 26

def frames_for_id(id, iface)
  iface.info.id3v2_tag.frame_list(id)
end

#read_all_id3(iface) ⇒ Object

read_all_id3

gets data from each frame id given only falls back to the id3v1 tag if none found



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/easytag/attributes/mp3.rb', line 65

def read_all_id3(iface)
  frames = []
  @id3v2_frames.each do |f| 
    frames += frames_for_id(f, iface)
  end

  data = []
  # only check id3v1 if no id3v2 frames found
  if frames.empty?
    data << iface.info.id3v1_tag.send(@id3v1_tag) unless @id3v1_tag.nil?
  else
    frames.each { |frame| data << data_from_frame(frame) }
  end

  data
end

#read_date(iface) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/easytag/attributes/mp3.rb', line 121

def read_date(iface)
  id3v1 = iface.info.id3v1_tag

  v10_year = id3v1.year.to_s if id3v1.year > 0
  v23_year = data_from_frame(first_frame_for_id('TYER', iface))
  v23_date = data_from_frame(first_frame_for_id('TDAT', iface))
  v24_date = data_from_frame(first_frame_for_id('TDRC', iface))

  # check variables in order of importance
  date_str = v24_date || v23_year || v10_year
  # only append v23_date if date_str is currently a year
  date_str << v23_date unless v23_date.nil? or date_str.length > 4
  puts "MP3#date: date_str = \"#{date_str}\"" if $DEBUG

  date_str
end

#read_field_list_as_key_value(iface) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/easytag/attributes/mp3.rb', line 105

def read_field_list_as_key_value(iface)
  kv_hash = {}
  frame_data = read_all_id3(iface)

  frame_data.each do |data|
    key = data[0]
    values = data[1..-1]

    key = Utilities.normalize_string(key) if @options[:normalize]
    key = key.to_sym if @options[:to_sym]
    kv_hash[key] = values.count > 1 ? values : values.first
  end

  kv_hash
end

#read_first_id3(iface) ⇒ Object

read_first_id3

Similar to read_all_id3, but optimized for reading only one frame at max



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/easytag/attributes/mp3.rb', line 85

def read_first_id3(iface)
  frame = nil
  @id3v2_frames.each do |f|
    frame = first_frame_for_id(f, iface) if frame.nil?
  end

  if frame.nil?
    data = iface.info.id3v1_tag.send(@id3v1_tag) unless @id3v1_tag.nil?
  else
    data = data_from_frame(frame)
  end

  data
end

#read_int_pair(iface) ⇒ Object



100
101
102
103
# File 'lib/easytag/attributes/mp3.rb', line 100

def read_int_pair(iface)
  int_pair_str = read_first_id3(iface).to_s
  EasyTag::Utilities.get_int_pair(int_pair_str)
end

#read_ufid(iface) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/easytag/attributes/mp3.rb', line 138

def read_ufid(iface)
  frames = iface.info.id3v2_tag.frame_list('UFID')
  ufid = nil

  frames.each do |frame|
    if @handler_opts[:owner].eql?(frame.owner)
      ufid = frame.identifier
      break
    end
  end

  ufid
end