shithub: aubio

ref: 0fe9aab4da39efa4585fccb5e3d931389f7ee592
dir: /python/test/bench/pitch/bench-pitch/

View raw version
#! /usr/bin/python

from aubio.bench.node import *
from aubio.tasks import *

class benchpitch(bench):
	
	""" list of values to store per file """
	valuenames = ['mode']
	""" list of lists to store per file """
	valuelists = ['orig', 'mean', 'med']
	""" list of values to print per dir """
	printnames = [ 'mode']

	""" per dir """
	formats = {'mode': "%12s" , 'thres': "%5.4s", 
		'dist':  "%5.4s", 'prec': "%5.4s", 'recl':  "%5.4s",
		'Ttrue': "%5.4s", 'Tfp':   "%5.4s", 'Tfn':   "%5.4s", 
		'Tm':    "%5.4s", 'Td':    "%5.4s",
		'aTtrue':"%5.4s", 'aTfp':  "%5.4s", 'aTfn':  "%5.4s", 
		'aTm':   "%5.4s", 'aTd':   "%5.4s",
		'mean':  "%5.40s", 'smean': "%5.40s", 
		'amean':  "%5.40s", 'samean': "%5.40s"}

	def dir_eval(self):
		""" evaluate statistical data over the directory """
		v = self.v

		v['mode']      = self.params.pitchmode

	def file_exec(self,input,output):
		filetask = self.task(input,params=self.params)
		computed_data = filetask.compute_all()
		orig,mean,med = filetask.eval(computed_data)
		
		self.v['orig'].append(orig)
		self.v['mean'].append(mean)
		self.v['med'].append(med)
		#print results#, computed_data
		#print input, results, results - float(input.split('.')[-2])
			
	def run_bench(self,modes=['schmitt']):
		from os.path import basename
		d = []
		self.modes = modes
		self.pretty_titles()
		for mode in self.modes:
			self.params.pitchmode = mode
			self.dir_eval_print()
			self.plotpitchtessiture(d,
				self.v['orig'], 
				self.v['med'],
				plottitle=self.v['mode'],
				plotmode='points')
		#d.append('beta = .25,orig(x) title \"-2 octave\"')
		d.append('beta = .50,orig(x) title \"-1 octave\"')
		d.append('beta = 1.0,orig(x) title \"original\"')
		d.append('beta = 2.0,orig(x) title \"+1 octave\"')
		title = basename(self.datadir)
		outplot = "_-_".join(('pitchtessiture',title))
		for ext in ('ps','png','svg',''):
			self.plotplotpitchtessiture(d,
				plottitle=title,
				outplot=outplot,
				extension=ext)

	"""
	Plot functions 
	"""

	def plotpitchtessiture(self,d,lx,ly,plottitle="",plotmode='linespoints'):
		import Gnuplot, Gnuplot.funcutils
		d.append(Gnuplot.Data(lx, ly, with=plotmode, title="%s" % (plottitle) ))

	def plotplotpitchtessiture(self,d,plottitle='',outplot=0,extension=''):
		from aubio.gnuplot import gnuplot_create
		g = gnuplot_create(outplot=outplot,extension=extension) 
		g.title(plottitle)
		g('orig(x) = beta*x')
		g.xlabel('original pitch (Hz)')
		g.ylabel('detected pitch (Hz)')
		g('set log xy')
		g.plot(*d)


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 = ['schmitt', 'yin', 'mcomb', 'fcomb']
	#modes = ['fcomb']

	params = taskparams()
	params.bufsize = 2048
	params.hopsize = params.bufsize/2
	benchpitch = benchpitch(datapath,params=params)
	benchpitch.task = taskpitch

	#benchpitch.titles  = [ 'mode', 'thres', 'avg', 'avgdist' ]
	#benchpitch.formats = ["%12s" , "| %6s", "| %6s", "| %6s", "| %6s", "| %6s" ]
	try:
		benchpitch.run_bench(modes=modes)
	except KeyboardInterrupt:
		print "Interrupted by user"
		sys.exit(1)

	sys.exit(0)