From: Paul Brossier Date: Thu, 15 Oct 2009 16:54:23 +0000 (+0200) Subject: src/pitch: use a string to set pitch method, add a new function to set pitch unit... X-Git-Tag: bzr2git~131 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=fe163ad79210270109544d4991b7130667712c8a;p=aubio.git 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 --- diff --git a/examples/aubionotes.c b/examples/aubionotes.c index 39af51b8..7017bd98 100644 --- a/examples/aubionotes.c +++ b/examples/aubionotes.c @@ -39,10 +39,6 @@ int aubio_process(smpl_t **input, smpl_t **output, int nframes) { /* 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); diff --git a/examples/aubioonset.c b/examples/aubioonset.c index d0b8d9f7..1af5b3ea 100644 --- a/examples/aubioonset.c +++ b/examples/aubioonset.c @@ -39,10 +39,6 @@ int aubio_process(smpl_t **input, smpl_t **output, int nframes) { /* 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 */ diff --git a/examples/utils.c b/examples/utils.c index 5fc76425..22e6b95a 100644 --- a/examples/utils.c +++ b/examples/utils.c @@ -64,7 +64,6 @@ fvec_t *pitch_obuf; 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 @@ aubio_peakpicker_t *parms; /* 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 @@ parse_args (int argc, char **argv) */ 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 @@ examples_common_init (int argc, char **argv) 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); @@ -325,10 +311,6 @@ examples_common_del (void) } del_fvec (pitch_obuf); } - if (usedoubled) { - del_aubio_onsetdetection (o2); - del_fvec (onset2); - } del_aubio_onsetdetection (o); del_aubio_peakpicker (parms); del_aubio_pvoc (pv); diff --git a/examples/utils.h b/examples/utils.h index 3a857aa1..c14ef600 100644 --- a/examples/utils.h +++ b/examples/utils.h @@ -107,7 +107,6 @@ extern aubio_peakpicker_t *parms; /* 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; diff --git a/plugins/puredata/aubiopitch~.c b/plugins/puredata/aubiopitch~.c index fad8882b..487c01b4 100644 --- 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 @@ static void *aubiopitch_tilde_new (t_symbol * s) 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); diff --git a/python/aubio/aubioclass.py b/python/aubio/aubioclass.py index 6ec95ace..530fa4b8 100644 --- a/python/aubio/aubioclass.py +++ b/python/aubio/aubioclass.py @@ -124,11 +124,12 @@ class onsetpick: 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): diff --git a/python/aubio/task/params.py b/python/aubio/task/params.py index 8907da03..b6be43f3 100644 --- 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 @@ class taskparams(object): self.pitchmax=20000. self.pitchdelay = -0.5 self.dcthreshold = -1. - self.omode = aubio_pitchm_freq + self.omode = "freq" self.verbose = False diff --git a/python/aubio/task/pitch.py b/python/aubio/task/pitch.py index 1db00b3b..1a10327d 100644 --- a/python/aubio/task/pitch.py +++ b/python/aubio/task/pitch.py @@ -13,7 +13,7 @@ class taskpitch(task): 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, diff --git a/python/aubio/task/utils.py b/python/aubio/task/utils.py index 95491a60..eb33f72e 100644 --- a/python/aubio/task/utils.py +++ b/python/aubio/task/utils.py @@ -25,23 +25,6 @@ def get_onset_mode(nvalue): 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 @@ def check_pitch_mode(option, opt, value, parser): 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) - - diff --git a/python/aubiopitch b/python/aubiopitch index 49d30a2f..3db19ea4 100755 --- a/python/aubiopitch +++ b/python/aubiopitch @@ -17,12 +17,11 @@ def parse_args(): 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 @@ def parse_args(): 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 @@ params.verbose = options.verbose 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) diff --git a/src/pitch/pitchdetection.c b/src/pitch/pitchdetection.c index da3d0fee..9c85b311 100644 --- 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 @@ smpl_t freqconvpass(smpl_t f,uint_t srate UNUSED,uint_t bufsize UNUSED){ 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 @@ aubio_pitchdetection_t * new_aubio_pitchdetection(uint_t bufsize, 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; } @@ -190,6 +207,43 @@ void aubio_pitchdetection_slideblock(aubio_pitchdetection_t *p, fvec_t *ibuf){ } } +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) { switch(p->type) { case aubio_pitch_yin: diff --git a/src/pitch/pitchdetection.h b/src/pitch/pitchdetection.h index fa013fd4..e2c9bfdf 100644 --- a/src/pitch/pitchdetection.h +++ b/src/pitch/pitchdetection.h @@ -32,23 +32,6 @@ extern "C" { */ -/** 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 @@ void del_aubio_pitchdetection(aubio_pitchdetection_t * p); \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 } diff --git a/swig/aubio.i b/swig/aubio.i index 8842cce5..599d2054 100644 --- a/swig/aubio.i +++ b/swig/aubio.i @@ -198,34 +198,13 @@ void aubio_pvoc_do(aubio_pvoc_t *pv, fvec_t *in, cvec_t * fftgrain); 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 */ aubio_pitchmcomb_t * new_aubio_pitchmcomb(uint_t bufsize, uint_t hopsize, uint_t channels); diff --git a/tests/src/test-pitchdetection.c b/tests/src/test-pitchdetection.c index 4d722327..e50f0aa6 100644 --- a/tests/src/test-pitchdetection.c +++ b/tests/src/test-pitchdetection.c @@ -1,28 +1,27 @@ #include -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; }