Mathematically generate all of the frequencies of the 88 keyboard piano.
Equal temperament.
Date Created:Tuesday February 27th, 2007 11:23 PM
Date Modified:Saturday August 02nd, 2008 01:55 PM
/*
*
* Creates a list of all notes on the piano and the frequency
* 01 March 2007 Added names of notes along with octaves
* 03 March 2007 Added html output generation
*
* Author: Dan Lynch
*
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main()
{
FILE *piano;
char filename[1024];
int i;
double magic;
double frequency;
double twelfth;
char notes[12] = "A#BC#D#EF#G#";
twelfth = 1.0/12.0;
magic = pow(2,twelfth);
printf("Enter a Filename:\n");
scanf("%s", &filename);
strncat(filename, ".html", (strlen(filename)+5) );
piano = fopen(filename, "w" );
fprintf( piano, "<center>\n<table border=1 width=100%%>\n\t<tr>\n\t\t<td>Piano Frequency</td>\n");
for (i = 1; i < 89; i++)
{
frequency = 27.5*pow(magic,i-1);
printf("%lf\t",frequency);
printf("%d, octave %d\t",i,i/12);
fprintf( piano, "\t</tr>\n\t<tr>\n\t\t<td align=\"center\"><p class=\"title\">%lf Hz</p></td>\n",frequency);
fprintf( piano, "\t\t<td align=\"center\"><p class=\"title\">Note %d, octave %d</p></td>\n",i,i/12);
if ( notes[(i-1)%12] != '#' )
{
// white keys
printf("%c %d\n",notes[(i-1)%12],i/12);
fprintf( piano, "\t\t<td bgcolor=\"FFFFFF\"><p class=\"btitle\">%c %d</p></td>\n",notes[(i-1)%12],i/12);
}
else
{
// sharps and flats
printf("%c#/%cb %d\n",notes[(i-1)%12-1],notes[i%12],i/12);
fprintf( piano, "\t\t<td bgcolor=\"000000\"><p class=\"title\">%c#/%cb %d</p></td>\n",notes[(i-1)%12-1],notes[i%12],i/12);
}
}
fprintf( piano, "\t</tr>\n</table>\n</center>");
fclose( piano );
return 0;
}
/*
*
* Creates a list of all notes on the piano and the frequency
* Author: Dan Lynch
*
*/
#include <stdio.h>
#include <math.h>
int main()
{
int i;
double magic;
double frequency;
double twelfth;
twelfth = 1.0/12.0;
magic = powl(2,twelfth);
printf("%lf %lf\n\n",magic, twelfth);
for (i = 1; i < 89; i++)
{
frequency = 27.5*powl(magic,i-1);
printf("%lf\t*",frequency);
printf("Note %d\n",i);
}
return 0;
}
/*
*
* Creates a list of all notes on the piano and the frequency
* 01 March 2007 Added names of notes along with octaves
* Author: Dan Lynch
*
*/
#include <stdio.h>
#include <math.h>
int main()
{
int i;
double magic;
double frequency;
double twelfth;
char notes[12] = "A#BC#D#EF#G#";
twelfth = 1.0/12.0;
magic = pow(2,twelfth);
printf("%lf %lf\n\n",magic, twelfth);
for (i = 1; i < 89; i++)
{
frequency = 27.5*pow(magic,i-1);
printf("%lf\t",frequency);
printf("%d, octave %d\t",i,i/12);
if ( notes[(i-1)%12] != '#' )
printf("%c %d\n",notes[(i-1)%12],i/12);
else
printf("%c#/%cb %d\n",notes[(i-1)%12-1],notes[i%12],i/12);
}
return 0;
}
Downloads:
Download: piano_freq_newer.c 843 B
Download: piano_frequency.c 582 B
Download: piano.c 2 KB
Please login or Click Here to register for downloads
Piano Frequency by Dan Lynch
is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
Based on a work at www.3daet.com
Permissions beyond the scope of this license may be available at http://www.3daet.com
