shithub: aubio

Download patch

ref: fe163ad79210270109544d4991b7130667712c8a
parent: cd77c150fae93e50164a354587589c231b3f4ddc
author: Paul Brossier <piem@piem.org>
date: Thu Oct 15 14:54:23 EDT 2009

src/pitch: use a string to set pitch method, add a new function to set pitch unit, keep pitch enums private, update pitch methods where they are used

--- a/examples/aubionotes.c
+++ b/examples/aubionotes.c
@@ -39,10 +39,6 @@
       /* block loop */
       aubio_pvoc_do (pv,ibuf, fftgrain);
       aubio_onsetdetection_do(o,fftgrain, onset);
-      if (usedoubled) {
-        aubio_onsetdetection_do(o2,fftgrain, onset2);
-        onset->data[0][0] *= onset2->data[0][0];
-      }
       isonset = aubio_peakpicker_do(parms, onset);
       
       aubio_pitchdetection_do (pitchdet, ibuf, pitch_obuf);
--- a/examples/aubioonset.c
+++ b/examples/aubioonset.c
@@ -39,10 +39,6 @@
       /* block loop */
       aubio_pvoc_do (pv,ibuf, fftgrain);
       aubio_onsetdetection_do (o,fftgrain, onset);
-      if (usedoubled) {
-        aubio_onsetdetection_do (o2,fftgrain, onset2);
-        onset->data[0][0] *= onset2->data[0][0];
-      }
       isonset = aubio_peakpicker_do(parms, onset);
       if (isonset) {
         /* test for silence */
--- a/examples/utils.c
+++ b/examples/utils.c
@@ -64,7 +64,6 @@
 cvec_t *fftgrain;
 fvec_t *woodblock;
 aubio_onsetdetection_t *o;
-aubio_onsetdetection_t *o2;
 fvec_t *onset;
 fvec_t *onset2;
 smpl_t isonset = 0;
@@ -74,8 +73,8 @@
 /* pitch objects */
 smpl_t pitch = 0.;
 aubio_pitchdetection_t *pitchdet;
-aubio_pitchdetection_type type_pitch = aubio_pitch_yinfft;      // aubio_pitch_mcomb
-aubio_pitchdetection_mode mode_pitch = aubio_pitchm_freq;
+char_t * pitch_unit = "default";
+char_t * pitch_mode = "default";
 uint_t median = 6;
 
 fvec_t *note_buffer = NULL;
@@ -176,20 +175,7 @@
          */
         break;
       case 'p':
-        if (strcmp (optarg, "mcomb") == 0)
-          type_pitch = aubio_pitch_mcomb;
-        else if (strcmp (optarg, "yinfft") == 0)
-          type_pitch = aubio_pitch_yin;
-        else if (strcmp (optarg, "yin") == 0)
-          type_pitch = aubio_pitch_yin;
-        else if (strcmp (optarg, "schmitt") == 0)
-          type_pitch = aubio_pitch_schmitt;
-        else if (strcmp (optarg, "fcomb") == 0)
-          type_pitch = aubio_pitch_fcomb;
-        else {
-          errmsg ("unknown pitch type.\n");
-          abort ();
-        }
+        pitch_mode = optarg;
         break;
       case 'a':
         averaging = 1;
@@ -293,8 +279,8 @@
   fftgrain = new_cvec (buffer_size, channels);
 
   if (usepitch) {
-    pitchdet = new_aubio_pitchdetection (buffer_size * 4,
-        overlap_size, channels, samplerate, type_pitch, mode_pitch);
+    pitchdet = new_aubio_pitchdetection (pitch_mode, buffer_size * 4,
+        overlap_size, channels, samplerate);
     aubio_pitchdetection_set_tolerance (pitchdet, 0.7);
     pitch_obuf = new_fvec (1, channels);
 
@@ -324,10 +310,6 @@
       del_fvec (note_buffer2);
     }
     del_fvec (pitch_obuf);
-  }
-  if (usedoubled) {
-    del_aubio_onsetdetection (o2);
-    del_fvec (onset2);
   }
   del_aubio_onsetdetection (o);
   del_aubio_peakpicker (parms);
