Code:
a440.c
======
#include <stdio.h>
#ifdef BORLAND
#include <stdlib.h>
#endif
#include <math.h>
char hdr[]={'R', 'I', 'F', 'F', 0, 0, 0, 0, 'W', 'A', 'V', 'E', 'f', 'm', 't', 32, 16, 0, 0, 0, 1, 0, 1, 0, 17, '+', 0, 0, 17, '+', 0, 0, 1, 0, 8, 0, 'd', 'a', 't', 'a', 0, 0, 0, 0};
FILE * f ;
main(argc,argv)
int argc;
char**argv;
{
#define SAMPLES 600L
#define FREQ 440
#define SPS 11025
#define PI 3.1415927
long i ;
float w, x ;
if ( argc < 2 )
{
fprintf( stderr, "Usage is '%s filename'\n", argv[0] ) ;
exit( -1 ) ;
}
if ( !( f = fopen( argv[1], "wb" ) ) )
{
fprintf( stderr, "Can't open '%s' to write\n", argv[1] ) ;
exit( -2 ) ;
}
w = 2 * PI * FREQ / SPS ;
i = SAMPLES + 36 ;
hdr[4] = i % 256 ;
hdr[5] = ( i / 256 ) % 256 ;
hdr[6] = ( i / 65536L ) % 256 ;
hdr[7] = i / 16777216L ;
i = SAMPLES ;
hdr[40] = i % 256 ;
hdr[41] = ( i / 256 ) % 256 ;
hdr[42] = ( i / 65536L ) % 256 ;
hdr[43] = i / 16777216L ;
for( i = 0L ; i < sizeof(hdr) ; i++ )
{
putc( hdr[i], f ) ;
}
i = 0 ;
while ( ++i <= SAMPLES )
{
putc( (char)( 128.0 + 127.999 * sin( w * i ) ), f ) ;
}
fclose( f ) ;
exit( 0 ) ;
}