#include <stdlib.h>
#include <stdio.h>
#include <math.h>
/* COSWAVE.C - Generates a cosine wave: 8000 samples, 1 s, 200 Hz
*/
/* (when played at 8000 samples/s), called cosine.dat
*/
void main()
{
int length, status, i;
short int *x;
/* a pointer to the array of samples */
float freq;
double arg, twopi;
FILE *file_id;
length = 8000;
freq = 0.025;
twopi = 8.0*atan(1.0);
/* calculate 2*PI */
arg = twopi * freq;
/* allocate the sample array and set its cells to zero
*/
x = (short int *) calloc(length,sizeof(short int));
if(!x) {
printf("Unable to allocate space
for samples\n");
exit(1);
}
/* Loop over the length of the array of samples
*/
for (i = 0 ; i < length ; i++)
x[i] = (short int) 32000 * cos(i*arg);
file_id = fopen("cosine.dat","wb");
if(file_id == NULL) {
printf("Unable to open file\n");
exit(1);
}
status = fwrite(x,sizeof(short int),length,file_id);
if(status < length) {
printf("Unable to write all samples\n");
exit(1);
}
fclose(file_id);
}