/*
         * WaveFileFormat ReadMainFileHeader - 2004 July 28
         * Read in the main file header.  Not much more to say, really.
         * For XML serialization purposes, I "correct" the dwFileLength
         * field to describe the whole file's length.
         */

        public riffChunk ReadMainFileHeader()
        {
            mainfile = new riffChunk();

            mainfile.sGroupID     = new string(reader.ReadChars(4));
            mainfile.dwFileLength = reader.ReadUInt32() + 8;
            mainfile.sRiffType    = new string(reader.ReadChars(4));
            return(mainfile);
        }
        public WaveFile(string filename)
        {
            reader            = new WaveFileReader(filename);
            maindata          = reader.ReadMainFileHeader();
            maindata.FileName = filename;
            long   chunkSize = 0;
            string chunkName = null;

            while (chunkName != "data" && reader.GetPosition() < (long)maindata.dwFileLength && reader.GetPosition() + chunkSize < maindata.dwFileLength)
            {
                chunkName = reader.GetChunkName();
                switch (chunkName)
                {
                case "fmt ":

                    format    = reader.ReadFormatHeader();
                    chunkSize = format.dwChunkSize;
                    break;

                case "fact":

                    fact      = reader.ReadFactHeader();
                    chunkSize = fact.dwChunkSize;
                    break;

                case "data":

                    data      = reader.ReadDataHeader();
                    chunkSize = data.dwChunkSize;
                    break;

                default:
                    //This provides the required skipping of unsupported chunks.
                    // reader.AdvanceToNext();

                    chunkSize = 0;
                    break;
                }
            }
        }