shithub: aubio

Download patch

ref: 19b56b07dedfce33814935e3ab7bd343e2e37c2b
parent: 7445aea4eaa48f1083b613251ed50992cc3f5525
author: Paul Brossier <piem@altern.org>
date: Tue Mar 29 10:51:44 EST 2005

updated python/aubio/aubioclass.py and python/aubiocut

--- a/python/aubio/aubioclass.py
+++ b/python/aubio/aubioclass.py
@@ -70,21 +70,120 @@
         del_aubio_peakpicker(self.pp)
 
 class onsetpick:
-    def __init__(self,bufsize,hopsize,channels,myvec,threshold):
+    def __init__(self,bufsize,hopsize,channels,myvec,threshold,mode='dual'):
         self.myfft    = cvec(bufsize,channels)
         self.pv       = pvoc(bufsize,hopsize,channels)
-        self.myod     = onsetdetection(hfc,bufsize,channels)
-        self.myod2    = onsetdetection(complexdomain,bufsize,channels)
-        self.myonset  = fvec(1,channels)
-        self.myonset2 = fvec(1,channels)
+        if mode in [complexdomain,hfc,phase,energy,specdiff]  :
+                self.myod     = onsetdetection(mode,bufsize,channels)
+                self.myonset  = fvec(1,channels)
+        else: 
+                self.myod     = onsetdetection(hfc,bufsize,channels)
+                self.myod2    = onsetdetection(complexdomain,bufsize,channels)
+                self.myonset  = fvec(1,channels)
+                self.myonset2 = fvec(1,channels)
+        self.mode     = mode
         self.pp       = peakpick(float(threshold))
 
     def do(self,myvec): 
         self.pv.do(myvec,self.myfft)
         self.myod.do(self.myfft,self.myonset)
-        self.myod2.do(self.myfft,self.myonset2)
-        self.myonset.set(self.myonset.get(0,0)*self.myonset2.get(0,0),0,0)
+        if self.mode == 'dual':
+                self.myod2.do(self.myfft,self.myonset2)
+                self.myonset.set(self.myonset.get(0,0)*self.myonset2.get(0,0),0,0)
         return self.pp.do(self.myonset),self.myonset.get(0,0)
+
+def getonsetsfunc(filein,threshold,silence,bufsize=1024,hopsize=512,mode='dual'):
+        #bufsize   = 1024
+        #hopsize   = bufsize/2
+        frameread = 0
+        filei     = sndfile(filein)
+        channels  = filei.channels()
+        myvec     = fvec(hopsize,channels)
+        readsize  = filei.read(hopsize,myvec)
+        opick     = onsetpick(bufsize,hopsize,channels,myvec,threshold,mode=mode)
+        mylist    = list()
+        #ovalist   = [0., 0., 0., 0., 0., 0.]
+        ovalist   = [0., 0., 0., 0., 0.]
+        ofunclist = []
+        while(readsize):
+                readsize = filei.read(hopsize,myvec)
+                isonset,val = opick.do(myvec)
+                if (aubio_silence_detection(myvec(),silence)):
+                        isonset=0
+                ovalist.append(val)
+                ovalist.pop(0)
+                ofunclist.append(val)
+                if (isonset == 1):
+                        i=len(ovalist)-1
+                        # find local minima before peak 
+                        while ovalist[i-1] < ovalist[i] and i > 0:
+                                i -= 1
+                        now = (frameread+1-i)
+                        if now > 0 :
+                                mylist.append(now)
+                        else:
+                                now = 0
+                                mylist.append(now)
+                frameread += 1
+        return mylist, ofunclist
+
+
+def getonsetscausal(filein,threshold,silence,bufsize=1024,hopsize=512,mode='dual'):
+        frameread = 0
+        filei     = sndfile(filein)
+        channels  = filei.channels()
+        myvec     = fvec(hopsize,channels)
+        readsize  = filei.read(hopsize,myvec)
+        opick     = onsetpick(bufsize,hopsize,channels,myvec,threshold,mode=mode)
+        mylist    = list()
+        while(readsize):
+                readsize = filei.read(hopsize,myvec)
+                isonset,val = opick.do(myvec)
+                if (aubio_silence_detection(myvec(),silence)):
+                        isonset=0
+                if (isonset == 1):
+                        now = frameread
+                        if now > 0 :
+                                mylist.append(now)
+                        else:
+                                now = 0
+                                mylist.append(now)
+                frameread += 1
+        return mylist
+
+
+def getonsets(filein,threshold=0.2,silence=-70.,bufsize=1024,hopsize=512,mode='dual'):
+        frameread = 0
+        filei     = sndfile(filein)
+        channels  = filei.channels()
+        samplerate= filei.samplerate()
+        myvec     = fvec(hopsize,channels)
+        readsize  = filei.read(hopsize,myvec)
+        opick     = onsetpick(bufsize,hopsize,channels,myvec,threshold,mode=mode)
+        mylist    = list()
+        #ovalist   = [0., 0., 0., 0., 0., 0.]
+        ovalist   = [0., 0., 0., 0., 0.]
+        while(readsize):
+                readsize = filei.read(hopsize,myvec)
+                isonset,val = opick.do(myvec)
+                if (aubio_silence_detection(myvec(),silence)):
+                        isonset=0
+                ovalist.append(val)
+                ovalist.pop(0)
+                if (isonset == 1):
+                        i=len(ovalist)-1
+                        # find local minima before peak 
+                        while ovalist[i-1] < ovalist[i] and i > 0:
+                                i -= 1
+                        now = (frameread+1-i)
+                        if now > 0 :
+                                mylist.append(now)
+                        else:
+                                now = 0
+                                mylist.append(now)
+                frameread += 1
+        return mylist
+
 
 class pitchpick:
     def __init__(self,bufsize,hopsize,channels,myvec,srate):