--- a/examples/utils.h
+++ b/examples/utils.h
@@ -107,7 +107,6 @@
 /* pitch objects */
 extern smpl_t pitch;
 extern aubio_pitchdetection_t *pitchdet;
-extern aubio_pitchdetection_type mode;
 extern uint_t median;
 
 extern fvec_t *note_buffer;
--- a/plugins/puredata/aubiopitch~.c
+++ b/plugins/puredata/aubiopitch~.c
@@ -13,9 +13,6 @@
 
 char aubiopitch_version[] = "aubiopitch~ version 0.1";
 
-aubio_pitchdetection_type type_pitch = aubio_pitch_yinfft;
-aubio_pitchdetection_mode mode_pitch = aubio_pitchm_freq;
-
 static t_class *aubiopitch_tilde_class;
 
 void aubiopitch_tilde_setup (void);
@@ -78,23 +75,9 @@
 	x->bufsize   = 2048;
 	x->hopsize   = x->bufsize / 2;
 
-        if (strcmp(s->s_name,"mcomb") == 0) 
-                type_pitch = aubio_pitch_mcomb;
-        else if (strcmp(s->s_name,"yinfft") == 0) 
-                type_pitch = aubio_pitch_yin;
-        else if (strcmp(s->s_name,"yin") == 0) 
-                type_pitch = aubio_pitch_yin;
-        else if (strcmp(s->s_name,"schmitt") == 0) 
-                type_pitch = aubio_pitch_schmitt;
-        else if (strcmp(s->s_name,"fcomb") == 0) 
-                type_pitch = aubio_pitch_fcomb;
-        else {
-                post("unknown pitch type, using default.\n");
-        }
-
 	//FIXME: get the real samplerate
-    	x->o = new_aubio_pitchdetection(x->bufsize, 
-                    x->hopsize, 1, 44100., type_pitch, mode_pitch);
+    x->o = new_aubio_pitchdetection(s->s_name, x->bufsize, 
+            x->hopsize, 1, 44100.);
 	aubio_pitchdetection_set_tolerance (x->o, 0.7);
 	x->vec = (fvec_t *)new_fvec(x->hopsize,1);
 	x->pitchvec = (fvec_t *)new_fvec(1,1);
--- a/python/aubio/aubioclass.py
+++ b/python/aubio/aubioclass.py
@@ -124,11 +124,12 @@
         return isonset, dval
 
 class pitchdetection:
-    def __init__(self,mode=aubio_pitch_mcomb,bufsize=2048,hopsize=1024,
-        channels=1,samplerate=44100.,omode=aubio_pitchm_freq,tolerance=0.1):
-        self.pitchp = new_aubio_pitchdetection(bufsize,hopsize,channels,
-                samplerate,mode,omode)
+    def __init__(self,mode="mcomb",bufsize=2048,hopsize=1024,
+        channels=1,samplerate=44100.,omode="freq",tolerance=0.1):
+        self.pitchp = new_aubio_pitchdetection(mode,bufsize,hopsize,channels,
+            samplerate)
         self.mypitch = fvec(1, channels)
+        aubio_pitchdetection_set_unit(self.pitchp,omode)
         aubio_pitchdetection_set_tolerance(self.pitchp,tolerance)
         #self.filt     = filter(srate,"adsgn")
     def __del__(self):
--- a/python/aubio/task/params.py
+++ b/python/aubio/task/params.py
@@ -1,4 +1,3 @@
-from aubio.aubioclass import aubio_pitchm_freq
 
 class taskparams(object):
 	""" default parameters for task classes """
@@ -29,6 +28,6 @@
 		self.pitchmax=20000.
 		self.pitchdelay = -0.5
 		self.dcthreshold = -1.
-		self.omode = aubio_pitchm_freq
+		self.omode = "freq"
 		self.verbose   = False
 
