3 """ this file was written by Paul Brossier
4 it is released under the GNU/GPL license.
8 from aubio.task import task,taskparams
9 from aubio.aubioclass import fvec
10 from aubio.gnuplot import gnuplot_create
11 from aubio.aubiowrapper import *
13 usage = "usage: %s [options] -i soundfile" % sys.argv[0]
16 from optparse import OptionParser
17 parser = OptionParser(usage=usage)
18 parser.add_option("-i","--input",
19 action="store", dest="filename",
20 help="input sound file")
21 parser.add_option("-n","--printframe",
22 action="store", dest="printframe", default=-1,
23 help="make a plot of the n_th frame")
24 parser.add_option("-x","--xsize",
25 action="store", dest="xsize", default=1.,
26 help="define xsize for plot")
27 parser.add_option("-y","--ysize",
28 action="store", dest="ysize", default=1.,
29 help="define ysize for plot")
30 parser.add_option("-O","--outplot",
31 action="store", dest="outplot", default=None,
32 help="save plot to output.{ps,png}")
33 (options, args) = parser.parse_args()
34 if not options.filename:
35 print "no file name given\n", usage
39 def plotdata(x,y,plottitle="",**keyw):
41 return Gnuplot.Data(x, y, title="%s" % plottitle,**keyw)
43 options, args = parse_args()
44 filename = options.filename
45 xsize = float(options.xsize)
46 ysize = float(options.ysize)*2
48 printframe = int(options.printframe)
50 print "Will wait for ^D to skip to next plot"
51 print "Press enter before to print to file"
56 params.hopsize = 2048 # 512
57 params.bufsize = params.hopsize #2048
58 taskfile = task(filename,params=params)
60 yin = fvec(params.bufsize/2,1)
62 t = [i for i in range(params.bufsize)]
63 a = [0 for i in range(params.bufsize)]
65 while (taskfile.readsize == params.hopsize):
68 n = [i for i in range(params.bufsize/2)]
69 a = [taskfile.myvec.get(i,0) for i in range(params.hopsize/2)]
70 aubio_pitchyin_diff(taskfile.myvec(),yin()) # compute d[t]
71 c = [yin.get(i,0) for i in range(params.bufsize/2)]
72 aubio_pitchyin_getcum(yin()) # compute d'[t]
73 y = [yin.get(i,0) for i in range(params.bufsize/2)]
74 thresh = [0.1 for i in range(params.bufsize/2)]
75 #t.append((i/float(params.hopsize)+taskfile.frameread)*params.step),t.pop(0)
76 d = [plotdata(n,a,plottitle="signal", with_='lines'),
77 plotdata(n,c,plottitle="d[t]",axes='x1y2', with_='lines lt 1'),
78 plotdata(n,y,plottitle="d'[t]",axes='x1y1', with_='lines lt 2'),
79 plotdata(n,thresh,plottitle="threshold",axes='x1y1', with_='lines lt 3')]
80 #g('set xrange [%f:%f]' % (t[0],t[-1]))
83 g('set yrange [-1:3]')
84 g('set xrange [0:%d]' % (params.bufsize/2))
85 g('set title \"%s\"' % "Example of period detection using YIN")
89 if a == "\n" or printframe == taskfile.frameread:
90 from os.path import basename
91 outplot = "_".join([basename(sys.argv[0]),'_'.join(basename(filename).split('.')),"%d" % taskfile.frameread])
93 f = gnuplot_create(outplot=outplot,extension='ps')
94 f('set size %f,%f;' % (xsize,ysize) )
95 f('set lmargin %f' % (15*xsize))
96 f('set rmargin %f' % (10*xsize))
97 #f('set title \"%s\"' % "Example of period detection using YIN")
99 f.ylabel('amplitude',offset=(+.5,0))
100 f.xlabel('time (samples)')
101 f('set size %f,%f;' % (xsize,ysize*0.4) )
102 f('set orig %f,%f;' % (0,ysize*0.6) )
103 sigmax = max(abs(min(a)),abs(max(a)))
104 f('set yrange [%f:%f]' % (-1.3*sigmax,1.3*sigmax))
105 f('set xrange [0:%d]' % (params.bufsize/2))
109 f.xlabel('lag (samples)')
110 f('set bmargin %f' % (4*ysize))
111 f('set size %f,%f;' % (xsize,ysize*0.6) )
112 f('set orig %f,%f;' % (0,0) )
114 f('set xrange [0:%d]' % (params.bufsize/2))
117 f('set ytics nomirror')
122 f.ylabel('amplitude')
124 f('set y2tics nomirror')
125 f('set ytics nomirror')
131 #f('set yrange [-1:3]')
133 print "saved plot", outplot, 'ps'
134 elif printframe < taskfile.frameread: