src/onset/onset.c: improve default parameters
[aubio.git] / src / onset / onset.c
index fa61261..5961c72 100644 (file)
@@ -48,7 +48,7 @@ struct _aubio_onset_t {
 
   uint_t apply_compression;
   smpl_t lambda_compression;
-  uint_t apply_adaptive_whitening;
+  uint_t apply_awhitening;      /**< apply adaptive spectral whitening */
   aubio_spectral_whitening_t *spectral_whitening;
 };
 
@@ -61,11 +61,11 @@ void aubio_onset_do (aubio_onset_t *o, const fvec_t * input, fvec_t * onset)
   if (apply_filtering) {
   }
   */
-  if (o->apply_adaptive_whitening) {
+  if (o->apply_awhitening) {
     aubio_spectral_whitening_do(o->spectral_whitening, o->fftgrain);
   }
   if (o->apply_compression) {
-    cvec_logmag(o->fftgrain, o->apply_compression);
+    cvec_logmag(o->fftgrain, o->lambda_compression);
   }
   aubio_specdesc_do (o->od, o->fftgrain, o->desc);
   aubio_peakpicker_do(o->pp, o->desc, onset);
@@ -117,15 +117,30 @@ smpl_t aubio_onset_get_last_ms (const aubio_onset_t *o)
   return aubio_onset_get_last_s (o) * 1000.;
 }
 
-uint_t aubio_onset_set_adaptive_whitening (aubio_onset_t *o, uint_t apply_adaptive_whitening)
+uint_t aubio_onset_set_awhitening (aubio_onset_t *o, uint_t enable)
 {
-  o->apply_adaptive_whitening = apply_adaptive_whitening;
+  o->apply_awhitening = enable == 1 ? 1 : 0;
   return AUBIO_OK;
 }
 
-uint_t aubio_onset_get_adaptive_whitening (aubio_onset_t *o)
+smpl_t aubio_onset_get_awhitening (aubio_onset_t *o)
 {
-  return o->apply_adaptive_whitening;
+  return o->apply_awhitening;
+}
+
+uint_t aubio_onset_set_compression (aubio_onset_t *o, smpl_t lambda)
+{
+  if (lambda < 0.) {
+    return AUBIO_FAIL;
+  }
+  o->lambda_compression = lambda;
+  o->apply_compression = (o->lambda_compression > 0.) ? 1 : 0;
+  return AUBIO_OK;
+}
+
+smpl_t aubio_onset_get_compression (aubio_onset_t *o)
+{
+  return o->apply_compression ? o->lambda_compression : 0;
 }
 
 uint_t aubio_onset_set_silence(aubio_onset_t * o, smpl_t silence) {
@@ -237,14 +252,12 @@ aubio_onset_t * new_aubio_onset (const char_t * onset_mode,
   if (o->od == NULL) goto beach_specdesc;
   o->fftgrain = new_cvec(buf_size);
   o->desc = new_fvec(1);
-
   o->spectral_whitening = new_aubio_spectral_whitening(buf_size, hop_size, samplerate);
 
-  aubio_onset_default_parameters (o, onset_mode);
-
   /* initialize internal variables */
-  o->last_onset = 0;
-  o->total_frames = 0;
+  aubio_onset_set_default_parameters (o, onset_mode);
+
+  aubio_onset_reset(o);
   return o;
 
 beach_specdesc:
@@ -255,45 +268,57 @@ beach:
   return NULL;
 }
 
-void aubio_onset_default_parameters (aubio_onset_t * o, const char_t * onset_mode)
+void aubio_onset_reset (aubio_onset_t *o) {
+  o->last_onset = 0;
+  o->total_frames = 0;
+}
+
+uint_t aubio_onset_set_default_parameters (aubio_onset_t * o, const char_t * onset_mode)
 {
+  uint_t ret = AUBIO_OK;
   /* set some default parameter */
   aubio_onset_set_threshold (o, 0.3);
   aubio_onset_set_delay (o, 4.3 * o->hop_size);
   aubio_onset_set_minioi_ms (o, 50.);
   aubio_onset_set_silence (o, -70.);
-  aubio_onset_set_adaptive_whitening (o, 0);
-
-  o->apply_compression = 0;
-  o->lambda_compression = 1.;
+  // disable spectral whitening
+  aubio_onset_set_awhitening (o, 0);
+  // disable logarithmic magnitude
+  aubio_onset_set_compression (o, 0.);
 
   /* method specific optimisations */
   if (strcmp (onset_mode, "energy") == 0) {
   } else if (strcmp (onset_mode, "hfc") == 0 || strcmp (onset_mode, "default") == 0) {
     aubio_onset_set_threshold (o, 0.058);
-    o->apply_compression = 1;
-    o->lambda_compression = 1.;
-    aubio_onset_set_adaptive_whitening (o, 0);
+    aubio_onset_set_compression (o, 1.);
   } else if (strcmp (onset_mode, "complexdomain") == 0
              || strcmp (onset_mode, "complex") == 0) {
     aubio_onset_set_delay (o, 4.6 * o->hop_size);
     aubio_onset_set_threshold (o, 0.15);
-    o->apply_compression = 1;
-    o->lambda_compression = 1.;
+    aubio_onset_set_awhitening(o, 1);
+    aubio_onset_set_compression (o, 1.);
   } else if (strcmp (onset_mode, "phase") == 0) {
     o->apply_compression = 0;
-    aubio_onset_set_adaptive_whitening (o, 0);
+    aubio_onset_set_awhitening (o, 0);
   } else if (strcmp (onset_mode, "mkl") == 0) {
     aubio_onset_set_threshold (o, 0.05);
+    aubio_onset_set_awhitening(o, 1);
+    aubio_onset_set_compression (o, 0.02);
   } else if (strcmp (onset_mode, "kl") == 0) {
     aubio_onset_set_threshold (o, 0.35);
+    aubio_onset_set_awhitening(o, 1);
+    aubio_onset_set_compression (o, 0.02);
   } else if (strcmp (onset_mode, "specflux") == 0) {
-    aubio_onset_set_threshold (o, 0.4);
+    aubio_onset_set_threshold (o, 0.25);
+    aubio_onset_set_awhitening(o, 1);
+    aubio_onset_set_compression (o, 20.);
   } else if (strcmp (onset_mode, "specdiff") == 0) {
   } else {
     AUBIO_WRN("onset: unknown spectral descriptor type %s, "
                "using default parameters.\n", onset_mode);
+    ret = AUBIO_FAIL;
   }
+  return ret;
 }
 
 void del_aubio_onset (aubio_onset_t *o)