/// <summary> /// Will write the data describing this metadata to the given stream. /// </summary> /// <param name="targetStream">Stream to write the data to.</param> public override void WriteBlockData(Stream targetStream) { uint totalLength = 0; long headerPosition = targetStream.Position; this.Header.WriteHeaderData(targetStream); // Write the vendor string (first write the length as a 32-bit uint and then the actual bytes byte[] vendorData = System.Text.Encoding.UTF8.GetBytes(this.vendor); byte[] number = BinaryDataHelper.GetBytesUInt32((uint)vendorData.Length); targetStream.Write(BinaryDataHelper.SwitchEndianness(number, 0, 4), 0, 4); targetStream.Write(vendorData, 0, vendorData.Length); totalLength += 4 + (uint)vendorData.Length; // In FlacLibSharp a single comment can have multiple values, but // in the FLAC format each value is a comment by itself. So // we can't use this.comments.Count, since 1 comment could have 10 // values, which results in 10 comments in the FLAC file. var totalValueCount = 0; foreach (var comment in this.comments) { foreach (var value in comment.Value) { totalValueCount++; } } number = BinaryDataHelper.GetBytesUInt32((uint)totalValueCount); targetStream.Write(BinaryDataHelper.SwitchEndianness(number, 0, 4), 0, 4); totalLength += 4; foreach (var comment in this.comments) { foreach (var value in comment.Value) { string commentText = string.Format("{0}={1}", comment.Key, value); byte[] commentData = System.Text.Encoding.UTF8.GetBytes(commentText); number = BinaryDataHelper.GetBytesUInt32((uint)commentData.Length); targetStream.Write(BinaryDataHelper.SwitchEndianness(number, 0, 4), 0, 4); targetStream.Write(commentData, 0, commentData.Length); totalLength += 4 + (uint)commentData.Length; } } long endPosition = targetStream.Position; targetStream.Seek(headerPosition, SeekOrigin.Begin); this.Header.MetaDataBlockLength = totalLength; this.Header.WriteHeaderData(targetStream); targetStream.Seek(endPosition, SeekOrigin.Begin); // Note: FLAC does NOT have the framing bit for vorbis so we don't have to write this. }
/// <summary> /// Will write the data describing this metadata block to the given stream. /// </summary> /// <param name="targetStream">Stream to write the data to.</param> public override void WriteBlockData(Stream targetStream) { uint totalLength = 0; long headerPosition = targetStream.Position; this.Header.WriteHeaderData(targetStream); // Write the vendor string (first write the length as a 32-bit uint and then the actual bytes byte[] vendorData = System.Text.Encoding.UTF8.GetBytes(this.vendor); byte[] number = BinaryDataHelper.GetBytesUInt32((uint)vendorData.Length); targetStream.Write(BinaryDataHelper.SwitchEndianness(number, 0, 4), 0, 4); targetStream.Write(vendorData, 0, vendorData.Length); totalLength += 4 + (uint)vendorData.Length; // Length of list of user comments (first a 32-bit uint, then the actual comments) number = BinaryDataHelper.GetBytesUInt32((uint)this.comments.Count); targetStream.Write(BinaryDataHelper.SwitchEndianness(number, 0, 4), 0, 4); totalLength += 4; foreach (var comment in this.comments) { foreach (var value in comment.Value) { string commentText = string.Format("{0}={1}", comment.Key, value); byte[] commentData = System.Text.Encoding.UTF8.GetBytes(commentText); number = BinaryDataHelper.GetBytesUInt32((uint)commentData.Length); targetStream.Write(BinaryDataHelper.SwitchEndianness(number, 0, 4), 0, 4); targetStream.Write(commentData, 0, commentData.Length); totalLength += 4 + (uint)commentData.Length; } } long endPosition = targetStream.Position; targetStream.Seek(headerPosition, SeekOrigin.Begin); this.Header.MetaDataBlockLength = totalLength; this.Header.WriteHeaderData(targetStream); targetStream.Seek(endPosition, SeekOrigin.Begin); // Note: FLAC does NOT have the framing bit for vorbis so we don't have to write this. }
/// <summary> /// Loads the Vorbis from a block of data. /// </summary> /// <param name="data"></param> public override void LoadBlockData(byte[] data) { UInt32 vendorLength = BinaryDataHelper.GetUInt32(BinaryDataHelper.SwitchEndianness(data, 0, 4), 0); this.vendor = Encoding.UTF8.GetString(BinaryDataHelper.GetDataSubset(data, 4, (int)vendorLength)); int startOfComments = 4 + (int)vendorLength; UInt32 userCommentListLength = BinaryDataHelper.GetUInt32(BinaryDataHelper.SwitchEndianness(data, startOfComments, 4), 0); // Start of comments actually four bytes further (first piece is the count of items in the list) startOfComments += 4; for (UInt32 i = 0; i < userCommentListLength; i++) { UInt32 commentLength = BinaryDataHelper.GetUInt32(BinaryDataHelper.SwitchEndianness(data, startOfComments, 4), 0); string comment = Encoding.UTF8.GetString(BinaryDataHelper.GetDataSubset(data, startOfComments + 4, (int)commentLength)); // We're moving on in the array ... startOfComments += 4 + (int)commentLength; AddComment(comment); } // All done, note that FLAC doesn't have the "fraiming bit" for vorbis ... }