public byte[] ToByteArray() { var array = new MemoryStream(); using (var writer = new BinaryWriter(array)) { VendorString = "LibFlacSharp 1.0.3 20181214"; writer.Write(_VendorString.Length); writer.Write(_VendorString); writer.Write(CommentList.Count); foreach (var set in CommentList) { var entry = new VorbisCommentEntry(); entry.Comment = set.Key + "=" + set.Value; writer.Write(entry._Comment.Length); writer.Write(entry._Comment); } return(array.ToArray()); } }
public static VorbisComment FromByteArray(byte[] array) { using (var reader = new BinaryReader(new MemoryStream(array))) { var vorbis = new VorbisComment(); var vendorLength = reader.ReadUInt32(); vorbis._VendorString = reader.ReadBytes((int)vendorLength); var length = reader.ReadUInt32(); for (int i = 0; i < length; i++) { var entry = new VorbisCommentEntry(); entry.Length = reader.ReadUInt32(); entry._Comment = reader.ReadBytes((int)entry.Length); var str = entry.Comment.Split('='); switch (str[0].ToLower()) { case "title": vorbis.CommentList[VorbisCommentType.Title] = str[1]; break; case "artist": vorbis.CommentList[VorbisCommentType.Artist] = str[1]; break; case "lyricist": vorbis.CommentList[VorbisCommentType.Lyricist] = str[1]; break; case "composer": vorbis.CommentList[VorbisCommentType.Composer] = str[1]; break; case "album": vorbis.CommentList[VorbisCommentType.Album] = str[1]; break; case "genre": vorbis.CommentList[VorbisCommentType.Genre] = str[1]; break; case "lyrics": vorbis.CommentList[VorbisCommentType.Lyrics] = str[1]; break; case "year": vorbis.CommentList[VorbisCommentType.Year] = str[1]; break; case "tracknumber": vorbis.CommentList[VorbisCommentType.TrackNumber] = str[1]; break; case "tracktotal": vorbis.CommentList[VorbisCommentType.TrackTotal] = str[1]; break; case "discnumber": vorbis.CommentList[VorbisCommentType.DiscNumber] = str[1]; break; case "disctotal": vorbis.CommentList[VorbisCommentType.DiscTotal] = str[1]; break; default: vorbis.CommentList[str[0]] = str[1]; break; } } return(vorbis); } }