src/pitch/pitch.c: add aliases for freq (hertz, Hz, and f0)
[aubio.git] / src / pitch / pitch.c
index 35c12a0..7247ae2 100644 (file)
 #include "pitch/pitchfcomb.h"
 #include "pitch/pitchschmitt.h"
 #include "pitch/pitchyinfft.h"
+#include "pitch/pitchspecacf.h"
 #include "pitch/pitch.h"
 
+#define DEFAULT_PITCH_SILENCE -50.
+
 /** pitch detection algorithms */
 typedef enum
 {
@@ -42,6 +45,7 @@ typedef enum
   aubio_pitcht_schmitt,    /**< `schmitt`, Schmitt trigger */
   aubio_pitcht_fcomb,      /**< `fcomb`, Fast comb filter */
   aubio_pitcht_yinfft,     /**< `yinfft`, Spectral YIN */
+  aubio_pitcht_specacf,    /**< `specacf`, Spectral autocorrelation */
   aubio_pitcht_default
     = aubio_pitcht_yinfft, /**< `default` */
 } aubio_pitch_type;
@@ -80,6 +84,7 @@ struct _aubio_pitch_t
   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 */
+  smpl_t silence;                 /**< silence threshold */
 };
 
 /* callback functions for pitch detection */
@@ -88,6 +93,7 @@ static void aubio_pitch_do_yin (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
 static void aubio_pitch_do_schmitt (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
 static void aubio_pitch_do_fcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
 static void aubio_pitch_do_yinfft (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
+static void aubio_pitch_do_specacf (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
 
 /* conversion functions for frequency conversions */
 smpl_t freqconvbin (smpl_t f, uint_t samplerate, uint_t bufsize);
@@ -114,6 +120,8 @@ new_aubio_pitch (char_t * pitch_mode,
     pitch_type = aubio_pitcht_schmitt;
   else if (strcmp (pitch_mode, "fcomb") == 0)
     pitch_type = aubio_pitcht_fcomb;
+  else if (strcmp (pitch_mode, "specacf") == 0)
+    pitch_type = aubio_pitcht_specacf;
   else if (strcmp (pitch_mode, "default") == 0)
     pitch_type = aubio_pitcht_default;
   else {
@@ -125,6 +133,7 @@ new_aubio_pitch (char_t * pitch_mode,
   p->type = pitch_type;
   aubio_pitch_set_unit (p, "default");
   p->bufsize = bufsize;
+  p->silence = DEFAULT_PITCH_SILENCE;
   p->conf_cb = NULL;
   switch (p->type) {
     case aubio_pitcht_yin:
@@ -153,11 +162,18 @@ new_aubio_pitch (char_t * pitch_mode,
       break;
     case aubio_pitcht_yinfft:
       p->buf = new_fvec (bufsize);
-      p->p_object = new_aubio_pitchyinfft (bufsize);
+      p->p_object = new_aubio_pitchyinfft (samplerate, 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;
+    case aubio_pitcht_specacf:
+      p->buf = new_fvec (bufsize);
+      p->p_object = new_aubio_pitchspecacf (bufsize);
+      p->detect_cb = aubio_pitch_do_specacf;
+      p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchspecacf_get_tolerance;
+      aubio_pitchspecacf_set_tolerance (p->p_object, 0.85);
+      break;
     default:
       break;
   }
@@ -190,6 +206,10 @@ del_aubio_pitch (aubio_pitch_t * p)
       del_fvec (p->buf);
       del_aubio_pitchyinfft (p->p_object);
       break;
+    case aubio_pitcht_specacf:
+      del_fvec (p->buf);
+      del_aubio_pitchspecacf (p->p_object);
+      break;
     default:
       break;
   }
@@ -212,9 +232,16 @@ aubio_pitch_slideblock (aubio_pitch_t * p, fvec_t * ibuf)
 uint_t
 aubio_pitch_set_unit (aubio_pitch_t * p, char_t * pitch_unit)
 {
+  uint_t err = AUBIO_OK;
   aubio_pitch_mode pitch_mode;
   if (strcmp (pitch_unit, "freq") == 0)
     pitch_mode = aubio_pitchm_freq;
+  else if (strcmp (pitch_unit, "hertz") == 0)
+    pitch_mode = aubio_pitchm_freq;
+  else if (strcmp (pitch_unit, "Hz") == 0)
+    pitch_mode = aubio_pitchm_freq;
+  else if (strcmp (pitch_unit, "f0") == 0)
+    pitch_mode = aubio_pitchm_freq;
   else if (strcmp (pitch_unit, "midi") == 0)
     pitch_mode = aubio_pitchm_midi;
   else if (strcmp (pitch_unit, "cent") == 0)
@@ -226,6 +253,7 @@ aubio_pitch_set_unit (aubio_pitch_t * p, char_t * pitch_unit)
   else {
     AUBIO_ERR ("unknown pitch detection unit %s, using default\n", pitch_unit);
     pitch_mode = aubio_pitchm_default;
+    err = AUBIO_FAIL;
   }
   p->mode = pitch_mode;
   switch (p->mode) {
@@ -245,7 +273,7 @@ aubio_pitch_set_unit (aubio_pitch_t * p, char_t * pitch_unit)
     default:
       break;
   }
-  return AUBIO_OK;
+  return err;
 }
 
 uint_t
@@ -264,12 +292,33 @@ aubio_pitch_set_tolerance (aubio_pitch_t * p, smpl_t tol)
   return AUBIO_OK;
 }
 
+uint_t
+aubio_pitch_set_silence (aubio_pitch_t * p, smpl_t silence)
+{
+  if (silence < 0 && silence > -200) {
+    p->silence = silence;
+    return AUBIO_OK;
+  } else {
+    AUBIO_ERR("pitch: could do set silence to %.2f", silence);
+    return AUBIO_FAIL;
+  }
+}
+
+smpl_t
+aubio_pitch_get_silence (aubio_pitch_t * p)
+{
+  return p->silence;
+}
+
 
 /* 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->detect_cb (p, ibuf, obuf);
+  if (aubio_silence_detection(ibuf, p->silence) == 1) {
+    obuf->data[0] = 0.;
+  }
   obuf->data[0] = p->conv_cb (obuf->data[0], p->samplerate, p->bufsize);
 }
 
@@ -315,6 +364,21 @@ aubio_pitch_do_yinfft (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
 }
 
 void
+aubio_pitch_do_specacf (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * out)
+{
+  aubio_pitch_slideblock (p, ibuf);
+  aubio_pitchspecacf_do (p->p_object, p->buf, out);
+  //out->data[0] = aubio_bintofreq (out->data[0], p->samplerate, p->bufsize);
+  smpl_t pitch = 0., period = out->data[0];
+  if (period > 0) {
+    pitch = p->samplerate / period;
+  } else {
+    pitch = 0.;
+  }
+  out->data[0] = pitch;
+}
+
+void
 aubio_pitch_do_fcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * out)
 {
   aubio_pitch_slideblock (p, ibuf);