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 | |
---|
9 | usage = "usage: %s [options] -i soundfile" % sys.argv[0] |
---|
10 | usage += "\n help: %s -h" % 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("-O","--onset-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 | parser.add_option("-b","--beat", |
---|
24 | action="store_true", dest="beat", default=False, |
---|
25 | help="use beat locations") |
---|
26 | """ |
---|
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("-r", "--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>", type='int', |
---|
43 | help="buffer size [default=512]") |
---|
44 | parser.add_option("-H","--hopsize", |
---|
45 | metavar = "<size>", type='int', |
---|
46 | action="store", dest="hopsize", default=256, |
---|
47 | help="overlap size [default=256]") |
---|
48 | parser.add_option("-t","--onset-threshold", |
---|
49 | metavar = "<value>", type="float", |
---|
50 | action="store", dest="threshold", default=0.3, |
---|
51 | help="onset peak picking threshold [default=0.3]") |
---|
52 | parser.add_option("-c","--cut", |
---|
53 | action="store_true", dest="cut", default=False, |
---|
54 | help="cut input sound file at detected labels \ |
---|
55 | best used with option -L") |
---|
56 | |
---|
57 | # minioi |
---|
58 | parser.add_option("-M","--minioi", |
---|
59 | metavar = "<value>", type='string', |
---|
60 | action="store", dest="minioi", default="12ms", |
---|
61 | help="minimum inter onset interval [default=12ms]") |
---|
62 | |
---|
63 | """ |
---|
64 | parser.add_option("-D","--delay", |
---|
65 | action = "store", dest = "delay", type = "float", |
---|
66 | metavar = "<seconds>", default=0, |
---|
67 | help="number of seconds to take back [default=system]\ |
---|
68 | default system delay is 3*hopsize/samplerate") |
---|
69 | parser.add_option("-C","--dcthreshold", |
---|
70 | metavar = "<value>", |
---|
71 | action="store", dest="dcthreshold", default=1., |
---|
72 | help="onset peak picking DC component [default=1.]") |
---|
73 | parser.add_option("-L","--localmin", |
---|
74 | action="store_true", dest="localmin", default=False, |
---|
75 | help="use local minima after peak detection") |
---|
76 | parser.add_option("-d","--derivate", |
---|
77 | action="store_true", dest="derivate", default=False, |
---|
78 | help="derivate onset detection function") |
---|
79 | parser.add_option("-z","--zerocross", |
---|
80 | metavar = "<value>", |
---|
81 | action="store", dest="zerothres", default=0.008, |
---|
82 | help="zero-crossing threshold for slicing [default=0.00008]") |
---|
83 | """ |
---|
84 | # plotting functions |
---|
85 | """ |
---|
86 | parser.add_option("-p","--plot", |
---|
87 | action="store_true", dest="plot", default=False, |
---|
88 | help="draw plot") |
---|
89 | parser.add_option("-x","--xsize", |
---|
90 | metavar = "<size>", |
---|
91 | action="store", dest="xsize", default=1., |
---|
92 | type='float', help="define xsize for plot") |
---|
93 | parser.add_option("-y","--ysize", |
---|
94 | metavar = "<size>", |
---|
95 | action="store", dest="ysize", default=1., |
---|
96 | type='float', help="define ysize for plot") |
---|
97 | parser.add_option("-f","--function", |
---|
98 | action="store_true", dest="func", default=False, |
---|
99 | help="print detection function") |
---|
100 | parser.add_option("-n","--no-onsets", |
---|
101 | action="store_true", dest="nplot", default=False, |
---|
102 | help="do not plot detected onsets") |
---|
103 | parser.add_option("-O","--outplot", |
---|
104 | metavar = "<output_image>", |
---|
105 | action="store", dest="outplot", default=None, |
---|
106 | help="save plot to output.{ps,png}") |
---|
107 | parser.add_option("-F","--spectrogram", |
---|
108 | action="store_true", dest="spectro", default=False, |
---|
109 | help="add spectrogram to the plot") |
---|
110 | """ |
---|
111 | parser.add_option("-o","--output", type = str, |
---|
112 | metavar = "<outputdir>", |
---|
113 | action="store", dest="output_directory", default=None, |
---|
114 | help="specify path where slices of the original file should be created") |
---|
115 | parser.add_option("--cut-until-nsamples", type = int, |
---|
116 | metavar = "<samples>", |
---|
117 | action = "store", dest = "cut_until_nsamples", default = None, |
---|
118 | help="how many extra samples should be added at the end of each slice") |
---|
119 | parser.add_option("--cut-every-nslices", type = int, |
---|
120 | metavar = "<samples>", |
---|
121 | action = "store", dest = "cut_every_nslices", default = None, |
---|
122 | help="how many slices should be groupped together at each cut") |
---|
123 | parser.add_option("--cut-until-nslices", type = int, |
---|
124 | metavar = "<slices>", |
---|
125 | action = "store", dest = "cut_until_nslices", default = None, |
---|
126 | help="how many extra slices should be added at the end of each slice") |
---|
127 | |
---|
128 | parser.add_option("-v","--verbose", |
---|
129 | action="store_true", dest="verbose", default=True, |
---|
130 | help="make lots of noise [default]") |
---|
131 | parser.add_option("-q","--quiet", |
---|
132 | action="store_false", dest="verbose", default=True, |
---|
133 | help="be quiet") |
---|
134 | (options, args) = parser.parse_args() |
---|
135 | if not options.source_file: |
---|
136 | import os.path |
---|
137 | if len(args) == 1: |
---|
138 | options.source_file = args[0] |
---|
139 | else: |
---|
140 | print ("no file name given\n" + usage) |
---|
141 | sys.exit(1) |
---|
142 | return options, args |
---|
143 | |
---|
144 | if __name__ == '__main__': |
---|
145 | options, args = parse_args() |
---|
146 | |
---|
147 | hopsize = options.hopsize |
---|
148 | bufsize = options.bufsize |
---|
149 | samplerate = options.samplerate |
---|
150 | source_file = options.source_file |
---|
151 | |
---|
152 | from aubio import onset, tempo, source, sink |
---|
153 | |
---|
154 | s = source(source_file, samplerate, hopsize) |
---|
155 | if samplerate == 0: samplerate = s.get_samplerate() |
---|
156 | |
---|
157 | if options.beat: |
---|
158 | o = tempo(options.onset_method, bufsize, hopsize) |
---|
159 | else: |
---|
160 | o = onset(options.onset_method, bufsize, hopsize) |
---|
161 | if options.minioi: |
---|
162 | if options.minioi.endswith('ms'): |
---|
163 | o.set_minioi_ms(int(options.minioi[:-2])) |
---|
164 | elif options.minioi.endswith('s'): |
---|
165 | o.set_minioi_s(int(options.minioi[:-1])) |
---|
166 | else: |
---|
167 | o.set_minioi(int(options.minioi)) |
---|
168 | o.set_threshold(options.threshold) |
---|
169 | |
---|
170 | timestamps = [] |
---|
171 | total_frames = 0 |
---|
172 | # analyze pass |
---|
173 | while True: |
---|
174 | samples, read = s() |
---|
175 | if o(samples): |
---|
176 | timestamps.append (o.get_last()) |
---|
177 | if options.verbose: print ("%.4f" % o.get_last_s()) |
---|
178 | total_frames += read |
---|
179 | if read < hopsize: break |
---|
180 | del s |
---|
181 | # print some info |
---|
182 | nstamps = len(timestamps) |
---|
183 | duration = float (total_frames) / float(samplerate) |
---|
184 | info = 'found %(nstamps)d timestamps in %(source_file)s' % locals() |
---|
185 | info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % locals() |
---|
186 | sys.stderr.write(info) |
---|
187 | |
---|
188 | # cutting pass |
---|
189 | if options.cut and nstamps > 0: |
---|
190 | # generate output files |
---|
191 | from aubio.slicing import slice_source_at_stamps |
---|
192 | timestamps_end = None |
---|
193 | if options.cut_every_nslices: |
---|
194 | timestamps = timestamps[::options.cut_every_nslices] |
---|
195 | nstamps = len(timestamps) |
---|
196 | if options.cut_until_nslices and options.cut_until_nsamples: |
---|
197 | print ("warning: using cut_until_nslices, but cut_until_nsamples is set") |
---|
198 | if options.cut_until_nsamples: |
---|
199 | timestamps_end = [t + options.cut_until_nsamples for t in timestamps[1:]] |
---|
200 | timestamps_end += [ 1e120 ] |
---|
201 | if options.cut_until_nslices: |
---|
202 | timestamps_end = [t for t in timestamps[1 + options.cut_until_nslices:]] |
---|
203 | timestamps_end += [ 1e120 ] * (options.cut_until_nslices + 1) |
---|
204 | slice_source_at_stamps(source_file, timestamps, timestamps_end = timestamps_end, |
---|
205 | output_dir = options.output_directory, |
---|
206 | samplerate = samplerate) |
---|
207 | |
---|
208 | # print some info |
---|
209 | duration = float (total_frames) / float(samplerate) |
---|
210 | info = 'created %(nstamps)d slices from %(source_file)s' % locals() |
---|
211 | info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % locals() |
---|
212 | sys.stderr.write(info) |
---|