shithub: aubio

Download patch

ref: 4f4a8a4fdc1225669e4f821cc96945524c6addb0
parent: 7c9ad741f6f9584821ab660d827d8ed2d8aa528d
author: Paul Brossier <piem@altern.org>
date: Mon Dec 19 16:25:51 EST 2005

move to new nodes and tasks
move to new nodes and tasks


--- a/python/aubiocut
+++ b/python/aubiocut
@@ -15,8 +15,8 @@
         parser.add_option("-i","--input",
                           action="store", dest="filename", 
                           help="input sound file")
-        parser.add_option("-m","--mode", action="callback", 
-                          callback=check_onset_mode, dest="mode", default=['dual'], 
+        parser.add_option("-m","--mode", 
+			  action="store", dest="mode", default=['dual'], 
                           help="onset detection mode [default=dual] \
                           complexdomain|hfc|phase|specdiff|energy|kl|mkl|dual")
         parser.add_option("-B","--bufsize",
@@ -80,18 +80,16 @@
 options, args = parse_args()
 
 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)
-zerothres  = float(options.zerothres)
-silence    = float(options.silence)
-mintol     = float(options.mintol)*step
-mode       = options.mode
+params = taskparams()
+params.hopsize    = int(options.hopsize)
+params.bufsize    = int(options.bufsize)
+params.threshold  = float(options.threshold)
+params.zerothres  = float(options.zerothres)
+params.silence    = float(options.silence)
+params.mintol     = float(options.mintol)
 # default take back system delay
 if options.delay: delay = float(options.delay)
-else:             delay = 3./step
+else:             delay = 3./params.step
 
 if options.beat:
         #onsets = getbeats(filename,threshold,silence,mode=options.mode)
@@ -98,27 +96,34 @@
         exit("not implemented yet")
 elif options.silencecut:
         onsets = getsilences(filename,hopsize=hopsize,silence=silence)
-elif options.plot: storefunc=True
-else:              storefunc=False
+elif options.plot: params.storefunc=True
+else:              params.storefunc=False
 
 lonsets, lofunc = [], []
-for i in range(len(mode)):
-        onsets, ofunc = getonsets(filename,threshold,silence,
-                mode=mode[i],localmin=options.localmin,
-                derivate=options.derivate,
-                bufsize=bufsize,hopsize=hopsize,storefunc=True)
+modes = options.mode.split(',')
+for i in range(len(modes)):
 
+	params.onsetmode = modes[i] 
+	filetask = taskonset(filename,params=params)
+	onsets = filetask.compute_all()
+	ofunc = filetask.ofunc
+        #onsets, ofunc = getonsets(filename,threshold,silence,
+        #        mode=mode[i],localmin=options.localmin,
+        #        derivate=options.derivate,
+        #        bufsize=bufsize,hopsize=hopsize,storefunc=True)
+
         # take back system delay
         if delay != 0:
-                for i in range(len(onsets)):
-                        onsets[i] -= delay*step
+                for each in range(len(onsets)):
+			onsets[each] = onsets[each][0] - delay*params.step
 
         # prune doubled 
-        if mintol > 0:
-                last = -2*mintol
+	params.mintol *= params.step
+        if params.mintol > 0:
+                last = -2*params.mintol
                 newonsets = []
                 for new in onsets:
-                        if (new - last > mintol): 
+                        if (new - last > params.mintol): 
                                 newonsets.append(new)
                         last = new
                 onsets = newonsets
@@ -126,17 +131,17 @@
         lonsets.append(onsets)
         lofunc.append(ofunc)
 
-# print times in second
-if options.verbose:
-        maxonset = 0
-        for j in range(len(mode)):
-                for i in range(len(lonsets[j])):
-                        print lonsets[j][i]/step
+	# print times in second
+	if options.verbose:
+		print modes[i]
+		maxonset = 0
+		for i in range(len(onsets)):
+				print onsets[i]*params.step
 
-if options.plot:
-        from aubio.gnuplot import plot_onsets
-        plot_onsets(filename, lonsets, lofunc, 
-                samplerate=samplerate, hopsize=hopsize, outplot=options.outplot)
+	if options.plot:
+		filetask.plot(onsets, ofunc)
+		filetask.plotplot(outplot=options.outplot)
 
 if options.cut:
-        cutfile(filename,onsets,zerothres=zerothres,bufsize=bufsize,hopsize=hopsize)
+        a = taskcut(filename,onsets,params=params)
+	a.compute_all()
--- a/python/bench-onset
+++ b/python/bench-onset
@@ -1,31 +1,17 @@
 #! /usr/bin/python
 
-from aubio.bench.config import *
 from aubio.bench.node import *
+from aubio.tasks import *
 
-class onset_parameters:
-	def __init__(self):
-		""" set default parameters """
-		self.silence = -70
-		self.derivate = False
-		self.localmin = False
-		self.bufsize = 512
-		self.hopsize = 256
-		self.samplerate = 44100
-		self.tol = 0.05
-		self.step = float(self.hopsize)/float(self.samplerate)
-		self.threshold = 0.1
-		self.mode = 'dual'
-
 class benchonset(bench):
 	
-	def compute_results(self):
+	def dir_eval(self):
 		self.P = 100*float(self.expc-self.missed-self.merged)/(self.expc-self.missed-self.merged + self.bad+self.doubled)
 		self.R = 100*float(self.expc-self.missed-self.merged)/(self.expc-self.missed-self.merged + self.missed+self.merged)
 		if self.R < 0: self.R = 0
 		self.F = 2* self.P*self.R / (self.P+self.R)
 
-		self.values = [self.params.mode, 
+		self.values = [self.params.onsetmode, 
 		"%2.3f" % self.params.threshold,
 		self.orig,
 		self.expc,
@@ -42,51 +28,17 @@
 		"%2.3f" % self.R,
 		"%2.3f" % self.F  ]
 
-	def compute_onset(self,input,output):
-		from aubio.tasks import getonsets, get_onset_mode
-		from aubio.onsetcompare import onset_roc, onset_diffs
-		from aubio.txtfile import read_datafile
-		amode = 'roc'
-		vmode = 'verbose'
-		vmode = ''
-		lres, ofunc = getonsets(input,
-			self.params.threshold,
-			self.params.silence,
-			mode=get_onset_mode(self.params.mode),
-			localmin=self.params.localmin,
-			derivate=self.params.derivate,
-			bufsize=self.params.bufsize,
-			hopsize=self.params.hopsize,
-			storefunc=False)
+	def file_exec(self,input,output):
+		filetask = self.task(input,params=self.params)
+		computed_data = filetask.compute_all()
+		results = filetask.eval(computed_data)
+		self.orig    += filetask.orig
+		self.missed  += filetask.missed
+		self.merged  += filetask.merged
+		self.expc    += filetask.expc
+		self.bad     += filetask.bad
+		self.doubled += filetask.doubled
 
-		for i in range(len(lres)): lres[i] = lres[i]*self.params.step
-		ltru = read_datafile(input.replace('.wav','.txt'),depth=0)
-		if vmode=='verbose':
-			print "Running with mode %s" % self.params.mode, 
-			print " and threshold %f" % self.params.threshold, 
-			print " on file", input
-		#print ltru; print lres
-		if amode == 'localisation':
-			l = onset_diffs(ltru,lres,self.params.tol)
-			mean = 0
-			for i in l: mean += i
-			if len(l): print "%.3f" % (mean/len(l))
-			else: print "?0"
-		elif amode == 'roc':
-			orig, missed, merged, expc, bad, doubled = onset_roc(ltru,lres,self.params.tol)
-			self.orig    += orig
-			self.missed  += missed
-			self.merged  += merged
-			self.expc    += expc
-			self.bad     += bad
-			self.doubled += doubled
-		self.compute_results()
-			
-	def compute_data(self):
-		self.orig, self.missed, self.merged, self.expc, \
-			self.bad, self.doubled = 0, 0, 0, 0, 0, 0
-		act_on_data(self.compute_onset,self.datadir,self.resdir, \
-			suffix='',filter='f -name \'*.wav\'')
 
 	def run_bench(self,modes=['dual'],thresholds=[0.5]):
 		self.modes = modes
@@ -94,11 +46,11 @@
 
 		self.pretty_print(self.titles)
 		for mode in self.modes:
-			self.params.mode = mode
+			self.params.onsetmode = mode
 			for threshold in self.thresholds:
 				self.params.threshold = threshold
-				self.compute_data()
-				self.compute_results()
+				self.dir_exec()
+				self.dir_eval()
 				self.pretty_print(self.values)
 
 	def auto_learn(self,modes=['dual'],thresholds=[0.1,1.5]):
@@ -109,21 +61,24 @@
 			steps = 10 
 			lesst = thresholds[0] 
 			topt = thresholds[1]
-			self.params.mode = mode
+			self.params.onsetmode = mode
 
 			self.params.threshold = topt 
-			self.compute_data()
+			self.dir_exec()
+			self.dir_eval()
 			self.pretty_print(self.values)
 			topF = self.F 
 
 			self.params.threshold = lesst 
-			self.compute_data()
+			self.dir_exec()
+			self.dir_eval()
 			self.pretty_print(self.values)
 			lessF = self.F 
 
 			for i in range(steps):
 				self.params.threshold = ( lesst + topt ) * .5 
-				self.compute_data()
+				self.dir_exec()
+				self.dir_eval()
 				self.pretty_print(self.values)
 				if self.F == 100.0 or self.F == topF: 
 					print "assuming we converged, stopping" 
@@ -143,34 +98,31 @@
 					lesst /= 2.
 
 
-#modes = [ 'complex' ]
-modes = ['complex', 'energy', 'phase', 'specdiff', 'kl', 'mkl', 'dual']
-#thresholds = [1.5]
-thresholds = [ 0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5]
+if __name__ == "__main__":
+	import sys
+	if len(sys.argv) > 1: datapath = sys.argv[1]
+	else: print "ERR: a path is required"; sys.exit(1)
+	modes = ['complex', 'energy', 'phase', 'specdiff', 'kl', 'mkl', 'dual']
+	#modes = [ 'complex' ]
+	thresholds = [ 0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5]
+	#thresholds = [1.5]
 
-#datapath = "%s%s" % (DATADIR,'/onset/DB/*/')
-datapath = "%s%s" % (DATADIR,'/onset/DB/PercussivePhrases/RobertRich')
-respath = '/var/tmp/DB-testings'
+	#datapath = "%s%s" % (DATADIR,'/onset/DB/*/')
+	respath = '/var/tmp/DB-testings'
 
-benchonset = benchonset(datapath,respath,checkres=True,checkanno=True)
+	benchonset = benchonset(datapath,respath,checkres=True,checkanno=True)
+	benchonset.params = taskparams()
+	benchonset.task = taskonset
 
-benchonset.params = onset_parameters()
+	benchonset.titles = [ 'mode', 'thres', 'orig', 'expc', 'missd', 'mergd',
+	'bad', 'doubl', 'corrt', 'GD', 'FP', 'GD-merged', 'FP-pruned',
+	'prec', 'recl', 'dist' ]
+	benchonset.formats = ["%12s" , "| %6s", "| %6s", "| %6s", "| %6s", "| %6s", 
+	"| %6s", "| %6s", "| %6s", "| %8s", "| %8s", "| %8s", "| %8s",
+	"| %6s", "| %6s", "| %6s"] 
 
-benchonset.titles = [ 'mode', 'thres', 'orig', 'expc', 'missd', 'mergd',
-'bad', 'doubl', 'corrt', 'GD', 'FP', 'GD-merged', 'FP-pruned',
-'prec', 'recl', 'dist' ]
-benchonset.formats = ["%12s" , "| %6s", "| %6s", "| %6s", "| %6s", "| %6s", 
-"| %6s", "| %6s", "| %6s", "| %8s", "| %8s", "| %8s", "| %8s",
-"| %6s", "| %6s", "| %6s"] 
-
-#benchonset.run_bench(modes=modes,thresholds=thresholds)
-benchonset.auto_learn(modes=modes)
-
-#        gatherdata
-#act_on_data(my_print,datapath,respath,suffix='.txt',filter='f -name \'*.wav\'')
-#        gatherthreshold
-#        gathermodes
-#        comparediffs
-#        gatherdiffs
-
-
+	try:
+		benchonset.auto_learn(modes=modes)
+		#benchonset.run_bench(modes=modes)
+	except KeyboardInterrupt:
+		sys.exit(1)
--- a/python/bench-pitch
+++ b/python/bench-pitch
@@ -5,7 +5,7 @@
 
 class benchpitch(bench):
 	
-	def compute_file(self,input,output):
+	def file_exec(self,input,output):
 		filetask = self.task(input,params=self.params)
 		computed_data = filetask.compute_all()
 		results = filetask.eval(computed_data)
@@ -12,38 +12,31 @@
 		self.results.append(results)
 		truth = filetask.gettruth()
 		#print input, results, results - float(input.split('.')[-2])
-		self.pretty_print((self.params.mode, truth, 
+		self.pretty_print((self.params.pitchmode, truth, 
 			truth - results[0], results[0],
 			truth - results[1], results[1]))
 			
-	def compute_data(self):
-		self.orig, self.missed, self.merged, self.expc, \
-			self.bad, self.doubled = 0, 0, 0, 0, 0, 0
-		act_on_data(self.compute_file,self.datadir, \
-			suffix='',filter='f -name \'*.wav\'')
-	
-	def compute_results(self,truth):
-		for i in self.results: print i
-
-	def run_bench(self,modes=['dual']):
+	def run_bench(self,modes=['schmitt']):
 		self.modes = modes
 		self.pretty_print(self.titles)
 		for mode in self.modes:
-			self.params.mode = mode
-			self.compute_data()
-			#self.compute_results()
-			#self.pretty_print(self.results)
+			self.params.pitchmode = mode
+			self.dir_exec()
+			self.dir_eval()
+			self.dir_plot()
 
 if __name__ == "__main__":
 	import sys
 	if len(sys.argv) > 1: datapath = sys.argv[1]
 	else: print "error: a path is required"; sys.exit(1)
+	if len(sys.argv) > 2:
+		for each in sys.argv[3:-1]: print each
+	modes = ['yin', 'schmitt', 'mcomb', 'fcomb']
 
-	modes = ['schmitt', 'yin', 'mcomb', 'fcomb']
-
 	benchpitch = benchpitch(datapath)
 	benchpitch.params = taskparams()
 	benchpitch.task = taskpitch
+
 
 	benchpitch.titles  = [ 'mode', 'thres', 'avg', 'avgdist' ]
 	benchpitch.formats = ["%12s" , "| %6s", "| %6s", "| %6s", "| %6s", "| %6s" ]