move aubiopitch to new tasks, comment out old task versions
authorPaul Brossier <piem@altern.org>
Wed, 15 Feb 2006 10:36:09 +0000 (10:36 +0000)
committerPaul Brossier <piem@altern.org>
Wed, 15 Feb 2006 10:36:09 +0000 (10:36 +0000)
move aubiopitch to new tasks, comment out old task versions

python/aubio/tasks.py
python/aubiocut
python/aubiopitch

index 7a5c03a3c0e29c8a716816de4ad335a7a1759b36..c67b20ca5e77b8c2403e583ea161b728fd3fa66d 100644 (file)
@@ -73,126 +73,126 @@ def check_pitchm_mode(option, opt, value, parser):
                  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
-        filei     = sndfile(filein)
-        channels  = filei.channels()
-        myvec     = fvec(hopsize,channels)
-        readsize  = filei.read(hopsize,myvec)
-        opick     = onsetpick(bufsize,hopsize,channels,myvec,threshold,
-                         mode=mode,derivate=derivate)
-        mylist    = list()
-        if localmin:
-                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
-                if localmin:
-                        if val > 0: ovalist.append(val)
-                        else: ovalist.append(0)
-                        ovalist.pop(0)
-                if storefunc:
-                        ofunclist.append(val)
-                if (isonset == 1):
-                        if localmin:
-                                i=len(ovalist)-1
-                                # find local minima before peak 
-                                while ovalist[i-1] < ovalist[i] and i > 0:
-                                        i -= 1
-                                now = (frameread+1-i)
-                        else:
-                                now = frameread
-                        if now > 0 :
-                                mylist.append(now)
-                        else:
-                                now = 0
-                                mylist.append(now)
-                frameread += 1
-        return mylist, ofunclist
-
-def cutfile(filein,slicetimes,zerothres=0.008,bufsize=1024,hopsize=512):
-    frameread = 0
-    readsize  = hopsize 
-    filei     = sndfile(filein)
-    framestep = hopsize/(filei.samplerate()+0.)
-    channels  = filei.channels()
-    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)
-    mycopy    = fvec(hopsize,channels)
-    while(readsize==hopsize):
-        readsize = filei.read(hopsize,myvec)
-        # write to current file
-        if len(slicetimes) and frameread >= slicetimes[0]:
-            slicetimes.pop(0)
-            # write up to 1st zero crossing
-            zerocross = 0
-            while ( abs( myvec.get(zerocross,0) ) > zerothres ):
-                zerocross += 1
-            writesize = fileo.write(zerocross,myvec)
-            fromcross = 0
-            while (zerocross < readsize):
-                for i in range(channels):
-                    mycopy.set(myvec.get(zerocross,i),fromcross,i)
-                    fromcross += 1
-                    zerocross += 1
-            del fileo
-            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)
-        else:
-            writesize = fileo.write(readsize,myvec)
-        frameread += 1
-    del fileo
-
-
-def getsilences(filein,hopsize=512,silence=-70):
-    frameread = 0
-    filei     = sndfile(filein)
-    srate     = filei.samplerate()
-    channels  = filei.channels()
-    myvec     = fvec(hopsize,channels)
-    readsize  = filei.read(hopsize,myvec)
-    mylist    = []
-    wassilence = 0
-    while(readsize==hopsize):
-        readsize = filei.read(hopsize,myvec)
-        if (aubio_silence_detection(myvec(),silence)==1):
-            if wassilence == 0:
-                mylist.append(frameread)
-                wassilence == 1
-        else: wassilence = 0
-        frameread += 1
-    return mylist
-
-
-def getpitch(filein,mode=aubio_pitch_mcomb,bufsize=1024,hopsize=512,omode=aubio_pitchm_freq,
-        samplerate=44100.,silence=-70):
-    frameread = 0
-    filei     = sndfile(filein)
-    srate     = filei.samplerate()
-    channels  = filei.channels()
-    myvec     = fvec(hopsize,channels)
-    readsize  = filei.read(hopsize,myvec)
-    pitchdet  = pitchdetection(mode=mode,bufsize=bufsize,hopsize=hopsize,
-                         channels=channels,samplerate=srate,omode=omode)
-    mylist    = []
-    while(readsize==hopsize):
-        readsize = filei.read(hopsize,myvec)
-        freq = pitchdet(myvec)
-        #print "%.3f     %.2f" % (now,freq)
-        if (aubio_silence_detection(myvec(),silence)!=1):
-                mylist.append(freq)
-        else: 
-                mylist.append(-1.)
-        frameread += 1
-    return mylist
+#def getonsets(filein,threshold=0.2,silence=-70.,bufsize=1024,hopsize=512,
+#                mode='dual',localmin=False,storefunc=False,derivate=False):
+#        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,derivate=derivate)
+#        mylist    = list()
+#        if localmin:
+#                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
+#                if localmin:
+#                        if val > 0: ovalist.append(val)
+#                        else: ovalist.append(0)
+#                        ovalist.pop(0)
+#                if storefunc:
+#                        ofunclist.append(val)
+#                if (isonset == 1):
+#                        if localmin:
+#                                i=len(ovalist)-1
+#                                # find local minima before peak 
+#                                while ovalist[i-1] < ovalist[i] and i > 0:
+#                                        i -= 1
+#                                now = (frameread+1-i)
+#                        else:
+#                                now = frameread
+#                        if now > 0 :
+#                                mylist.append(now)
+#                        else:
+#                                now = 0
+#                                mylist.append(now)
+#                frameread += 1
+#        return mylist, ofunclist
+#
+#def cutfile(filein,slicetimes,zerothres=0.008,bufsize=1024,hopsize=512):
+#    frameread = 0
+#    readsize  = hopsize 
+#    filei     = sndfile(filein)
+#    framestep = hopsize/(filei.samplerate()+0.)
+#    channels  = filei.channels()
+#    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)
+#    mycopy    = fvec(hopsize,channels)
+#    while(readsize==hopsize):
+#        readsize = filei.read(hopsize,myvec)
+#        # write to current file
+#        if len(slicetimes) and frameread >= slicetimes[0]:
+#            slicetimes.pop(0)
+#            # write up to 1st zero crossing
+#            zerocross = 0
+#            while ( abs( myvec.get(zerocross,0) ) > zerothres ):
+#                zerocross += 1
+#            writesize = fileo.write(zerocross,myvec)
+#            fromcross = 0
+#            while (zerocross < readsize):
+#                for i in range(channels):
+#                    mycopy.set(myvec.get(zerocross,i),fromcross,i)
+#                    fromcross += 1
+#                    zerocross += 1
+#            del fileo
+#            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)
+#        else:
+#            writesize = fileo.write(readsize,myvec)
+#        frameread += 1
+#    del fileo
+#
+#
+#def getsilences(filein,hopsize=512,silence=-70):
+#    frameread = 0
+#    filei     = sndfile(filein)
+#    srate     = filei.samplerate()
+#    channels  = filei.channels()
+#    myvec     = fvec(hopsize,channels)
+#    readsize  = filei.read(hopsize,myvec)
+#    mylist    = []
+#    wassilence = 0
+#    while(readsize==hopsize):
+#        readsize = filei.read(hopsize,myvec)
+#        if (aubio_silence_detection(myvec(),silence)==1):
+#            if wassilence == 0:
+#                mylist.append(frameread)
+#                wassilence == 1
+#        else: wassilence = 0
+#        frameread += 1
+#    return mylist
+#
+#
+#def getpitch(filein,mode=aubio_pitch_mcomb,bufsize=1024,hopsize=512,omode=aubio_pitchm_freq,
+#        samplerate=44100.,silence=-70):
+#    frameread = 0
+#    filei     = sndfile(filein)
+#    srate     = filei.samplerate()
+#    channels  = filei.channels()
+#    myvec     = fvec(hopsize,channels)
+#    readsize  = filei.read(hopsize,myvec)
+#    pitchdet  = pitchdetection(mode=mode,bufsize=bufsize,hopsize=hopsize,
+#                         channels=channels,samplerate=srate,omode=omode)
+#    mylist    = []
+#    while(readsize==hopsize):
+#        readsize = filei.read(hopsize,myvec)
+#        freq = pitchdet(myvec)
+#        #print "%.3f     %.2f" % (now,freq)
+#        if (aubio_silence_detection(myvec(),silence)!=1):
+#                mylist.append(freq)
+#        else: 
+#                mylist.append(-1.)
+#        frameread += 1
+#    return mylist
 
 
 class taskparams(object):
