Esempio n. 1
0
        /// <summary>Writes the WAV file header.</summary>
        private void WriteHeader()
        {
            // swap byte order if necessary
            WavHeader hdrTemp = _header;

            _endian.Swap32(ref hdrTemp.Riff.PackageLength);
            _endian.Swap32(ref hdrTemp.Format.FormatLen);
            _endian.Swap16(ref hdrTemp.Format.Fixed);
            _endian.Swap16(ref hdrTemp.Format.ChannelNumber);
            _endian.Swap32(ref hdrTemp.Format.SampleRate);
            _endian.Swap32(ref hdrTemp.Format.ByteRate);
            _endian.Swap16(ref hdrTemp.Format.BytePerSample);
            _endian.Swap16(ref hdrTemp.Format.BitsPerSample);
            _endian.Swap32(ref hdrTemp.Data.DataLen);
            _endian.Swap32(ref hdrTemp.Fact.FactLength);
            _endian.Swap32(ref hdrTemp.Fact.FactSampleLength);

            // write the supplemented header in the beginning of the file
            _fileStream.Seek(0, SeekOrigin.Begin);

            int size = Marshal.SizeOf(hdrTemp);
            var data = new byte[size];

            GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);

            try
            {
                Marshal.StructureToPtr(hdrTemp, handle.AddrOfPinnedObject(), false);
            }
            finally
            {
                handle.Free();
            }
            _fileStream.Write(data, 0, size);

            // jump back to the end of the file
            _fileStream.Seek(0, SeekOrigin.End);
        }
Esempio n. 2
0
        /// <summary>Read WAV file headers.</summary>
        /// <returns>zero if all ok, nonzero if file format is invalid.
        /// </returns>
        private int ReadWavHeaders()
        {
            _header = new WavHeader();

            int res = ReadRiffBlock();

            if (res != 0)
            {
                return(-1);
            }
            // read header blocks until data block is found
            do
            {
                // read header blocks
                res = ReadHeaderBlock();
                if (res < 0)
                {
                    return(-1);         // error in file structure
                }
            } while (res == 0);
            // check that all required tags are legal
            return(CheckCharTags());
        }