/// <summary> /// Reads and returns a frame from a stream. /// </summary> /// <param name="stream">The stream to read from.</param> /// <param name="version">The ID3v2 version of the tag being parsed.</param> /// <returns>The frame read from the stream.</returns> public static ID3v2Frame ReadFrame(Stream stream, ID3Versions version) { if (stream == null) { throw new ArgumentNullException("stream"); } ID3v2Frame frame = null; FrameParameters parameters = new FrameParameters(version); byte[] header = new byte[parameters.HeaderLength]; char[] idChars = new char[parameters.IDLength]; byte[] sizeBytes = new byte[parameters.SizeLength]; byte[] flags = new byte[parameters.FlagsLength]; byte[] frameData; string frameID; int size; stream.Read(header, 0, header.Length); Array.Copy(header, 0, idChars, 0, idChars.Length); Array.Copy(header, parameters.IDLength, sizeBytes, 0, sizeBytes.Length); Array.Copy(header, parameters.IDLength + parameters.SizeLength, flags, 0, flags.Length); if (idChars[0] != 0x0) { frameID = new String(idChars); if (parameters.SizeIsSynchSafe) { size = SynchsafeInteger.UnSynchsafe(sizeBytes); } else { size = EncodedInteger.ToInt(sizeBytes); } frameData = new byte[size]; stream.Read(frameData, 0, frameData.Length); FrameType frameType = FrameRegistry.GetFrameType(frameID, version); frame = FrameRegistry.GetNewFrame(frameType); frame.Initialize(flags, frameData, version); } else { frame = null; } return(frame); }
/// <summary> /// Sets the value of a text frame. If the frame is a composite, the /// first frame is used; if no frames exist for the given FrameType, /// a new frame is added; if null is passed as the value, any frames /// of the specified type are removed. /// </summary> /// <param name="frameType">The type of frame to set the value of.</param> /// <param name="textValue">The value to assign to the frame.</param> private void SetFrameTextValue(FrameType frameType, string textValue) { if (textValue == null) { this.RemoveFrame(frameType); } else { TextInformationFrame textFrame = GetFirstFrame(frameType) as TextInformationFrame; if (textFrame == null) { textFrame = (TextInformationFrame)FrameRegistry.GetNewFrame(frameType); this.AddFrame(textFrame); } textFrame.Text = textValue; } }
/// <summary> /// Returns a new frame of the given type. /// </summary> /// <param name="frameType">The type of frame to return.</param> /// <returns>A new frame of the given type.</returns> public static ID3v2Frame GetNewFrame(FrameType frameType) { return(FrameRegistry.GetNewFrame(frameType)); }