A C program fails to generate a simple WAV file

I don’t know what would be a better forum to ask this question, so I’ll ask it here. I’ve tried to make a C program which will output a WAV file containing the octave, each note lasting one second. However, for some reason, which I haven’t been able to find for hours now, the WAV file won’t open.

#include <stdint.h>
#include <stdio.h>
#include <math.h>

int main() {
	FILE *wav=fopen("example.wav","wb");
	fprintf(wav,"RIFF");
	const int sampleRate=8192;
	int32_t ChunkSize=36+8*sampleRate*2;
	fwrite(&ChunkSize,4,1,wav);
	fprintf(wav,"WAVEfmt");
	int32_t Subchunk1Size=16; //PCM header is always 16 bytes
	fwrite(&Subchunk1Size,4,1,wav);
	int16_t AudioFormat=1; //PCM
	fwrite(&AudioFormat,2,1,wav);
	int16_t NumChannels=1; //MONO audio.
	fwrite(&NumChannels,2,1,wav);
	int32_t SampleRate=sampleRate;
	fwrite(&SampleRate,4,1,wav);
	int32_t ByteRate=2*sampleRate;
	fwrite(&ByteRate,4,1,wav);
	int16_t BlockAlign=2;
	fwrite(&BlockAlign,2,1,wav);
	int16_t BitsPerSample=16;
	fwrite(&BitsPerSample,2,1,wav);
	fprintf(wav,"data");
	int32_t Subchunk2Size=ChunkSize-36;
	for (int i=0; i<8*sampleRate; i++) {
		float currentFrequency;
		if (i<=sampleRate)
			currentFrequency=262; //The C tone is 262Hz.
		else if (i>sampleRate && i<=2*sampleRate)
			currentFrequency=294; //The D tone is 294Hz.
		else if (i>2*sampleRate && i<=3*sampleRate)
			currentFrequency=330; //The E tone is 330Hz.
		else if (i>3*sampleRate && i<=4*sampleRate)
			currentFrequency=349; //The F tone.
		else if (i>4*sampleRate && i<=5*sampleRate)
			currentFrequency=391; //The G tone.
		else if (i>5*sampleRate && i<=6*sampleRate)
			currentFrequency=440; //The A tone.
		else if (i>6*sampleRate && i<=7*sampleRate)
			currentFrequency=494;  //The H sound.
		else
			currentFrequency=523; //The Tenor C.
		float baseFrequency=sin(2*M_PI*currentFrequency*i/sampleRate)*16384;
		float secondHarmony=sin(2*M_PI*2*currentFrequency*i/sampleRate)*4096;
		float thirdHarmony=sin(2*M_PI*3*currentFrequency*i/sampleRate)*41024;
		float currentAmplitude=baseFrequency+secondHarmony+thirdHarmony*exp(-(float)(i%sampleRate+2000)/2000); 
		int16_t numberToBeWritten=currentAmplitude;
		fwrite(&numberToBeWritten,2,1,wav);
	}
	fclose(wav);
}

Can you guess what’s going on here? I can’t.

Solved it myself. Here is the correct program:

#include <stdint.h>
#include <stdio.h>
#include <math.h>

int main() {
	FILE *wav=fopen("example.wav","wb");
	fprintf(wav,"RIFF");
	const int sampleRate=8192;
	int32_t ChunkSize=36+8*sampleRate*2;
	fwrite(&ChunkSize,4,1,wav);
	fprintf(wav,"WAVEfmt "); //This line has been changed!
	int32_t Subchunk1Size=16; //PCM header is always 16 bytes
	fwrite(&Subchunk1Size,4,1,wav);
	int16_t AudioFormat=1; //PCM
	fwrite(&AudioFormat,2,1,wav);
	int16_t NumChannels=1; //MONO audio.
	fwrite(&NumChannels,2,1,wav);
	int32_t SampleRate=sampleRate;
	fwrite(&SampleRate,4,1,wav);
	int32_t ByteRate=2*sampleRate;
	fwrite(&ByteRate,4,1,wav);
	int16_t BlockAlign=2;
	fwrite(&BlockAlign,2,1,wav);
	int16_t BitsPerSample=16;
	fwrite(&BitsPerSample,2,1,wav);
	fprintf(wav,"data");
	int32_t Subchunk2Size=ChunkSize-36;
	for (int i=0; i<8*sampleRate; i++) {
		float currentFrequency;
		if (i<=sampleRate)
			currentFrequency=262; //The C tone is 262Hz.
		else if (i>sampleRate && i<=2*sampleRate)
			currentFrequency=294; //The D tone is 294Hz.
		else if (i>2*sampleRate && i<=3*sampleRate)
			currentFrequency=330; //The E tone is 330Hz.
		else if (i>3*sampleRate && i<=4*sampleRate)
			currentFrequency=349; //The F tone.
		else if (i>4*sampleRate && i<=5*sampleRate)
			currentFrequency=391; //The G tone.
		else if (i>5*sampleRate && i<=6*sampleRate)
			currentFrequency=440; //The A tone.
		else if (i>6*sampleRate && i<=7*sampleRate)
			currentFrequency=494;  //The H sound.
		else
			currentFrequency=523; //The Tenor C.
		float baseFrequency=sin(2*M_PI*currentFrequency*i/sampleRate)*16384;
		float secondHarmony=sin(2*M_PI*2*currentFrequency*i/sampleRate)*4096;
		float thirdHarmony=sin(2*M_PI*3*currentFrequency*i/sampleRate)*41024;
		float currentAmplitude=baseFrequency+secondHarmony+thirdHarmony*exp(-(float)(i%sampleRate+2000)/2000); 
		int16_t numberToBeWritten=currentAmplitude;
		fwrite(&numberToBeWritten,2,1,wav);
	}
	fclose(wav);
}

Basically, I forgot to put the space character somewhere, and the program still compiled, but it didn’t do what it was supposed to do.

In case it is of any interest to you, it can be done very easily using Nyquist.

In the Nyquist Prompt, run this code:

;type generate
(setf notes (list C4 D4 E4 F4 G4 A4 B4 C5))
(seqrep (pitch (length notes))
        (osc (nth pitch notes)))