added midi and hertz output modes
authorPaul Brossier <piem@altern.org>
Mon, 22 Aug 2005 19:52:17 +0000 (19:52 +0000)
committerPaul Brossier <piem@altern.org>
Mon, 22 Aug 2005 19:52:17 +0000 (19:52 +0000)
python/aubio/aubioclass.py
python/aubiopitch
src/pitchdetection.c

index 4fdececc50f32ee50907ee16967591333d5fe93d..d28fdce03cd07eeaa1cf688cd0a1569477097532 100644 (file)
@@ -124,21 +124,35 @@ def check_onset_mode(option, opt, value, parser):
         elif nvalue == 'dual'          :
                  setattr(parser.values, option.dest, 'dual')
         else:
-                 print "unknown detection function selected\n", usage
+                 print "unknown onset detection function selected"
                  sys.exit(1)
 
 def check_pitch_mode(option, opt, value, parser):
         nvalue = parser.rargs[0]
-        if   nvalue == 'mcomb' :
+        if   nvalue == 'mcomb'  :
                  setattr(parser.values, option.dest, aubio_pitch_mcomb)
-        elif nvalue == 'yin'           :
+        elif nvalue == 'yin'    :
                  setattr(parser.values, option.dest, aubio_pitch_yin)
-        elif nvalue == 'fcomb'         :
+        elif nvalue == 'fcomb'  :
                  setattr(parser.values, option.dest, aubio_pitch_fcomb)
-        elif nvalue == 'schmitt'      :
+        elif nvalue == 'schmitt':
                  setattr(parser.values, option.dest, aubio_pitch_schmitt)
         else:
-                 print "unknown detection function selected\n", usage
+                 print "error: unknown pitch detection function selected"
+                 sys.exit(1)
+
+def check_pitchm_mode(option, opt, value, parser):
+        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:
+                 print "error: unknown pitch detection output selected"
                  sys.exit(1)
 
 
@@ -191,7 +205,7 @@ def cutfile(filein,slicetimes,zerothres=0.008,bufsize=1024,hopsize=512):
     filei     = sndfile(filein)
     framestep = hopsize/(filei.samplerate()+0.)
     channels  = filei.channels()
