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 |
---|
8 | from aubio.task import * |
---|
9 | |
---|
10 | usage = "usage: %s [options] -i soundfile" % sys.argv[0] |
---|
11 | |
---|
12 | def parse_args(): |
---|
13 | from optparse import OptionParser |
---|
14 | parser = OptionParser(usage=usage) |
---|
15 | parser.add_option("-i","--input", |
---|
16 | action="store", dest="filename", |
---|
17 | help="input sound file") |
---|
18 | parser.add_option("-m","--mode", |
---|
19 | action="store", dest="mode", default='dual', |
---|
20 | help="onset detection mode [default=dual] \ |
---|
21 | complexdomain|hfc|phase|specdiff|energy|kl|mkl|dual") |
---|
22 | parser.add_option("-B","--bufsize", |
---|
23 | action="store", dest="bufsize", default=512, |
---|
24 | help="buffer size [default=512]") |
---|
25 | parser.add_option("-H","--hopsize", |
---|
26 | action="store", dest="hopsize", default=256, |
---|
27 | help="overlap size [default=256]") |
---|
28 | parser.add_option("-t","--threshold", |
---|
29 | action="store", dest="threshold", default=0.3, |
---|
30 | help="onset peak picking threshold [default=0.3]") |
---|
31 | parser.add_option("-C","--dcthreshold", |
---|
32 | action="store", dest="dcthreshold", default=1., |
---|
33 | help="onset peak picking DC component [default=1.]") |
---|
34 | parser.add_option("-s","--silence", |
---|
35 | action="store", dest="silence", default=-70, |
---|
36 | help="silence threshold [default=-70]") |
---|
37 | parser.add_option("-M","--mintol", |
---|
38 | action="store", dest="mintol", default=0.048, |
---|
39 | help="minimum inter onset interval [default=0.048]") |
---|
40 | parser.add_option("-D","--delay", |
---|
41 | action="store", dest="delay", |
---|
42 | help="number of seconds to take back [default=system]\ |
---|
43 | default system delay is 3*hopsize/samplerate") |
---|
44 | parser.add_option("-L","--localmin", |
---|
45 | action="store_true", dest="localmin", default=False, |
---|
46 | help="use local minima after peak detection") |
---|
47 | parser.add_option("-c","--cut", |
---|
48 | action="store_true", dest="cut", default=False, |
---|
49 | help="cut input sound file at detected labels \ |
---|
50 | best used with option -L") |
---|
51 | parser.add_option("-d","--derivate", |
---|
52 | action="store_true", dest="derivate", default=False, |
---|
53 | help="derivate onset detection function") |
---|
54 | parser.add_option("-S","--silencecut", |
---|
55 | action="store_true", dest="silencecut", default=False, |
---|
56 | help="outputs silence locations") |
---|
57 | parser.add_option("-z","--zerocross", |
---|
58 | action="store", dest="zerothres", default=0.008, |
---|
59 | help="zero-crossing threshold for slicing [default=0.00008]") |
---|
60 | # plotting functions |
---|
61 | parser.add_option("-p","--plot", |
---|
62 | action="store_true", dest="plot", default=False, |
---|
63 | help="draw plot") |
---|
64 | parser.add_option("-x","--xsize", |
---|
65 | action="store", dest="xsize", default=1., |
---|
66 | type='float', help="define xsize for plot") |
---|
67 | parser.add_option("-y","--ysize", |
---|
68 | action="store", dest="ysize", default=1., |
---|
69 | type='float', help="define ysize for plot") |
---|
70 | parser.add_option("-f","--function", |
---|
71 | action="store_true", dest="func", default=False, |
---|
72 | help="print detection function") |
---|
73 | parser.add_option("-n","--no-onsets", |
---|
74 | action="store_true", dest="nplot", default=False, |
---|
75 | help="do not plot detected onsets") |
---|
76 | parser.add_option("-O","--outplot", |
---|
77 | action="store", dest="outplot", default=None, |
---|
78 | help="save plot to output.{ps,png}") |
---|
79 | parser.add_option("-F","--spectrogram", |
---|
80 | action="store_true", dest="spectro", default=False, |
---|
81 | help="add spectrogram to the plot") |
---|
82 | parser.add_option("-v","--verbose", |
---|
83 | action="store_true", dest="verbose", default=False, |
---|
84 | help="make lots of noise [default]") |
---|
85 | parser.add_option("-q","--quiet", |
---|
86 | action="store_false", dest="verbose", default=False, |
---|
87 | help="be quiet") |
---|
88 | # to be implemented |
---|
89 | parser.add_option("-b","--beat", |
---|
90 | action="store_true", dest="beat", default=False, |
---|
91 | help="output beat locations") |
---|
92 | (options, args) = parser.parse_args() |
---|
93 | if not options.filename: |
---|
94 | print "no file name given\n", usage |
---|
95 | sys.exit(1) |
---|
96 | return options, args |
---|
97 | |
---|
98 | options, args = parse_args() |
---|
99 | |
---|
100 | filename = options.filename |
---|
101 | params = taskparams() |
---|
102 | params.hopsize = int(options.hopsize) |
---|
103 | params.bufsize = int(options.bufsize) |
---|
104 | params.threshold = float(options.threshold) |
---|
105 | params.dcthreshold = float(options.dcthreshold) |
---|
106 | params.zerothres = float(options.zerothres) |
---|
107 | params.silence = float(options.silence) |
---|
108 | params.mintol = float(options.mintol) |
---|
109 | params.verbose = options.verbose |
---|
110 | # default take back system delay |
---|
111 | if options.delay: params.delay = int(float(options.delay)/params.step) |
---|
112 | |
---|
113 | dotask = taskonset |
---|
114 | if options.beat: |
---|
115 | dotask = taskbeat |
---|
116 | elif options.silencecut: |
---|
117 | dotask = tasksilence |
---|
118 | elif options.plot or options.func: |
---|
119 | params.storefunc=True |
---|
120 | else: |
---|
121 | params.storefunc=False |
---|
122 | |
---|
123 | lonsets, lofunc = [], [] |
---|
124 | wplot,oplots = [],[] |
---|
125 | modes = options.mode.split(',') |
---|
126 | for i in range(len(modes)): |
---|
127 | params.onsetmode = modes[i] |
---|
128 | filetask = dotask(filename,params=params) |
---|
129 | onsets = filetask.compute_all() |
---|
130 | |
---|
131 | #lonsets.append(onsets) |
---|
132 | if not options.silencecut: |
---|
133 | ofunc = filetask.ofunc |
---|
134 | lofunc.append(ofunc) |
---|
135 | |
---|
136 | if options.plot: filetask.plot(onsets, ofunc, wplot, oplots, nplot=options.nplot) |
---|
137 | |
---|
138 | if options.func: |
---|
139 | for i in ofunc: |
---|
140 | print i |
---|
141 | |
---|
142 | if options.outplot: |
---|
143 | extension = options.outplot.split('.')[-1] |
---|
144 | outplot = '.'.join(options.outplot.split('.')[:-1]) |
---|
145 | else: |
---|
146 | extension,outplot = None,None |
---|
147 | if options.plot: filetask.plotplot(wplot, oplots, outplot=outplot, extension=extension, |
---|
148 | xsize=options.xsize,ysize=options.ysize,spectro=options.spectro) |
---|
149 | |
---|
150 | if options.cut: |
---|
151 | a = taskcut(filename,onsets,params=params) |
---|
152 | a.compute_all() |
---|