shithub: aubio

Download patch

ref: aa175814750ff59d3dc11040ef9929b614860a45
parent: f97445ca7bef8ecc309b272684a6c174747b9774
author: Paul Brossier <piem@altern.org>
date: Mon Aug 22 15:52:17 EDT 2005

added midi and hertz output modes

--- a/python/aubio/aubioclass.py
+++ b/python/aubio/aubioclass.py
@@ -124,24 +124,38 @@
         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)
 
+
 def getonsets(filein,threshold=0.2,silence=-70.,bufsize=1024,hopsize=512,
                 mode='dual',localmin=False,storefunc=False,derivate=False):
         frameread = 0
@@ -191,7 +205,7 @@
     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 @@
                     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 @@
         if (aubio_silence_detection(myvec(),silence)!=1):
                 mylist.append(freq)
         else: 
-                mylist.append(0)
+                mylist.append(-1.)
         frameread += 1
     return mylist
 
--- a/python/aubiopitch
+++ b/python/aubiopitch
@@ -22,11 +22,16 @@
                           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 @@
                           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 @@
 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:
--- a/src/pitchdetection.c
+++ b/src/pitchdetection.c
@@ -27,8 +27,14 @@
 #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 @@
 	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 *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 @@
                 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 @@
 }
 
 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) {