Index: src/mathutils.c
===================================================================
--- src/mathutils.c	(revision 1498ced017e1a7817cc72913634a7e68b5160b45)
+++ src/mathutils.c	(revision 352fd5f7b20761a99dd66181d8c61e4acbceb5df)
@@ -26,5 +26,7 @@
 #include "config.h"
 
-fvec_t * new_aubio_window(uint_t size, aubio_window_type wintype) {
+fvec_t *
+new_aubio_window (uint_t size, aubio_window_type wintype)
+{
   // create fvec of size x 1 channel
   fvec_t * win = new_fvec( size, 1);
@@ -79,131 +81,166 @@
 }
 
-smpl_t aubio_unwrap2pi(smpl_t phase) {
+smpl_t
+aubio_unwrap2pi (smpl_t phase)
+{
   /* mod(phase+pi,-2pi)+pi */
-  return phase + TWO_PI * (1. + FLOOR(-(phase+PI)/TWO_PI));
-}
-
-smpl_t fvec_mean(fvec_t *s) {
-  uint_t i,j;
+  return phase + TWO_PI * (1. + FLOOR (-(phase + PI) / TWO_PI));
+}
+
+smpl_t
+fvec_mean (fvec_t * s)
+{
+  uint_t i, j;
   smpl_t tmp = 0.0;
-  for (i=0; i < s->channels; i++)
-    for (j=0; j < s->length; j++)
+  for (i = 0; i < s->channels; i++)
+    for (j = 0; j < s->length; j++)
       tmp += s->data[i][j];
-  return tmp/(smpl_t)(s->length);
-}
-
-smpl_t fvec_sum(fvec_t *s) {
-  uint_t i,j;
+  return tmp / (smpl_t) (s->length);
+}
+
+smpl_t
+fvec_sum (fvec_t * s)
+{
+  uint_t i, j;
   smpl_t tmp = 0.0;
-  for (i=0; i < s->channels; i++)
-    for (j=0; j < s->length; j++)
+  for (i = 0; i < s->channels; i++) {
+    for (j = 0; j < s->length; j++) {
       tmp += s->data[i][j];
+    }
+  }
   return tmp;
 }
 
-smpl_t fvec_max(fvec_t *s) {
-  uint_t i,j;
+smpl_t
+fvec_max (fvec_t * s)
+{
+  uint_t i, j;
   smpl_t tmp = 0.0;
-  for (i=0; i < s->channels; i++)
-    for (j=0; j < s->length; j++)
-      tmp = (tmp > s->data[i][j])? tmp : s->data[i][j];
+  for (i = 0; i < s->channels; i++) {
+    for (j = 0; j < s->length; j++) {
+      tmp = (tmp > s->data[i][j]) ? tmp : s->data[i][j];
+    }
+  }
   return tmp;
 }
 
-smpl_t fvec_min(fvec_t *s) {
-  uint_t i,j;
+smpl_t
+fvec_min (fvec_t * s)
+{
+  uint_t i, j;
   smpl_t tmp = s->data[0][0];
-  for (i=0; i < s->channels; i++)
-    for (j=0; j < s->length; j++)
-      tmp = (tmp < s->data[i][j])? tmp : s->data[i][j]  ;
+  for (i = 0; i < s->channels; i++) {
+    for (j = 0; j < s->length; j++) {
+      tmp = (tmp < s->data[i][j]) ? tmp : s->data[i][j];
+    }
+  }
   return tmp;
 }
 
-uint_t fvec_min_elem(fvec_t *s) {
-  uint_t i,j=0, pos=0.;
+uint_t
+fvec_min_elem (fvec_t * s)
+{
+  uint_t i, j = 0, pos = 0.;
   smpl_t tmp = s->data[0][0];
-  for (i=0; i < s->channels; i++)
-    for (j=0; j < s->length; j++) {
-      pos = (tmp < s->data[i][j])? pos : j;
-      tmp = (tmp < s->data[i][j])? tmp : s->data[i][j]  ;
-    }
+  for (i = 0; i < s->channels; i++) {
+    for (j = 0; j < s->length; j++) {
+      pos = (tmp < s->data[i][j]) ? pos : j;
+      tmp = (tmp < s->data[i][j]) ? tmp : s->data[i][j];
+    }
+  }
   return pos;
 }
 
-uint_t fvec_max_elem(fvec_t *s) {
-  uint_t i,j=0, pos=0.;
+uint_t
+fvec_max_elem (fvec_t * s)
+{
+  uint_t i, j, pos;
   smpl_t tmp = 0.0;
-  for (i=0; i < s->channels; i++)
-    for (j=0; j < s->length; j++) {
-      pos = (tmp > s->data[i][j])? pos : j;
-      tmp = (tmp > s->data[i][j])? tmp : s->data[i][j]  ;
-    }
+  for (i = 0; i < s->channels; i++) {
+    for (j = 0; j < s->length; j++) {
+      pos = (tmp > s->data[i][j]) ? pos : j;
+      tmp = (tmp > s->data[i][j]) ? tmp : s->data[i][j];
+    }
+  }
   return pos;
 }
 
-void fvec_shift(fvec_t *s) {
-  uint_t i,j;
-  //smpl_t tmp = 0.0;
-  for (i=0; i < s->channels; i++)
-    for (j=0; j < s->length / 2 ; j++) {
-      //tmp = s->data[i][j];
-      //s->data[i][j] = s->data[i][j+s->length/2];
-      //s->data[i][j+s->length/2] = tmp;
-      ELEM_SWAP(s->data[i][j],s->data[i][j+s->length/2]);
-    }
-}
-
-smpl_t fvec_local_energy(fvec_t * f) {
-  smpl_t locE = 0.;
-  uint_t i,j;
-  for (i=0;i<f->channels;i++)
-    for (j=0;j<f->length;j++)
-      locE+=SQR(f->data[i][j]);
-  return locE;
-}
-
-smpl_t fvec_local_hfc(fvec_t * f) {
-  smpl_t locE = 0.;
-  uint_t i,j;
-  for (i=0;i<f->channels;i++)
-    for (j=0;j<f->length;j++)
-      locE+=(i+1)*f->data[i][j];
-  return locE;
-}
-
-smpl_t fvec_alpha_norm(fvec_t * DF, smpl_t alpha) {
+void
+fvec_shift (fvec_t * s)
+{
+  uint_t i, j;
+  for (i = 0; i < s->channels; i++) {
+    for (j = 0; j < s->length / 2; j++) {
+      ELEM_SWAP (s->data[i][j], s->data[i][j + s->length / 2]);
+    }
+  }
+}
+
+smpl_t
+fvec_local_energy (fvec_t * f)
+{
+  smpl_t energy = 0.;
+  uint_t i, j;
+  for (i = 0; i < f->channels; i++) {
+    for (j = 0; j < f->length; j++) {
+      energy += SQR (f->data[i][j]);
+    }
+  }
+  return energy;
+}
+
+smpl_t
+fvec_local_hfc (fvec_t * v)
+{
+  smpl_t hfc = 0.;
+  uint_t i, j;
+  for (i = 0; i < v->channels; i++) {
+    for (j = 0; j < v->length; j++) {
+      hfc += (i + 1) * v->data[i][j];
+    }
+  }
+  return hfc;
+}
+
+void
+fvec_min_removal (fvec_t * v)
+{
+  smpl_t v_min = fvec_min (v);
+  fvec_add (v,  - v_min );
+}
+
+smpl_t
+fvec_alpha_norm (fvec_t * o, smpl_t alpha)
+{
+  uint_t i, j;
   smpl_t tmp = 0.;
-  uint_t i,j;
-  for (i=0;i<DF->channels;i++)
-    for (j=0;j<DF->length;j++)
-      tmp += POW(ABS(DF->data[i][j]),alpha);
-  return POW(tmp/DF->length,1./alpha);
-}
-
-void
-fvec_min_removal (fvec_t * o)
-{
-  uint_t i, j;
-  smpl_t mini = fvec_min (o);
   for (i = 0; i < o->channels; i++) {
     for (j = 0; j < o->length; j++) {
-      o->data[i][j] -= mini;
-    }
-  }
-}
-
-void fvec_alpha_normalise(fvec_t * mag, uint_t alpha) {
-  smpl_t alphan = 1.;
-  uint_t length = mag->length, i=0, j;
-  alphan = fvec_alpha_norm(mag,alpha);
-  for (j=0;j<length;j++){
-    mag->data[i][j] /= alphan;
-  }
-}
-
-void fvec_add(fvec_t * mag, smpl_t threshold) {
-  uint_t length = mag->length, i=0, j;
-  for (j=0;j<length;j++) {
-    mag->data[i][j] += threshold;
+      tmp += POW (ABS (o->data[i][j]), alpha);
+    }
+  }
+  return POW (tmp / o->length, 1. / alpha);
+}
+
+void
+fvec_alpha_normalise (fvec_t * o, smpl_t alpha)
+{
+  uint_t i, j;
+  smpl_t norm = fvec_alpha_norm (o, alpha);
+  for (i = 0; i < o->channels; i++) {
+    for (j = 0; j < o->length; j++) {
+      o->data[i][j] /= norm;
+    }
+  }
+}
+
+void
+fvec_add (fvec_t * o, smpl_t val)
+{
+  uint_t i, j;
+  for (i = 0; i < o->channels; i++) {
+    for (j = 0; j < o->length; j++) {
+      o->data[i][j] += val;
+    }
   }
 }
@@ -217,28 +254,30 @@
 }
 
-smpl_t fvec_moving_thres(fvec_t * vec, fvec_t * tmpvec,
-    uint_t post, uint_t pre, uint_t pos) {
-  smpl_t * medar = (smpl_t *)tmpvec->data[0];
+smpl_t
+fvec_moving_thres (fvec_t * vec, fvec_t * tmpvec,
+    uint_t post, uint_t pre, uint_t pos)
+{
+  smpl_t *medar = (smpl_t *) tmpvec->data[0];
   uint_t k;
-  uint_t win_length =  post+pre+1;
-  uint_t length =  vec->length;
+  uint_t win_length = post + pre + 1;
+  uint_t length = vec->length;
   /* post part of the buffer does not exist */
-  if (pos<post+1) {
-    for (k=0;k<post+1-pos;k++)
-      medar[k] = 0.; /* 0-padding at the beginning */
-    for (k=post+1-pos;k<win_length;k++)
-      medar[k] = vec->data[0][k+pos-post];
-  /* the buffer is fully defined */
-  } else if (pos+pre<length) {
-    for (k=0;k<win_length;k++)
-      medar[k] = vec->data[0][k+pos-post];
-  /* pre part of the buffer does not exist */
+  if (pos < post + 1) {
+    for (k = 0; k < post + 1 - pos; k++)
+      medar[k] = 0.;            /* 0-padding at the beginning */
+    for (k = post + 1 - pos; k < win_length; k++)
+      medar[k] = vec->data[0][k + pos - post];
+    /* the buffer is fully defined */
+  } else if (pos + pre < length) {
+    for (k = 0; k < win_length; k++)
+      medar[k] = vec->data[0][k + pos - post];
+    /* pre part of the buffer does not exist */
   } else {
-    for (k=0;k<length-pos+post;k++)
-      medar[k] = vec->data[0][k+pos-post];
-    for (k=length-pos+post;k<win_length;k++)
-      medar[k] = 0.; /* 0-padding at the end */
-  }
-  return fvec_median(tmpvec);
+    for (k = 0; k < length - pos + post; k++)
+      medar[k] = vec->data[0][k + pos - post];
+    for (k = length - pos + post; k < win_length; k++)
+      medar[k] = 0.;            /* 0-padding at the end */
+  }
+  return fvec_median (tmpvec);
 }
 
@@ -301,12 +340,7 @@
   if (x2 == pos) return (x->data[0][pos] <= x->data[0][x0]) ? pos : x0;
   s0 = x->data[0][x0];
-  s1 = x->data[0][pos]     ;
+  s1 = x->data[0][pos];
   s2 = x->data[0][x2];
   return pos + 0.5 * (s2 - s0 ) / (s2 - 2.* s1 + s0);
-}
-
-smpl_t aubio_quadfrac(smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf) {
-  smpl_t tmp = s0 + (pf/2.) * (pf * ( s0 - 2.*s1 + s2 ) - 3.*s0 + 4.*s1 - s2);
-  return tmp;
 }
 
@@ -320,8 +354,18 @@
 }
 
-smpl_t aubio_freqtomidi(smpl_t freq) {
+smpl_t
+aubio_quadfrac (smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf)
+{
+  smpl_t tmp =
+      s0 + (pf / 2.) * (pf * (s0 - 2. * s1 + s2) - 3. * s0 + 4. * s1 - s2);
+  return tmp;
+}
+
+smpl_t
+aubio_freqtomidi (smpl_t freq)
+{
   /* log(freq/A-2)/log(2) */
-  smpl_t midi = freq/6.875;
-  midi = LOG(midi)/0.69314718055995;
+  smpl_t midi = freq / 6.875;
+  midi = LOG (midi) / 0.69314718055995;
   midi *= 12;
   midi -= 3;
@@ -329,107 +373,116 @@
 }
 
-smpl_t aubio_miditofreq(smpl_t midi) {
-  smpl_t freq = (midi+3.)/12.;
-  freq = EXP(freq*0.69314718055995);
+smpl_t
+aubio_miditofreq (smpl_t midi)
+{
+  smpl_t freq = (midi + 3.) / 12.;
+  freq = EXP (freq * 0.69314718055995);
   freq *= 6.875;
   return freq;
 }
 
-smpl_t aubio_bintofreq(smpl_t bin, smpl_t samplerate, smpl_t fftsize) {
-  smpl_t freq = samplerate/fftsize;
-  return freq*bin;
-}
-
-smpl_t aubio_bintomidi(smpl_t bin, smpl_t samplerate, smpl_t fftsize) {
-  smpl_t midi = aubio_bintofreq(bin,samplerate,fftsize);
-  return aubio_freqtomidi(midi);
-}
-
-smpl_t aubio_freqtobin(smpl_t freq, smpl_t samplerate, smpl_t fftsize) {
-  smpl_t bin = fftsize/samplerate;
-  return freq*bin;
-}
-
-smpl_t aubio_miditobin(smpl_t midi, smpl_t samplerate, smpl_t fftsize) {
-  smpl_t freq = aubio_miditofreq(midi);
-  return aubio_freqtobin(freq,samplerate,fftsize);
-}
-
-/** returns 1 if wassilence is 0 and RMS(ibuf)<threshold
- * \bug mono
- */
-uint_t aubio_silence_detection(fvec_t * ibuf, smpl_t threshold) {
-  smpl_t loudness = 0;
-  uint_t i=0,j;
-  for (j=0;j<ibuf->length;j++) {
-    loudness += SQR(ibuf->data[i][j]);
-  }
-  loudness = SQRT(loudness);
-  loudness /= (smpl_t)ibuf->length;
-  loudness = LIN2DB(loudness);
-
-  return (loudness < threshold);
-}
-
-/** returns level log(RMS(ibuf)) if < threshold, 1 otherwise
- * \bug mono
- */
-smpl_t aubio_level_detection(fvec_t * ibuf, smpl_t threshold) {
-  smpl_t loudness = 0;
-  uint_t i=0,j;
-  for (j=0;j<ibuf->length;j++) {
-    loudness += SQR(ibuf->data[i][j]);
-  }
-  loudness = SQRT(loudness);
-  loudness /= (smpl_t)ibuf->length;
-  loudness = LIN2DB(loudness);
-
-  if (loudness < threshold)
+smpl_t
+aubio_bintofreq (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
+{
+  smpl_t freq = samplerate / fftsize;
+  return freq * bin;
+}
+
+smpl_t
+aubio_bintomidi (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
+{
+  smpl_t midi = aubio_bintofreq (bin, samplerate, fftsize);
+  return aubio_freqtomidi (midi);
+}
+
+smpl_t
+aubio_freqtobin (smpl_t freq, smpl_t samplerate, smpl_t fftsize)
+{
+  smpl_t bin = fftsize / samplerate;
+  return freq * bin;
+}
+
+smpl_t
+aubio_miditobin (smpl_t midi, smpl_t samplerate, smpl_t fftsize)
+{
+  smpl_t freq = aubio_miditofreq (midi);
+  return aubio_freqtobin (freq, samplerate, fftsize);
+}
+
+smpl_t
+aubio_db_spl (fvec_t * o)
+{
+  smpl_t val = SQRT (fvec_local_energy (o));
+  val /= (smpl_t) o->length;
+  return LIN2DB (val);
+}
+
+uint_t
+aubio_silence_detection (fvec_t * o, smpl_t threshold)
+{
+  return (aubio_db_spl (o) < threshold);
+}
+
+smpl_t
+aubio_level_detection (fvec_t * o, smpl_t threshold)
+{
+  smpl_t db_spl = aubio_db_spl (o);
+  if (db_spl < threshold) {
     return 1.;
-  else
-    return loudness;
-}
-
-smpl_t aubio_zero_crossing_rate(fvec_t * input) {
-  uint_t i=0,j;
+  } else {
+    return db_spl;
+  }
+}
+
+smpl_t
+aubio_zero_crossing_rate (fvec_t * input)
+{
+  uint_t i = 0, j;
   uint_t zcr = 0;
-  for ( j = 1; j < input->length; j++ ) {
+  for (j = 1; j < input->length; j++) {
     // previous was strictly negative
-    if( input->data[i][j-1] < 0. ) {
+    if (input->data[i][j - 1] < 0.) {
       // current is positive or null
-      if ( input->data[i][j] >= 0. ) {
+      if (input->data[i][j] >= 0.) {
         zcr += 1;
       }
-    // previous was positive or null
+      // previous was positive or null
     } else {
       // current is strictly negative
-      if ( input->data[i][j] < 0. ) {
+      if (input->data[i][j] < 0.) {
         zcr += 1;
       }
     }
   }
-  return zcr/(smpl_t)input->length;
-}
-
-void aubio_autocorr(fvec_t * input, fvec_t * output) {
-  uint_t i = 0, j = 0, length = input->length;
-  smpl_t * data = input->data[0];
-  smpl_t * acf = output->data[0];
-  smpl_t tmp =0.;
-  for(i=0;i<length;i++){
-    for(j=i;j<length;j++){
-      tmp += data[j-i]*data[j];
-    }
-    acf[i] = tmp /(smpl_t)(length-i);
-    tmp = 0.0;
-  }
-}
-
-void aubio_cleanup(void) {
+  return zcr / (smpl_t) input->length;
+}
+
+void
+aubio_autocorr (fvec_t * input, fvec_t * output)
+{
+  uint_t i, j, k, length = input->length;
+  smpl_t *data, *acf;
+  smpl_t tmp = 0;
+  for (k = 0; k < input->channels; k++) {
+    data = input->data[k];
+    acf = output->data[k];
+    for (i = 0; i < length; i++) {
+      tmp = 0.;
+      for (j = i; j < length; j++) {
+        tmp += data[j - i] * data[j];
+      }
+      acf[i] = tmp / (smpl_t) (length - i);
+    }
+  }
+}
+
+void
+aubio_cleanup (void)
+{
 #if HAVE_FFTW3
-  fftw_cleanup();
+  fftw_cleanup ();
 #else
 #if HAVE_FFTW3F
-  fftwf_cleanup();
+  fftwf_cleanup ();
 #endif
 #endif