--- a/python/aubiocut
+++ b/python/aubiocut
@@ -10,38 +10,13 @@
 bufsize   = 1024
 hopsize   = bufsize/2
 
-def getonsets(filein,threshold):
-        frameread = 0
-        filei     = sndfile(filein)
-        srate     = filei.samplerate()
-        channels  = filei.channels()
-        myvec     = fvec(hopsize,channels)
-        readsize  = filei.read(hopsize,myvec)
-        opick     = onsetpick(bufsize,hopsize,channels,myvec,threshold)
-        #newname   = "%s%.8f%s" % ("/tmp/",0.0000000,filein[-4:])
-        #fileo     = sndfile(newname,model=filei)
-        mylist    = list()
-	ovalist   = [0., 0., 0., 0., 0., 0.]
-        while(readsize==hopsize):
-                readsize = filei.read(hopsize,myvec)
-                isonset,val = opick.do(myvec)
-		ovalist.append(val)
-		ovalist.pop(0)
-                if (isonset == 1):
-			i=len(ovalist)-1
-			# find local minima 
-			while ovalist[i-1] < ovalist[i] and i > 0:
-				i -= 1
-                    	now = (frameread+1-i)*hopsize/(srate+0.)
-                    	mylist.append(now)
-                frameread += 1
-        return mylist
 
 def cutfile(filein,onsets):
         frameread = 0
+        zerothres = 0.002
         readsize  = hopsize 
         filei     = sndfile(filein)
-        srate     = filei.samplerate()
+        framestep = hopsize/(filei.samplerate()+0.)
         channels  = filei.channels()
         newname   = "%s%f%s" % ("/tmp/",0.0000000,filein[-4:])
         fileo     = sndfile(newname,model=filei)
@@ -49,13 +24,12 @@
         mycopy    = fvec(hopsize,channels)
         while(readsize==hopsize):
                 readsize = filei.read(hopsize,myvec)
-                now = (frameread)*hopsize/(srate+0.)
 		# write to current file
-                if len(onsets) and now >= onsets[0]:
+                if len(onsets) and frameread >= onsets[0]:
                     onsets.pop(0)
 		    # write up to 1st zero crossing
 		    zerocross = 0
