/* This takes one of our sound data files, as produced by verifywave.c, read from standard input, and repacks the data into a .WAV file. The restriction is that the file length be no longer than 110250 samples long---that is, 10 seconds of sound. The .WAV file is written to stdout, so you have to redirect the output. */ #include #include main() { float fpdata[110250],nextval; int i,j,numsamples; char nextbyte; /* read the data from the file and load it into the array */ for(i=0;i<110250;i++) { if(scanf("%d%g",&j,&nextval)==EOF) break; fpdata[i]=nextval; } numsamples=i; /* Now write the header */ printf("RIFF"); /* Now do the total length of the package, which will be numsamples+36 remaining bytes. */ nextbyte=(numsamples+36)%256; fputc(nextbyte,stdout); nextbyte=((numsamples+36)/256)%256; fputc(nextbyte,stdout); nextbyte=((numsamples+36)/256)/256; fputc(nextbyte,stdout); nextbyte=0; fputc(nextbyte,stdout); printf("WAVE"); /* Now do the format chunk */ printf("fmt "); /* length of format chunk in next 4 bytes, always 16 */ nextbyte=16; fputc(nextbyte,stdout); nextbyte=0; fputc(nextbyte,stdout); fputc(nextbyte,stdout); fputc(nextbyte,stdout); /* next two bytes are always the same */ nextbyte=1; fputc(nextbyte,stdout); nextbyte=0; fputc(nextbyte,stdout); /* Next two bytes give channel number: 1 for mono.*/ nextbyte=1; fputc(nextbyte,stdout); nextbyte=0; fputc(nextbyte,stdout); /* The next four bytes give the sample rate in hz. We'll do 11025 hz. */ nextbyte=11025%256; fputc(nextbyte,stdout); nextbyte=11025/256; fputc(nextbyte,stdout); nextbyte=0; fputc(nextbyte,stdout); fputc(nextbyte,stdout); /* The next four bytes are the number of bytes per second, which in this case (8-bit mono) is the same. */ nextbyte=11025%256; fputc(nextbyte,stdout); nextbyte=11025/256; fputc(nextbyte,stdout); nextbyte=0; fputc(nextbyte,stdout); fputc(nextbyte,stdout); /* The next four bytes say 1 byte per sample, 8 bits per sample */ nextbyte=1; fputc(nextbyte,stdout); nextbyte=0; fputc(nextbyte,stdout); nextbyte=8; fputc(nextbyte,stdout); nextbyte=0; fputc(nextbyte,stdout); printf("data"); /* The next four bytes contain the number of samples*/ nextbyte=numsamples%256; fputc(nextbyte,stdout); nextbyte=(numsamples/256)%256; fputc(nextbyte,stdout); nextbyte=(numsamples/256)/256; fputc(nextbyte,stdout); nextbyte=0; fputc(nextbyte,stdout); /* For the remaining bytes, we convert the floating-point data back to one-byte samples.*/ for(i=0;i