#include /* Write an WAVE sound file header */ struct { unsigned long nsamples; int NumChannels; unsigned long SampleRate; unsigned long BytesPerSec; int BlockAlign; int BitsPerSample; } fmt; void Write_Wave_Header(FILE *fptr) { unsigned long totalsize; /* Write the form chunk */ fprintf(fptr,"RIFF"); totalsize = fmt.nsamples + 36; fputc((totalsize & 0x000000ff),fptr); /* File size */ fputc((totalsize & 0x0000ff00) >> 8,fptr); fputc((totalsize & 0x00ff0000) >> 16,fptr); fputc((totalsize & 0xff000000) >> 24,fptr); fprintf(fptr,"WAVE"); fprintf(fptr,"fmt "); /* fmt_ chunk */ fputc(16,fptr); /* Chunk size */ fputc(0,fptr); fputc(0,fptr); fputc(0,fptr); fputc(1,fptr); /* Format tag - PCM = 1 */ fputc(0,fptr); fputc(fmt.NumChannels,fptr); /* Channels */ fputc(0,fptr); fputc((fmt.SampleRate & 0x000000ff),fptr); /* Sample Rate (Hz) */ fputc((fmt.SampleRate & 0x0000ff00) >> 8,fptr); fputc((fmt.SampleRate & 0x00ff0000) >> 16,fptr); fputc((fmt.SampleRate & 0xff000000) >> 24,fptr); fputc((fmt.BytesPerSec & 0x000000ff),fptr); /* Average bytes per second */ fputc((fmt.BytesPerSec & 0x0000ff00) >> 8,fptr); fputc((fmt.BytesPerSec & 0x00ff0000) >> 16,fptr); fputc((fmt.BytesPerSec & 0xff000000) >> 24,fptr); fputc(fmt.BlockAlign,fptr); /* Block alignment */ fputc(0,fptr); fputc(fmt.BitsPerSample,fptr); /* Bits per sample */ fputc(0,fptr); fprintf(fptr,"data"); fputc((fmt.nsamples & 0x000000ff),fptr); /* Data size */ fputc((fmt.nsamples & 0x0000ff00) >> 8,fptr); fputc((fmt.nsamples & 0x00ff0000) >> 16,fptr); fputc((fmt.nsamples & 0xff000000) >> 24,fptr); } main(int argc, char *argv[]) { FILE *out; if (argc < 5) { fprintf(stderr,"Invalid number of arguments ..\n"); fprintf(stderr,"Usage: %s sampl chan rate bits\n", argv[0]); fprintf(stderr,"sampl = number of samples\n"); fprintf(stderr,"chan = number of channels\n"); fprintf(stderr,"rate = sampling rate\n"); fprintf(stderr,"bits = bits per sample\n"); exit(1); } /* Initialize the format structure */ fmt.nsamples = atol(argv[1]); /* Number of voice samples */ fmt.NumChannels = atoi(argv[2]); /* Channels: mono=1, stereo=2 */ fmt.SampleRate = atoi(argv[3]); /* Sampling rate in Hz */ fmt.BitsPerSample = atoi(argv[4]); /* Bits per sample */ /* BytesPerSec = Average bytes per second */ /* BytesPerSec = SampleRate * NumChannels * BitsPerSample/8 */ fmt.BytesPerSec = (fmt.SampleRate * fmt.NumChannels * fmt.BitsPerSample) / 8; /* BlockAlign = block alignment */ /* BlockAlign = NumChannels * BitsPerSample/8 */ fmt.BlockAlign = (fmt.NumChannels * fmt.BitsPerSample) / 8; out = fopen("xxx.wav", "w"); Write_Wave_Header(out); fclose(out); exit(0); }