shithub: aubio

Download patch

ref: 7a6cbbe849a5fddc633e33e4df88bb34ceae3955
parent: 22d33e22c85d5ff1699897f0ef5b6c251a6a5a13
author: Paul Brossier <piem@piem.org>
date: Thu Oct 8 16:51:06 EDT 2009

src/pitch/pitchdetection.{c,h}: clean and update prototypes, sync with latest pitch objects

--- a/src/pitch/pitchdetection.c
+++ b/src/pitch/pitchdetection.c
@@ -31,18 +31,18 @@
 #include "pitch/pitchyinfft.h"
 #include "pitch/pitchdetection.h"
 
-typedef smpl_t (*aubio_pitchdetection_func_t)
-  (aubio_pitchdetection_t *p, fvec_t * ibuf);
+typedef void (*aubio_pitchdetection_func_t)
+  (aubio_pitchdetection_t *p, fvec_t * ibuf, fvec_t *obuf);
 typedef smpl_t (*aubio_pitchdetection_conv_t)
   (smpl_t value, uint_t srate, uint_t bufsize);
 
 void aubio_pitchdetection_slideblock(aubio_pitchdetection_t *p, fvec_t *ibuf);
 
-smpl_t aubio_pitchdetection_mcomb   (aubio_pitchdetection_t *p, fvec_t *ibuf);
-smpl_t aubio_pitchdetection_yin     (aubio_pitchdetection_t *p, fvec_t *ibuf);
-smpl_t aubio_pitchdetection_schmitt (aubio_pitchdetection_t *p, fvec_t *ibuf);
-smpl_t aubio_pitchdetection_fcomb   (aubio_pitchdetection_t *p, fvec_t *ibuf);
-smpl_t aubio_pitchdetection_yinfft  (aubio_pitchdetection_t *p, fvec_t *ibuf);
+void aubio_pitchdetection_mcomb   (aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t *obuf);
+void aubio_pitchdetection_yin     (aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t *obuf);
+void aubio_pitchdetection_schmitt (aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t *obuf);
+void aubio_pitchdetection_fcomb   (aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t *obuf);
+void aubio_pitchdetection_yinfft  (aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t *obuf);
 
 /** generic pitch detection structure */
 struct _aubio_pitchdetection_t {
@@ -54,12 +54,11 @@
   aubio_pitchfcomb_t * fcomb;     /**< fcomb object */
   aubio_pitchschmitt_t * schmitt; /**< schmitt object */
   aubio_pitchyinfft_t * yinfft;   /**< yinfft object */
+  aubio_pitchyin_t * yin;   /**< yinfft object */
   aubio_filter_t * filter;        /**< filter */
   aubio_pvoc_t * pv;              /**< phase vocoder for mcomb */ 
   cvec_t * fftgrain;              /**< spectral frame for mcomb */
   fvec_t * buf;                   /**< temporary buffer for yin */
-  fvec_t * yin;                   /**< yin function */
-  smpl_t yinthres;                /**< yin peak picking threshold parameter */
   aubio_pitchdetection_func_t callback; /**< pointer to current pitch detection method */
   aubio_pitchdetection_conv_t freqconv; /**< pointer to current pitch conversion method */ 
 };
