1 | #! /usr/bin/env 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", action = "store", dest = "source_file", |
---|
16 | help="input sound file to analyse", metavar = "<source_file>") |
---|
17 | parser.add_option("-m","--method", |
---|
18 | action="store", dest="onset_method", default='default', |
---|
19 | metavar = "<onset_method>", |
---|
20 | help="onset detection method [default=default] \ |
---|
21 | complexdomain|hfc|phase|specdiff|energy|kl|mkl") |
---|
22 | # cutting methods |
---|
23 | """ |
---|
24 | parser.add_option("-b","--beat", |
---|
25 | action="store_true", dest="beat", default=False, |
---|
26 | help="use beat locations") |
---|
27 | parser.add_option("-S","--silencecut", |
---|
28 | action="store_true", dest="silencecut", default=False, |
---|
29 | help="use silence locations") |
---|
30 | parser.add_option("-s","--silence", |
---|
31 | metavar = "<value>", |
---|
32 | action="store", dest="silence", default=-70, |
---|
33 | help="silence threshold [default=-70]") |
---|
34 | """ |
---|
35 | # algorithm parameters |
---|
36 | parser.add_option("--samplerate", |
---|
37 | metavar = "<freq>", type='int', |
---|
38 | action="store", dest="samplerate", default=0, |
---|
39 | help="samplerate at which the file should be represented") |
---|
40 | parser.add_option("-B","--bufsize", |
---|
41 | action="store", dest="bufsize", default=512, |
---|
42 | metavar = "<size>", |
---|
43 | help="buffer size [default=512]") |
---|
44 | parser.add_option("-H","--hopsize", |
---|
45 | metavar = "<size>", |
---|
46 | action="store", dest="hopsize", default=256, |
---|
47 | help="overlap size [default=256]") |
---|
48 | parser.add_option("-t","--threshold", |
---|
49 | metavar = "<value>", type="float", |
---|
50 | action="store", dest="threshold", default=0.3, |
---|
51 | help="onset peak picking threshold [default=0.3]") |
---|
52 | """ |
---|
53 | parser.add_option("-c","--cut", |
---|
54 | action="store_true", dest="cut", default=False, |
---|
55 | help="cut input sound file at detected labels \ |
---|
56 | best used with option -L") |
---|
57 | parser.add_option("-D","--delay", |
---|
58 | action = "store", dest = "delay", type = "float", |
---|
59 | metavar = "<seconds>", default=0, |
---|
60 | help="number of seconds to take back [default=system]\ |
---|
61 | default system delay is 3*hopsize/samplerate") |
---|
62 | parser.add_option("-C","--dcthreshold", |
---|
63 | metavar = "<value>", |
---|
64 | action="store", dest="dcthreshold", default=1., |
---|
65 | help="onset peak picking DC component [default=1.]") |
---|
66 | parser.add_option("-M","--mintol", |
---|
67 | metavar = "<value>", |
---|
68 | action="store", dest="mintol", default=0.048, |
---|
69 | help="minimum inter onset interval [default=0.048]") |
---|
70 | parser.add_option("-L","--localmin", |
---|
71 | action="store_true", dest="localmin", default=False, |
---|
72 | help="use local minima after peak detection") |
---|
73 | parser.add_option("-d","--derivate", |
---|
74 | action="store_true", dest="derivate", default=False, |
---|
75 | help="derivate onset detection function") |
---|
76 | parser.add_option("-z","--zerocross", |
---|
77 | metavar = "<value>", |
---|
78 | action="store", dest="zerothres", default=0.008, |
---|
79 | help="zero-crossing threshold for slicing [default=0.00008]") |
---|
80 | """ |
---|
81 | # plotting functions |
---|
82 | """ |
---|
83 | parser.add_option("-p","--plot", |
---|
84 | action="store_true", dest="plot", default=False, |
---|
85 | help="draw plot") |
---|
86 | parser.add_option("-x","--xsize", |
---|
87 | metavar = "<size>", |
---|
88 | action="store", dest="xsize", default=1., |
---|
89 | type='float', help="define xsize for plot") |
---|
90 | parser.add_option("-y","--ysize", |
---|
91 | metavar = "<size>", |
---|
92 | action="store", dest="ysize", default=1., |
---|
93 | type='float', help="define ysize for plot") |
---|
94 | parser.add_option("-f","--function", |
---|
95 | action="store_true", dest="func", default=False, |
---|
96 | help="print detection function") |
---|
97 | parser.add_option("-n","--no-onsets", |
---|
98 | action="store_true", dest="nplot", default=False, |
---|
99 | help="do not plot detected onsets") |
---|
100 | parser.add_option("-O","--outplot", |
---|
101 | metavar = "<output_image>", |
---|
102 | action="store", dest="outplot", default=None, |
---|
103 | help="save plot to output.{ps,png}") |
---|
104 | parser.add_option("-F","--spectrogram", |
---|
105 | action="store_true", dest="spectro", default=False, |
---|
106 | help="add spectrogram to the plot") |
---|
107 | """ |
---|
108 | parser.add_option("-v","--verbose", |
---|
109 | action="store_true", dest="verbose", default=True, |
---|
110 | help="make lots of noise [default]") |
---|
111 | parser.add_option("-q","--quiet", |
---|
112 | action="store_false", dest="verbose", default=True, |
---|
113 | help="be quiet") |
---|
114 | (options, args) = parser.parse_args() |
---|
115 | if not options.source_file: |
---|
116 | print "no file name given\n", usage |
---|
117 | sys.exit(1) |
---|
118 | return options, args |
---|
119 | |
---|
120 | if __name__ == '__main__': |
---|
121 | options, args = parse_args() |
---|
122 | |
---|
123 | hopsize = options.hopsize |
---|
124 | bufsize = options.bufsize |
---|
125 | samplerate = options.samplerate |
---|
126 | source_file = options.source_file |
---|
127 | |
---|
128 | from aubio import onset, source |
---|
129 | |
---|
130 | s = source(source_file, samplerate, hopsize) |
---|
131 | if samplerate == 0: samplerate = s.get_samplerate() |
---|
132 | |
---|
133 | o = onset(options.onset_method, bufsize, hopsize) |
---|
134 | o.set_threshold(options.threshold) |
---|
135 | |
---|
136 | |
---|
137 | timestamps = [] |
---|
138 | block_read = 0 |
---|
139 | while True: |
---|
140 | samples, read = s() |
---|
141 | is_onset = o(samples) |
---|
142 | if is_onset: |
---|
143 | this_onset = (block_read * hopsize - is_onset[0]) |
---|
144 | if options.verbose: |
---|
145 | print "%.4f" % ( this_onset / samplerate ) |
---|
146 | timestamps.append (this_onset) |
---|
147 | block_read += 1 |
---|
148 | if read < hopsize: break |
---|
149 | |
---|
150 | # print some info |
---|
151 | duration = float ( block_read * hopsize + read ) / samplerate |
---|
152 | nstamps = len(timestamps) |
---|
153 | info = 'found %(nstamps)d timestamps in %(source_file)s' % locals() |
---|
154 | info += ' (read %(duration).2fs at %(samplerate)dHz)\n' % locals() |
---|
155 | sys.stderr.write(info) |
---|