[pitch] comment out unused functions in mcomb and yin
[aubio.git] / src / pitch / pitchyin.c
index 73907a9..85b4566 100644 (file)
@@ -18,7 +18,7 @@
 
 */
 
-/* This algorithm was developped by A. de Cheveigne and H. Kawahara and
+/* This algorithm was developed by A. de Cheveigné and H. Kawahara and
  * published in:
  * 
  * de Cheveigné, A., Kawahara, H. (2002) "YIN, a fundamental frequency
@@ -36,36 +36,40 @@ struct _aubio_pitchyin_t
 {
   fvec_t *yin;
   smpl_t tol;
+  uint_t peak_pos;
 };
 
+#if 0
 /** compute difference function
-  
-  \param input input signal 
+
+  \param input input signal
   \param yinbuf output buffer to store difference function (half shorter than input)
 
 */
 void aubio_pitchyin_diff (fvec_t * input, fvec_t * yinbuf);
 
-/** in place computation of the YIN cumulative normalised function 
-  
-  \param yinbuf input signal (a square difference function), also used to store function 
+/** in place computation of the YIN cumulative normalised function
+
+  \param yinbuf input signal (a square difference function), also used to store function
 
 */
 void aubio_pitchyin_getcum (fvec_t * yinbuf);
 
 /** detect pitch in a YIN function
-  
+
   \param yinbuf input buffer as computed by aubio_pitchyin_getcum
 
 */
-uint_t aubio_pitchyin_getpitch (fvec_t * yinbuf);
+uint_t aubio_pitchyin_getpitch (const fvec_t * yinbuf);
+#endif
 
 aubio_pitchyin_t *
 new_aubio_pitchyin (uint_t bufsize)
 {
   aubio_pitchyin_t *o = AUBIO_NEW (aubio_pitchyin_t);
-  o->yin = new_fvec (bufsize / 2, 1);
+  o->yin = new_fvec (bufsize / 2);
   o->tol = 0.15;
+  o->peak_pos = 0;
   return o;
 }
 
@@ -76,21 +80,20 @@ del_aubio_pitchyin (aubio_pitchyin_t * o)
   AUBIO_FREE (o);
 }
 
