Module: EasyTag::MP3AttributeAccessors

Includes:
BaseAttributeAccessors
Included in:
MP3Tagger
Defined in:
lib/easytag/attributes/mp3.rb

Instance Method Summary collapse

Methods included from BaseAttributeAccessors

#audio_prop_reader, #cast, #extract, #post_process, #read_audio_property

Instance Method Details

#all_tags_reader(attr_name, id3v2_frames = nil, id3v1_tag = nil, **opts) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/easytag/attributes/mp3.rb', line 19

def all_tags_reader(attr_name, id3v2_frames = nil, id3v1_tag = nil, **opts)
  id3v2_frames = Array(id3v2_frames)
  define_method(attr_name) do
    v = self.class.read_all_tags(taglib, id3v2_frames, id3v1_tag, opts)
    self.class.post_process(v, opts)
  end
end

#data_from_frame(frame, **opts) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/easytag/attributes/mp3.rb', line 89

def data_from_frame(frame, **opts)
  case
  when frame.is_a?(TagLib::ID3v2::TextIdentificationFrame)
    field_list = frame.field_list
    opts[:field_list] ? field_list : field_list.first
  when frame.is_a?(TagLib::ID3v2::UnsynchronizedLyricsFrame)
    frame.text
  when frame.is_a?(TagLib::ID3v2::CommentsFrame)
    frame.text
  when frame.is_a?(TagLib::ID3v2::AttachedPictureFrame)
    EasyTag::Image.new(frame.picture).tap do |img|
      img.desc = frame.description
      img.type = frame.type
      img.mime_type = frame.mime_type
    end
  else
    nil
  end
end

#date_reader(attr_name, **opts) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/easytag/attributes/mp3.rb', line 42

def date_reader(attr_name, **opts)
  opts[:returns] = :datetime unless opts.has_key?(:returns)
  define_method(attr_name) do
    v = self.class.read_date(taglib, opts)
    self.class.post_process(v, opts)
  end
end

#id3v1_tag(taglib, tag_name) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/easytag/attributes/mp3.rb', line 71

def id3v1_tag(taglib, tag_name)
  return nil if taglib.id3v1_tag.empty?
  v = taglib.id3v1_tag.send(tag_name)
  # TEMPFIX: nonexistent id3v1 tags return an empty string (taglib-ruby issue #49)
  case
  when v.is_a?(Fixnum) && v.zero?
    nil
  when v.is_a?(String) && v.empty?
    nil
  else
    v
  end
end

#id3v2_frames(taglib, frame_id) ⇒ Object



85
86
87
# File 'lib/easytag/attributes/mp3.rb', line 85

def id3v2_frames(taglib, frame_id)
  taglib.id3v2_tag.frame_list(frame_id)
end

#read_all_tags(taglib, id3v2_frames, id3v1_tag = nil, **opts) ⇒ Object

gets data from each frame id given only falls back to the id3v1 tag if no id3v2 frames were found



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/easytag/attributes/mp3.rb', line 52

def read_all_tags(taglib, id3v2_frames, id3v1_tag = nil, **opts)
  frames = []
  id3v2_frames.each { |frame_id| frames += id3v2_frames(taglib, frame_id) }

  data = []
  # only check id3v1 if no id3v2 frames found
  if frames.empty?
    data << id3v1_tag(taglib, id3v1_tag) unless id3v1_tag.nil?
  else
    frames.each { |frame| data << data_from_frame(frame, **opts) }
  end

  data.compact
end

#read_date(taglib, **opts) ⇒ Object

NOTE: id3v2.3 tags (TYER+TDAT) will lose month/day information due to taglib’s internal frame conversion. During the conversion, the TDAT frame is dropped and only the TYER frame is used in the conversion to TDRC. (see: github.com/taglib/taglib/issues/127)



127
128
129
130
131
132
133
134
135
136
# File 'lib/easytag/attributes/mp3.rb', line 127

def read_date(taglib, **opts)
  v10_year = taglib.id3v1_tag.year.to_s if taglib.id3v1_tag.year > 0
  v24_date = read_first_tag(taglib, ['TDRC'])

  # check variables in order of importance
  date_str = v24_date || v10_year
  puts "MP3#date: date_str = \"#{date_str}\"" if $DEBUG

  date_str
end

#read_first_tag(taglib, id3v2_frames, id3v1_tag = nil, **opts) ⇒ Object



67
68
69
# File 'lib/easytag/attributes/mp3.rb', line 67

def read_first_tag(taglib, id3v2_frames, id3v1_tag = nil, **opts)
  read_all_tags(taglib, id3v2_frames, id3v1_tag, **opts).first
end

#read_ufid(taglib, owner, opts) ⇒ Object



138
139
140
141
142
# File 'lib/easytag/attributes/mp3.rb', line 138

def read_ufid(taglib, owner, opts)
  frames = taglib.id3v2_tag.frame_list('UFID')
  frames.each { |frame| return frame.identifier if owner.eql?(frame.owner) }
  nil
end

#read_user_info(taglib, **opts) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/easytag/attributes/mp3.rb', line 109

def (taglib, **opts)
   = {}
  frame_data = read_all_tags(taglib, ['TXXX'], nil, {field_list: true})

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

    [key] = values.count > 1 ? values : values.first
  end

  
end

#single_tag_reader(attr_name, id3v2_frames = nil, id3v1_tag = nil, **opts) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/easytag/attributes/mp3.rb', line 11

def single_tag_reader(attr_name, id3v2_frames = nil, id3v1_tag = nil, **opts)
  id3v2_frames = Array(id3v2_frames)
  define_method(attr_name) do
    v = self.class.read_first_tag(taglib, id3v2_frames, id3v1_tag, opts)
    self.class.post_process(v, opts)
  end
end

#ufid_reader(attr_name, owner, **opts) ⇒ Object



35
36
37
38
39
40
# File 'lib/easytag/attributes/mp3.rb', line 35

def ufid_reader(attr_name, owner, **opts)
  define_method(attr_name) do
    v = self.class.read_ufid(taglib, owner, opts)
    self.class.post_process(v, opts)
  end
end

#user_info_reader(attr_name, key = nil, **opts) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/easytag/attributes/mp3.rb', line 27

def (attr_name, key = nil, **opts)
  key = attr_name if key.nil?
  define_method(attr_name) do
    @user_info = self.class.(taglib, **opts) if @user_info.nil?
    self.class.post_process(@user_info[key], opts)
  end
end