shithub: aubio

Download patch

ref: 2eb52bd821788405d5e380c71249ac91d58d22ed
parent: adde1baafb8d35ab9ed7c0e59bd410cbc4a3c9e3
parent: 8eecb9f21796dba241e6c8292166fe085eb90d8e
author: Paul Brossier <piem@piem.org>
date: Sat Nov 17 11:36:09 EST 2018

Merge branch 'feature/mfccparams'

--- a/python/lib/gen_code.py
+++ b/python/lib/gen_code.py
@@ -462,23 +462,36 @@
 // {shortname} setters
 """.format(**self.__dict__)
         for set_param in self.prototypes['set']:
-            params = get_params_types_names(set_param)[1]
-            paramtype = params['type']
+            params = get_params_types_names(set_param)[1:]
+            param = self.shortname.split('_set_')[-1]
+            paramdecls = "".join(["""
+   {0} {1};""".format(p['type'], p['name']) for p in params])
             method_name = get_name(set_param)
             param = method_name.split('aubio_'+self.shortname+'_set_')[-1]
-            pyparamtype = pyargparse_chars[paramtype]
+            refs = ", ".join(["&%s" % p['name'] for p in params])
+            paramlist = ", ".join(["%s" % p['name'] for p in params])
+            if len(params):
+                paramlist = "," + paramlist
+            pyparamtypes = ''.join([pyargparse_chars[p['type']] for p in params])
             out += """
 static PyObject *
 Pyaubio_{shortname}_set_{param} (Py_{shortname} *self, PyObject *args)
 {{
   uint_t err = 0;
-  {paramtype} {param};
+  {paramdecls}
+""".format(param = param, paramdecls = paramdecls, **self.__dict__)
 
-  if (!PyArg_ParseTuple (args, "{pyparamtype}", &{param})) {{
+            if len(refs) and len(pyparamtypes):
+                out += """
+
+  if (!PyArg_ParseTuple (args, "{pyparamtypes}", {refs})) {{
     return NULL;
   }}
-  err = aubio_{shortname}_set_{param} (self->o, {param});
+""".format(pyparamtypes = pyparamtypes, refs = refs)
 
+            out += """
+  err = aubio_{shortname}_set_{param} (self->o {paramlist});
+
   if (err > 0) {{
     if (PyErr_Occurred() == NULL) {{
       PyErr_SetString (PyExc_ValueError, "error running aubio_{shortname}_set_{param}");
@@ -492,7 +505,8 @@
   }}
   Py_RETURN_NONE;
 }}
-""".format(param = param, paramtype = paramtype, pyparamtype = pyparamtype, **self.__dict__)
+""".format(param = param, refs = refs, paramdecls = paramdecls,
+        pyparamtypes = pyparamtypes, paramlist = paramlist, **self.__dict__)
         return out
 
     def gen_get(self):
--- a/src/mathutils.c
+++ b/src/mathutils.c
@@ -387,6 +387,15 @@
   }
 }
 
+void
+fvec_mul (fvec_t *o, smpl_t val)
+{
+  uint_t j;
+  for (j = 0; j < o->length; j++) {
+    o->data[j] *= val;
+  }
+}
+
 void fvec_adapt_thres(fvec_t * vec, fvec_t * tmp,
     uint_t post, uint_t pre) {
   uint_t length = vec->length, j;
--- a/src/mathutils.h
+++ b/src/mathutils.h
@@ -193,6 +193,14 @@
 */
 void fvec_add (fvec_t * v, smpl_t c);
 
+/** multiply each elements of a vector by a scalar
+
+  \param v vector to add constant to
+  \param s constant to scale v with
+
+*/
+void fvec_mul (fvec_t * v, smpl_t s);
+
 /** remove the minimum value of the vector to each elements
 
   \param v vector to remove minimum from
--- a/src/spectral/filterbank_mel.h
+++ b/src/spectral/filterbank_mel.h
@@ -55,7 +55,7 @@
 /** filterbank initialization for Mel filters using Slaney's coefficients
 
   \param fb filterbank object
-  \param samplerate audio sampling rate
+  \param samplerate audio sampling rate, in Hz
 
   The filter coefficients are built to match exactly Malcolm Slaney's Auditory
   Toolbox implementation (see file mfcc.m). The number of filters should be 40.
--- a/src/spectral/mfcc.c
+++ b/src/spectral/mfcc.c
@@ -51,6 +51,7 @@
   aubio_dct_t *dct;
   fvec_t *output;
 #endif
+  smpl_t scale;
 };
 
 
@@ -74,7 +75,11 @@
 
   /* filterbank allocation */
   mfcc->fb = new_aubio_filterbank (n_filters, mfcc->win_s);
-  aubio_filterbank_set_mel_coeffs_slaney (mfcc->fb, samplerate);
+  if (n_filters == 40)
+    aubio_filterbank_set_mel_coeffs_slaney (mfcc->fb, samplerate);
+  else
+    aubio_filterbank_set_mel_coeffs(mfcc->fb, samplerate,
+        0, samplerate/2.);
 
   /* allocating buffers */
   mfcc->in_dct = new_fvec (n_filters);
@@ -97,6 +102,8 @@
   mfcc->output = new_fvec (n_filters);
 #endif
 
+  mfcc->scale = 1.;
+
   return mfcc;
 }
 
@@ -127,6 +134,7 @@
 #ifndef HAVE_SLOW_DCT
   fvec_t tmp;
 #endif
