GAudio.GATWavHelper.GetHeader C# (CSharp) Method

GetHeader() public static method

Returns the header for a 16 bit uncompressed wav file.
public static GetHeader ( int numChannels, int sampleRate, int numBytes ) : byte[]
numChannels int
sampleRate int
numBytes int
return byte[]
        public static byte[] GetHeader( int numChannels, int sampleRate, int numBytes )
        {
            byte[] header;
            int val;
            int offset;

            header = new byte[ 44 ];
            Buffer.BlockCopy( __canonicalHeader, 0, header, 0, 44 );

            // Chunk size
            offset 	= 4;
            val 	= numBytes - 8; //Chunk size
            Buffer.BlockCopy( BitConverter.GetBytes( ( Int32 )val ), 0, header, offset, 4 );

            // SubChunk1 size
            offset 	= 16;
            val 	= 16;
            Buffer.BlockCopy( BitConverter.GetBytes( ( Int32 )val ), 0, header, offset, 4 );

            // Format 1
            offset 	= 20;
            val 	= 1;
            Buffer.BlockCopy( BitConverter.GetBytes( ( Int16 )val ), 0, header, offset, 2 );

            // NumChannels
            offset 	= 22;
            val 	= numChannels;
            Buffer.BlockCopy( BitConverter.GetBytes( ( Int16 )val ), 0, header, offset, 2 );

            // SampleRate
            offset 	= 24;
            val 	= sampleRate;
            Buffer.BlockCopy( BitConverter.GetBytes( ( Int32 )val ), 0, header, offset, 4 );

            // ByteRate
            offset 	= 28;
            val 	= sampleRate * numChannels * 16 / 8;
            Buffer.BlockCopy( BitConverter.GetBytes( ( Int32 )val ), 0, header, offset,4 );

            // BlockAlign
            offset 	= 32;
            val 	= numChannels * 2;
            Buffer.BlockCopy( BitConverter.GetBytes( ( Int16 )val ), 0, header, offset, 2 );

            // BitDepth
            offset 	= 34;
            val 	= 16;
            Buffer.BlockCopy( BitConverter.GetBytes( ( Int16 )val ), 0, header, offset, 2 );

            // Data size
            offset 	= 40;
            val 	= numBytes - headerSize; //Chunk size
            Buffer.BlockCopy( BitConverter.GetBytes( ( Int32 )val ), 0, header, offset, 4 );

            return header;
        }

Usage Example

Esempio n. 1
0
        void AsyncWriteLoop()
        {
            _fs.Seek(GATWavHelper.headerSize, SeekOrigin.Begin);

            int sleepMS;
            int receivedFrames = 0;

            sleepMS = ( int )(GATInfo.AudioBufferDuration * 2000 / 3);

            while (_vDoWrite)
            {
                receivedFrames = _vReceivedFrames;                 // cache volatile locally
                if (receivedFrames >= _writtenFrames + WriteChunkFrames)
                {
                    ConvertAndWriteChunk(WriteChunkFrames);
                }
                else
                {
                    Thread.Sleep(sleepMS);
                }
            }

            while (receivedFrames > _writtenFrames)
            {
                if (receivedFrames < WriteChunkFrames)
                {
                    ConvertAndWriteChunk(receivedFrames - _writtenFrames);
                }
                else
                {
                    ConvertAndWriteChunk(WriteChunkFrames);
                }
            }

            byte[] header = null;

            header = GATWavHelper.GetHeader(_numChannels, GATInfo.OutputSampleRate, ( int )_fs.Length);

            _fs.Seek(0, SeekOrigin.Begin);

            _fs.Write(header, 0, header.Length);

            _fs.Close();
        }