--- a/python/aubio/task/pitch.py
+++ b/python/aubio/task/pitch.py
@@ -13,7 +13,7 @@
 			tolerance = self.params.yinfftthresh
 		else:
 			tolerance = 0.
-		self.pitchdet	= pitchdetection(mode=get_pitch_mode(self.params.pitchmode),
+		self.pitchdet	= pitchdetection(mode=self.params.pitchmode,
 			bufsize=self.params.bufsize,
 			hopsize=self.params.hopsize,
 			channels=self.channels,
--- a/python/aubio/task/utils.py
+++ b/python/aubio/task/utils.py
@@ -25,23 +25,6 @@
 		 print "unknown onset detection function selected: %s" % nvalue
 		 sys.exit(1)
 
-def get_pitch_mode(nvalue):
-	""" utility function to convert a string to aubio_pitchdetection_type """
-	if   nvalue == 'mcomb'  :
-		 return aubio_pitch_mcomb
-	elif nvalue == 'yin'    :
-		 return aubio_pitch_yin
-	elif nvalue == 'fcomb'  :
-		 return aubio_pitch_fcomb
-	elif nvalue == 'schmitt':
-		 return aubio_pitch_schmitt
-	elif nvalue == 'yinfft':
-		 return aubio_pitch_yinfft
-	else:
-		 import sys
-		 print "error: unknown pitch detection function selected"
-		 sys.exit(1)
-
 def check_onset_mode(option, opt, value, parser):
 	""" wrapper function to convert a list of modes to 
 		aubio_onsetdetection_type """
@@ -58,21 +41,3 @@
 	for nvalue in nvalues:
 		val.append(get_pitch_mode(nvalue))
 		setattr(parser.values, option.dest, val)
-
-def check_pitchm_mode(option, opt, value, parser):
-	""" utility function to convert a string to aubio_pitchdetection_mode """
-	nvalue = parser.rargs[0]
-	if   nvalue == 'freq'  :
-		 setattr(parser.values, option.dest, aubio_pitchm_freq)
-	elif nvalue == 'midi'  :
-		 setattr(parser.values, option.dest, aubio_pitchm_midi)
-	elif nvalue == 'cent'  :
-		 setattr(parser.values, option.dest, aubio_pitchm_cent)
-	elif nvalue == 'bin'   :
-		 setattr(parser.values, option.dest, aubio_pitchm_bin)
-	else:
-		 import sys
-		 print "error: unknown pitch detection output selected"
-		 sys.exit(1)
-
-
--- a/python/aubiopitch
+++ b/python/aubiopitch
@@ -17,12 +17,11 @@
       action="store", dest="filename", 
       help="input sound file")
   parser.add_option("-m","--mode", 
-      action="store", dest="mode", default='mcomb',
+      action="store", dest="mode", default='yinfft',
       help="pitch detection mode [default=mcomb] \
       mcomb|yin|fcomb|schmitt")
-  parser.add_option("-u","--units", action="callback", 
-      callback=check_pitchm_mode, dest="omode",
-      default=aubio_pitchm_freq,
+  parser.add_option("-u","--units",
+      action="store", dest="omode", default="freq",
       help="output pitch in units [default=Hz] \
       freq|midi|cent|bin")
   parser.add_option("-B","--bufsize",
@@ -77,10 +76,10 @@
       help="be quiet")
   (options, args) = parser.parse_args()
   if not options.bufsize:
-    if options.mode == aubio_pitch_yin:     options.bufsize = 1024
-    if options.mode == aubio_pitch_schmitt: options.bufsize = 2048
-    if options.mode == aubio_pitch_mcomb:   options.bufsize = 4096
-    if options.mode == aubio_pitch_fcomb:   options.bufsize = 4096 
+    if options.mode == "yin":     options.bufsize = 1024
+    if options.mode == "schmitt": options.bufsize = 2048
+    if options.mode == "mcomb":   options.bufsize = 4096
+    if options.mode == "fcomb":   options.bufsize = 4096 
     else: options.bufsize = 2048
   if not options.hopsize:
     options.hopsize = float(options.bufsize) / 2
@@ -105,7 +104,6 @@
 if options.smoothing: params.pitchsmooth = int(options.smoothing)
 if options.pitchmax:  params.pitchmax    = int(options.pitchmax)
 if options.pitchmin:  params.pitchmin    = int(options.pitchmin)
-if options.omode:     params.omode       = int(options.omode)
 #mintol     = float(options.mintol)*step
 # default take back system delay
 if options.delay: params.pitchdelay = float(options.delay)
--- a/src/pitch/pitchdetection.c
+++ b/src/pitch/pitchdetection.c
@@ -31,6 +31,25 @@
 #include "pitch/pitchyinfft.h"
 #include "pitch/pitchdetection.h"
 
+/** pitch detection algorithm */
+typedef enum {
+  aubio_pitch_yin,     /**< YIN algorithm */
+  aubio_pitch_mcomb,   /**< Multi-comb filter */
+  aubio_pitch_schmitt, /**< Schmitt trigger */
+  aubio_pitch_fcomb,   /**< Fast comb filter */
+  aubio_pitch_yinfft,   /**< Spectral YIN */
+  aubio_pitch_default = aubio_pitch_yinfft, /**< the one used when "default" is asked */
+} aubio_pitchdetection_type;
+
+/** pitch detection output mode */
+typedef enum {
+  aubio_pitchm_freq,   /**< Frequency (Hz) */
+  aubio_pitchm_midi,   /**< MIDI note (0.,127) */
+  aubio_pitchm_cent,   /**< Cent */
+  aubio_pitchm_bin,    /**< Frequency bin (0,bufsize) */
+  aubio_pitchm_default = aubio_pitchm_freq, /**< the one used when "default" is asked */
+} aubio_pitchdetection_mode;
+
 typedef void (*aubio_pitchdetection_func_t)
   (aubio_pitchdetection_t *p, fvec_t * ibuf, fvec_t *obuf);
 typedef smpl_t (*aubio_pitchdetection_conv_t)
@@ -80,17 +99,32 @@
   return f;
 }
 
-aubio_pitchdetection_t * new_aubio_pitchdetection(uint_t bufsize, 
-    uint_t hopsize, 
-    uint_t channels,
-    uint_t samplerate,
-    aubio_pitchdetection_type type,
-    aubio_pitchdetection_mode mode)
+aubio_pitchdetection_t *
+new_aubio_pitchdetection (char_t * pitch_mode,
+    uint_t bufsize, uint_t hopsize, uint_t channels, uint_t samplerate)
 {
   aubio_pitchdetection_t *p = AUBIO_NEW(aubio_pitchdetection_t);
+  aubio_pitchdetection_type pitch_type;
+  if (strcmp (pitch_mode, "mcomb") == 0)
+      pitch_type = aubio_pitch_mcomb;
+  else if (strcmp (pitch_mode, "yinfft") == 0)
+      pitch_type = aubio_pitch_yin;
+  else if (strcmp (pitch_mode, "yin") == 0)
+      pitch_type = aubio_pitch_yin;
+  else if (strcmp (pitch_mode, "schmitt") == 0)
+      pitch_type = aubio_pitch_schmitt;
+  else if (strcmp (pitch_mode, "fcomb") == 0)
+      pitch_type = aubio_pitch_fcomb;
+  else if (strcmp (pitch_mode, "default") == 0)
+      pitch_type = aubio_pitch_default;
+  else {
+      AUBIO_ERR ("unknown pitch detection method %s, using default.\n", pitch_mode);
+      pitch_type = aubio_pitch_default;
+      return NULL;
+  }
   p->srate = samplerate;
-  p->type = type;
-  p->mode = mode;
+  p->type = pitch_type;
+  aubio_pitchdetection_set_unit (p, "default");
   p->bufsize = bufsize;
   switch(p->type) {
     case aubio_pitch_yin:
@@ -125,23 +159,6 @@
     default:
       break;
   }
-  switch(p->mode) {
-    case aubio_pitchm_freq:
-      p->freqconv = freqconvpass;
-      break;
-    case aubio_pitchm_midi:
-      p->freqconv = freqconvmidi;
-      break;
-    case aubio_pitchm_cent:
-      /* bug: not implemented */
-      p->freqconv = freqconvmidi;
-      break;
-    case aubio_pitchm_bin:
-      p->freqconv = freqconvbin;
-      break;
-    default:
-      break;
-  }
   return p;
 }
 
@@ -188,6 +205,43 @@
       p->buf->data[i][j+overlap_size] = ibuf->data[i][j];
     }
   }