-		    while ( abs( myvec.get(zerocross,0) ) > 0.002 ):
+		    while ( abs( myvec.get(zerocross,0) ) > zerothres ):
 		    	zerocross += 1
                     writesize = fileo.write(zerocross,myvec)
 		    fromcross = 0
@@ -66,8 +40,8 @@
 			zerocross += 1
                     del fileo
                     fileo = sndfile("%s%s%f%s%s" % 
-		    	(filein.split(".")[0].split("/")[-1],".",now,".",filein.split(".")[-1]),model=filei)
-		    # write after 1st zero crossing to new file
+		    	(filein.split(".")[0].split("/")[-1],".",
+			frameread*framestep,".",filein.split(".")[-1]),model=filei)
 		    writesize = fileo.write(fromcross,mycopy)
 		else:
 		    writesize = fileo.write(readsize,myvec)
--- a/python/aubioonset
+++ b/python/aubioonset
@@ -1,25 +1,52 @@
-#!/usr/bin/python
+#! /usr/bin/python
 
-def do(filein,threshold):
-        import aubio.aubioclass as aubio
-        hopsize   = 512
-        bufsize   = 1024
-        frameread = 0
-        filei     = aubio.sndfile(filein)
-        srate     = filei.samplerate()
-        channels  = filei.channels() 
-        myvec     = aubio.fvec(hopsize,channels)
-        readsize  = filei.read(hopsize,myvec)
-        opick     = aubio.onsetpick(bufsize,hopsize,channels,myvec,threshold)
-        mylist    = list()
-        while(readsize==hopsize):
-                readsize = filei.read(hopsize,myvec)
-                isonset,val = opick.do(myvec) 
-                if (isonset == 1):
-                    now = (frameread-3)*hopsize/(srate+0.)
-                    print "%.8f\t%.2f"% (now,val)
-                frameread += 1
+import sys
+import numarray
+from aubio.aubioclass import *
 
-if __name__ == "__main__":
-        import sys
-        do(sys.argv[1],sys.argv[2])
+usage = "usage: %s [options] soundfile" % sys.argv[0]
+
+def parse_args():
+        from optparse import OptionParser
+        parser = OptionParser(usage=usage)
+        parser.add_option("-v","--verbose",
+                          action="store_true", dest="verbose", default=False,
+                          help="make lots of noise")
+        parser.add_option("-q","--quiet",
+                          action="store_false", dest="verbose", default=True, 
+                          help="be quiet [default]")
+        parser.add_option("-t","--threshold",
+                          action="store", dest="threshold", default=0.3, 
+                          help="onset detection threshold [default=0.3]")
+        parser.add_option("-s","--silence",
+                          action="store", dest="silence", default=-70, 
+                          help="silence [default=-70]")
+        def check_mode(option, opt, value, parser):
+                nvalue = parser.rargs[0]
+                if   nvalue == 'complexdomain' : setattr(parser.values, option.dest, complexdomain)
+                elif nvalue == 'hfc'           : setattr(parser.values, option.dest, hfc)
+                elif nvalue == 'phase'         : setattr(parser.values, option.dest, phase)
+                elif nvalue == 'specdiff'      : setattr(parser.values, option.dest, specdiff)
+                elif nvalue == 'energy'        : setattr(parser.values, option.dest, energy)
+                elif nvalue == 'dual'          : setattr(parser.values, option.dest, 'dual')
+        parser.add_option("-m","--mode",
+                          action="callback", callback=check_mode, dest="mode", default='dual', 
+                          help="onsetdetection mode [default=dual]")
+        parser.add_option("-o","--outplot",
+                          action="store", dest="outplot", default=None, 
+                          help="be quiet [default=None]")
+        (options, args) = parser.parse_args()
+        if not len(args): 
+                 print "no file name given\n", usage
+                 sys.exit(1)
+        return options, args
+
+options, args = parse_args()
+
+filename  = args[0] 
+threshold = float(options.threshold)
+silence   = float(options.silence)
+
+#onsets = getonsets(filename,threshold,silence,mode=options.mode)
+onsets = getonsetscausal(filename,threshold,silence,mode=options.mode)
+for i in onsets: print i*512./44100.