/*
 * This program calculates the frequencies of any arbitrary equally
 * tempered scale and lists them, starting with an initial value and ending
 * with the highest calculated value under 20kHz.
 * Command:
 * freq tpo sfreq	where:
 *			tpo = tones per octave
 *			sfreq = starting frequency
 * Compiled by: cc -o freq freq.c -lm
 * Written by Bill Fox for the MMML, 8/31/1989.
 */
#include <math.h>
main(argc, argv)
int	argc;
char	**argv;
{
double	pow(), atof(), freq, root_of_2, init_freq;
int	i, tpo;

if ( argc < 3 )
	{
	printf("Usage:\tfreq tpo sfreq\nwhere:\n\ttpo = tones per octave\n");
	printf("\tsfreq = starting frequency.\n");
	exit (1);
	}

printf("\tEQUALLY TEMPERED SCALE - FREQUENCY CHART GENERATOR\n\n");
printf("\t\tTones per octave   = %s\n", argv[1]);
printf("\t\tStarting frequency = %s Hz\n\n", argv[2]);

tpo = atoi(argv[1]);
init_freq = atof(argv[2]);

if ( ( init_freq <= 0 ) || ( init_freq >= 20000 ) )
	{
	printf("Your starting frequency is an illegal value.\n");
	printf("It must be between 0 and 20000 Hertz.\n\n");
	exit (1);
	}

/*
 * The (tpo)th root of two:
 *
 * root_of_2 = double pow (2,1/tpo)
 */
root_of_2 = pow (  2.0, ( 1.0 / (double)tpo )  );
/*
printf("The %dth root of 2 = %7.6f\n", tpo, root_of_2);
*/

printf("\t\t  Arbitrary\tFrequency\n");
printf("\t\tScale  Degree\t  in Hz\n");
printf("\t\t-------------\t---------\n");
for ( i = 1, freq = init_freq; freq < 20000.0; ++i, freq *= root_of_2)
	{
	if ( i > tpo )
		i = 1;
	printf("\t\t\t%d\t%7.1f\n", i, freq);
	}
exit (0);
}