+}
+
+uint_t aubio_pitchdetection_set_unit (aubio_pitchdetection_t *p, char_t * pitch_unit) {
+  aubio_pitchdetection_mode pitch_mode;
+  if (strcmp (pitch_unit, "freq") == 0)
+      pitch_mode = aubio_pitchm_freq;
+  else if (strcmp (pitch_unit, "midi") == 0)
+      pitch_mode = aubio_pitchm_midi;
+  else if (strcmp (pitch_unit, "cent") == 0)
+      pitch_mode = aubio_pitchm_cent;
+  else if (strcmp (pitch_unit, "bin") == 0)
+      pitch_mode = aubio_pitchm_bin;
+  else if (strcmp (pitch_unit, "default") == 0)
+      pitch_mode = aubio_pitchm_default;
+  else {
+      AUBIO_ERR ("unknown pitch detection unit %s, using default\n", pitch_unit);
+      pitch_mode = aubio_pitchm_default;
+  }
+  p->mode = pitch_mode;
+  switch(p->mode) {
+    case aubio_pitchm_freq:
+      p->freqconv = freqconvpass;
+      break;
+    case aubio_pitchm_midi:
+      p->freqconv = freqconvmidi;
+      break;
+    case aubio_pitchm_cent:
+      /* bug: not implemented */
+      p->freqconv = freqconvmidi;
+      break;
+    case aubio_pitchm_bin:
+      p->freqconv = freqconvbin;
+      break;
+    default:
+      break;
+  }
+  return 0;
 }
 
 void aubio_pitchdetection_set_tolerance(aubio_pitchdetection_t *p, smpl_t tol) {
--- a/src/pitch/pitchdetection.h
+++ b/src/pitch/pitchdetection.h
@@ -32,23 +32,6 @@
 
 */
 
-/** pitch detection algorithm */
-typedef enum {
-  aubio_pitch_yin,     /**< YIN algorithm */
-  aubio_pitch_mcomb,   /**< Multi-comb filter */
-  aubio_pitch_schmitt, /**< Schmitt trigger */
-  aubio_pitch_fcomb,   /**< Fast comb filter */
-  aubio_pitch_yinfft   /**< Spectral YIN */
-} aubio_pitchdetection_type;
-
-/** pitch detection output mode */
-typedef enum {
-  aubio_pitchm_freq,   /**< Frequency (Hz) */
-  aubio_pitchm_midi,   /**< MIDI note (0.,127) */
-  aubio_pitchm_cent,   /**< Cent */
-  aubio_pitchm_bin     /**< Frequency bin (0,bufsize) */
-} aubio_pitchdetection_mode;
-
 /** pitch detection object */
 typedef struct _aubio_pitchdetection_t aubio_pitchdetection_t;
 
@@ -84,12 +67,11 @@
   \param mode set pitch units for output
 
 */
-aubio_pitchdetection_t * new_aubio_pitchdetection(uint_t bufsize,
-    uint_t hopsize,
-    uint_t channels,
-    uint_t samplerate,
-    aubio_pitchdetection_type pitch_type,
-    aubio_pitchdetection_mode pitch_mode);
+aubio_pitchdetection_t *new_aubio_pitchdetection (char_t * pitch_mode,
+    uint_t bufsize, uint_t hopsize, uint_t channels, uint_t samplerate);
+
+/** set the output unit of the pitch detection object */
+uint_t aubio_pitchdetection_set_unit (aubio_pitchdetection_t *p, char_t * pitch_unit);
 
 #ifdef __cplusplus
 }
