示例#1
0
        /// <summary>
        /// FIX - This has a couple of bugs, making the resultant WAV file incorrect without hand-tuning it:
        /// 1.) The serialization includes the text class information,
        /// 2.) The file size comes out incorrect.
        /// </summary>
        /// <returns></returns>
        private bool WriteWAVFileHeader()
        {
            bool       bRet = true, bRes;
            string     sFile;
            FileInfo   fiFile;
            FileStream fsFile;
            int        iLen;

            byte[]          abFile;
            WavHeader       whFile = null;
            BinaryFormatter formatter;

            Console.WriteLine("Write a WAV file header.");
            Console.Write("Filename:  ");
            sFile = Console.ReadLine();

            if (!File.Exists(sFile))
            {
                bRet = false;
                Console.Error.WriteLine("Invalid filename '{0}'.", sFile);
            }
            else
            {
                // Read in file
                fiFile = new FileInfo(sFile);
                fsFile = fiFile.OpenRead();
                iLen   = (int)(fiFile.Length);
                abFile = new byte[iLen];
                fsFile.Read(abFile, 0, iLen);
                fsFile.Close();

                // Create header
                whFile = new WavHeader();
                bRes   = whFile.Set(iLen);
                fsFile = fiFile.OpenWrite();

                // Write out header and binary data
                formatter = new BinaryFormatter();
                formatter.Serialize(fsFile, whFile);
                fsFile.Write(abFile, 0, iLen);

                // Clean up
                fsFile.Close();
                abFile = null;                          // Flag for GC cleanup.
            }

            return(bRet);
        } // WriteWAVFileHeader
示例#2
0
        private bool ReadWAVFileHeader()
        {
            bool       bRet = true, bRes;
            string     sFile;
            FileInfo   fiFile;
            FileStream fsFile;
            int        iLen;

            byte[]    abFile;
            WavHeader whFile = null;

            Console.WriteLine("Read a WAV file header.");
            Console.Write("Filename:  ");
            sFile = Console.ReadLine();

            if (!File.Exists(sFile))
            {
                bRet = false;
                Console.Error.WriteLine("Invalid filename '{0}'.", sFile);
            }
            else
            {
                fiFile = new FileInfo(sFile);
                fsFile = fiFile.OpenRead();
                iLen   = (int)(fiFile.Length);
                abFile = new byte[iLen];
                fsFile.Read(abFile, 0, iLen);
                fsFile.Close();

                whFile = new WavHeader();
                bRes   = whFile.Extract(abFile);
                bRes   = whFile.Valid();

                Console.WriteLine("{0}", whFile);       // NOTE - until WavHeader.ToString() is overridden, this will display the type name, not the contents of the header.

                abFile = null;                          // Flag for GC cleanup.
            }

            return(bRet);
        }