shithub: aubio

Download patch

ref: 350211958fe957d85c19fad553f88327bf39d5d2
parent: 0eca01fde43b53a98ede876a6bf7ec9785ca9ae3
parent: c82859f347b082d73b66648b10070a4c45434b9b
author: Paul Brossier <piem@piem.org>
date: Wed Apr 5 07:49:57 EDT 2017

Merge branch 'awhitening'

--- a/examples/aubioonset.c
+++ b/examples/aubioonset.c
@@ -43,10 +43,11 @@
   } else {
     aubio_wavetable_stop ( wavetable );
   }
-  if (mix_input)
+  if (mix_input) {
     aubio_wavetable_do (wavetable, ibuf, obuf);
-  else
+  } else {
     aubio_wavetable_do (wavetable, obuf, obuf);
+  }
 }
 
 void process_print (void)
@@ -61,13 +62,6 @@
   int ret = 0;
   examples_common_init(argc,argv);
 
-  verbmsg ("using source: %s at %dHz\n", source_uri, samplerate);
-  verbmsg ("onset method: %s, ", onset_method);
-  verbmsg ("buffer_size: %d, ", buffer_size);
-  verbmsg ("hop_size: %d, ", hop_size);
-  verbmsg ("silence: %f, ", silence_threshold);
-  verbmsg ("threshold: %f\n", onset_threshold);
-
   o = new_aubio_onset (onset_method, buffer_size, hop_size, samplerate);
   if (o == NULL) { ret = 1; goto beach; }
   if (onset_threshold != 0.)
@@ -76,6 +70,15 @@
     aubio_onset_set_silence (o, silence_threshold);
   if (onset_minioi != 0.)
     aubio_onset_set_minioi_s (o, onset_minioi);
+
+  verbmsg ("using source: %s at %dHz\n", source_uri, samplerate);
+  verbmsg ("onset method: %s, ", onset_method);
+  verbmsg ("buffer_size: %d, ", buffer_size);
+  verbmsg ("hop_size: %d, ", hop_size);
+  verbmsg ("silence: %f, ", aubio_onset_get_silence(o));
+  verbmsg ("threshold: %f, ", aubio_onset_get_threshold(o));
+  verbmsg ("awhitening: %f, ", aubio_onset_get_awhitening(o));
+  verbmsg ("compression: %f\n", aubio_onset_get_compression(o));
 
   onset = new_fvec (1);
 
--- a/examples/aubiotrack.c
+++ b/examples/aubiotrack.c
@@ -46,10 +46,11 @@
   } else {
     aubio_wavetable_stop ( wavetable );
   }
-  if (mix_input)
+  if (mix_input) {
     aubio_wavetable_do (wavetable, ibuf, obuf);
-  else
+  } else {
     aubio_wavetable_do (wavetable, obuf, obuf);
+  }
 }
 
 void process_print (void) {
--- a/python/lib/gen_code.py
+++ b/python/lib/gen_code.py
@@ -183,17 +183,21 @@
 
     def gen_code(self):
         out = ""
-        out += self.gen_struct()
-        out += self.gen_doc()
-        out += self.gen_new()
-        out += self.gen_init()
-        out += self.gen_del()
-        out += self.gen_do()
-        out += self.gen_memberdef()
-        out += self.gen_set()
-        out += self.gen_get()
-        out += self.gen_methodef()
-        out += self.gen_typeobject()
+        try:
+            out += self.gen_struct()
+            out += self.gen_doc()
+            out += self.gen_new()
+            out += self.gen_init()
+            out += self.gen_del()
+            out += self.gen_do()
+            out += self.gen_memberdef()
+            out += self.gen_set()
+            out += self.gen_get()
+            out += self.gen_methodef()
+            out += self.gen_typeobject()
+        except Exception as e:
+            print ("Failed generating code for", self.shortname)
+            raise
         return out
 
     def gen_struct(self):
--- a/python/lib/gen_external.py
+++ b/python/lib/gen_external.py
@@ -39,6 +39,7 @@
   'source_wavread',
   #'sampler',
   'audio_unit',
+  'spectral_whitening',
   ]
 
 def get_preprocessor():
--- a/python/tests/test_onset.py
+++ b/python/tests/test_onset.py
@@ -19,25 +19,25 @@
         self.o = onset(samplerate = self.samplerate)
 
     def test_get_delay(self):
-        assert_equal (self.o.get_delay(), int(4.3 * self.o.hop_size))
+        self.assertGreater(self.o.get_delay(), 0)
 
     def test_get_delay_s(self):
-        assert_almost_equal (self.o.get_delay_s(), self.o.get_delay() / float(self.samplerate))
+        self.assertGreater(self.o.get_delay_s(), 0.)
 
     def test_get_delay_ms(self):
-        assert_almost_equal (self.o.get_delay_ms(), self.o.get_delay() * 1000. / self.samplerate, 5)
+        self.assertGreater(self.o.get_delay_ms(), 0.)
 
     def test_get_minioi(self):
-        assert_almost_equal (self.o.get_minioi(), 0.02 * self.samplerate)
+        self.assertGreater(self.o.get_minioi(), 0)
 
     def test_get_minioi_s(self):
-        assert_almost_equal (self.o.get_minioi_s(), 0.02)
+        self.assertGreater(self.o.get_minioi_s(), 0.)
 
     def test_get_minioi_ms(self):
-        assert_equal (self.o.get_minioi_ms(), 20.)
+        self.assertGreater(self.o.get_minioi_ms(), 0.)
 
     def test_get_threshold(self):
-        assert_almost_equal (self.o.get_threshold(), 0.3)
+        self.assertGreater(self.o.get_threshold(), 0.)
 
     def test_set_delay(self):
         val = 256
--- a/src/aubio.h
+++ b/src/aubio.h
@@ -187,6 +187,7 @@
 #include "spectral/filterbank_mel.h"
 #include "spectral/mfcc.h"
 #include "spectral/specdesc.h"
+#include "spectral/awhitening.h"
 #include "spectral/tss.h"
 #include "pitch/pitch.h"
 #include "onset/onset.h"
--- a/src/cvec.c
+++ b/src/cvec.c
@@ -139,3 +139,10 @@
   cvec_norm_zeros(s);
   cvec_phas_zeros(s);
 }
+
+void cvec_logmag(cvec_t *s, smpl_t lambda) {
+  uint_t j;
+  for (j=0; j< s->length; j++) {
+    s->norm[j] = LOG(lambda * s->norm[j] + 1);
+  }
+}
--- a/src/cvec.h
+++ b/src/cvec.h
@@ -230,6 +230,16 @@
 */
 void cvec_zeros(cvec_t *s);
 
+/** take logarithmic magnitude
+
+  \param s input cvec to compress
+  \param lambda value to use for normalisation
+
+  \f$ S_k = log( \lambda * S_k + 1 ) \f$
+
+*/
+void cvec_logmag(cvec_t *s, smpl_t lambda);
+
 #ifdef __cplusplus
 }
 #endif
--- a/src/mathutils.c
+++ b/src/mathutils.c
@@ -289,6 +289,25 @@
   }
 }
 
+void fvec_push(fvec_t *in, smpl_t new_elem) {
+  uint_t i;
+  for (i = 0; i < in->length - 1; i++) {
+    in->data[i] = in->data[i + 1];
+  }
+  in->data[in->length - 1] = new_elem;
+}
+
+void fvec_clamp(fvec_t *in, smpl_t absmax) {
+  uint_t i;
+  for (i = 0; i < in->length; i++) {
+    if (in->data[i] > 0 && in->data[i] > ABS(absmax)) {
+      in->data[i] = absmax;
+    } else if (in->data[i] < 0 && in->data[i] < -ABS(absmax)) {
+      in->data[i] = -absmax;
+    }
+  }
+}
+
 smpl_t
 aubio_level_lin (const fvec_t * f)
 {
--- a/src/mathutils.h
+++ b/src/mathutils.h
@@ -117,6 +117,17 @@
 */
 void fvec_ishift (fvec_t * v);
 
+/** push a new element to the end of a vector, erasing the first element and
+ * sliding all others
+
+  \param in vector to push to
+  \param new_elem new_element to add at the end of the vector
+
+  In numpy words, this is equivalent to: in = np.concatenate([in, [new_elem]])[1:]
+
+*/
+void fvec_push(fvec_t *in, smpl_t new_elem);
+
 /** compute the sum of all elements of a vector
 
   \param v vector to compute the sum of
--- a/src/musicutils.h
+++ b/src/musicutils.h
@@ -156,6 +156,14 @@
 */
 smpl_t aubio_level_detection (const fvec_t * v, smpl_t threshold);
 
+/** clamp the values of a vector within the range [-abs(max), abs(max)]
+
+  \param in vector to clamp
+  \param absmax maximum value over which input vector elements should be clamped
+
+*/
+void fvec_clamp(fvec_t *in, smpl_t absmax);
+
 #ifdef __cplusplus
 }
 #endif
--- a/src/onset/onset.c
+++ b/src/onset/onset.c
@@ -23,10 +23,13 @@
 #include "cvec.h"
 #include "spectral/specdesc.h"
 #include "spectral/phasevoc.h"
+#include "spectral/awhitening.h"
 #include "onset/peakpicker.h"
 #include "mathutils.h"
 #include "onset/onset.h"
 
+void aubio_onset_default_parameters (aubio_onset_t *o, const char_t * method);
+
 /** structure to store object state */
 struct _aubio_onset_t {
   aubio_pvoc_t * pv;            /**< phase vocoder */
@@ -42,6 +45,11 @@
 
   uint_t total_frames;          /**< total number of frames processed since the beginning */
   uint_t last_onset;            /**< last detected onset location, in frames */
+
+  uint_t apply_compression;
+  smpl_t lambda_compression;
+  uint_t apply_awhitening;      /**< apply adaptive spectral whitening */
+  aubio_spectral_whitening_t *spectral_whitening;
 };
 
 /* execute onset detection function on iput buffer */
@@ -49,6 +57,16 @@
 {
   smpl_t isonset = 0;
   aubio_pvoc_do (o->pv,input, o->fftgrain);
+  /*
+  if (apply_filtering) {
+  }
+  */
+  if (o->apply_awhitening) {
+    aubio_spectral_whitening_do(o->spectral_whitening, o->fftgrain);
+  }
+  if (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);
   isonset = onset->data[0];
@@ -57,10 +75,17 @@
       //AUBIO_DBG ("silent onset, not marking as onset\n");
       isonset  = 0;
     } else {
+      // we have an onset
       uint_t new_onset = o->total_frames + (uint_t)ROUND(isonset * o->hop_size);
+      // check if last onset time was more than minioi ago
       if (o->last_onset + o->minioi < new_onset) {
-        //AUBIO_DBG ("accepted detection, marking as onset\n");
-        o->last_onset = new_onset;
+        // start of file: make sure (new_onset - delay) >= 0
+        if (o->last_onset > 0 && o->delay > new_onset) {
+          isonset = 0;
+        } else {
+          //AUBIO_DBG ("accepted detection, marking as onset\n");
+          o->last_onset = MAX(o->delay, new_onset);
+        }
       } else {
         //AUBIO_DBG ("doubled onset, not marking as onset\n");
         isonset  = 0;
@@ -99,6 +124,32 @@
   return aubio_onset_get_last_s (o) * 1000.;
 }
 
+uint_t aubio_onset_set_awhitening (aubio_onset_t *o, uint_t enable)
+{
+  o->apply_awhitening = enable == 1 ? 1 : 0;
+  return AUBIO_OK;
+}
+
+smpl_t aubio_onset_get_awhitening (aubio_onset_t *o)
+{
+  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) {
   o->silence = silence;
   return AUBIO_OK;
@@ -208,16 +259,12 @@
   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);
 
-  /* set some default parameter */
-  aubio_onset_set_threshold (o, 0.3);
-  aubio_onset_set_delay(o, 4.3 * hop_size);
-  aubio_onset_set_minioi_ms(o, 20.);
-  aubio_onset_set_silence(o, -70.);
-
   /* 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:
@@ -228,8 +275,69 @@
   return NULL;
 }
 
+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.);
+  // 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);
+    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);
+    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_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.18);
+    aubio_onset_set_awhitening(o, 1);
+    aubio_spectral_whitening_set_relax_time(o->spectral_whitening, 100);
+    aubio_spectral_whitening_set_floor(o->spectral_whitening, 1.);
+    aubio_onset_set_compression (o, 10.);
+  } else if (strcmp (onset_mode, "specdiff") == 0) {
+  } else if (strcmp (onset_mode, "old_default") == 0) {
+    // used to reproduce results obtained with the previous version
+    aubio_onset_set_threshold (o, 0.3);
+    aubio_onset_set_minioi_ms (o, 20.);
+    aubio_onset_set_compression (o, 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)
 {
+  del_aubio_spectral_whitening(o->spectral_whitening);
   del_aubio_specdesc(o->od);
   del_aubio_peakpicker(o->pp);
   del_aubio_pvoc(o->pv);
--- a/src/onset/onset.h
+++ b/src/onset/onset.h
@@ -117,6 +117,44 @@
 */
 smpl_t aubio_onset_get_last_ms (const aubio_onset_t *o);
 
+/** set onset detection adaptive whitening
+
+  \param o onset detection object as returned by new_aubio_onset()
+  \param enable 1 to enable, 0 to disable
+
+  \return 0 if successful, 1 otherwise
+
+*/
+uint_t aubio_onset_set_awhitening(aubio_onset_t * o, uint_t enable);
+
+/** get onset detection adaptive whitening
+
+  \param o onset detection object as returned by new_aubio_onset()
+
+  \return 1 if enabled, 0 otherwise
+
+*/
+smpl_t aubio_onset_get_awhitening(aubio_onset_t * o);
+
+/** set or disable log compression
+
+  \param o onset detection object as returned by new_aubio_onset()
+  \param lambda logarithmic compression factor, 0 to disable
+
+  \return 0 if successful, 1 otherwise
+
+ */
+uint_t aubio_onset_set_compression(aubio_onset_t *o, smpl_t lambda);
+
+/** get onset detection log compression
+
+  \param o onset detection object as returned by new_aubio_onset()
+
+  \returns 0 if disabled, compression factor otherwise
+
+ */
+smpl_t aubio_onset_get_compression(aubio_onset_t *o);
+
 /** set onset detection silence threshold
 
   \param o onset detection object as returned by new_aubio_onset()
@@ -273,6 +311,27 @@
 
 */
 smpl_t aubio_onset_get_threshold(const aubio_onset_t * o);
+
+/** set default parameters
+
+  \param o onset detection object as returned by new_aubio_onset()
+  \param onset_mode detection mode to adjust
+
+  This function is called at the end of new_aubio_onset().
+
+ */
+uint_t aubio_onset_set_default_parameters (aubio_onset_t * o, const char_t * onset_mode);
+
+/** reset onset detection
+
+  \param o onset detection object as returned by new_aubio_onset()
+
+  Reset current time and last onset to 0.
+
+  This function is called at the end of new_aubio_onset().
+
+ */
+void aubio_onset_reset(aubio_onset_t * o);
 
 /** delete onset detection object
 
--- a/src/onset/peakpicker.c
+++ b/src/onset/peakpicker.c
@@ -92,27 +92,21 @@
   fvec_t *thresholded = p->thresholded;
   fvec_t *scratch = p->scratch;
   smpl_t mean = 0., median = 0.;
-  uint_t length = p->win_post + p->win_pre + 1;
   uint_t j = 0;
 
-  /* store onset in onset_keep */
-  /* shift all elements but last, then write last */
-  for (j = 0; j < length - 1; j++) {
-    onset_keep->data[j] = onset_keep->data[j + 1];
-    onset_proc->data[j] = onset_keep->data[j];
-  }
-  onset_keep->data[length - 1] = onset->data[0];
-  onset_proc->data[length - 1] = onset->data[0];
+  /* push new novelty to the end */
+  fvec_push(onset_keep, onset->data[0]);
+  /* store a copy */
+  fvec_copy(onset_keep, onset_proc);
 
-  /* filter onset_proc */
-  /** \bug filtfilt calculated post+pre times, should be only once !? */
+  /* filter this copy */
   aubio_filter_do_filtfilt (p->biquad, onset_proc, scratch);
 
   /* calculate mean and median for onset_proc */
   mean = fvec_mean (onset_proc);
-  /* copy to scratch */
-  for (j = 0; j < length; j++)
-    scratch->data[j] = onset_proc->data[j];
+
+  /* copy to scratch and compute its median */
+  fvec_copy(onset_proc, scratch);
   median = p->thresholdfn (scratch);
 
   /* shift peek array */
--- /dev/null
+++ b/src/spectral/awhitening.c
@@ -1,0 +1,120 @@
+/*
+ * Copyright (C) 2003-2015 Paul Brossier <piem@aubio.org>
+ *
+ * This file is part of aubio.
+ *
+ * aubio is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ *
+ * aubio is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * aubio.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include "aubio_priv.h"
+#include "fvec.h"
+#include "cvec.h"
+#include "mathutils.h"
+#include "spectral/awhitening.h"
+
+#define aubio_spectral_whitening_default_relax_time   250   // in seconds, between 22 and 446
+#define aubio_spectral_whitening_default_decay        0.001 // -60dB attenuation
+#define aubio_spectral_whitening_default_floor        1.e-4 // from 1.e-6 to .2
+
+/** structure to store object state */
+struct _aubio_spectral_whitening_t {
+  uint_t buf_size;
+  uint_t hop_size;
+  uint_t samplerate;
+  smpl_t relax_time;
+  smpl_t r_decay;
+  smpl_t floor;
+  fvec_t *peak_values;
+};
+
+void
+aubio_spectral_whitening_do (aubio_spectral_whitening_t * o, cvec_t * fftgrain)
+{
+  uint_t i = 0;
+  for (i = 0; i < o->peak_values->length; i++) {
+    smpl_t tmp = MAX(o->r_decay * o->peak_values->data[i], o->floor);
+    o->peak_values->data[i] = MAX(fftgrain->norm[i], tmp);
+    fftgrain->norm[i] /= o->peak_values->data[i];
+  }
+}
+
+aubio_spectral_whitening_t *
+new_aubio_spectral_whitening (uint_t buf_size, uint_t hop_size, uint_t samplerate)
+{
+  aubio_spectral_whitening_t *o = AUBIO_NEW (aubio_spectral_whitening_t);
+  if ((sint_t)buf_size < 1) {
+    AUBIO_ERR("spectral_whitening: got buffer_size %d, but can not be < 1\n", buf_size);
+    goto beach;
+  } else if ((sint_t)hop_size < 1) {
+    AUBIO_ERR("spectral_whitening: got hop_size %d, but can not be < 1\n", hop_size);
+    goto beach;
+  } else if ((sint_t)samplerate < 1) {
+    AUBIO_ERR("spectral_whitening: got samplerate %d, but can not be < 1\n", samplerate);
+    goto beach;
+  }
+  o->peak_values = new_fvec (buf_size / 2 + 1);
+  o->buf_size = buf_size;
+  o->hop_size = hop_size;
+  o->samplerate = samplerate;
+  o->floor = aubio_spectral_whitening_default_floor;
+  aubio_spectral_whitening_set_relax_time (o, aubio_spectral_whitening_default_relax_time);
+  aubio_spectral_whitening_reset (o);
+  return o;
+
+beach:
+  AUBIO_FREE(o);
+  return NULL;
+}
+
+uint_t
+aubio_spectral_whitening_set_relax_time (aubio_spectral_whitening_t * o, smpl_t relax_time)
+{
+  o->relax_time = relax_time;
+  o->r_decay = POW (aubio_spectral_whitening_default_decay,
+      (o->hop_size / (float) o->samplerate) / o->relax_time);
+  return AUBIO_OK;
+}
+
+smpl_t
+aubio_spectral_whitening_get_relax_time (aubio_spectral_whitening_t * o)
+{
+  return o->relax_time;
+}
+
+uint_t
+aubio_spectral_whitening_set_floor (aubio_spectral_whitening_t *o, smpl_t floor)
+{
+  o->floor = floor;
+  return AUBIO_OK;
+}
+
+smpl_t aubio_spectral_whitening_get_floor (aubio_spectral_whitening_t *o)
+{
+  return o->floor;
+}
+
+void
+aubio_spectral_whitening_reset (aubio_spectral_whitening_t * o)
+{
+  /* cover the case n == 0. */
+  fvec_set_all (o->peak_values, o->floor);
+}
+
+void
+del_aubio_spectral_whitening (aubio_spectral_whitening_t * o)
+{
+  del_fvec (o->peak_values);
+  AUBIO_FREE (o);
+}
--- /dev/null
+++ b/src/spectral/awhitening.h
@@ -1,0 +1,125 @@
+/*
+  Copyright (C) 2003-2015 Paul Brossier <piem@aubio.org>
+
+  This file is part of aubio.
+
+  aubio is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  aubio is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+/** \file
+
+  Spectral adaptive whitening
+
+  References:
+
+  D. Stowell and M. D. Plumbley. Adaptive whitening for improved real-time
+  audio onset detection. In Proceedings of the International Computer Music
+  Conference (ICMC), 2007, Copenhagen, Denmark.
+
+  http://www.eecs.qmul.ac.uk/~markp/2007/StowellPlumbley07-icmc.pdf
+
+  S. Böck,, F. Krebs, and M. Schedl. Evaluating the Online Capabilities of
+  Onset Detection Methods. In Proceedings of the 13th International Society for
+  Music Information Retrieval Conference (ISMIR), 2012, Porto, Portugal.
+
+  http://ismir2012.ismir.net/event/papers/049_ISMIR_2012.pdf
+  http://www.cp.jku.at/research/papers/Boeck_etal_ISMIR_2012.pdf
+
+*/
+
+
+#ifndef _AUBIO_SPECTRAL_WHITENING_H
+#define _AUBIO_SPECTRAL_WHITENING_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** spectral whitening structure */
+typedef struct _aubio_spectral_whitening_t aubio_spectral_whitening_t;
+
+/** execute spectral adaptive whitening, in-place
+
+  \param o spectral whitening object as returned by new_aubio_spectral_whitening()
+  \param fftgrain input signal spectrum as computed by aubio_pvoc_do() or aubio_fft_do()
+
+*/
+void aubio_spectral_whitening_do (aubio_spectral_whitening_t * o,
+                                  cvec_t * fftgrain);
+
+/** creation of a spectral whitening object
+
+  \param buf_size window size of input grains
+  \param hop_size number of samples between two consecutive input grains
+  \param samplerate sampling rate of the input signal
+
+*/
+aubio_spectral_whitening_t *new_aubio_spectral_whitening (uint_t buf_size,
+                                                          uint_t hop_size,
+                                                          uint_t samplerate);
+
+/** reset spectral whitening object
+
+  \param o spectral whitening object as returned by new_aubio_spectral_whitening()
+
+ */
+void aubio_spectral_whitening_reset (aubio_spectral_whitening_t * o);
+
+/** set relaxation time for spectral whitening
+
+  \param o spectral whitening object as returned by new_aubio_spectral_whitening()
+  \param relax_time relaxation time in seconds between 20 and 500, defaults 250
+
+  */
+uint_t aubio_spectral_whitening_set_relax_time (aubio_spectral_whitening_t * o,
+    smpl_t relax_time);
+
+/** get relaxation time of spectral whitening
+
+  \param o spectral whitening object as returned by new_aubio_spectral_whitening()
+  \return relaxation time in seconds
+
+*/
+smpl_t aubio_spectral_whitening_get_relax_time (aubio_spectral_whitening_t * o);
+
+/** set floor for spectral whitening
+
+  \param o spectral whitening object as returned by new_aubio_spectral_whitening()
+  \param floor value (typically between 1.e-6 and .2, defaults to 1.e-4)
+
+  */
+uint_t aubio_spectral_whitening_set_floor (aubio_spectral_whitening_t * o,
+    smpl_t floor);
+
+/** get floor of spectral whitening
+
+  \param o spectral whitening object as returned by new_aubio_spectral_whitening()
+  \return floor value
+
+*/
+smpl_t aubio_spectral_whitening_get_floor (aubio_spectral_whitening_t * o);
+
+/** deletion of a spectral whitening
+
+  \param o spectral whitening object as returned by new_aubio_spectral_whitening()
+
+*/
+void del_aubio_spectral_whitening (aubio_spectral_whitening_t * o);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _AUBIO_SPECTRAL_WHITENING_H */
--- a/src/spectral/specdesc.c
+++ b/src/spectral/specdesc.c
@@ -30,6 +30,7 @@
 void aubio_specdesc_hfc(aubio_specdesc_t *o, const cvec_t * fftgrain, fvec_t * onset);
 void aubio_specdesc_complex(aubio_specdesc_t *o, const cvec_t * fftgrain, fvec_t * onset);
 void aubio_specdesc_phase(aubio_specdesc_t *o, const cvec_t * fftgrain, fvec_t * onset);
+void aubio_specdesc_wphase(aubio_specdesc_t *o, const cvec_t * fftgrain, fvec_t * onset);
 void aubio_specdesc_specdiff(aubio_specdesc_t *o, const cvec_t * fftgrain, fvec_t * onset);
 void aubio_specdesc_kl(aubio_specdesc_t *o, const cvec_t * fftgrain, fvec_t * onset);
 void aubio_specdesc_mkl(aubio_specdesc_t *o, const cvec_t * fftgrain, fvec_t * onset);
@@ -57,6 +58,7 @@
         aubio_onset_hfc,            /**< high frequency content */
         aubio_onset_complex,        /**< complex domain */        
         aubio_onset_phase,          /**< phase fast */            
+        aubio_onset_wphase,         /**< weighted phase */
         aubio_onset_kl,             /**< Kullback Liebler */
         aubio_onset_mkl,            /**< modified Kullback Liebler */
         aubio_onset_specflux,       /**< spectral flux */
@@ -159,6 +161,23 @@
   //onset->data[0] = fvec_mean(o->dev1);
 }
 
+/* weighted phase */
+void
+aubio_specdesc_wphase(aubio_specdesc_t *o,
+    const cvec_t *fftgrain, fvec_t *onset) {
+  uint_t i;
+  aubio_specdesc_phase(o, fftgrain, onset);
+  for (i = 0; i < fftgrain->length; i++) {
+    o->dev1->data[i] *= fftgrain->norm[i];
+  }
+  /* apply o->histogram */
+  aubio_hist_dyn_notnull(o->histog,o->dev1);
+  /* weight it */
+  aubio_hist_weight(o->histog);
+  /* its mean is the result */
+  onset->data[0] = aubio_hist_mean(o->histog);
+}
+
 /* Spectral difference method onset detection function */
 void aubio_specdesc_specdiff(aubio_specdesc_t *o,
     const cvec_t * fftgrain, fvec_t * onset){
@@ -250,6 +269,8 @@
       onset_type = aubio_onset_complex;
   else if (strcmp (onset_mode, "phase") == 0)
       onset_type = aubio_onset_phase;
+  else if (strcmp (onset_mode, "wphase") == 0)
+      onset_type = aubio_onset_wphase;
   else if (strcmp (onset_mode, "mkl") == 0)
       onset_type = aubio_onset_mkl;
   else if (strcmp (onset_mode, "kl") == 0)
@@ -270,6 +291,8 @@
       onset_type = aubio_specmethod_decrease;
   else if (strcmp (onset_mode, "rolloff") == 0)
       onset_type = aubio_specmethod_rolloff;
+  else if (strcmp (onset_mode, "old_default") == 0)
+      onset_type = aubio_onset_default;
   else if (strcmp (onset_mode, "default") == 0)
       onset_type = aubio_onset_default;
   else {
@@ -291,6 +314,7 @@
       o->theta2 = new_fvec(rsize);
       break;
     case aubio_onset_phase:
+    case aubio_onset_wphase:
       o->dev1   = new_fvec(rsize);
       o->theta1 = new_fvec(rsize);
       o->theta2 = new_fvec(rsize);
@@ -325,6 +349,9 @@
     case aubio_onset_phase:
       o->funcpointer = aubio_specdesc_phase;
       break;
+    case aubio_onset_wphase:
+      o->funcpointer = aubio_specdesc_wphase;
+      break;
     case aubio_onset_specdiff:
       o->funcpointer = aubio_specdesc_specdiff;
       break;
@@ -378,6 +405,7 @@
       del_fvec(o->theta2);
       break;
     case aubio_onset_phase:
+    case aubio_onset_wphase:
       del_fvec(o->dev1);
       del_fvec(o->theta1);
       del_fvec(o->theta2);
--- a/src/spectral/specdesc.h
+++ b/src/spectral/specdesc.h
@@ -59,6 +59,13 @@
   Conference on Acoustics Speech and Signal Processing, pages 441­444,
   Hong-Kong, 2003.
 
+  \b \p wphase : Weighted Phase Deviation onset detection function
+
+  S. Dixon. Onset detection revisited. In Proceedings of the 9th International
+  Conference on Digital Audio Ef- fects (DAFx) , pages 133–137, 2006.
+
+  http://www.eecs.qmul.ac.uk/~simond/pub/2006/dafx.pdf
+
   \b \p specdiff : Spectral difference method onset detection function
 
   Jonhatan Foote and Shingo Uchihashi. The beat spectrum: a new approach to
@@ -174,8 +181,11 @@
 
   The parameter \p method is a string that can be any of:
 
-    - `energy`, `hfc`, `complex`, `phase`, `specdiff`, `kl`, `mkl`, `specflux`
-    - `centroid`, `spread`, `skewness`, `kurtosis`, `slope`, `decrease`, `rolloff`
+    - onset novelty functions: `complex`, `energy`, `hfc`, `kl`, `mkl`,
+    `phase`, `specdiff`, `specflux`, `wphase`,
+
+    - spectral descriptors: `centroid`, `decrease`, `kurtosis`, `rolloff`,
+    `skewness`, `slope`, `spread`.
 
 */
 aubio_specdesc_t *new_aubio_specdesc (const char_t * method, uint_t buf_size);
--- a/src/synth/wavetable.c
+++ b/src/synth/wavetable.c
@@ -103,6 +103,7 @@
     for (i = 0; i < output->length; i++) {
       output->data[i] += input->data[i];
     }
+    fvec_clamp(output, 1.);
   }
 }
 