+#if 0
 /* outputs the difference function */
 void
 aubio_pitchyin_diff (fvec_t * input, fvec_t * yin)
 {
-  uint_t c, j, tau;
+  uint_t j, tau;
   smpl_t tmp;
-  for (c = 0; c < input->channels; c++) {
-    for (tau = 0; tau < yin->length; tau++) {
-      yin->data[c][tau] = 0.;
-    }
-    for (tau = 1; tau < yin->length; tau++) {
-      for (j = 0; j < yin->length; j++) {
-        tmp = input->data[c][j] - input->data[c][j + tau];
-        yin->data[c][tau] += SQR (tmp);
-      }
+  for (tau = 0; tau < yin->length; tau++) {
+    yin->data[tau] = 0.;
+  }
+  for (tau = 1; tau < yin->length; tau++) {
+    for (j = 0; j < yin->length; j++) {
+      tmp = input->data[j] - input->data[j + tau];
+      yin->data[tau] += SQR (tmp);
     }
   }
 }
@@ -99,28 +102,25 @@ aubio_pitchyin_diff (fvec_t * input, fvec_t * yin)
 void
 aubio_pitchyin_getcum (fvec_t * yin)
 {
-  uint_t c, tau;
-  smpl_t tmp;
-  for (c = 0; c < yin->channels; c++) {
-    tmp = 0.;
-    yin->data[c][0] = 1.;
-    //AUBIO_DBG("%f\t",yin->data[c][0]);
-    for (tau = 1; tau < yin->length; tau++) {
-      tmp += yin->data[c][tau];
-      yin->data[c][tau] *= tau / tmp;
-      //AUBIO_DBG("%f\t",yin->data[c][tau]);
-    }
-    //AUBIO_DBG("\n");
+  uint_t tau;
+  smpl_t tmp = 0.;
+  yin->data[0] = 1.;
+  //AUBIO_DBG("%f\t",yin->data[0]);
+  for (tau = 1; tau < yin->length; tau++) {
+    tmp += yin->data[tau];
+    yin->data[tau] *= tau / tmp;
+    //AUBIO_DBG("%f\t",yin->data[tau]);
   }
+  //AUBIO_DBG("\n");
 }
 
 uint_t
-aubio_pitchyin_getpitch (fvec_t * yin)
+aubio_pitchyin_getpitch (const fvec_t * yin)
 {
-  uint_t c = 0, tau = 1;
+  uint_t tau = 1;
   do {
-    if (yin->data[c][tau] < 0.1) {
-      while (yin->data[c][tau + 1] < yin->data[c][tau]) {
+    if (yin->data[tau] < 0.1) {
+      while (yin->data[tau + 1] < yin->data[tau]) {
         tau++;
       }
       return tau;
@@ -130,39 +130,49 @@ aubio_pitchyin_getpitch (fvec_t * yin)
   //AUBIO_DBG("No pitch found");
   return 0;
 }
-
+#endif
 
 /* all the above in one */
 void
-aubio_pitchyin_do (aubio_pitchyin_t * o, fvec_t * input, fvec_t * out)
+aubio_pitchyin_do (aubio_pitchyin_t * o, const fvec_t * input, fvec_t * out)
 {
-  smpl_t tol = o->tol;
-  fvec_t *yin = o->yin;
-  uint_t c, j, tau = 0;
+  const smpl_t tol = o->tol;
+  fvec_t* yin = o->yin;
+  const smpl_t *input_data = input->data;
+  const uint_t length = yin->length;
+  smpl_t *yin_data = yin->data;
+  uint_t j, tau;
   sint_t period;
-  smpl_t tmp = 0., tmp2 = 0.;
-  for (c = 0; c < input->channels; c++) {
-    yin->data[c][0] = 1.;
-    for (tau = 1; tau < yin->length; tau++) {
-      yin->data[c][tau] = 0.;
-      for (j = 0; j < yin->length; j++) {
-        tmp = input->data[c][j] - input->data[c][j + tau];
-        yin->data[c][tau] += SQR (tmp);
-      }
-      tmp2 += yin->data[c][tau];
-      yin->data[c][tau] *= tau / tmp2;
-      period = tau - 3;
-      if (tau > 4 && (yin->data[c][period] < tol) &&
-          (yin->data[c][period] < yin->data[c][period + 1])) {
-        out->data[c][0] = fvec_quadint (yin, period, c);
-        goto beach;
-      }
+  smpl_t tmp, tmp2 = 0.;
+
+  yin_data[0] = 1.;
+  for (tau = 1; tau < length; tau++) {
+    yin_data[tau] = 0.;
+    for (j = 0; j < length; j++) {
+      tmp = input_data[j] - input_data[j + tau];
+      yin_data[tau] += SQR (tmp);
+    }
+    tmp2 += yin_data[tau];
+    if (tmp2 != 0) {
+      yin->data[tau] *= tau / tmp2;
+    } else {
+      yin->data[tau] = 1.;
+    }
+    period = tau - 3;
+    if (tau > 4 && (yin_data[period] < tol) &&
+        (yin_data[period] < yin_data[period + 1])) {
+      o->peak_pos = (uint_t)period;
+      out->data[0] = fvec_quadratic_peak_pos (yin, o->peak_pos);
+      return;
     }
-    out->data[c][0] = fvec_quadint (yin, fvec_min_elem (yin), c);
-  beach:
-    continue;
   }
-  //return 0;
+  o->peak_pos = (uint_t)fvec_min_elem (yin);
+  out->data[0] = fvec_quadratic_peak_pos (yin, o->peak_pos);
+}
+
+smpl_t
+aubio_pitchyin_get_confidence (aubio_pitchyin_t * o) {
+  return 1. - o->yin->data[o->peak_pos];
 }
 
 uint_t