public override AudioFile Read(string fileName) { AudioFile audioFile = new AudioFile(); byte[] vs = File.ReadAllBytes(fileName + ".wav"); int index = 0; //Check File Validity if (ByteTools.BytesToString(vs, ref index, 4) != "RIFF") { throw new FileLoadException("File Header Invalid"); } int tFileLength = (int)ByteTools.BytesToUInt(vs, ref index); if (ByteTools.BytesToString(vs, ref index, 4) != "WAVE") { throw new FileLoadException("File Header Invalid"); } if (ByteTools.BytesToString(vs, ref index, 4) != "fmt ") { throw new FileLoadException("File Header Invalid"); } //Read Format AudioFileFormat format = new AudioFileFormat(); int lengthOfHeader = (int)ByteTools.BytesToUInt(vs, ref index); int indexAfterHeader = index + lengthOfHeader; index += 2; format.nChannels = ByteTools.BytesToUShort(vs, ref index); format.SampleRate = ByteTools.BytesToUInt(vs, ref index); index += 6; format.BitsPerSample = ByteTools.BytesToUShort(vs, ref index); index = indexAfterHeader; //Check Validity Again while (ByteTools.BytesToString(vs, ref index, 4) != "data") { index -= 3; if (index > 100) { throw new FileLoadException("File Header Invalid"); } } //Read Audio Data audioFile.audioSize = ByteTools.BytesToUInt(vs, ref index); audioFile.audioData = new byte[(int)audioFile.audioSize]; Array.Copy(vs, index, audioFile.audioData, 0, (int)audioFile.audioSize); //Return All Necessary Data audioFile.fileFormat = format; return(audioFile); }
private void readID3(byte[] vs, ref int index) { //Read ID3 Header if (ByteTools.BytesToString(vs, ref index, 3) != "ID3") { throw new FileLoadException("Invalid ID3"); } byte[] ver = new byte[2]; Array.Copy(vs, index, ver, 0, 2); ID3Ver = (float)ver[0] + ((float)ver[1] / 10.0f); index += 2; BitArray flags = ByteTools.BytesToBitArray(vs, ref index, 1); SInt28 size = ByteTools.BytesToSInt28(vs, ref index, true); //Set Important Flags async = flags.Get(0); if (ver[0] == 0x00) { } else if (ver[0] == 0x00) { bool ext = flags.Get(1); } else if (ver[0] == 0x04) { bool ext = flags.Get(1); bool foot = flags.Get(3); bool upd = false; bool crc = false; SInt35 crcd = 0; bool rst = false; byte rstd = 0; if (ext) { SInt28 esize = ByteTools.BytesToSInt28(vs, ref index, true); BitArray extFlags = ByteTools.BytesToBitArray(vs, ref index, 1); upd = extFlags.Get(1); crc = extFlags.Get(2); rst = extFlags.Get(3); if (upd) { index++; } if (crc) { index++; crcd = ByteTools.BytesToSInt35(vs, ref index, true); } if (rst) { index++; rstd = vs[index]; index++; /* * rstd layout %ppqrrstt * p - Tag size restrictions * 00 No more than 128 frames and 1 MB total tag size. * 01 No more than 64 frames and 128 KB total tag size. * 10 No more than 32 frames and 40 KB total tag size. * 11 No more than 32 frames and 4 KB total tag size. * q - Text encoding restrictions * 0 No restrictions * 1 Strings are only encoded with ISO-8859-1 [ISO-8859-1] or UTF-8 [UTF-8]. * r - Text fields size restrictions * 00 No restrictions * 01 No string is longer than 1024 characters. * 10 No string is longer than 128 characters. * 11 No string is longer than 30 characters. * s - Image encoding restrictions * 0 No restrictions * 1 Images are encoded only with PNG [PNG] or JPEG [JFIF]. * t - Image size restrictions * 00 No restrictions * 01 All images are 256x256 pixels or smaller. * 10 All images are 64x64 pixels or smaller. * 11 All images are exactly 64x64 pixels, unless required otherwise. */ } } } else { throw new FileLoadException("ID3v2." + ID3Ver + " Not Supported"); } if (ver[0] == 0x03 || ver[0] == 0x04) { frames = new ArrayList(); while (vs[index] != 0xFF && (vs[index + 1] & 0b1110) != 0) { //Read Frame Header ID3Frames frame = new ID3Frames(); frame.ID = ByteTools.BytesToString(vs, ref index, 4); frame.frameSize = ByteTools.BytesToSInt28(vs, ref index, true); frame.frameFlags = ByteTools.BytesToBitArray(vs, ref index, 2); //Read Frame Data byte encoding = vs[index++]; frame.frameSize = frame.frameSize - 1; switch (encoding) { case 0: frame.disenc = "ISO-8859-1"; frame.txt = ByteTools.BytesToString(vs, ref index, (int)(uint)frame.frameSize, Encoding.GetEncoding("ISO-8859-1")); break; case 1: frame.disenc = "UTF-16"; frame.txt = ByteTools.BytesToString(vs, ref index, (int)(uint)frame.frameSize, Encoding.Unicode); break; case 2: frame.disenc = "UTF-16BE"; frame.txt = ByteTools.BytesToString(vs, ref index, (int)(uint)frame.frameSize, Encoding.BigEndianUnicode); break; case 3: frame.disenc = "UTF-8"; frame.txt = ByteTools.BytesToString(vs, ref index, (int)(uint)frame.frameSize, Encoding.UTF8); break; } //Add Data to Frame Collection frames.Add(frame); //Report Data Collected Console.WriteLine("Frame ID: " + frame.ID); Console.WriteLine("Frame Size: " + (uint)frame.frameSize); Console.Write("Flags: "); foreach (bool bit in frame.frameFlags) { Console.Write(bit ? "1" : "0"); } Console.WriteLine("\nText Encoding Type: " + frame.disenc); Console.WriteLine("Text Data: " + frame.txt + "\n"); } } }