public static int frequencyToMIDINote(double aFrequency){
  double r=1.05946309436;
  double ref=523.251;
  int supinf=0;
  int i=0;
  double hautnb=1;
  double ref1=0;
  double ref2=0;
  double flag=0;
  int nmidi=72;
  while (aFrequency < ref) {
    ref=Math.floor(1000 * ref / r) / 1000;
    i=i + 1;
    supinf=-1;
    flag=1;
    ref1=ref;
  }
  while (aFrequency > ref) {
    ref=Math.floor(1000 * ref * r) / 1000;
    i=i - 1;
    supinf=1;
    ref2=ref;
  }
  if (Math.abs(aFrequency - ref1) < Math.abs(aFrequency - ref2)) {
    supinf=-1;
    i=i + 1;
  }
 else {
    if (flag == 1)     supinf=-1;
  }
  if (ref1 == 0) {
    ref1=Math.floor(1000 * ref / r) / 1000;
    if (Math.abs(aFrequency - ref1) < Math.abs(aFrequency - ref2)) {
      i=i + 1;
      supinf=1;
    }
  }
  i=Math.abs(i);
  while (i-- != 0) {
    if ((hautnb == 1 && supinf == -1) || (hautnb == 12 && supinf == 1)) {
      if (supinf == 1)       hautnb=0;
      if (supinf == -1)       hautnb=13;
    }
    hautnb=hautnb + supinf;
    nmidi=nmidi + supinf;
  }
  return nmidi;
}

public static int frequencyToMIDINote ( double aFrequency ) { double r = 1.05946309436 ; double ref = 523.251 ; int supinf = 0 ; int i = 0 ; double hautnb = 1 ; double ref1 = 0 ; double ref2 = 0 ; double flag = 0 ; int nmidi = 72 ; while ( aFrequency < ref ) { ref = Math . floor ( 1000 * ref / r ) / 1000 ; i = i + 1 ; supinf = - 1 ; flag = 1 ; ref1 = ref ; } while ( aFrequency > ref ) { ref = Math . floor ( 1000 * ref * r ) / 1000 ; i = i - 1 ; supinf = 1 ; ref2 = ref ; } if ( Math . abs ( aFrequency - ref1 ) < Math . abs ( aFrequency - ref2 ) ) { supinf = - 1 ; i = i + 1 ; } else { if ( flag == 1 ) supinf = - 1 ; } if ( ref1 == 0 ) { ref1 = Math . floor ( 1000 * ref / r ) / 1000 ; if ( Math . abs ( aFrequency - ref1 ) < Math . abs ( aFrequency - ref2 ) ) { i = i + 1 ; supinf = 1 ; } } i = Math . abs ( i ) ; while ( i -- != 0 ) { if ( ( hautnb == 1 && supinf == - 1 ) || ( hautnb == 12 && supinf == 1 ) ) { if ( supinf == 1 ) hautnb = 0 ; if ( supinf == - 1 ) hautnb = 13 ; } hautnb = hautnb + supinf ; nmidi = nmidi + supinf ; } return nmidi ; }
convert a frequency in hz to the midi note number
