Sean O'Connor's
 Windows Games
 
Conquer the island
Slay
Board game
Conquest
Command a squadron of spaceships
Critical Mass
Board game of bluffing
The General
Conquer the world
Mother Of All Battles
Real time WWII battle
Firefight
Capture The Flag
Capture The Flag
A card game
Niggle
Lead your tribe to safety
End Of Atlantis
Play in the Jurassic League
Football-o-
saurus

Kill all of the rats
Rats!
Play Foosball
Foosball
Collect the crystals
UFOs

The Games
Slay
Conquest
Critical Mass
The General
Mother Of All Battles
Firefight
Capture The Flag
Niggle
End Of Atlantis
Football-o-saurus
Rats!
Foosball
UFOs

Info
arrowLatest news
arrowDownload free versions
arrowBuy full versions
arrowHistory of the games
arrowLink to this site
arrowBecome an affiliate
arrowOther sites
arrowUseful code
arrowThanks to...

Contact
arrowForums
arrowJoin mailing list
arrowEmail me

About Sean O'Connor
arrowCV
arrowOther Projects
arrowPhotos

Creating a WAV file

Here is all the code you need to create, save and play WAV files. It shows the format used by WAV files and all you need to do is to create the WAV data itself for the sounds you want (eg a sine wave etc...)
			
		
#define MAX_LENGTH	32700

// this is the WAV structure with the length of each part commented in bytes
typedef struct
{
	BYTE	RIFF1;			// 1		// 8
	BYTE	RIFF2;			// 1
	BYTE	RIFF3;			// 1
	BYTE	RIFF4;			// 1
	DWORD	RIFFSize;		// 4

	BYTE	WAVE1;			// 1		// 4
	BYTE	WAVE2;			// 1
	BYTE	WAVE3;			// 1
	BYTE	WAVE4;			// 1

	BYTE	FMT1;			// 1		// 8
	BYTE	FMT2;			// 1
	BYTE	FMT3;			// 1
	BYTE	FMT4;			// 1
	DWORD	FMTSize;		// 4

	WORD	FormatTag;		// 2		// 16		 		
	WORD	Channels;		// 2
	DWORD	SamplesPerSec;	// 4
	DWORD	AvgBytesPerSec;	// 4
	WORD	BlockAlign;		// 2
	WORD	BitsPerSample;	// 2

	BYTE	DATA1;			// 1		// 8
	BYTE	DATA2;			// 1
	BYTE	DATA3;			// 1
	BYTE	DATA4;			// 1
	DWORD	DATASize;		// 4

	BYTE	data[MAX_LENGTH];
} WAVE;
		
	// this is how you initialize a WAV file's data
	WAVE wave;
					
	wave.DATASize = 2000;				// or choose whatever size you want (up to MAX_LENGTH)

	wave.RIFF1 = 'R';
	wave.RIFF2 = 'I';
	wave.RIFF3 = 'F';
	wave.RIFF4 = 'F';
	wave.RIFFSize = wave.DATASize+36;

	wave.WAVE1 = 'W';
	wave.WAVE2 = 'A';
	wave.WAVE3 = 'V';
	wave.WAVE4 = 'E';

	wave.FMT1 = 'f';
	wave.FMT2 = 'm';
	wave.FMT3 = 't';
	wave.FMT4 = ' ';
	wave.FMTSize = 16;

	wave.FormatTag = 1;
	wave.Channels = 1;
	wave.SamplesPerSec = 11025; 		// or choose 22050;
	wave.AvgBytesPerSec = 11025; 		// or choose 22050;
	wave.BlockAlign = 1;
	wave.BitsPerSample = 8;

	wave.DATA1 = 'd';
	wave.DATA2 = 'a';
	wave.DATA3 = 't';
	wave.DATA4 = 'a';
		
	// now write your WAV data to wave.data[]


	// this writes the WAV to a file		
	hFile = CreateFile(szFilename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 

	if (hFile != NULL)
	{
		// write the header
		WriteFile(hFile, &wave, 8+4+8+16+8, &dwFileSize, NULL);
		
		// write the data
		WriteFile(hFile, &wave.data, wave.DATASize, &dwFileSize, NULL);
	
		// close up
		CloseHandle(hFile);
	}
		
		
	// you can play a WAV file using
	sndPlaySound(szFilename, SND_ASYNC);