@@ -96,25 +95,25 @@
   switch(p->type) {
     case aubio_pitch_yin:
       p->buf      = new_fvec(bufsize,channels);
-      p->yin      = new_fvec(bufsize/2,channels);
+      p->yin      = new_aubio_pitchyin(bufsize);
       p->callback = aubio_pitchdetection_yin;
-      p->yinthres = 0.15;
+      aubio_pitchyin_set_tolerance (p->yin, 0.15);
       break;
     case aubio_pitch_mcomb:
       p->pv       = new_aubio_pvoc(bufsize, hopsize, channels);
       p->fftgrain = new_cvec(bufsize, channels);
-      p->mcomb    = new_aubio_pitchmcomb(bufsize,hopsize,channels,samplerate);
+      p->mcomb    = new_aubio_pitchmcomb(bufsize,hopsize,channels);
       p->filter   = new_aubio_filter_c_weighting (samplerate, channels);
       p->callback = aubio_pitchdetection_mcomb;
       break;
     case aubio_pitch_fcomb:
       p->buf      = new_fvec(bufsize,channels);
-      p->fcomb    = new_aubio_pitchfcomb(bufsize,hopsize,samplerate);
+      p->fcomb    = new_aubio_pitchfcomb(bufsize,hopsize,channels);
       p->callback = aubio_pitchdetection_fcomb;
       break;
     case aubio_pitch_schmitt:
       p->buf      = new_fvec(bufsize,channels);
-      p->schmitt  = new_aubio_pitchschmitt(bufsize,samplerate);
+      p->schmitt  = new_aubio_pitchschmitt(bufsize);
       p->callback = aubio_pitchdetection_schmitt;
       break;
     case aubio_pitch_yinfft:
@@ -121,7 +120,7 @@
       p->buf      = new_fvec(bufsize,channels);
       p->yinfft   = new_aubio_pitchyinfft(bufsize);
       p->callback = aubio_pitchdetection_yinfft;
-      p->yinthres = 0.85;
+      aubio_pitchyinfft_set_tolerance (p->yinfft, 0.85);
       break;
     default:
       break;
@@ -149,8 +148,8 @@
 void del_aubio_pitchdetection(aubio_pitchdetection_t * p) {
   switch(p->type) {
     case aubio_pitch_yin:
-      del_fvec(p->yin);
       del_fvec(p->buf);
+      del_aubio_pitchyin(p->yin);
       break;
     case aubio_pitch_mcomb:
       del_aubio_pvoc(p->pv);
@@ -191,59 +190,91 @@
   }
 }
 
-void aubio_pitchdetection_set_yinthresh(aubio_pitchdetection_t *p, smpl_t thres) {
-  p->yinthres = thres;
+void aubio_pitchdetection_set_tolerance(aubio_pitchdetection_t *p, smpl_t tol) {
+  switch(p->type) {
+    case aubio_pitch_yin:
+      aubio_pitchyin_set_tolerance (p->yin, tol);
+      break;
+    case aubio_pitch_yinfft:
+      aubio_pitchyinfft_set_tolerance (p->yinfft, tol);
+      break;
+    default:
+      break;
+  }
 }
 
-smpl_t aubio_pitchdetection_do (aubio_pitchdetection_t *p, fvec_t * ibuf) {
-  return p->freqconv(p->callback(p,ibuf),p->srate,p->bufsize);
+void aubio_pitchdetection_do (aubio_pitchdetection_t *p, fvec_t * ibuf, fvec_t *obuf) {
+  uint_t i;
+  p->callback(p, ibuf, obuf);
+  for (i = 0; i < obuf->channels; i++) {
+    p->freqconv(obuf->data[i][0],p->srate,p->bufsize);
+  }
 }
 
-smpl_t aubio_pitchdetection_mcomb(aubio_pitchdetection_t *p, fvec_t *ibuf) {
-  smpl_t pitch = 0.;
+void aubio_pitchdetection_mcomb(aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t * obuf) {
+  uint_t i;
   aubio_filter_do(p->filter,ibuf);
   aubio_pvoc_do(p->pv,ibuf,p->fftgrain);
-  pitch = aubio_pitchmcomb_do(p->mcomb,p->fftgrain);
-  /** \bug should move the >0 check within aubio_bintofreq */
-  if (pitch>0.) {
-    pitch = aubio_bintofreq(pitch,p->srate,p->bufsize);
-  } else {
-    pitch = 0.;
+  aubio_pitchmcomb_do(p->mcomb,p->fftgrain, obuf);
+  for (i = 0; i < obuf->channels; i++) {
+    obuf->data[i][0] = aubio_bintofreq (obuf->data[i][0], p->srate, p->bufsize);
   }
-  return pitch;
 }
 
-smpl_t aubio_pitchdetection_yin(aubio_pitchdetection_t *p, fvec_t *ibuf) {
+void aubio_pitchdetection_yin(aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t * obuf) {
   smpl_t pitch = 0.;
+  uint_t i;
   aubio_pitchdetection_slideblock(p,ibuf);
-  pitch = aubio_pitchyin_getpitchfast(p->buf,p->yin, p->yinthres);
-  if (pitch>0) {
-    pitch = p->srate/(pitch+0.);
-  } else {
-    pitch = 0.;
+  aubio_pitchyin_do(p->yin,p->buf, obuf);
+  for (i = 0; i < obuf->channels; i++) {
+    pitch = obuf->data[i][0];
+    if (pitch>0) {
+      pitch = p->srate/(pitch+0.);
+    } else {
+      pitch = 0.;
+    }
+    obuf->data[i][0] = pitch;
   }
-  return pitch;
 }
 
 
-smpl_t aubio_pitchdetection_yinfft(aubio_pitchdetection_t *p, fvec_t *ibuf){
+void aubio_pitchdetection_yinfft(aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t * obuf){
   smpl_t pitch = 0.;
+  uint_t i;
   aubio_pitchdetection_slideblock(p,ibuf);
-  pitch = aubio_pitchyinfft_do(p->yinfft,p->buf,p->yinthres);
-  if (pitch>0) {
-    pitch = p->srate/(pitch+0.);
-  } else {
-    pitch = 0.;
+  aubio_pitchyinfft_do(p->yinfft,p->buf,obuf);
+  for (i = 0; i < obuf->channels; i++) {
+    pitch = obuf->data[i][0];
+    if (pitch>0) {
+      pitch = p->srate/(pitch+0.);
+    } else {
+      pitch = 0.;
+    }
+    obuf->data[i][0] = pitch;
   }
-  return pitch; 
 }
 
-smpl_t aubio_pitchdetection_fcomb(aubio_pitchdetection_t *p, fvec_t *ibuf){
+void aubio_pitchdetection_fcomb(aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t * out){
+  uint_t i;
   aubio_pitchdetection_slideblock(p,ibuf);
-  return aubio_pitchfcomb_do(p->fcomb,p->buf);
+  aubio_pitchfcomb_do(p->fcomb,p->buf, out);
+  for (i = 0; i < out->channels; i++) {
+    out->data[i][0] = aubio_bintofreq (out->data[i][0], p->srate, p->bufsize);
+  }
 }
 
-smpl_t aubio_pitchdetection_schmitt(aubio_pitchdetection_t *p, fvec_t *ibuf){
+void aubio_pitchdetection_schmitt(aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t *out){
+  smpl_t period, pitch = 0.;
+  uint_t i;
   aubio_pitchdetection_slideblock(p,ibuf);
-  return aubio_pitchschmitt_do(p->schmitt,p->buf);
+  aubio_pitchschmitt_do(p->schmitt,p->buf, out);
+  for (i = 0; i < out->channels; i++) {
+    period = out->data[i][0];
+    if (period>0) {
+      pitch = p->srate/period;
+    } else {
+      pitch = 0.;
+    }
+    out->data[i][0] = pitch;
+  }
 }
--- a/src/pitch/pitchdetection.h
+++ b/src/pitch/pitchdetection.h
@@ -58,7 +58,7 @@
   \param ibuf input signal of length hopsize
 
 */
-smpl_t aubio_pitchdetection_do (aubio_pitchdetection_t * p, fvec_t * ibuf);
+void aubio_pitchdetection_do (aubio_pitchdetection_t * p, fvec_t * ibuf, fvec_t * obuf);
 
 /** change yin or yinfft tolerance threshold
 
@@ -65,7 +65,7 @@
   default is 0.15 for yin and 0.85 for yinfft
 
 */
-void aubio_pitchdetection_set_yinthresh(aubio_pitchdetection_t *p, smpl_t thres);
+void aubio_pitchdetection_set_tolerance(aubio_pitchdetection_t *p, smpl_t tol);
 
 /** deletion of the pitch detection object
 
--- a/tests/src/test-pitchdetection.c
+++ b/tests/src/test-pitchdetection.c
@@ -9,13 +9,14 @@
         aubio_pitchdetection_mode mode = aubio_pitchm_freq;
         aubio_pitchdetection_type type = aubio_pitch_yinfft;
         fvec_t * in       = new_fvec (hop_s, channels); /* input buffer */
+        fvec_t * out       = new_fvec (1, channels); /* input buffer */
         aubio_pitchdetection_t * o  = new_aubio_pitchdetection(
           win_s, hop_s, channels, samplerate, type, mode
           );
         uint_t i = 0;
 
-        while (i < 1000) {
-          aubio_pitchdetection_do (o,in);
+        while (i < 100) {
+          aubio_pitchdetection_do (o,in, out);
           i++;
         };