--- a/swig/aubio.i
+++ b/swig/aubio.i
@@ -198,33 +198,12 @@
 void aubio_pvoc_rdo(aubio_pvoc_t *pv, cvec_t * fftgrain, fvec_t *out);
 
 /* pitch detection */
-typedef enum {
-        aubio_pitch_yin,
-        aubio_pitch_mcomb,
-        aubio_pitch_schmitt,
-        aubio_pitch_fcomb,
-        aubio_pitch_yinfft
-} aubio_pitchdetection_type;
-
-typedef enum {
-        aubio_pitchm_freq,
-        aubio_pitchm_midi,
-        aubio_pitchm_cent,
-        aubio_pitchm_bin
-} aubio_pitchdetection_mode;
-
+aubio_pitchdetection_t *new_aubio_pitchdetection (char *pitch_mode,
+    uint_t bufsize, uint_t hopsize, uint_t channels, uint_t samplerate);
 void aubio_pitchdetection_do (aubio_pitchdetection_t * p, fvec_t * ibuf, fvec_t * obuf);
-
 void aubio_pitchdetection_set_tolerance(aubio_pitchdetection_t *p, smpl_t thres);
-
+void aubio_pitchdetection_set_unit(aubio_pitchdetection_t *p, char * pitch_unit);
 void del_aubio_pitchdetection(aubio_pitchdetection_t * p);
