/// <summary> /// Converts track to byte array of specified version /// Versions: /// 0 - Biggest and basic, codes frequency and duration /// 1 - Some optimization, codes note number and duration /// 2 - With dictionary with size of 120, good in big files /// </summary> /// <param name="version">Version of file</param> /// <returns>Created file without header</returns> private byte[] GetAsByteArray(int version) { if (version == 1 || version == 0) { MemoryStream ms = new MemoryStream(); BinaryWriter bw = new BinaryWriter(ms); for (int i = 0; i < notes.Count; i++) { var ntn = notes[i]; if (version == 1) bw.Write((byte)ntn.Number); else bw.Write((ushort)ntn.Frequency); bw.Write((ushort)ntn.Length); } return ms.ToArray(); } if (version == 2 | version == 3 | version == 4 | version == 5) { MemoryStream ms = new MemoryStream(); BinaryWriter bw = new BinaryWriter(ms); List<NoteTwoCount> dictionary = new List<NoteTwoCount>(); for (int i = 0; i < notes.Count; i++) { var ntn = notes[i]; for (int ii = 0; ii < dictionary.Count; ii++) if (dictionary[ii].isEqual(ntn)) { dictionary[ii].count++; goto ex0; } var ntctmp = new NoteTwoCount(); ntctmp.length = ntn.Length; ntctmp.number = ntn.Number; ntctmp.isBass = ntn.IsBass; ntctmp.count = 1; dictionary.Add(ntctmp); ex0: { } } dictionary.Sort(); dictionary.Reverse(); int dictLen = 0; if (version == 2) dictLen = 128; if (version == 3) dictLen = 64; if (version == 4) dictLen = 32; if (version == 5) dictLen = 16; for (int i = 0; i < dictLen; i++) //Filling dictionary { if (i < dictionary.Count) { bw.Write((byte)dictionary[i].number); bw.Write((ushort)dictionary[i].length); } else { bw.Write((byte)0); bw.Write((ushort)0); } } if (dictionary.Count > dictLen) { for (int i = dictLen; i < dictionary.Count; i++) { dictionary.RemoveAt(i); i--; } } for (int i = 0; i < notes.Count; i++) { var ntn = notes[i]; for (int ii = 0; ii < dictionary.Count; ii++) if (dictionary[ii].isEqual(ntn)) { bw.Write((byte)((byte)ii + (byte)0x80)); goto ex1; } bw.Write((byte)ntn.Number); bw.Write((ushort)ntn.Length); ex1: { } } return ms.ToArray(); } return null; }
/// <summary> /// Converts track to byte array of specified version /// Versions: /// 0 - Biggest and basic, codes frequency and duration /// 1 - Some optimization, codes note number and duration /// 2 - With dictionary with size of 120, good in big files /// </summary> /// <param name="version">Version of file</param> /// <returns>Created file without header</returns> private byte[] GetAsByteArray(int version) { if (version == 1 || version == 0) { MemoryStream ms = new MemoryStream(); BinaryWriter bw = new BinaryWriter(ms); for (int i = 0; i < notes.Count; i++) { var ntn = notes[i]; if (version == 1) { bw.Write((byte)ntn.Number); } else { bw.Write((ushort)ntn.Frequency); } bw.Write((ushort)ntn.Length); } return(ms.ToArray()); } if (version == 2 | version == 3 | version == 4 | version == 5) { MemoryStream ms = new MemoryStream(); BinaryWriter bw = new BinaryWriter(ms); List <NoteTwoCount> dictionary = new List <NoteTwoCount>(); for (int i = 0; i < notes.Count; i++) { var ntn = notes[i]; for (int ii = 0; ii < dictionary.Count; ii++) { if (dictionary[ii].isEqual(ntn)) { dictionary[ii].count++; goto ex0; } } var ntctmp = new NoteTwoCount(); ntctmp.length = ntn.Length; ntctmp.number = ntn.Number; ntctmp.isBass = ntn.IsBass; ntctmp.count = 1; dictionary.Add(ntctmp); ex0 : { } } dictionary.Sort(); dictionary.Reverse(); int dictLen = 0; if (version == 2) { dictLen = 128; } if (version == 3) { dictLen = 64; } if (version == 4) { dictLen = 32; } if (version == 5) { dictLen = 16; } for (int i = 0; i < dictLen; i++) //Filling dictionary { if (i < dictionary.Count) { bw.Write((byte)dictionary[i].number); bw.Write((ushort)dictionary[i].length); } else { bw.Write((byte)0); bw.Write((ushort)0); } } if (dictionary.Count > dictLen) { for (int i = dictLen; i < dictionary.Count; i++) { dictionary.RemoveAt(i); i--; } } for (int i = 0; i < notes.Count; i++) { var ntn = notes[i]; for (int ii = 0; ii < dictionary.Count; ii++) { if (dictionary[ii].isEqual(ntn)) { bw.Write((byte)((byte)ii + (byte)0x80)); goto ex1; } } bw.Write((byte)ntn.Number); bw.Write((ushort)ntn.Length); ex1 : { } } return(ms.ToArray()); } return(null); }