// Generate 29 consecutive notes in ulaw format, starting with A (440 Hz)

#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

FILE *outfile;
short value;
double alpha=440,beta,maxvol=10000,timeStep=0.000125; // 8000 samples/sec
char buffer[20],command[200];
int tonelength=3500;
int i,j;

double wave(int i,double freq) // Simple square waveform
{
  if (((int)(i*freq*timeStep*2.0))&1)
    return(maxvol);
  else
    return(-maxvol);
}

main()
{
  for (j=0;j<29;j++)
    {
      beta=alpha*pow(2.0,((double)j / 12.0)); // Compute frequency
      outfile=fopen("tempnote.raw","w");
      for (i=0;i<tonelength;i++)
	{
	  value=(short) (wave(i,beta)*exp(-0.001*i)); // Exponential decay
	  fwrite(&value,sizeof(value),1,outfile); // Write raw sound data
	}
      fclose(outfile);


      sprintf(buffer,"note%0d.au",j); // Convert to ulaw format
      sprintf(command,"cat tempnote.raw | audioconvert -F -i rate=8000,channel=mono,encoding=linear16,format=raw -f encoding=ulaw,format=sun -o %s -",buffer);
      system(command);
    }
}