+
   /* compute filterbank */
   aubio_filterbank_do (mf->fb, in, mf->in_dct);
 
@@ -133,8 +141,7 @@
   /* compute log10 */
   fvec_log10 (mf->in_dct);
 
-  /* raise power */
-  //fvec_pow (mf->in_dct, 3.);
+  if (mf->scale != 1) fvec_mul (mf->in_dct, mf->scale);
 
   /* compute mfccs */
 #if defined(HAVE_SLOW_DCT)
@@ -149,4 +156,44 @@
 #endif
 
   return;
+}
+
+uint_t aubio_mfcc_set_power (aubio_mfcc_t *mf, smpl_t power)
+{
+  return aubio_filterbank_set_power(mf->fb, power);
+}
+
+uint_t aubio_mfcc_get_power (aubio_mfcc_t *mf)
+{
+  return aubio_filterbank_get_power(mf->fb);
+}
+
+uint_t aubio_mfcc_set_scale (aubio_mfcc_t *mf, smpl_t scale)
+{
+  mf->scale = scale;
+  return AUBIO_OK;
+}
+
+uint_t aubio_mfcc_get_scale (aubio_mfcc_t *mf)
+{
+  return mf->scale;
+}
+
+uint_t aubio_mfcc_set_mel_coeffs (aubio_mfcc_t *mf, smpl_t freq_min,
+    smpl_t freq_max)
+{
+  return aubio_filterbank_set_mel_coeffs(mf->fb, mf->samplerate,
+      freq_min, freq_max);
+}
+
+uint_t aubio_mfcc_set_mel_coeffs_htk (aubio_mfcc_t *mf, smpl_t freq_min,
+    smpl_t freq_max)
+{
+  return aubio_filterbank_set_mel_coeffs_htk(mf->fb, mf->samplerate,
+      freq_min, freq_max);
+}
+
+uint_t aubio_mfcc_set_mel_coeffs_slaney (aubio_mfcc_t *mf)
+{
+  return aubio_filterbank_set_mel_coeffs_slaney (mf->fb, mf->samplerate);
 }
--- a/src/spectral/mfcc.h
+++ b/src/spectral/mfcc.h
@@ -73,6 +73,100 @@
 */
 void aubio_mfcc_do (aubio_mfcc_t * mf, const cvec_t * in, fvec_t * out);
 
+/** set power parameter
+
+  \param mf mfcc object, as returned by new_aubio_mfcc()
+  \param power Raise norm of the input spectrum norm to this power before
+  computing filterbank.  Defaults to `1`.
+
+  See aubio_filterbank_set_power().
+
+ */
+uint_t aubio_mfcc_set_power (aubio_mfcc_t *mf, smpl_t power);
+
+/** get power parameter
+
+  \param mf mfcc object, as returned by new_aubio_mfcc()
+  \return current power parameter. Defaults to `1`.
+
+  See aubio_filterbank_get_power().
+
+ */
+uint_t aubio_mfcc_get_power (aubio_mfcc_t *mf);
+
+/** set scaling parameter
+
+  \param mf mfcc object, as returned by new_aubio_mfcc()
+  \param scale Scaling value to apply.
+
+  Scales the output of the filterbank after taking its logarithm and before
+  computing the DCT. Defaults to `1`.
+
+*/
+uint_t aubio_mfcc_set_scale (aubio_mfcc_t *mf, smpl_t scale);
+
+/** get scaling parameter
+
+  \param mf mfcc object, as returned by new_aubio_mfcc()
+  \return current scaling parameter. Defaults to `1`.
+
+ */
+uint_t aubio_mfcc_get_scale (aubio_mfcc_t *mf);
+
+/** Mel filterbank initialization
+
+  \param mf mfcc object
+  \param fmin start frequency, in Hz
+  \param fmax end frequency, in Hz
+
+  The filterbank will be initialized with bands linearly spaced in the mel
+  scale, from `fmin` to `fmax`.
+
+  See also
+  --------
+
+  aubio_filterbank_set_mel_coeffs()
+
+*/
+uint_t aubio_mfcc_set_mel_coeffs (aubio_mfcc_t *mf,
+        smpl_t fmin, smpl_t fmax);
+
+/** Mel filterbank initialization
+
+  \param mf mfcc object
+  \param fmin start frequency, in Hz
+  \param fmax end frequency, in Hz
+
+  The bank of filters will be initalized to to cover linearly spaced bands in
+  the Htk mel scale, from `fmin` to `fmax`.
+
+  See also
+  --------
+
+  aubio_filterbank_set_mel_coeffs_htk()
+
+*/
+uint_t aubio_mfcc_set_mel_coeffs_htk (aubio_mfcc_t *mf,
+        smpl_t fmin, smpl_t fmax);
+
+/** Mel filterbank initialization (Auditory Toolbox's parameters)
+
+  \param mf mfcc object
+  \param samplerate audio sampling rate, in Hz
+
+  The filter coefficients are built to match exactly Malcolm Slaney's Auditory
+  Toolbox implementation. The number of filters should be 40.
+
+  This is the default filterbank when `mf` was created with `n_filters = 40`.
+
+  See also
+  --------
+
+  aubio_filterbank_set_mel_coeffs_slaney()
+
+*/
+uint_t aubio_mfcc_set_mel_coeffs_slaney (aubio_mfcc_t *mf);
+
 #ifdef __cplusplus
 }
 #endif