-    newname   = "%s%s%f%s%s" % (filein.split(".")[0].split("/")[-1],".",
+    newname   = "%s%s%09.5f%s%s" % (filein.split(".")[0].split("/")[-1],".",
                 frameread*framestep,".",filein.split(".")[-1])
     fileo     = sndfile(newname,model=filei)
     myvec     = fvec(hopsize,channels)
@@ -213,7 +227,7 @@ def cutfile(filein,slicetimes,zerothres=0.008,bufsize=1024,hopsize=512):
                     fromcross += 1
                     zerocross += 1
             del fileo
-            fileo = sndfile("%s%s%f%s%s" % 
+            fileo = sndfile("%s%s%09.5f%s%s" % 
                 (filein.split(".")[0].split("/")[-1],".",
                 frameread*framestep,".",filein.split(".")[-1]),model=filei)
             writesize = fileo.write(fromcross,mycopy)
@@ -260,7 +274,7 @@ def getpitch(filein,mode=aubio_pitch_mcomb,bufsize=1024,hopsize=512,omode=aubio_
         if (aubio_silence_detection(myvec(),silence)!=1):
                 mylist.append(freq)
         else: 
-                mylist.append(0)
+                mylist.append(-1.)
         frameread += 1
     return mylist
 
index 28bf30ac20de0878b8755d69b103c4ecb39cf860..525d5925ed28f0d5a0416ad0a2f92ae484f60056 100755 (executable)
@@ -22,11 +22,16 @@ def parse_args():
                           default=aubio_pitch_mcomb,
                           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,
+                          help="output pitch in units [default=Hz] \
+                          freq|midi|cent|bin")
         parser.add_option("-B","--bufsize",
-                          action="store", dest="bufsize", default=1024
+                          action="store", dest="bufsize", default=None
                           help="buffer size [default=1024]")
         parser.add_option("-H","--hopsize",
-                          action="store", dest="hopsize", default=512
+                          action="store", dest="hopsize", default=None
                           help="overlap size [default=512]")
         parser.add_option("-t","--threshold",
                           action="store", dest="threshold", default=0.1, 
@@ -63,13 +68,22 @@ def parse_args():
                           action="store_false", dest="verbose", default=False, 
                           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 not options.hopsize:
+                options.hopsize = float(options.bufsize) / 2
         if not options.filename: 
-                 print "no file name given\n", usage
-                 sys.exit(1)
+                print "no file name given\n", usage
+                sys.exit(1)
         return options, args
 
 options, args = parse_args()
 
+#print options.bufsize, options.hopsize
+
 filename   = options.filename
 samplerate = float(sndfile(filename).samplerate())
 hopsize    = int(options.hopsize)
@@ -85,9 +99,11 @@ else:             delay = 2./step
 if options.note:
         exit("not implemented yet")
 else:
-        pitch = getpitch(filename, #threshold,silence,
+        pitch = getpitch(filename, #threshold,
                 mode=options.mode,
-                bufsize=bufsize,hopsize=hopsize)
+                omode=options.omode,
+                bufsize=bufsize,hopsize=hopsize,
+                silence=silence)
 
 ## take back system delay
 #if delay != 0:
index fb181c65ae19484f37a0bfa938a8039f3eb44d66..67cb6b4434a7d89ace82feeb1cdb13ab5e6392e5 100644 (file)
 #include "pitchschmitt.h"
 #include "pitchdetection.h"
 
+smpl_t freqconvpass(smpl_t f);
+smpl_t freqconvpass(smpl_t f){
+        return f;
+}
+
 typedef smpl_t (*aubio_pitchdetection_func_t)(aubio_pitchdetection_t *p, 
                 fvec_t * ibuf);
+typedef smpl_t (*aubio_pitchdetection_conv_t)(smpl_t value);
 void aubio_pitchdetection_slideblock(aubio_pitchdetection_t *p, fvec_t *ibuf);
 
 struct _aubio_pitchdetection_t {
@@ -47,6 +53,7 @@ struct _aubio_pitchdetection_t {
        fvec_t * buf;
        fvec_t * yin;
         aubio_pitchdetection_func_t callback;
+        aubio_pitchdetection_conv_t freqconv;
 };
 
 aubio_pitchdetection_t * new_aubio_pitchdetection(uint_t bufsize, 
@@ -59,6 +66,7 @@ aubio_pitchdetection_t * new_aubio_pitchdetection(uint_t bufsize,
        aubio_pitchdetection_t *p = AUBIO_NEW(aubio_pitchdetection_t);
        p->srate = samplerate;
        p->type = type;
+       p->mode = mode;
        p->bufsize = bufsize;
        switch(p->type) {
                case aubio_pitch_yin:
@@ -85,6 +93,24 @@ 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 = aubio_freqtomidi;
+                        break;
+               case aubio_pitchm_cent:
+                        /** bug: not implemented */
+                        p->freqconv = freqconvpass;
+                        break;
+               case aubio_pitchm_bin:
+                        /** bug: not implemented */
+                        p->freqconv = freqconvpass;
+                        break;
+                default:
+                        break;
+        }
        return p;
 }
 
@@ -131,7 +157,7 @@ void aubio_pitchdetection_slideblock(aubio_pitchdetection_t *p, fvec_t *ibuf){
 }
 
 smpl_t aubio_pitchdetection(aubio_pitchdetection_t *p, fvec_t * ibuf) {
-        return p->callback(p,ibuf);
+        return p->freqconv(p->callback(p,ibuf));
 }
 
 smpl_t aubio_pitchdetection_mcomb(aubio_pitchdetection_t *p, fvec_t *ibuf) {