sonic_lite.c
static int findPitchPeriodInRange(short *samples, int minPeriod, int maxPeriod,
                                  int* retMinDiff, int* retMaxDiff) {
  int period, bestPeriod = 0, worstPeriod = 255;
  short *s;
  short *p;
  short sVal, pVal;
  unsigned long diff, minDiff = 1, maxDiff = 0;
  int i;

  for (period = minPeriod; period <= maxPeriod; period++) {
    diff = 0;
    s = samples;
    p = samples + period;
    for (i = 0; i < period; i++) {
      sVal = *s++;
      pVal = *p++;
      diff += sVal >= pVal ? (unsigned short)(sVal - pVal)
                           : (unsigned short)(pVal - sVal);
    }
    /* Note that the highest number of samples we add into diff will be less
       than 256, since we skip samples.  Thus, diff is a 24 bit number, and
       we can safely multiply by numSamples without overflow */
    if (bestPeriod == 0 || diff * bestPeriod < minDiff * period) {
      minDiff = diff;
      bestPeriod = period;
    }
    if (diff * worstPeriod > maxDiff * period) {
      maxDiff = diff;
      worstPeriod = period;
    }
  }
  *retMinDiff = minDiff / bestPeriod;
  *retMaxDiff = maxDiff / worstPeriod;
  return bestPeriod;
}
/* Find the best frequency match in the range, and given a sample skip multiple.
   For now, just find the pitch of the first channel. */
Find the best frequency match in the range, and given a sample skip multiple.
For now, just find the pitch of the first channel.
Find the best frequency match in the range and given a sample skip multiple . For now just find the pitch of the first channel .
static int findPitchPeriodInRange(short *samples, int minPeriod, int maxPeriod,
                                  int* retMinDiff, int* retMaxDiff) {
  int period, bestPeriod = 0, worstPeriod = 255;
  short *s;
  short *p;
  short sVal, pVal;
  unsigned long diff, minDiff = 1, maxDiff = 0;
  int i;
  for (period = minPeriod; period <= maxPeriod; period++) {
    diff = 0;
    s = samples;
    p = samples + period;
    for (i = 0; i < period; i++) {
      sVal = *s++;
      pVal = *p++;
      diff += sVal >= pVal ? (unsigned short)(sVal - pVal)
                           : (unsigned short)(pVal - sVal);
    }
    if (bestPeriod == 0 || diff * bestPeriod < minDiff * period) {
      minDiff = diff;
      bestPeriod = period;
    }
    if (diff * worstPeriod > maxDiff * period) {
      maxDiff = diff;
      worstPeriod = period;
    }
  }
  *retMinDiff = minDiff / bestPeriod;
  *retMaxDiff = maxDiff / worstPeriod;
  return bestPeriod;
}
static int findPitchPeriodInRange ( short * samples , int minPeriod , int maxPeriod , int * retMinDiff , int * retMaxDiff ) { int period , bestPeriod = 0 , worstPeriod = 255 ; short * s ; short * p ; short sVal , pVal ; unsigned long diff , minDiff = 1 , maxDiff = 0 ; int i ; for ( period = minPeriod ; period <= maxPeriod ; period ++ ) { diff = 0 ; s = samples ; p = samples + period ; for ( i = 0 ; i < period ; i ++ ) { sVal = * s ++ ; pVal = * p ++ ; diff += sVal >= pVal ? ( unsigned short ) ( sVal - pVal ) : ( unsigned short ) ( pVal - sVal ) ; } if ( bestPeriod == 0 || diff * bestPeriod < minDiff * period ) { minDiff = diff ; bestPeriod = period ; } if ( diff * worstPeriod > maxDiff * period ) { maxDiff = diff ; worstPeriod = period ; } } * retMinDiff = minDiff / bestPeriod ; * retMaxDiff = maxDiff / worstPeriod ; return bestPeriod ; }
Find the best frequency match in the range, and given a sample skip multiple.
Find the best frequency match in the range and given a sample skip multiple .
/* Note that the highest number of samples we add into diff will be less
       than 256, since we skip samples.  Thus, diff is a 24 bit number, and
       we can safely multiply by numSamples without overflow */