//------------------------------------------------------------------- // 12throot.cpp // // This program computes and displays the frequencies of notes // on the standard American piano having 88 keys. The program // output has C-language syntax (for redirection as a header). // // compile using: $ make 12throot // execute using: $ 12throot > pianokey.h // // programmer: ALLAN CRUSE // written on: 22 JUN 2005 // revised on: 17 SEP 2005 -- to show the names of these tones //------------------------------------------------------------------- #include // for printf(), perror() #include // for log() and exp() char *notename[] = { "A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#" }; int main( int argc, char **argv ) { double radicand = 2.0; double root12th; // print a file-header showing filename and computed values printf( "// \n//\n" ); root12th = exp( log( radicand )/12.0 ); printf( "// Twelfth-root of 2 is %0.5f \n", root12th ); // check for inaccuracy in the accumulated rounding error double check = 1.0; for (int i = 0; i < 12; i++) check *= root12th; printf( "// Twelfth power equals %0.5f \n//\n", check ); // compute and print the array of piano-key frequencies double note = 27.5; printf( "\ndouble\tpianokey[] = { " ); for (int i = 0; i < 88; i++) { printf( "\n\t\t\t %4.5f", note ); if ( i == 87 ) printf( " \t" ); else printf( ",\t" ); printf( " /* %-2s */ ", notename[ i % 12 ] ); note *= root12th; } printf( " };\n\n" ); }