Created
December 14, 2012 09:49
-
-
Save xVir/4284169 to your computer and use it in GitHub Desktop.
WAV file header writer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WaveHeaderWriter | |
{ | |
static byte[] RIFF_HEADER = new byte[] { 0x52, 0x49, 0x46, 0x46 }; | |
static byte[] FORMAT_WAVE = new byte[] { 0x57, 0x41, 0x56, 0x45 }; | |
static byte[] FORMAT_TAG = new byte[] { 0x66, 0x6d, 0x74, 0x20 }; | |
static byte[] AUDIO_FORMAT = new byte[] { 0x01, 0x00 }; | |
static byte[] SUBCHUNK_ID = new byte[] { 0x64, 0x61, 0x74, 0x61 }; | |
private const int BYTES_PER_SAMPLE = 2; | |
public static void WriteHeader( | |
System.IO.Stream targetStream, | |
int byteStreamSize, | |
int channelCount, | |
int sampleRate) | |
{ | |
int byteRate = sampleRate * channelCount * BYTES_PER_SAMPLE; | |
int blockAlign = channelCount * BYTES_PER_SAMPLE; | |
targetStream.Write(RIFF_HEADER, 0, RIFF_HEADER.Length); | |
targetStream.Write(PackageInt(byteStreamSize + 36, 4), 0, 4); | |
targetStream.Write(FORMAT_WAVE, 0, FORMAT_WAVE.Length); | |
targetStream.Write(FORMAT_TAG, 0, FORMAT_TAG.Length); | |
targetStream.Write(PackageInt(16, 4), 0, 4);//Subchunk1Size | |
targetStream.Write(AUDIO_FORMAT, 0, AUDIO_FORMAT.Length);//AudioFormat | |
targetStream.Write(PackageInt(channelCount, 2), 0, 2); | |
targetStream.Write(PackageInt(sampleRate, 4), 0, 4); | |
targetStream.Write(PackageInt(byteRate, 4), 0, 4); | |
targetStream.Write(PackageInt(blockAlign, 2), 0, 2); | |
targetStream.Write(PackageInt(BYTES_PER_SAMPLE * 8), 0, 2); | |
//targetStream.Write(PackageInt(0,2), 0, 2);//Extra param size | |
targetStream.Write(SUBCHUNK_ID, 0, SUBCHUNK_ID.Length); | |
targetStream.Write(PackageInt(byteStreamSize, 4), 0, 4); | |
} | |
static byte[] PackageInt(int source, int length = 2) | |
{ | |
if ((length != 2) && (length != 4)) | |
throw new ArgumentException("length must be either 2 or 4", "length"); | |
var retVal = new byte[length]; | |
retVal[0] = (byte)(source & 0xFF); | |
retVal[1] = (byte)((source >> 8) & 0xFF); | |
if (length == 4) | |
{ | |
retVal[2] = (byte)((source >> 0x10) & 0xFF); | |
retVal[3] = (byte)((source >> 0x18) & 0xFF); | |
} | |
return retVal; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment