Merge branch 'master' into awhitening
authorPaul Brossier <piem@piem.org>
Sun, 26 Mar 2017 12:36:43 +0000 (14:36 +0200)
committerPaul Brossier <piem@piem.org>
Sun, 26 Mar 2017 12:36:43 +0000 (14:36 +0200)
18 files changed:
examples/aubioonset.c
examples/aubiotrack.c
python/lib/gen_code.py
python/lib/gen_external.py
src/aubio.h
src/cvec.c
src/cvec.h
src/mathutils.c
src/mathutils.h
src/musicutils.h
src/onset/onset.c
src/onset/onset.h
src/onset/peakpicker.c
src/spectral/awhitening.c [new file with mode: 0644]
src/spectral/awhitening.h [new file with mode: 0644]
src/spectral/specdesc.c
src/spectral/specdesc.h
tests/src/spectral/test-awhitening.c [new file with mode: 0644]

index 032eb00..680471c 100644 (file)
@@ -43,10 +43,12 @@ void process_block(fvec_t *ibuf, fvec_t *obuf)
   } else {
     aubio_wavetable_stop ( wavetable );
   }
-  if (mix_input)
+  if (mix_input) {
     aubio_wavetable_do (wavetable, ibuf, obuf);
-  else
+    fvec_clamp(obuf, 1.);
+  } else {
     aubio_wavetable_do (wavetable, obuf, obuf);
+  }
 }
 
 void process_print (void)
index 36267cf..ba047d3 100644 (file)
@@ -46,10 +46,12 @@ void process_block(fvec_t * ibuf, fvec_t *obuf) {
   } else {
     aubio_wavetable_stop ( wavetable );
   }
-  if (mix_input)
+  if (mix_input) {
     aubio_wavetable_do (wavetable, ibuf, obuf);
-  else
+    fvec_clamp(obuf, 1.);
+  } else {
     aubio_wavetable_do (wavetable, obuf, obuf);
+  }
 }
 
 void process_print (void) {
index c075cc5..d391197 100644 (file)
@@ -183,17 +183,21 @@ class MappedObject(object):
 
     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):
index d46c1f5..c1c69bd 100644 (file)
@@ -39,6 +39,7 @@ skip_objects = [
   'source_wavread',
   #'sampler',
   'audio_unit',
+  'spectral_whitening',
   ]
 
 def get_preprocessor():
index 8c74acb..6a2ca4e 100644 (file)
@@ -187,6 +187,7 @@ extern "C"
 #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"
index 2e98cf5..4ed2a43 100644 (file)
@@ -139,3 +139,10 @@ void cvec_zeros(cvec_t *s) {
   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);
+  }
+}
index d0a9c2b..be4bbab 100644 (file)
@@ -230,6 +230,14 @@ void cvec_phas_ones(cvec_t *s);
 */
 void cvec_zeros(cvec_t *s);
 
+/** take logarithmic magnitude
+
+  \param s input cvec to compress
+  \param lambda value to use for normalisation
+
+*/
+void cvec_logmag(cvec_t *s, smpl_t lambda);
+
 #ifdef __cplusplus
 }
 #endif
index 45f61c9..cd1b719 100644 (file)
@@ -289,6 +289,25 @@ fvec_ishift (fvec_t * s)
   }
 }
 
+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)
 {
index 6638f69..7bef5a4 100644 (file)
@@ -117,6 +117,17 @@ of the resulting spectrum. See Amalia de Götzen's paper referred to above.
 */
 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
index f71d20b..d9638ac 100644 (file)
@@ -156,6 +156,14 @@ uint_t aubio_silence_detection (const fvec_t * v, smpl_t threshold);
 */
 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
index af0b357..fa61261 100644 (file)
 #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 @@ struct _aubio_onset_t {
 
   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_adaptive_whitening;
+  aubio_spectral_whitening_t *spectral_whitening;
 };
 
 /* execute onset detection function on iput buffer */
@@ -49,6 +57,16 @@ void aubio_onset_do (aubio_onset_t *o, const fvec_t * input, fvec_t * onset)
 {
   smpl_t isonset = 0;
   aubio_pvoc_do (o->pv,input, o->fftgrain);
+  /*
+  if (apply_filtering) {
+  }
+  */
+  if (o->apply_adaptive_whitening) {
+    aubio_spectral_whitening_do(o->spectral_whitening, o->fftgrain);
+  }
+  if (o->apply_compression) {
+    cvec_logmag(o->fftgrain, o->apply_compression);
+  }
   aubio_specdesc_do (o->od, o->fftgrain, o->desc);
   aubio_peakpicker_do(o->pp, o->desc, onset);
   isonset = onset->data[0];
@@ -99,6 +117,17 @@ 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)
+{
+  o->apply_adaptive_whitening = apply_adaptive_whitening;
+  return AUBIO_OK;
+}
+
+uint_t aubio_onset_get_adaptive_whitening (aubio_onset_t *o)
+{
+  return o->apply_adaptive_whitening;
+}
+
 uint_t aubio_onset_set_silence(aubio_onset_t * o, smpl_t silence) {
   o->silence = silence;
   return AUBIO_OK;
@@ -209,11 +238,9 @@ aubio_onset_t * new_aubio_onset (const char_t * onset_mode,
   o->fftgrain = new_cvec(buf_size);
   o->desc = new_fvec(1);
 
-  /* 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.);
+  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;
@@ -228,8 +255,50 @@ beach:
   return NULL;
 }
 
+void aubio_onset_default_parameters (aubio_onset_t * o, const char_t * onset_mode)
+{
+  /* 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.;
+
+  /* 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);
+  } 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.;
+  } else if (strcmp (onset_mode, "phase") == 0) {
+    o->apply_compression = 0;
+    aubio_onset_set_adaptive_whitening (o, 0);
+  } else if (strcmp (onset_mode, "mkl") == 0) {
+    aubio_onset_set_threshold (o, 0.05);
+  } else if (strcmp (onset_mode, "kl") == 0) {
+    aubio_onset_set_threshold (o, 0.35);
+  } else if (strcmp (onset_mode, "specflux") == 0) {
+    aubio_onset_set_threshold (o, 0.4);
+  } else if (strcmp (onset_mode, "specdiff") == 0) {
+  } else {
+    AUBIO_WRN("onset: unknown spectral descriptor type %s, "
+               "using default parameters.\n", onset_mode);
+  }
+}
+
 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);
index e788603..17d061c 100644 (file)
@@ -117,6 +117,23 @@ smpl_t aubio_onset_get_last_s (const aubio_onset_t *o);
 */
 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 apply_adaptive_whitening 1 to enable, 0 to disable
+
+*/
+uint_t aubio_onset_set_adaptive_whitening(aubio_onset_t * o, uint_t apply_adaptive_whitening);
+
+/** get onset detection silence threshold
+
+  \param o onset detection object as returned by new_aubio_onset()
+
+  \return adaptive whitening mode, 1 if enabled, 0 otherwise
+
+*/
+uint_t aubio_onset_get_adaptive_whitening(aubio_onset_t * o);
+
 /** set onset detection silence threshold
 
   \param o onset detection object as returned by new_aubio_onset()
index c47d049..fa1ee38 100644 (file)
@@ -92,27 +92,21 @@ aubio_peakpicker_do (aubio_peakpicker_t * p, fvec_t * onset, fvec_t * out)
   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 */
diff --git a/src/spectral/awhitening.c b/src/spectral/awhitening.c
new file mode 100644 (file)
index 0000000..20f7a76
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+ * 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;
+}
+
+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);
+}
diff --git a/src/spectral/awhitening.h b/src/spectral/awhitening.h
new file mode 100644 (file)
index 0000000..e3f8032
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+  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
+
+  */
+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);
+
+/** 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 */
index 67b95c3..adade61 100644 (file)
@@ -30,6 +30,7 @@ void aubio_specdesc_energy(aubio_specdesc_t *o, const cvec_t * fftgrain, fvec_t
 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 @@ typedef enum {
         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 @@ void aubio_specdesc_phase(aubio_specdesc_t *o,
   //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 @@ new_aubio_specdesc (const char_t * onset_mode, uint_t size){
       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)
@@ -291,6 +312,7 @@ new_aubio_specdesc (const char_t * onset_mode, uint_t size){
       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 +347,9 @@ new_aubio_specdesc (const char_t * onset_mode, uint_t size){
     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 +403,7 @@ void del_aubio_specdesc (aubio_specdesc_t *o){
       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);
index 2cdb87a..0f688c1 100644 (file)
   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 @@ void aubio_specdesc_do (aubio_specdesc_t * o, const cvec_t * fftgrain,
 
   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);
diff --git a/tests/src/spectral/test-awhitening.c b/tests/src/spectral/test-awhitening.c
new file mode 100644 (file)
index 0000000..e483141
--- /dev/null
@@ -0,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;
+}
+