Пример #1
0
        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);
        }