source: tests/demo/bench/tempo/demo-tempo-acf @ e03f74d

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

move tests/python/demo to tests/, add localaubio.py module helpers

  • Property mode set to 100755
File size: 4.8 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, gnuplot_addargs
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        gnuplot_addargs(parser)
26        (options, args) = parser.parse_args()
27        if not options.filename:
28                 print "no file name given\n", usage
29                 sys.exit(1)
30        return options, args
31
32def plotdata(x,y,plottitle="",**keyw):
33        import Gnuplot
34        return Gnuplot.Data(x, y, title="%s" % plottitle,**keyw)
35
36options, args = parse_args()
37filename = options.filename
38xsize = float(options.xsize)
39ysize = float(options.ysize)
40
41printframe = int(options.printframe)
42
43if options.outplot and printframe > 0:
44  extension = options.outplot.split('.')[-1]
45  outplot = '.'.join(options.outplot.split('.')[:-1])
46else:
47  extension = ''
48  outplot = None
49f = gnuplot_create(outplot,extension,options)
50
51params = taskparams()
52params.onsetmode = 'specdiff'
53task = taskbeat(filename,params=params)
54
55hopsize = params.hopsize
56bufsize = params.bufsize
57btstep = task.btstep
58winlen = task.btwinlen
59laglen = winlen/4
60step = winlen/4
61
62timesig = 0
63maxnumelem = 4
64gp = 0
65counter = 0
66flagconst = 0
67constthresh = 3.901
68g_var      = 3.901
69rp = 0
70rp1 = 0
71rp2 = 0
72g_mu = 0
73
74rayparam = 48/512.*winlen
75
76t     = [i for i in range(hopsize)]
77#tlong = [i for i in range(hopsize*(btstep-1))]
78#tall  = [i for i in range(hopsize*btstep)]
79sig    = [0 for i in range(hopsize*btstep*4)]
80dfx = [i for i in range(winlen)]
81dfframe = [0 for i in range(winlen)]
82dfrev = [0 for i in range(winlen)]
83acframe = [0 for i in range(winlen/2)]
84
85localacf = [0 for i in range(winlen)]
86inds = [0 for i in range(maxnumelem)]
87
88acx = [i for i in range(laglen)]
89acfout  = [0 for i in range(laglen)]
90
91phwv  = [0 for i in range(2*laglen)]
92phwvx = [i for i in range(2*laglen)]
93
94dfwvnorm = exp(log(2.0)*(winlen+2.)/rayparam);
95dfwv = [exp(log(2.)*(i+1.)/rayparam)/dfwvnorm for i in range(winlen)]
96
97gwv = [exp(-.5*(j+1.-g_mu)**2/g_var**2) for j in range(laglen)]
98rwv = [(i+1.)/rayparam**2 * exp(-(i+1.)**2 / (2.*rayparam)**2)
99        for i in range(0,laglen)]
100acf = fvec(winlen,1)
101
102nrframe = 0
103while (task.readsize == params.hopsize):
104  task()
105  #print task.pos2
106  sig[:-hopsize] = [i for i in sig[-(btstep*4-1)*hopsize:]]
107  sig[-hopsize:] = [task.myvec.get(i,0) for i in t]
108
109  #g('set xrange [%f:%f]' % (t[0],t[-1]))
110  #time.sleep(.2)
111  if task.pos2==btstep-1:
112    nrframe += 1
113    dfframe = [task.dfframe.get(i,0) for i in range(winlen)]
114    # start beattracking_do
115    for i in range(winlen):
116      dfrev[winlen-1-i] = 0.
117      dfrev[winlen-1-i] = dfframe[i]*dfwv[i]
118    aubio_autocorr(task.dfframe(),acf());
119    acframe = [acf.get(i,0) for i in range(winlen/2)]
120    if printframe == nrframe or printframe == -1:
121      d  = [[plotdata(range(0,btstep*hopsize*4,4),sig[0:-1:4],plottitle="input signal", with='lines')]]
122      d  += [[plotdata(range(-winlen,0),dfframe,plottitle="onset detection", with='lines')]]
123      d  += [[plotdata(range(winlen/2),acframe,plottitle="autocorrelation", with='lines')]]
124
125    # plot all this
126    if printframe == nrframe or printframe == -1:
127
128      f('set lmargin 4')
129      f('set rmargin 4')
130      f('set size %f,%f' % (1.0*xsize,1.0*ysize) )
131      f('set key spacing 1.3')
132      f('set multiplot')
133
134      f('set size %f,%f' % (1.0*xsize,0.33*ysize) )
135      f('set orig %f,%f' % (0.0*xsize,0.66*ysize) )
136      f('set xrange [%f:%f]' % (0,btstep*hopsize*4) )
137      f('set yrange [%f:%f]' % (-1.2*max(sig),1.2*max(sig)) )
138      f.title('Input signal')
139      f.xlabel('time (samples)')
140      f.plot(*d[0])
141      f('set size %f,%f' % (1.0*xsize,0.33*ysize) )
142      f('set orig %f,%f' % (0.0*xsize,0.33*ysize) )
143      f('set xrange [%f:%f]' % (-winlen,0) )
144      f('set autoscale y')
145      f.title('Onset detection function')
146      f.xlabel('time (df samples)')
147      f.plot(*d[1])
148      f('set size %f,%f' % (1.0*xsize,0.33*ysize) )
149      f('set orig %f,%f' % (0.0*xsize,0.00*ysize) )
150      f('set xrange [%f:%f]' % (0,winlen/2) )
151      f.title('Autocorrelation')
152      f.xlabel('lag (df samples)')
153      f.plot(*d[2])
154      f('set nomultiplot')
155    if printframe == -1: a = sys.stdin.read()
156    elif 0 < printframe and printframe < nrframe:
157      break
Note: See TracBrowser for help on using the repository browser.