-
-aubio_pitchdetection_t * new_aubio_pitchdetection(uint_t bufsize, 
-    uint_t hopsize, 
-    uint_t channels,
-    uint_t samplerate,
-    aubio_pitchdetection_type type,
-    aubio_pitchdetection_mode mode);
 
 
 /* pitch mcomb */
--- a/tests/src/test-pitchdetection.c
+++ b/tests/src/test-pitchdetection.c
@@ -1,28 +1,27 @@
 #include <aubio.h>
 
-int main(){
-        /* allocate some memory */
-        uint_t win_s      = 1024;                       /* window size */
-        uint_t hop_s      = win_s/4;                    /* hop size */
-        uint_t samplerate = 44100;                      /* samplerate */
-        uint_t channels   = 1;                          /* number of channel */
-        aubio_pitchdetection_mode mode = aubio_pitchm_freq;
-        aubio_pitchdetection_type type = aubio_pitch_yinfft;
-        fvec_t * in       = new_fvec (hop_s, channels); /* input buffer */
-        fvec_t * out       = new_fvec (1, channels); /* input buffer */
-        aubio_pitchdetection_t * o  = new_aubio_pitchdetection(
-          win_s, hop_s, channels, samplerate, type, mode
-          );
-        uint_t i = 0;
+int
+main ()
+{
+  /* allocate some memory */
+  uint_t win_s = 1024;          /* window size */
+  uint_t hop_s = win_s / 4;     /* hop size */
+  uint_t samplerate = 44100;    /* samplerate */
+  uint_t channels = 1;          /* number of channel */
+  fvec_t *in = new_fvec (hop_s, channels);      /* input buffer */
+  fvec_t *out = new_fvec (1, channels); /* input buffer */
+  aubio_pitchdetection_t *o =
+      new_aubio_pitchdetection ("default", win_s, hop_s, channels, samplerate);
+  uint_t i = 0;
 
-        while (i < 100) {
-          aubio_pitchdetection_do (o,in, out);
-          i++;
-        };
+  while (i < 100) {
+    aubio_pitchdetection_do (o, in, out);
+    i++;
+  };
 
-        del_aubio_pitchdetection(o);
-        del_fvec(in);
-        aubio_cleanup();
+  del_aubio_pitchdetection (o);
+  del_fvec (in);
+  aubio_cleanup ();
 
-        return 0;
+  return 0;
 }