--- /dev/null
+++ b/tests/src/spectral/test-awhitening.c
@@ -1,0 +1,84 @@
+#include <aubio.h>
+#include "utils_tests.h"
+
+int main (int argc, char **argv)
+{
+  sint_t err = 0;
+
+  if (argc < 3) {
+    err = 2;
+    PRINT_ERR("not enough arguments\n");
+    PRINT_MSG("usage: %s <input_path> <output_path> [samplerate] [hop_size]\n", argv[0]);
+    return err;
+  }
+
+  uint_t samplerate = 0;
+  uint_t win_size = 1024;
+  uint_t hop_size = 512;
+  uint_t n_frames = 0, read = 0;
+
+  char_t *source_path = argv[1];
+  char_t *sink_path = argv[2];
+
+  if ( argc >= 4 ) samplerate = atoi(argv[3]);
+  if ( argc >= 5 ) hop_size = atoi(argv[4]);
+  if ( argc >= 6 ) {
+    err = 2;
+    PRINT_ERR("too many arguments\n");
+    return err;
+  }
+
+  fvec_t *vec = new_fvec(hop_size);
+  fvec_t *out = new_fvec(hop_size); // output buffer
+  fvec_t *scale = new_fvec(hop_size);
+  cvec_t *fftgrain = new_cvec(win_size); // fft norm and phase
+  if (!vec) { err = 1; goto beach_fvec; }
+
+  aubio_source_t *i = new_aubio_source(source_path, samplerate, hop_size);
+  if (!i) { err = 1; goto beach_source; }
+
+  if (samplerate == 0 ) samplerate = aubio_source_get_samplerate(i);
+
+  aubio_sink_t *o = new_aubio_sink(sink_path, samplerate);
+  if (!o) { err = 1; goto beach_sink; }
+
+  aubio_pvoc_t *pv = new_aubio_pvoc(win_size, hop_size);
+
+  aubio_spectral_whitening_t *awhitening =
+    new_aubio_spectral_whitening (win_size, hop_size, samplerate);
+
+  aubio_spectral_whitening_set_relax_time(awhitening, 20.);
+  fvec_set_all(scale, 3.);
+
+  PRINT_MSG("spectral whitening relaxation time is %f\n",
+      aubio_spectral_whitening_get_relax_time(awhitening));
+
+  do {
+    aubio_source_do(i, vec, &read);
+    aubio_pvoc_do(pv, vec, fftgrain);
+    // apply spectral whitening
+    aubio_spectral_whitening_do(awhitening, fftgrain);
+    // rebuild the signal
+    aubio_pvoc_rdo(pv, fftgrain, out);
+    // make louder
+    fvec_weight(out, scale);
+    // make sure we dont saturate
+    fvec_clamp(out, 1.);
+    // write output
+    aubio_sink_do(o, out, read);
+    n_frames += read;
+  } while ( read == hop_size );
+
+  PRINT_MSG("read %d frames at %dHz (%d blocks) from %s written to %s\n",
+      n_frames, samplerate, n_frames / hop_size,
+      source_path, sink_path);
+
+  del_aubio_sink(o);
+beach_sink:
+  del_aubio_source(i);
+beach_source:
+  del_fvec(vec);
+beach_fvec:
+  return err;
+}
+