Index: src/pitch/pitch.c
===================================================================
--- src/pitch/pitch.c	(revision 426e6f799cd4153b7e165bb9fccac200f49c2ae2)
+++ src/pitch/pitch.c	(revision c25d5b5c805acb93bb8736fac522ccecf22a5831)
@@ -57,13 +57,31 @@
 } aubio_pitch_mode;
 
-typedef void (*aubio_pitch_func_t)
-  (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
-typedef smpl_t (*aubio_pitch_conv_t)
-  (smpl_t value, uint_t srate, uint_t bufsize);
-
-typedef smpl_t (*aubio_conf_cb_t) (void * p);
-
-void aubio_pitch_slideblock (aubio_pitch_t * p, fvec_t * ibuf);
-
+/** callback to get pitch candidate, defined below */
+typedef void (*aubio_pitch_detect_t) (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
+
+/** callback to convert pitch from one unit to another, defined below */
+typedef smpl_t(*aubio_pitch_convert_t) (smpl_t value, uint_t samplerate, uint_t bufsize);
+
+/** callback to fetch the confidence of the algorithm */
+typedef smpl_t (*aubio_pitch_get_conf_t) (void * p);
+
+/** generic pitch detection structure */
+struct _aubio_pitch_t
+{
+  aubio_pitch_type type;          /**< pitch detection mode */
+  aubio_pitch_mode mode;          /**< pitch detection output mode */
+  uint_t samplerate;              /**< samplerate */
+  uint_t bufsize;                 /**< buffer size */
+  void *p_object;                 /**< pointer to pitch 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 */
+  aubio_pitch_detect_t detect_cb; /**< callback to get the pitch candidates */
+  aubio_pitch_convert_t conv_cb;  /**< callback to convert it to the desired unit */
+  aubio_pitch_get_conf_t conf_cb; /**< pointer to the current confidence callback */
+};
+
+/* callback functions for pitch detection */
 static void aubio_pitch_do_mcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
 static void aubio_pitch_do_yin (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
@@ -72,48 +90,12 @@
 static void aubio_pitch_do_yinfft (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
 
-/** generic pitch detection structure */
-struct _aubio_pitch_t
-{
-  aubio_pitch_type type; /**< pitch detection mode */
-  aubio_pitch_mode mode; /**< pitch detection output mode */
-  uint_t srate;                   /**< samplerate */
-  uint_t bufsize;                 /**< buffer size */
-  aubio_pitchmcomb_t *mcomb;      /**< mcomb object */
-  aubio_pitchfcomb_t *fcomb;      /**< fcomb object */
-  aubio_pitchschmitt_t *schmitt;  /**< schmitt object */
-  aubio_pitchyinfft_t *yinfft;    /**< yinfft object */
-  aubio_pitchyin_t *yin;    /**< yinfft object */
-  void *pitch;
-  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 */
-  aubio_pitch_func_t callback; /**< pointer to current pitch detection method */
-  aubio_pitch_conv_t freqconv; /**< pointer to current pitch conversion method */
-  aubio_conf_cb_t confidence_callback; /**< pointer to the current confidence callback */
-};
-
-/* convenience wrapper function for frequency unit conversions 
- * should probably be rewritten with #defines */
-smpl_t freqconvbin (smpl_t f, uint_t srate, uint_t bufsize);
-smpl_t
-freqconvbin (smpl_t f, uint_t srate, uint_t bufsize)
-{
-  return aubio_freqtobin (f, srate, bufsize);
-}
-
-smpl_t freqconvmidi (smpl_t f, uint_t srate, uint_t bufsize);
-smpl_t
-freqconvmidi (smpl_t f, uint_t srate UNUSED, uint_t bufsize UNUSED)
-{
-  return aubio_freqtomidi (f);
-}
-
-smpl_t freqconvpass (smpl_t f, uint_t srate, uint_t bufsize);
-smpl_t
-freqconvpass (smpl_t f, uint_t srate UNUSED, uint_t bufsize UNUSED)
-{
-  return f;
-}
+/* conversion functions for frequency conversions */
+smpl_t freqconvbin (smpl_t f, uint_t samplerate, uint_t bufsize);
+smpl_t freqconvmidi (smpl_t f, uint_t samplerate, uint_t bufsize);
+smpl_t freqconvpass (smpl_t f, uint_t samplerate, uint_t bufsize);
+
+/* adapter to stack ibuf new samples at the end of buf, and trim `buf` to `bufsize` */
+void aubio_pitch_slideblock (aubio_pitch_t * p, fvec_t * ibuf);
+
 
 aubio_pitch_t *
@@ -140,42 +122,40 @@
     pitch_type = aubio_pitcht_default;
   }
-  p->srate = samplerate;
+  p->samplerate = samplerate;
   p->type = pitch_type;
   aubio_pitch_set_unit (p, "default");
   p->bufsize = bufsize;
-  p->confidence_callback = NULL;
+  p->conf_cb = NULL;
   switch (p->type) {
     case aubio_pitcht_yin:
       p->buf = new_fvec (bufsize);
-      p->yin = new_aubio_pitchyin (bufsize);
-      p->callback = aubio_pitch_do_yin;
-      p->confidence_callback = (aubio_conf_cb_t)aubio_pitchyin_get_confidence;
-      p->pitch = (void*)p->yin;
-      aubio_pitchyin_set_tolerance (p->yin, 0.15);
+      p->p_object = new_aubio_pitchyin (bufsize);
+      p->detect_cb = aubio_pitch_do_yin;
+      p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchyin_get_confidence;
+      aubio_pitchyin_set_tolerance (p->p_object, 0.15);
       break;
     case aubio_pitcht_mcomb:
       p->pv = new_aubio_pvoc (bufsize, hopsize);
       p->fftgrain = new_cvec (bufsize);
-      p->mcomb = new_aubio_pitchmcomb (bufsize, hopsize);
+      p->p_object = new_aubio_pitchmcomb (bufsize, hopsize);
       p->filter = new_aubio_filter_c_weighting (samplerate);
-      p->callback = aubio_pitch_do_mcomb;
+      p->detect_cb = aubio_pitch_do_mcomb;
       break;
     case aubio_pitcht_fcomb:
       p->buf = new_fvec (bufsize);
-      p->fcomb = new_aubio_pitchfcomb (bufsize, hopsize);
-      p->callback = aubio_pitch_do_fcomb;
+      p->p_object = new_aubio_pitchfcomb (bufsize, hopsize);
+      p->detect_cb = aubio_pitch_do_fcomb;
       break;
     case aubio_pitcht_schmitt:
       p->buf = new_fvec (bufsize);
-      p->schmitt = new_aubio_pitchschmitt (bufsize);
-      p->callback = aubio_pitch_do_schmitt;
+      p->p_object = new_aubio_pitchschmitt (bufsize);
+      p->detect_cb = aubio_pitch_do_schmitt;
       break;
     case aubio_pitcht_yinfft:
       p->buf = new_fvec (bufsize);
-      p->yinfft = new_aubio_pitchyinfft (bufsize);
-      p->callback = aubio_pitch_do_yinfft;
-      p->confidence_callback = (aubio_conf_cb_t)aubio_pitchyinfft_get_confidence;
-      p->pitch = (void*)p->yin;
-      aubio_pitchyinfft_set_tolerance (p->yinfft, 0.85);
+      p->p_object = new_aubio_pitchyinfft (bufsize);
+      p->detect_cb = aubio_pitch_do_yinfft;
+      p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchyinfft_get_confidence;
+      aubio_pitchyinfft_set_tolerance (p->p_object, 0.85);
       break;
     default:
@@ -191,5 +171,5 @@
     case aubio_pitcht_yin:
       del_fvec (p->buf);
-      del_aubio_pitchyin (p->yin);
+      del_aubio_pitchyin (p->p_object);
       break;
     case aubio_pitcht_mcomb:
@@ -197,17 +177,17 @@
       del_cvec (p->fftgrain);
       del_aubio_filter (p->filter);
-      del_aubio_pitchmcomb (p->mcomb);
+      del_aubio_pitchmcomb (p->p_object);
       break;
     case aubio_pitcht_schmitt:
       del_fvec (p->buf);
-      del_aubio_pitchschmitt (p->schmitt);
+      del_aubio_pitchschmitt (p->p_object);
       break;
     case aubio_pitcht_fcomb:
       del_fvec (p->buf);
-      del_aubio_pitchfcomb (p->fcomb);
+      del_aubio_pitchfcomb (p->p_object);
       break;
     case aubio_pitcht_yinfft:
       del_fvec (p->buf);
-      del_aubio_pitchyinfft (p->yinfft);
+      del_aubio_pitchyinfft (p->p_object);
       break;
     default:
@@ -251,15 +231,15 @@
   switch (p->mode) {
     case aubio_pitchm_freq:
-      p->freqconv = freqconvpass;
+      p->conv_cb = freqconvpass;
       break;
     case aubio_pitchm_midi:
-      p->freqconv = freqconvmidi;
+      p->conv_cb = freqconvmidi;
       break;
     case aubio_pitchm_cent:
       /* bug: not implemented */
-      p->freqconv = freqconvmidi;
+      p->conv_cb = freqconvmidi;
       break;
     case aubio_pitchm_bin:
-      p->freqconv = freqconvbin;
+      p->conv_cb = freqconvbin;
       break;
     default:
@@ -274,8 +254,8 @@
   switch (p->type) {
     case aubio_pitcht_yin:
-      aubio_pitchyin_set_tolerance (p->yin, tol);
+      aubio_pitchyin_set_tolerance (p->p_object, tol);
       break;
     case aubio_pitcht_yinfft:
-      aubio_pitchyinfft_set_tolerance (p->yinfft, tol);
+      aubio_pitchyinfft_set_tolerance (p->p_object, tol);
       break;
     default:
@@ -285,11 +265,14 @@
 }
 
+
+/* do method, calling the detection callback, then the conversion callback */
 void
 aubio_pitch_do (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
 {
-  p->callback (p, ibuf, obuf);
-  obuf->data[0] = p->freqconv (obuf->data[0], p->srate, p->bufsize);
-}
-
+  p->detect_cb (p, ibuf, obuf);
+  obuf->data[0] = p->conv_cb (obuf->data[0], p->samplerate, p->bufsize);
+}
+
+/* do method for each algorithm */
 void
 aubio_pitch_do_mcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
@@ -297,6 +280,6 @@
   aubio_filter_do (p->filter, ibuf);
   aubio_pvoc_do (p->pv, ibuf, p->fftgrain);
-  aubio_pitchmcomb_do (p->mcomb, p->fftgrain, obuf);
-  obuf->data[0] = aubio_bintofreq (obuf->data[0], p->srate, p->bufsize);
+  aubio_pitchmcomb_do (p->p_object, p->fftgrain, obuf);
+  obuf->data[0] = aubio_bintofreq (obuf->data[0], p->samplerate, p->bufsize);
 }
 
@@ -306,8 +289,8 @@
   smpl_t pitch = 0.;
   aubio_pitch_slideblock (p, ibuf);
-  aubio_pitchyin_do (p->yin, p->buf, obuf);
+  aubio_pitchyin_do (p->p_object, p->buf, obuf);
   pitch = obuf->data[0];
   if (pitch > 0) {
-    pitch = p->srate / (pitch + 0.);
+    pitch = p->samplerate / (pitch + 0.);
   } else {
     pitch = 0.;
@@ -322,8 +305,8 @@
   smpl_t pitch = 0.;
   aubio_pitch_slideblock (p, ibuf);
-  aubio_pitchyinfft_do (p->yinfft, p->buf, obuf);
+  aubio_pitchyinfft_do (p->p_object, p->buf, obuf);
   pitch = obuf->data[0];
   if (pitch > 0) {
-    pitch = p->srate / (pitch + 0.);
+    pitch = p->samplerate / (pitch + 0.);
   } else {
     pitch = 0.;
@@ -336,6 +319,6 @@
 {
   aubio_pitch_slideblock (p, ibuf);
-  aubio_pitchfcomb_do (p->fcomb, p->buf, out);
-  out->data[0] = aubio_bintofreq (out->data[0], p->srate, p->bufsize);
+  aubio_pitchfcomb_do (p->p_object, p->buf, out);
+  out->data[0] = aubio_bintofreq (out->data[0], p->samplerate, p->bufsize);
 }
 
@@ -345,12 +328,31 @@
   smpl_t period, pitch = 0.;
   aubio_pitch_slideblock (p, ibuf);
-  aubio_pitchschmitt_do (p->schmitt, p->buf, out);
+  aubio_pitchschmitt_do (p->p_object, p->buf, out);
   period = out->data[0];
   if (period > 0) {
-    pitch = p->srate / period;
+    pitch = p->samplerate / period;
   } else {
     pitch = 0.;
   }
   out->data[0] = pitch;
+}
+
+/* conversion callbacks */
+smpl_t
+freqconvbin(smpl_t f, uint_t samplerate, uint_t bufsize)
+{
+  return aubio_freqtobin(f, samplerate, bufsize);
+}
+
+smpl_t
+freqconvmidi (smpl_t f, uint_t samplerate UNUSED, uint_t bufsize UNUSED)
+{
+  return aubio_freqtomidi (f);
+}
+
+smpl_t
+freqconvpass (smpl_t f, uint_t samplerate UNUSED, uint_t bufsize UNUSED)
+{
+  return f;
 }
 
@@ -359,6 +361,6 @@
 aubio_pitch_get_confidence (aubio_pitch_t * p)
 {
-  if (p->confidence_callback) {
-    return p->confidence_callback ((void*)(p->pitch));
+  if (p->conf_cb) {
+    return p->conf_cb(p->p_object);
   }
   return 0.;
