source: python/test/bench/tempo/demo-tempo-acf @ e638bcf

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since e638bcf was aa3637a, checked in by Paul Brossier <piem@altern.org>, 18 years ago

add demo-tempo scripts
add demo-tempo scripts

  • Property mode set to 100755
File size: 5.4 KB
Line 
1#! /usr/bin/python
2
3""" this file was written by Paul Brossier
4  it is released under the GNU/GPL license.
5"""
6
7import sys,time
8from aubio.task import taskbeat,taskparams
9from aubio.aubioclass import fvec, aubio_autocorr
10from aubio.gnuplot import gnuplot_create
11from aubio.aubiowrapper import *
12from math import exp,log
13
14usage = "usage: %s [options] -i soundfile" % sys.argv[0]
15
16def parse_args():
17        from optparse import OptionParser
18        parser = OptionParser(usage=usage)
19        parser.add_option("-i","--input",
20                          action="store", dest="filename",
21                          help="input sound file")
22        parser.add_option("-n","--printframe",
23                          action="store", dest="printframe", default=-1,
24                          help="make a plot of the n_th frame")
25        parser.add_option("-x","--xsize",
26                          action="store", dest="xsize", default=1.,
27                          type='float', help="define xsize for plot")
28        parser.add_option("-y","--ysize",
29                          action="store", dest="ysize", default=1.,
30                          type='float', help="define ysize for plot")
31        parser.add_option("-O","--outplot",
32                          action="store", dest="outplot", default=None,
33                          help="save plot to output.{ps,png}")
34        (options, args) = parser.parse_args()
35        if not options.filename:
36                 print "no file name given\n", usage
37                 sys.exit(1)
38        return options, args
39
40def plotdata(x,y,plottitle="",**keyw):
41        import Gnuplot
42        return Gnuplot.Data(x, y, title="%s" % plottitle,**keyw)
43
44options, args = parse_args()
45filename = options.filename
46xsize = float(options.xsize)
47ysize = float(options.ysize)
48
49printframe = int(options.printframe)
50
51if options.outplot and printframe > 0:
52  extension = options.outplot.split('.')[-1]
53  outplot = '.'.join(options.outplot.split('.')[:-1])
54else:
55  extension = ''
56  outplot = None
57f = gnuplot_create(outplot=outplot,extension=extension)
58
59params = taskparams()
60params.onsetmode = 'specdiff'
61task = taskbeat(filename,params=params)
62
63hopsize = params.hopsize
64bufsize = params.bufsize
65btstep = task.btstep
66winlen = task.btwinlen
67laglen = winlen/4
68step = winlen/4
69
70timesig = 0
71maxnumelem = 4
72gp = 0
73counter = 0
74flagconst = 0
75constthresh = 3.901
76g_var      = 3.901
77rp = 0
78rp1 = 0
79rp2 = 0
80g_mu = 0
81
82rayparam = 48/512.*winlen
83
84t     = [i for i in range(hopsize)]
85#tlong = [i for i in range(hopsize*(btstep-1))]
86#tall  = [i for i in range(hopsize*btstep)]
87sig    = [0 for i in range(hopsize*btstep)]
88dfx = [i for i in range(winlen)]
89dfframe = [0 for i in range(winlen)]
90dfrev = [0 for i in range(winlen)]
91acframe = [0 for i in range(winlen/2)]
92
93localacf = [0 for i in range(winlen)]
94inds = [0 for i in range(maxnumelem)]
95
96acx = [i for i in range(laglen)]
97acfout  = [0 for i in range(laglen)]
98
99phwv  = [0 for i in range(2*laglen)]
100phwvx = [i for i in range(2*laglen)]
101
102dfwvnorm = exp(log(2.0)*(winlen+2.)/rayparam);
103dfwv = [exp(log(2.)*(i+1.)/rayparam)/dfwvnorm for i in range(winlen)]
104
105gwv = [exp(-.5*(j+1.-g_mu)**2/g_var**2) for j in range(laglen)]
106rwv = [(i+1.)/rayparam**2 * exp(-(i+1.)**2 / (2.*rayparam)**2)
107        for i in range(0,laglen)]
108acf = fvec(winlen,1)
109
110if options.outplot and printframe > 0:
111  extension = options.outplot.split('.')[-1]
112  outplot = '.'.join(options.outplot.split('.')[:-1])
113else:
114  extension = ''
115  outplot = None
116f = gnuplot_create(outplot=outplot,extension=extension)
117nrframe = 0
118while (task.readsize == params.hopsize):
119  task()
120  #print task.pos2
121  sig[:-hopsize] = [i for i in sig[-(btstep-1)*hopsize:]]
122  sig[-hopsize:] = [task.myvec.get(i,0) for i in t]
123
124  #g('set xrange [%f:%f]' % (t[0],t[-1]))
125  #time.sleep(.2)
126  if task.pos2==btstep-1:
127    nrframe += 1
128    dfframe = [task.dfframe.get(i,0) for i in range(winlen)]
129    # start beattracking_do
130    for i in range(winlen):
131      dfrev[winlen-1-i] = 0.
132      dfrev[winlen-1-i] = dfframe[i]*dfwv[i]
133    aubio_autocorr(task.dfframe(),acf());
134    acframe = [acf.get(i,0) for i in range(winlen/2)]
135    if printframe == nrframe or printframe == -1:
136      d  = [[plotdata(range(btstep*hopsize),sig,plottitle="input signal", with='lines')]]
137      d  += [[plotdata(range(-winlen,0),dfframe,plottitle="onset detection", with='lines')]]
138      d  += [[plotdata(range(winlen/2),acframe,plottitle="autocorrelation", with='lines')]]
139
140    # plot all this
141    if printframe == nrframe or printframe == -1:
142
143      f('set lmargin 4')
144      f('set rmargin 4')
145      f('set size %f,%f' % (1.0*xsize,1.0*ysize) )
146      f('set key spacing 1.3')
147      f('set multiplot')
148
149      f('set size %f,%f' % (1.0*xsize,0.33*ysize) )
150      f('set orig %f,%f' % (0.0*xsize,0.66*ysize) )
151      f('set xrange [%f:%f]' % (0,btstep*hopsize) )
152      f.title('Input signal')
153      f.xlabel('time (samples)')
154      f.plot(*d[0])
155      f('set size %f,%f' % (1.0*xsize,0.33*ysize) )
156      f('set orig %f,%f' % (0.0*xsize,0.33*ysize) )
157      f('set xrange [%f:%f]' % (-winlen,0) )
158      f.title('Onset detection function')
159      f.xlabel('time (df samples)')
160      f.plot(*d[1])
161      f('set size %f,%f' % (1.0*xsize,0.33*ysize) )
162      f('set orig %f,%f' % (0.0*xsize,0.00*ysize) )
163      f('set xrange [%f:%f]' % (0,winlen/2) )
164      f.title('Autocorrelation')
165      f.xlabel('lag (df samples)')
166      f.plot(*d[2])
167      f('set nomultiplot')
168    if printframe == -1: a = sys.stdin.read()
169    elif 0 < printframe and printframe < nrframe:
170      break
Note: See TracBrowser for help on using the repository browser.