1 | #! /usr/bin/python |
---|
2 | |
---|
3 | """ this file was written by Paul Brossier |
---|
4 | it is released under the GNU/GPL license. |
---|
5 | """ |
---|
6 | |
---|
7 | import sys,time |
---|
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 * |
---|
12 | |
---|
13 | usage = "usage: %s [options] -i soundfile" % sys.argv[0] |
---|
14 | |
---|
15 | def parse_args(): |
---|
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 |
---|
36 | sys.exit(1) |
---|
37 | return options, args |
---|
38 | |
---|
39 | def plotdata(x,y,plottitle="",**keyw): |
---|
40 | import Gnuplot |
---|
41 | return Gnuplot.Data(x, y, title="%s" % plottitle,**keyw) |
---|
42 | |
---|
43 | options, args = parse_args() |
---|
44 | filename = options.filename |
---|
45 | xsize = float(options.xsize) |
---|
46 | ysize = float(options.ysize)*2 |
---|
47 | |
---|
48 | printframe = int(options.printframe) |
---|
49 | if printframe == -1: |
---|
50 | print "Will wait for ^D to skip to next plot" |
---|
51 | print "Press enter before to print to file" |
---|
52 | |
---|
53 | |
---|
54 | g = gnuplot_create() |
---|
55 | params = taskparams() |
---|
56 | params.hopsize = 2048 # 512 |
---|
57 | params.bufsize = params.hopsize #2048 |
---|
58 | taskfile = task(filename,params=params) |
---|
59 | |
---|
60 | yin = fvec(params.bufsize/2,1) |
---|
61 | |
---|
62 | t = [i for i in range(params.bufsize)] |
---|
63 | a = [0 for i in range(params.bufsize)] |
---|
64 | |
---|
65 | while (taskfile.readsize == params.hopsize): |
---|
66 | taskfile() |
---|
67 | |
---|
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])) |
---|
81 | #time.sleep(.2) |
---|
82 | g.reset() |
---|
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") |
---|
86 | if printframe == -1: |
---|
87 | g.replot(*d) |
---|
88 | a = sys.stdin.read() |
---|
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]) |
---|
92 | print outplot |
---|
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") |
---|
98 | f('set multiplot') |
---|
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)) |
---|
106 | f.plot(d[0]) |
---|
107 | |
---|
108 | f.ylabel('') |
---|
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) ) |
---|
113 | f('set autoscale') |
---|
114 | f('set xrange [0:%d]' % (params.bufsize/2)) |
---|
115 | f('set notitle') |
---|
116 | f('set y2tics') |
---|
117 | f('set ytics nomirror') |
---|
118 | f('set noytics') |
---|
119 | f('set key right') |
---|
120 | f.plot(d[1]) |
---|
121 | |
---|
122 | f.ylabel('amplitude') |
---|
123 | f.xlabel('') |
---|
124 | f('set y2tics nomirror') |
---|
125 | f('set ytics nomirror') |
---|
126 | f('set noy2tics') |
---|
127 | f('set noxtics') |
---|
128 | f('set ytics') |
---|
129 | f('set key left') |
---|
130 | f.plot(d[2],d[3]) |
---|
131 | #f('set yrange [-1:3]') |
---|
132 | #f.plot(*d) |
---|
133 | print "saved plot", outplot, 'ps' |
---|
134 | elif printframe < taskfile.frameread: |
---|
135 | break |
---|