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

double skok(double origin,int ofset)
{
  double chrom;
  int sign;

  chrom=pow(2.,1./12.);
  if (ofset <= 0) {
    while (ofset++ != 0) origin/=chrom;
  } else
  {
    while (ofset-- != 0) origin*=chrom;
  }

  return origin;
}

double skok_presny(double origin, int oktava, int ofset)
{
  double chrom;
  int sign;

  chrom=pow(2.,1./12.);
  if (oktava <= 2) {
    while (oktava++ != 2) origin/=2.;
  } else
  {
    while (oktava-- != 2) origin*=2.;
  }
  while (ofset-- > 0) origin*=chrom;
 
  return origin;
}


int main(argc,argv)
int argc;
char **argv;
{
  int i, j, k;
  double komorni_a = 440.000; /* Hz */
  double c;
  char ton[4];
  FILE *out;

  char tony[12][4] = {{"c"},{"cis"},{"d"},{"dis"},{"e"},{"f"},{"fis"},{"g"},{"gis"},{"a"},{"b"},{"h"}};

  out = fopen("klavtony.txt","wt");
/*  for ( i = 0; i <= 12; i++ )
  {
    if (!strcmp(argv[1],tony[i])) break;
  } */
  c = skok(komorni_a,3);
  printf("c=%e [Hz]\n",c);
  printf("ton=%e [Hz]\n\n\n\n",skok(komorni_a,3));
  for (i = -5; i <= 5; i++)
  for (j = 0; j < 12; j++) {
    if (i > 0 ) fprintf(out,"%s%d",tony[j],i);
    if (i == 0 ) fprintf(out,"%s ",tony[j]);
    if (i < 0 ) {
      for (k = 0; k < 4; k++) {
	if (tony[j][k] != NULL)
	  ton[k]=tony[j][k]-32;
	else
	  ton[k]=NULL;
      }
    }
    if (i < -1) fprintf(out,"%s%d ",ton,abs(i)-1);
    if (i == -1) fprintf(out,"%s  ",ton);
    k=10-strlen(tony[j]);
    if (i < 0) k--;
    while(k--) fprintf(out," ");
    fprintf(out,"%15.10e\n",skok_presny(c,i,j));
  }
  fclose(out);

  return 1;
}
