/**Prints the frequencies in Hz of all the notes in the western scale,
* in the range of human hearing. */
public class NoteFreqPrinter
{
public static void main(String[] args)
{
String[] noteNames = {"A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"};
//the ratio between adjacent notes in equal temperament:
//these values are interchangeable
double ratio = Math.pow(2, 1.0/12.0);//calculated by java, or
double wikiRatio = 1.059463094359295264561825;//borrowed from wikipedia's page on equal temperament
int numFreqs = 24;//how many notes to print. Recalculated later
int octave = 55;//the frequency (note) to start at. 55Hz is A1.
//human hearing is between 20Hz and 20KHz;
//calculate how many notes we'll need, to determine size of array
while(octave < 20000)
{
octave *= 2;
numFreqs += 12;
}
numFreqs -= 5;//cheap fix to discard inaudible notes in last octave
double[] freqs = new double[numFreqs];
//these values are very close, but might differ slightly due to precision
System.out.println("derived ratio value : "+ratio);
System.out.println("wikipedia ratio value: "+wikiRatio);
freqs[0] = 13.75; // the first and lowest note: A-1
System.out.print(noteNames[0]+": ");
System.out.print(freqs[0]+"\n");
for(int i = 1; i < freqs.length; i++)
{
freqs[i] = freqs[i-1] * ratio;
if(((i % 12) == 0)//if note should have an integral frequency
&& (i > 12))// (octaves of A),
{// cast it to an int to discard precision loss
freqs[i] = Math.round(freqs[i]);
}
System.out.print(noteNames[i%12]+": ");
System.out.print(freqs[i]+"\n");
}
}
}