/// <summary> /// Saves the changes made in the current instance to the /// file it represents. /// </summary> public override void Save() { // Boilerplate PreSave(); Mode = AccessMode.Write; try { ByteVector data = new ByteVector(); // Enclose the Id3v2 tag in an "id3 " item and // embed it as the first tag. if (id32_tag != null) { ByteVector tag_data = id32_tag.Render(); if (tag_data.Count > 10) { if (tag_data.Count % 2 == 1) { tag_data.Add(0); } data.Add("id3 "); data.Add(ByteVector.FromUInt( (uint)tag_data.Count, false)); data.Add(tag_data); } } // Embed "INFO" as the second tag. if (info_tag != null) { data.Add(info_tag.RenderEnclosed()); } // Embed "MID " as the third tag. if (mid_tag != null) { data.Add(mid_tag.RenderEnclosed()); } // Embed the DivX tag in "IDVX and embed it as // the fourth tag. if (divx_tag != null && !divx_tag.IsEmpty) { ByteVector tag_data = divx_tag.Render(); data.Add("IDVX"); data.Add(ByteVector.FromUInt( (uint)tag_data.Count, false)); data.Add(tag_data); } // Read the file to determine the current RIFF // size and the area tagging does in. uint riff_size; long tag_start, tag_end; Read(false, ReadStyle.None, out riff_size, out tag_start, out tag_end); // If tagging info cannot be found, place it at // the end of the file. if (tag_start < 12 || tag_end < tag_start) { tag_start = tag_end = Length; } int length = (int)(tag_end - tag_start); // If the tag isn't at the end of the file, // try appending using padding to improve // write time now or for subsequent writes. if (tag_end != Length) { int padding_size = length - data.Count - 8; if (padding_size < 0) { padding_size = 1024; } data.Add("JUNK"); data.Add(ByteVector.FromUInt( (uint)padding_size, false)); data.Add(new ByteVector(padding_size)); } // Insert the tagging data. Insert(data, tag_start, length); // If the data size changed, and the tagging // data is within the RIFF portion of the file, // update the riff size. if (data.Count - length != 0 && tag_start <= riff_size) { Insert(ByteVector.FromUInt((uint) (riff_size + data.Count - length), false), 4, 4); } // Update the tag types. TagTypesOnDisk = TagTypes; } finally { Mode = AccessMode.Closed; } }
/// <summary> /// Saves the changes made in the current instance to the /// file it represents. /// </summary> public override void Save() { Mode = AccessMode.Write; try { ByteVector data = new ByteVector(); // Add the ID3 chunk and ID32 tag to the vector if (tag != null) { ByteVector tag_data = tag.Render(); if (tag_data.Count > 10) { if (tag_data.Count % 2 == 1) { tag_data.Add(0); } data.Add("ID3 "); data.Add(ByteVector.FromUInt( (uint)tag_data.Count, true)); data.Add(tag_data); } } // Read the file to determine the current AIFF // size and the area tagging is in. uint aiff_size; long tag_start, tag_end; Read(false, ReadStyle.None, out aiff_size, out tag_start, out tag_end); // If tagging info cannot be found, place it at // the end of the file. if (tag_start < 12 || tag_end < tag_start) { tag_start = tag_end = Length; } int length = (int)(tag_end - tag_start + 8); // Insert the tagging data. Insert(data, tag_start, length); // If the data size changed update the aiff size. if (data.Count - length != 0 && tag_start <= aiff_size) { // Depending, if a Tag has been added or removed, // the length needs to be adjusted if (tag == null) { length -= 16; } else { length -= 8; } Insert(ByteVector.FromUInt((uint) (aiff_size + data.Count - length), true), 4, 4); } // Update the tag types. TagTypesOnDisk = TagTypes; } finally { Mode = AccessMode.Closed; } }
public override void Save() { Mode = AccessMode.Write; try { ByteVector data = new ByteVector(); if (id32_tag != null) { ByteVector tag_data = id32_tag.Render(); if (tag_data.Count > 10) { if (tag_data.Count % 2 == 1) { tag_data.Add(0); } data.Add("ID32"); data.Add(ByteVector.FromUInt( (uint)tag_data.Count, false)); data.Add(tag_data); } } if (info_tag != null) { data.Add(info_tag.RenderEnclosed()); } if (mid_tag != null) { data.Add(mid_tag.RenderEnclosed()); } if (divx_tag != null && !divx_tag.IsEmpty) { ByteVector tag_data = divx_tag.Render(); data.Add("IDVX"); data.Add(ByteVector.FromUInt( (uint)tag_data.Count, false)); data.Add(tag_data); } uint riff_size; long tag_start, tag_end; Read(false, ReadStyle.None, out riff_size, out tag_start, out tag_end); if (tag_start < 12 || tag_end < tag_start) { tag_start = tag_end = Length; } int length = (int)(tag_end - tag_start); int padding_size = length - data.Count - 8; if (padding_size < 0) { padding_size = 1024; } data.Add("JUNK"); data.Add(ByteVector.FromUInt( (uint)padding_size, false)); data.Add(new ByteVector(padding_size)); Insert(data, tag_start, length); if (data.Count - length != 0 && tag_start <= riff_size) { Insert(ByteVector.FromUInt((uint) (riff_size + data.Count - length), false), 4, 4); } TagTypesOnDisk = TagTypes; } finally { Mode = AccessMode.Closed; } }