js/pitchdetect.js
// this is the "bar" for how close a correlation needs to be
this is the "bar" for how close a correlation needs to be
this is the " bar " for how close a correlation needs to be
function autoCorrelate(buf, sampleRate) {
  const SIZE = buf.length;
  const MAX_SAMPLES = Math.floor(SIZE / 2);
  var best_offset = -1;
  var best_correlation = 0;
  var rms = 0;
  var foundGoodCorrelation = false;
  var correlations = new Array(MAX_SAMPLES);
  for (var i = 0; i < SIZE; i++) {
    var val = buf[i];
    rms += val * val;
  }
  rms = Math.sqrt(rms / SIZE);
  if (rms < 0.01) return -1;
  var lastCorrelation = 1;
  for (var offset = MIN_SAMPLES; offset < MAX_SAMPLES; offset++) {
    var correlation = 0;
    for (var i = 0; i < MAX_SAMPLES; i++) {
      correlation += Math.abs((buf[i]) - (buf[i + offset]));
    }
    correlation = 1 - (correlation / MAX_SAMPLES);
    correlations[offset] = correlation; 
    if ((correlation > GOOD_ENOUGH_CORRELATION) && (correlation > lastCorrelation)) {
      foundGoodCorrelation = true;
      if (correlation > best_correlation) {
        best_correlation = correlation;
        best_offset = offset;
      }
    } else if (foundGoodCorrelation) {
      var shift = (correlations[best_offset + 1] - correlations[best_offset - 1]) / correlations[best_offset];
      return sampleRate / (best_offset + (8 * shift));
    }
    lastCorrelation = correlation;
  }
  if (best_correlation > 0.01) {
    return sampleRate / best_offset;
  }
  return -1;
}
function autoCorrelate ( buf , sampleRate ) { const SIZE = buf . length ; const MAX_SAMPLES = Math . floor ( SIZE / 2 ) ; var best_offset = - 1 ; var best_correlation = 0 ; var rms = 0 ; var foundGoodCorrelation = false ; var correlations = new Array ( MAX_SAMPLES ) ; for ( var i = 0 ; i < SIZE ; i ++ ) { var val = buf [ i ] ; rms += val * val ; } rms = Math . sqrt ( rms / SIZE ) ; if ( rms < 0.01 ) return - 1 ; var lastCorrelation = 1 ; for ( var offset = MIN_SAMPLES ; offset < MAX_SAMPLES ; offset ++ ) { var correlation = 0 ; for ( var i = 0 ; i < MAX_SAMPLES ; i ++ ) { correlation += Math . abs ( ( buf [ i ] ) - ( buf [ i + offset ] ) ) ; } correlation = 1 - ( correlation / MAX_SAMPLES ) ; correlations [ offset ] = correlation ; if ( ( correlation > GOOD_ENOUGH_CORRELATION ) && ( correlation > lastCorrelation ) ) { foundGoodCorrelation = true ; if ( correlation > best_correlation ) { best_correlation = correlation ; best_offset = offset ; } } else if ( foundGoodCorrelation ) { var shift = ( correlations [ best_offset + 1 ] - correlations [ best_offset - 1 ] ) / correlations [ best_offset ] ; return sampleRate / ( best_offset + ( 8 * shift ) ) ; } lastCorrelation = correlation ; } if ( best_correlation > 0.01 ) { return sampleRate / best_offset ; } return - 1 ; }
this is the "bar" for how close a correlation needs to be
this is the " bar " for how close a correlation needs to be
// store it, for the tweaking we need to do below. //	var best_frequency = sampleRate/best_offset;