@@ -218,6 +218,8 @@ class task(taskparams):
                """ open the input file and initialize default argument 
                parameters should be set *before* calling this method.
                """
+               import time
+               self.tic = time.time()
                if params == None: self.params = taskparams()
                else: self.params = params
                self.frameread = 0
@@ -229,6 +231,7 @@ class task(taskparams):
                self.step      = float(self.srate)/float(self.params.hopsize)
                self.myvec     = fvec(self.params.hopsize,self.channels)
                self.output    = output
+
        def __call__(self):
                self.readsize = self.filei.read(self.params.hopsize,self.myvec)
                self.frameread += 1
@@ -249,6 +252,11 @@ class task(taskparams):
                """ Plot data """
                pass
 
+       def time(self):
+               import time
+               print "CPU time is now %f seconds," % time.clock(),
+               print "task execution took %f seconds" % (time.time() - self.tic)
+
 class tasksilence(task):
        wassilence = 1
        issilence  = 1
@@ -316,13 +324,13 @@ class taskpitch(task):
                meddist = self.truth - med
                return avgdist, meddist
 
-       def plot(self):
+       def plot(self,pitch,outplot=None):
                from aubio.gnuplot import plot_pitch
                plot_pitch(self.input, 
                        pitch, 
-                       samplerate=samplerate
+                       samplerate=float(self.srate)
                        hopsize=self.params.hopsize, 
-                       outplot=options.outplot)
+                       outplot=outplot)
 
 
 class taskonset(task):
