Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Note Frequency Printer

By: a guest on Jul 19th, 2012  |  syntax: Java  |  size: 1.66 KB  |  hits: 555  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**Prints the frequencies in Hz of all the notes in the western scale,
  2.  * in the range of human hearing. */
  3. public class NoteFreqPrinter
  4. {
  5.         public static void main(String[] args)
  6.         {
  7.                 String[] noteNames = {"A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"};
  8.                 //the ratio between adjacent notes in equal temperament:
  9.                 //these values are interchangeable
  10.                 double ratio = Math.pow(2, 1.0/12.0);//calculated by java, or
  11.                 double wikiRatio = 1.059463094359295264561825;//borrowed from wikipedia's page on equal temperament
  12.                
  13.                 int numFreqs = 24;//how many notes to print. Recalculated later
  14.                 int octave = 55;//the frequency (note) to start at. 55Hz is A1.
  15.                
  16.                 //human hearing is between 20Hz and 20KHz;
  17.                 //calculate how many notes we'll need, to determine size of array
  18.                 while(octave < 20000)
  19.                 {
  20.                         octave *= 2;
  21.                         numFreqs += 12;
  22.                 }
  23.                 numFreqs -= 5;//cheap fix to discard inaudible notes in last octave
  24.                 double[] freqs = new double[numFreqs];
  25.                
  26.                 //these values are very close, but might differ slightly due to precision
  27.                 System.out.println("derived ratio value  : "+ratio);
  28.                 System.out.println("wikipedia ratio value: "+wikiRatio);
  29.                
  30.                 freqs[0] = 13.75; // the first and lowest note: A-1
  31.                
  32.                 System.out.print(noteNames[0]+": ");
  33.                 System.out.print(freqs[0]+"\n");
  34.                
  35.                 for(int i = 1; i < freqs.length; i++)
  36.                 {
  37.                         freqs[i] = freqs[i-1] * ratio;
  38.                         if(((i % 12) == 0)//if note should have an integral frequency
  39.                         && (i > 12))// (octaves of A),
  40.                         {// cast it to an int to discard precision loss
  41.                                 freqs[i] = Math.round(freqs[i]);
  42.                         }
  43.                        
  44.                         System.out.print(noteNames[i%12]+": ");
  45.                         System.out.print(freqs[i]+"\n");
  46.                 }
  47.         }
  48. }