Class: EasyTag::Attributes::MP3Attribute
- Inherits:
-
BaseAttribute
- Object
- BaseAttribute
- EasyTag::Attributes::MP3Attribute
- Defined in:
- lib/easytag/attributes/mp3.rb
Constant Summary
Constants inherited from BaseAttribute
Instance Attribute Summary collapse
-
#ivar ⇒ Object
readonly
Returns the value of attribute ivar.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #data_from_frame(frame) ⇒ Object
- #first_frame_for_id(id, iface) ⇒ Object
- #frames_for_id(id, iface) ⇒ Object
-
#initialize(args) ⇒ MP3Attribute
constructor
A new instance of MP3Attribute.
-
#read_all_id3(iface) ⇒ Object
read_all_id3.
- #read_date(iface) ⇒ Object
- #read_field_list_as_key_value(iface) ⇒ Object
-
#read_first_id3(iface) ⇒ Object
read_first_id3.
- #read_int_pair(iface) ⇒ Object
Methods inherited from BaseAttribute
#call, can_clone?, deep_copy, #default, name_to_ivar, obj_or_nil, #post_process, #type_cast
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) [:is_flag] ||= false # return entire field list instead of first item in field list [:field_list] ||= false end |
Instance Attribute Details
#ivar ⇒ Object (readonly)
Returns the value of attribute ivar.
9 10 11 |
# File 'lib/easytag/attributes/mp3.rb', line 9 def ivar @ivar end |
#name ⇒ Object (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 |
# 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 = [: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 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
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/easytag/attributes/mp3.rb', line 63 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
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/easytag/attributes/mp3.rb', line 117 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
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/easytag/attributes/mp3.rb', line 103 def read_field_list_as_key_value(iface) kv_hash = {} frame_data = read_all_id3(iface) frame_data.each do |data| key, value = data key = Utilities.normalize_string(key) if [:normalize] key = key.to_sym if [:to_sym] kv_hash[key] = value 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
83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/easytag/attributes/mp3.rb', line 83 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
98 99 100 101 |
# File 'lib/easytag/attributes/mp3.rb', line 98 def read_int_pair(iface) int_pair_str = read_first_id3(iface).to_s EasyTag::Utilities.get_int_pair(int_pair_str) end |