|
| 1 | +using System.Collections.Generic; |
| 2 | + |
| 3 | +namespace TagLib.Id3v2 |
| 4 | +{ |
| 5 | + public class EventTimeCodesFrame : Frame |
| 6 | + { |
| 7 | + #region Private Properties |
| 8 | + |
| 9 | + private TimestampFormat timestampFormat; |
| 10 | + |
| 11 | + private List<EventTimeCode> events; |
| 12 | + |
| 13 | + #endregion |
| 14 | + |
| 15 | + #region Constructors |
| 16 | + |
| 17 | + public EventTimeCodesFrame() : base (FrameType.ETCO, 4) |
| 18 | + { |
| 19 | + Flags = FrameFlags.FileAlterPreservation; |
| 20 | + } |
| 21 | + |
| 22 | + public EventTimeCodesFrame(TimestampFormat timestampFormat) : base(FrameType.ETCO, 4) |
| 23 | + { |
| 24 | + this.timestampFormat = timestampFormat; |
| 25 | + Flags = FrameFlags.FileAlterPreservation; |
| 26 | + } |
| 27 | + |
| 28 | + public EventTimeCodesFrame(ByteVector data, |
| 29 | + byte version) : base (data, version) |
| 30 | + { |
| 31 | + SetData(data, 0, version, true); |
| 32 | + } |
| 33 | + |
| 34 | + public EventTimeCodesFrame(FrameHeader frameHeader) : base (frameHeader) |
| 35 | + { |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + public EventTimeCodesFrame(ByteVector data, |
| 40 | + int offset, |
| 41 | + FrameHeader header, |
| 42 | + byte version) : base (header) |
| 43 | + { |
| 44 | + SetData(data, offset, version, false); |
| 45 | + } |
| 46 | + |
| 47 | + #endregion |
| 48 | + |
| 49 | + #region Public Properties |
| 50 | + |
| 51 | + public TimestampFormat TimestampFormat |
| 52 | + { |
| 53 | + get { return timestampFormat; } |
| 54 | + set { timestampFormat = value; } |
| 55 | + } |
| 56 | + |
| 57 | + public List<EventTimeCode> Events |
| 58 | + { |
| 59 | + get { return events; } |
| 60 | + set { events = value; } |
| 61 | + } |
| 62 | + |
| 63 | + #endregion |
| 64 | + |
| 65 | + #region Public Static Methods |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Gets a play count frame from a specified tag, optionally |
| 69 | + /// creating it if it does not exist. |
| 70 | + /// </summary> |
| 71 | + /// <param name="tag"> |
| 72 | + /// A <see cref="Tag" /> object to search in. |
| 73 | + /// </param> |
| 74 | + /// <param name="create"> |
| 75 | + /// A <see cref="bool" /> specifying whether or not to create |
| 76 | + /// and add a new frame to the tag if a match is not found. |
| 77 | + /// </param> |
| 78 | + /// <returns> |
| 79 | + /// A <see cref="EventTimeCodesFrame" /> object containing the |
| 80 | + /// matching frame, or <see langword="null" /> if a match |
| 81 | + /// wasn't found and <paramref name="create" /> is <see |
| 82 | + /// langword="false" />. |
| 83 | + /// </returns> |
| 84 | + public static EventTimeCodesFrame Get(Tag tag, bool create) |
| 85 | + { |
| 86 | + EventTimeCodesFrame etco; |
| 87 | + foreach (Frame frame in tag) |
| 88 | + { |
| 89 | + etco = frame as EventTimeCodesFrame; |
| 90 | + |
| 91 | + if (etco != null) |
| 92 | + return etco; |
| 93 | + } |
| 94 | + |
| 95 | + if (!create) |
| 96 | + return null; |
| 97 | + |
| 98 | + etco = new EventTimeCodesFrame(); |
| 99 | + tag.AddFrame(etco); |
| 100 | + return etco; |
| 101 | + } |
| 102 | + |
| 103 | + #endregion |
| 104 | + |
| 105 | + #region Protected Methods |
| 106 | + |
| 107 | + protected override void ParseFields(ByteVector data, byte version) |
| 108 | + { |
| 109 | + events = new List<EventTimeCode>(); |
| 110 | + timestampFormat = (TimestampFormat)data.Data[0]; |
| 111 | + |
| 112 | + var incomingEventsData = data.Mid(1); |
| 113 | + for (var i = 0; i < incomingEventsData.Count; i++) |
| 114 | + { |
| 115 | + var eventType = (EventType)incomingEventsData.Data[1]; |
| 116 | + i++; |
| 117 | + |
| 118 | + var timestampData = new ByteVector(incomingEventsData.Data[i], |
| 119 | + incomingEventsData.Data[i+1], |
| 120 | + incomingEventsData.Data[i+2], |
| 121 | + incomingEventsData.Data[i+3]); |
| 122 | + |
| 123 | + i += 3; |
| 124 | + |
| 125 | + var timestamp = timestampData.ToInt(); |
| 126 | + |
| 127 | + events.Add(new EventTimeCode(eventType, timestamp)); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + protected override ByteVector RenderFields(byte version) |
| 132 | + { |
| 133 | + var data = new List<byte>(); |
| 134 | + data.Add((byte)timestampFormat); |
| 135 | + |
| 136 | + foreach (var @event in events) |
| 137 | + { |
| 138 | + data.Add((byte)@event.TypeOfEvent); |
| 139 | + |
| 140 | + var timeData = ByteVector.FromInt(@event.Time); |
| 141 | + data.AddRange(timeData.Data); |
| 142 | + } |
| 143 | + |
| 144 | + return new ByteVector(data.ToArray()); |
| 145 | + } |
| 146 | + |
| 147 | + #endregion |
| 148 | + |
| 149 | + #region ICloneable |
| 150 | + |
| 151 | + /// <summary> |
| 152 | + /// Creates a deep copy of the current instance. |
| 153 | + /// </summary> |
| 154 | + /// <returns> |
| 155 | + /// A new <see cref="Frame" /> object identical to the |
| 156 | + /// current instance. |
| 157 | + /// </returns> |
| 158 | + public override Frame Clone() |
| 159 | + { |
| 160 | + var frame = new EventTimeCodesFrame(header); |
| 161 | + frame.timestampFormat = timestampFormat; |
| 162 | + frame.events = events.ConvertAll(item => (EventTimeCode)item.Clone()); |
| 163 | + return frame; |
| 164 | + } |
| 165 | + |
| 166 | + #endregion |
| 167 | + } |
| 168 | +} |
0 commit comments