index d774be6f915df08cc1ee7d44af6396bb6ab6fe0c..d3bbe64a947260a81836adfdff0cded81a2de953 100755 (executable)
@@ -70,7 +70,7 @@ def parse_args():
         # to be implemented
         parser.add_option("-b","--beat",
                           action="store_true", dest="beat", default=False,
-                          help="NOT IMPLEMENTED output beat locations")
+                          help="output beat locations")
         (options, args) = parser.parse_args()
         if not options.filename: 
                  print "no file name given\n", usage
index 2060d0e64b227847a229d3123792efcce656e3d2..4eecdcc2cd9e601d6d3be76e13a47779c6e475e1 100755 (executable)
@@ -16,9 +16,8 @@ def parse_args():
         parser.add_option("-i","--input",
                           action="store", dest="filename", 
                           help="input sound file")
-        parser.add_option("-m","--mode", action="callback", 
-                          callback=check_pitch_mode, dest="mode", 
-                          default=[aubio_pitch_mcomb],
+        parser.add_option("-m","--mode", 
+                         action="store", dest="mode", default='mcomb',
                           help="pitch detection mode [default=mcomb] \
                           mcomb|yin|fcomb|schmitt")
         parser.add_option("-u","--units", action="callback", 
@@ -85,56 +84,43 @@ options, args = parse_args()
 #print options.bufsize, options.hopsize
 
 filename   = options.filename
-samplerate = float(sndfile(filename).samplerate())
-hopsize    = int(options.hopsize)
-bufsize    = int(options.bufsize)
-step       = float(samplerate)/float(hopsize)
-threshold  = float(options.threshold)
-silence    = float(options.silence)
-mode       = options.mode
+params = taskparams()
+#params.samplerate = float(sndfile(filename).samplerate())
+params.hopsize    = int(options.hopsize)
+params.bufsize    = int(options.bufsize)
+#params.step       = float(samplerate)/float(hopsize)
+params.threshold  = float(options.threshold)
+params.silence    = float(options.silence)
 #mintol     = float(options.mintol)*step
 # default take back system delay
 if options.delay: delay = float(options.delay)
-else:             delay = 2./step
+else:             delay = 2./params.step
 
 if options.note:
         exit("not implemented yet")
-else:
-        pitch = []
-        for i in range(len(mode)):
-                pitch.append(getpitch(filename, #threshold,
-                        mode=mode[i],
-                        omode=options.omode,
-                        bufsize=bufsize,hopsize=hopsize,
-                        silence=silence))
-                for j in range(len(pitch[i])):
-                        if pitch[i][j] > 1500 or pitch[i][j] < 40:
-                                pitch[i][j] = 0.;
 
-## take back system delay
-#if delay != 0:
-#        for i in range(len(onsets)):
-#                onsets[i] -= delay*step
-#
-## prune doubled 
-#if mintol > 0:
-#        last = -2*mintol
-#        newonsets = []
-#        for new in onsets:
-#                if (new - last > mintol): 
-#                        newonsets.append(new)
-#                last = new
-#        onsets = newonsets
 
-# print times in second
-if options.verbose:
-        for j in range(len(pitch[0])): 
-                print "%f\t" % (j/step),
-                for i in range(len(pitch)): 
-                        print "%f\t" % pitch[i][j],
-                print
+pitch = []
+modes = options.mode.split(',')
+for i in range(len(modes)):
+       params.pitchmode  = modes[i]
+       dotask = taskpitch
+       #pitch.append(getpitch(filename, #threshold,
+       #       mode=mode[i],
+       #       omode=options.omode,
+       #       bufsize=bufsize,hopsize=hopsize,
+       #       silence=silence))
+       filetask = dotask(filename,params=params)
+       pitch.append(filetask.compute_all())
+       for j in range(len(pitch[i])):
+               if pitch[i][j] > 10000 or pitch[i][j] < 40:
+                       pitch[i][j] = 0.;
 
-if options.plot:
-        from aubio.gnuplot import plot_pitch
-        plot_pitch(filename, pitch, 
-                samplerate=samplerate, hopsize=hopsize, outplot=options.outplot)
+       if options.verbose:
+               for j in range(len(pitch[i])): 
+                       print "%f\t" % (j/params.step),
+                       print "%f\t" % pitch[i][j],
+                       print
+
+       if options.plot:
+               filetask.plot(pitch,outplot=options.outplot)