1 | #! /usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | |
---|
4 | """aubio command line tool |
---|
5 | |
---|
6 | This file was written by Paul Brossier <piem@aubio.org> and is released under |
---|
7 | the GNU/GPL v3. |
---|
8 | |
---|
9 | Note: this script is mostly about parsing command line arguments. For more |
---|
10 | readable code examples, check out the `python/demos` folder.""" |
---|
11 | |
---|
12 | import sys |
---|
13 | import argparse |
---|
14 | import aubio |
---|
15 | |
---|
16 | def aubio_parser(): |
---|
17 | epilog = 'use "%(prog)s <command> --help" for more info about each command' |
---|
18 | parser = argparse.ArgumentParser(epilog=epilog) |
---|
19 | parser.add_argument('-V', '--version', help="show version", |
---|
20 | action="store_true", dest="show_version") |
---|
21 | |
---|
22 | subparsers = parser.add_subparsers(title='commands', dest='command', |
---|
23 | parser_class= AubioArgumentParser, |
---|
24 | metavar="") |
---|
25 | |
---|
26 | parser_add_subcommand_help(subparsers) |
---|
27 | |
---|
28 | parser_add_subcommand_onset(subparsers) |
---|
29 | parser_add_subcommand_pitch(subparsers) |
---|
30 | parser_add_subcommand_beat(subparsers) |
---|
31 | parser_add_subcommand_tempo(subparsers) |
---|
32 | parser_add_subcommand_notes(subparsers) |
---|
33 | parser_add_subcommand_mfcc(subparsers) |
---|
34 | parser_add_subcommand_melbands(subparsers) |
---|
35 | parser_add_subcommand_quiet(subparsers) |
---|
36 | parser_add_subcommand_cut(subparsers) |
---|
37 | |
---|
38 | return parser |
---|
39 | |
---|
40 | def parser_add_subcommand_help(subparsers): |
---|
41 | # global help subcommand |
---|
42 | subparsers.add_parser('help', |
---|
43 | help='show help message', |
---|
44 | formatter_class = argparse.ArgumentDefaultsHelpFormatter) |
---|
45 | |
---|
46 | def parser_add_subcommand_onset(subparsers): |
---|
47 | # onset subcommand |
---|
48 | subparser = subparsers.add_parser('onset', |
---|
49 | help='estimate time of onsets (beginning of sound event)', |
---|
50 | formatter_class = argparse.ArgumentDefaultsHelpFormatter) |
---|
51 | subparser.add_input() |
---|
52 | subparser.add_buf_hop_size() |
---|
53 | helpstr = "onset novelty function" |
---|
54 | helpstr += " <default|energy|hfc|complex|phase|specdiff|kl|mkl|specflux>" |
---|
55 | subparser.add_method(helpstr=helpstr) |
---|
56 | subparser.add_threshold() |
---|
57 | subparser.add_silence() |
---|
58 | subparser.add_minioi() |
---|
59 | subparser.add_time_format() |
---|
60 | subparser.add_verbose_help() |
---|
61 | subparser.set_defaults(process=process_onset) |
---|
62 | |
---|
63 | def parser_add_subcommand_pitch(subparsers): |
---|
64 | # pitch subcommand |
---|
65 | subparser = subparsers.add_parser('pitch', |
---|
66 | help='estimate fundamental frequency (monophonic)') |
---|
67 | subparser.add_input() |
---|
68 | subparser.add_buf_hop_size(buf_size=2048) |
---|
69 | helpstr = "pitch detection method <default|yinfft|yin|mcomb|fcomb|schmitt>" |
---|
70 | subparser.add_method(helpstr=helpstr) |
---|
71 | subparser.add_threshold() |
---|
72 | subparser.add_pitch_unit() |
---|
73 | subparser.add_silence() |
---|
74 | subparser.add_time_format() |
---|
75 | subparser.add_verbose_help() |
---|
76 | subparser.set_defaults(process=process_pitch) |
---|
77 | |
---|
78 | def parser_add_subcommand_beat(subparsers): |
---|
79 | # beat subcommand |
---|
80 | subparser = subparsers.add_parser('beat', |
---|
81 | help='estimate location of beats') |
---|
82 | subparser.add_input() |
---|
83 | subparser.add_buf_hop_size(buf_size=1024, hop_size=512) |
---|
84 | subparser.add_time_format() |
---|
85 | subparser.add_verbose_help() |
---|
86 | subparser.set_defaults(process=process_beat) |
---|
87 | |
---|
88 | def parser_add_subcommand_tempo(subparsers): |
---|
89 | # tempo subcommand |
---|
90 | subparser = subparsers.add_parser('tempo', |
---|
91 | help='estimate overall tempo in bpm') |
---|
92 | subparser.add_input() |
---|
93 | subparser.add_buf_hop_size(buf_size=1024, hop_size=512) |
---|
94 | subparser.add_time_format() |
---|
95 | subparser.add_verbose_help() |
---|
96 | subparser.set_defaults(process=process_tempo) |
---|
97 | |
---|
98 | def parser_add_subcommand_notes(subparsers): |
---|
99 | # notes subcommand |
---|
100 | subparser = subparsers.add_parser('notes', |
---|
101 | help='estimate midi-like notes (monophonic)') |
---|
102 | subparser.add_input() |
---|
103 | subparser.add_buf_hop_size() |
---|
104 | subparser.add_time_format() |
---|
105 | subparser.add_verbose_help() |
---|
106 | subparser.set_defaults(process=process_notes) |
---|
107 | |
---|
108 | def parser_add_subcommand_mfcc(subparsers): |
---|
109 | # mfcc subcommand |
---|
110 | subparser = subparsers.add_parser('mfcc', |
---|
111 | help='extract Mel-Frequency Cepstrum Coefficients') |
---|
112 | subparser.add_input() |
---|
113 | subparser.add_buf_hop_size() |
---|
114 | subparser.add_time_format() |
---|
115 | subparser.add_verbose_help() |
---|
116 | subparser.set_defaults(process=process_mfcc) |
---|
117 | |
---|
118 | def parser_add_subcommand_melbands(subparsers): |
---|
119 | # melbands subcommand |
---|
120 | subparser = subparsers.add_parser('melbands', |
---|
121 | help='extract energies in Mel-frequency bands') |
---|
122 | subparser.add_input() |
---|
123 | subparser.add_buf_hop_size() |
---|
124 | subparser.add_time_format() |
---|
125 | subparser.add_verbose_help() |
---|
126 | subparser.set_defaults(process=process_melbands) |
---|
127 | |
---|
128 | def parser_add_subcommand_quiet(subparsers): |
---|
129 | # quiet subcommand |
---|
130 | subparser = subparsers.add_parser('quiet', |
---|
131 | help='extract timestamps of quiet and loud regions') |
---|
132 | subparser.add_input() |
---|
133 | subparser.add_hop_size() |
---|
134 | subparser.add_silence() |
---|
135 | subparser.add_time_format() |
---|
136 | subparser.add_verbose_help() |
---|
137 | subparser.set_defaults(process=process_quiet) |
---|
138 | |
---|
139 | def parser_add_subcommand_cut(subparsers): |
---|
140 | # cut subcommand |
---|
141 | subparser = subparsers.add_parser('cut', |
---|
142 | help='slice at timestamps') |
---|
143 | subparser.add_input() |
---|
144 | helpstr = "onset novelty function" |
---|
145 | helpstr += " <default|energy|hfc|complex|phase|specdiff|kl|mkl|specflux>" |
---|
146 | subparser.add_method(helpstr=helpstr) |
---|
147 | subparser.add_buf_hop_size() |
---|
148 | subparser.add_silence() |
---|
149 | subparser.add_threshold(default=0.3) |
---|
150 | subparser.add_minioi() |
---|
151 | subparser.add_slicer_options() |
---|
152 | subparser.add_time_format() |
---|
153 | subparser.add_verbose_help() |
---|
154 | subparser.set_defaults(process=process_cut) |
---|
155 | |
---|
156 | class AubioArgumentParser(argparse.ArgumentParser): |
---|
157 | |
---|
158 | def add_input(self): |
---|
159 | self.add_argument("source_uri", default=None, nargs='?', |
---|
160 | help="input sound file to analyse", metavar = "<source_uri>") |
---|
161 | self.add_argument("-i", "--input", dest = "source_uri2", |
---|
162 | help="input sound file to analyse", metavar = "<source_uri>") |
---|
163 | self.add_argument("-r", "--samplerate", |
---|
164 | metavar = "<freq>", type=int, |
---|
165 | action="store", dest="samplerate", default=0, |
---|
166 | help="samplerate at which the file should be represented") |
---|
167 | |
---|
168 | def add_verbose_help(self): |
---|
169 | self.add_argument("-v","--verbose", |
---|
170 | action="count", dest="verbose", default=1, |
---|
171 | help="make lots of noise [default]") |
---|
172 | self.add_argument("-q","--quiet", |
---|
173 | action="store_const", dest="verbose", const=0, |
---|
174 | help="be quiet") |
---|
175 | |
---|
176 | def add_buf_hop_size(self, buf_size=512, hop_size=256): |
---|
177 | self.add_buf_size(buf_size=buf_size) |
---|
178 | self.add_hop_size(hop_size=hop_size) |
---|
179 | |
---|
180 | def add_buf_size(self, buf_size=512): |
---|
181 | self.add_argument("-B","--bufsize", |
---|
182 | action="store", dest="buf_size", default=buf_size, |
---|
183 | metavar = "<size>", type=int, |
---|
184 | help="buffer size [default=%d]" % buf_size) |
---|
185 | |
---|
186 | def add_hop_size(self, hop_size=256): |
---|
187 | self.add_argument("-H","--hopsize", |
---|
188 | metavar = "<size>", type=int, |
---|
189 | action="store", dest="hop_size", default=hop_size, |
---|
190 | help="overlap size [default=%d]" % hop_size) |
---|
191 | |
---|
192 | def add_method(self, method='default', helpstr='method'): |
---|
193 | self.add_argument("-m","--method", |
---|
194 | metavar = "<method>", type=str, |
---|
195 | action="store", dest="method", default=method, |
---|
196 | help="%s [default=%s]" % (helpstr, method)) |
---|
197 | |
---|
198 | def add_threshold(self, default=None): |
---|
199 | self.add_argument("-t","--threshold", |
---|
200 | metavar = "<threshold>", type=float, |
---|
201 | action="store", dest="threshold", default=default, |
---|
202 | help="threshold [default=%s]" % default) |
---|
203 | |
---|
204 | def add_silence(self): |
---|
205 | self.add_argument("-s", "--silence", |
---|
206 | metavar = "<value>", type=float, |
---|
207 | action="store", dest="silence", default=-70, |
---|
208 | help="silence threshold") |
---|
209 | |
---|
210 | def add_minioi(self, default="12ms"): |
---|
211 | self.add_argument("-M", "--minioi", |
---|
212 | metavar = "<value>", type=str, |
---|
213 | action="store", dest="minioi", default=default, |
---|
214 | help="minimum Inter-Onset Interval [default=%s]" % default) |
---|
215 | |
---|
216 | def add_pitch_unit(self, default="Hz"): |
---|
217 | help_str = "frequency unit, should be one of Hz, midi, bin, cent" |
---|
218 | help_str += " [default=%s]" % default |
---|
219 | self.add_argument("-u", "--pitch-unit", |
---|
220 | metavar = "<value>", type=str, |
---|
221 | action="store", dest="pitch_unit", default=default, |
---|
222 | help=help_str) |
---|
223 | |
---|
224 | def add_time_format(self): |
---|
225 | helpstr = "select time values output format (samples, ms, seconds)" |
---|
226 | helpstr += " [default=seconds]" |
---|
227 | self.add_argument("-T", "--time-format", |
---|
228 | metavar='format', |
---|
229 | dest="time_format", |
---|
230 | default=None, |
---|
231 | help=helpstr) |
---|
232 | |
---|
233 | def add_slicer_options(self): |
---|
234 | self.add_argument("-o","--output", type = str, |
---|
235 | metavar = "<outputdir>", |
---|
236 | action="store", dest="output_directory", default=None, |
---|
237 | help="specify path where slices of the original file should be created") |
---|
238 | self.add_argument("--cut-until-nsamples", type = int, |
---|
239 | metavar = "<samples>", |
---|
240 | action = "store", dest = "cut_until_nsamples", default = None, |
---|
241 | help="how many extra samples should be added at the end of each slice") |
---|
242 | self.add_argument("--cut-every-nslices", type = int, |
---|
243 | metavar = "<samples>", |
---|
244 | action = "store", dest = "cut_every_nslices", default = None, |
---|
245 | help="how many slices should be groupped together at each cut") |
---|
246 | self.add_argument("--cut-until-nslices", type = int, |
---|
247 | metavar = "<slices>", |
---|
248 | action = "store", dest = "cut_until_nslices", default = None, |
---|
249 | help="how many extra slices should be added at the end of each slice") |
---|
250 | self.add_argument("--create-first", |
---|
251 | action = "store_true", dest = "create_first", default = False, |
---|
252 | help="always include first slice") |
---|
253 | |
---|
254 | # some utilities |
---|
255 | |
---|
256 | def samples2seconds(n_frames, samplerate): |
---|
257 | return "%f\t" % (n_frames / float(samplerate)) |
---|
258 | |
---|
259 | def samples2milliseconds(n_frames, samplerate): |
---|
260 | return "%f\t" % (1000. * n_frames / float(samplerate)) |
---|
261 | |
---|
262 | def samples2samples(n_frames, _samplerate): |
---|
263 | return "%d\t" % n_frames |
---|
264 | |
---|
265 | def timefunc(mode): |
---|
266 | if mode is None or mode == 'seconds' or mode == 's': |
---|
267 | return samples2seconds |
---|
268 | elif mode == 'ms' or mode == 'milliseconds': |
---|
269 | return samples2milliseconds |
---|
270 | elif mode == 'samples': |
---|
271 | return samples2samples |
---|
272 | else: |
---|
273 | raise ValueError("invalid time format '%s'" % mode) |
---|
274 | |
---|
275 | # definition of processing classes |
---|
276 | |
---|
277 | class default_process(object): |
---|
278 | def __init__(self, args): |
---|
279 | if 'time_format' in args: |
---|
280 | self.time2string = timefunc(args.time_format) |
---|
281 | if args.verbose > 2 and hasattr(self, 'options'): |
---|
282 | name = type(self).__name__.split('_')[1] |
---|
283 | optstr = ' '.join(['running', name, 'with options', repr(self.options), '\n']) |
---|
284 | sys.stderr.write(optstr) |
---|
285 | def flush(self, frames_read, samplerate): |
---|
286 | # optionally called at the end of process |
---|
287 | pass |
---|
288 | |
---|
289 | def parse_options(self, args, valid_opts): |
---|
290 | # get any valid options found in a dictionnary of arguments |
---|
291 | options = {k :v for k,v in vars(args).items() if k in valid_opts} |
---|
292 | self.options = options |
---|
293 | |
---|
294 | def remap_pvoc_options(self, options): |
---|
295 | # FIXME: we need to remap buf_size to win_s, hop_size to hop_s |
---|
296 | # adjust python/ext/py-phasevoc.c to understand buf_size/hop_size |
---|
297 | if 'buf_size' in options: |
---|
298 | options['win_s'] = options['buf_size'] |
---|
299 | del options['buf_size'] |
---|
300 | if 'hop_size' in options: |
---|
301 | options['hop_s'] = options['hop_size'] |
---|
302 | del options['hop_size'] |
---|
303 | self.options = options |
---|
304 | |
---|
305 | class process_onset(default_process): |
---|
306 | valid_opts = ['method', 'hop_size', 'buf_size', 'samplerate'] |
---|
307 | def __init__(self, args): |
---|
308 | self.parse_options(args, self.valid_opts) |
---|
309 | self.onset = aubio.onset(**self.options) |
---|
310 | if args.threshold is not None: |
---|
311 | self.onset.set_threshold(args.threshold) |
---|
312 | if args.minioi: |
---|
313 | if args.minioi.endswith('ms'): |
---|
314 | self.onset.set_minioi_ms(float(args.minioi[:-2])) |
---|
315 | elif args.minioi.endswith('s'): |
---|
316 | self.onset.set_minioi_s(float(args.minioi[:-1])) |
---|
317 | else: |
---|
318 | self.onset.set_minioi(int(args.minioi)) |
---|
319 | if args.silence: |
---|
320 | self.onset.set_silence(args.silence) |
---|
321 | super(process_onset, self).__init__(args) |
---|
322 | def __call__(self, block): |
---|
323 | return self.onset(block) |
---|
324 | def repr_res(self, res, _frames_read, samplerate): |
---|
325 | if res[0] != 0: |
---|
326 | outstr = self.time2string(self.onset.get_last(), samplerate) |
---|
327 | sys.stdout.write(outstr + '\n') |
---|
328 | |
---|
329 | class process_pitch(default_process): |
---|
330 | valid_opts = ['method', 'hop_size', 'buf_size', 'samplerate'] |
---|
331 | def __init__(self, args): |
---|
332 | self.parse_options(args, self.valid_opts) |
---|
333 | self.pitch = aubio.pitch(**self.options) |
---|
334 | if args.pitch_unit is not None: |
---|
335 | self.pitch.set_unit(args.pitch_unit) |
---|
336 | if args.threshold is not None: |
---|
337 | self.pitch.set_tolerance(args.threshold) |
---|
338 | if args.silence is not None: |
---|
339 | self.pitch.set_silence(args.silence) |
---|
340 | super(process_pitch, self).__init__(args) |
---|
341 | def __call__(self, block): |
---|
342 | return self.pitch(block) |
---|
343 | def repr_res(self, res, frames_read, samplerate): |
---|
344 | fmt_out = self.time2string(frames_read, samplerate) |
---|
345 | sys.stdout.write(fmt_out + "%.6f\n" % res[0]) |
---|
346 | |
---|
347 | class process_beat(default_process): |
---|
348 | valid_opts = ['method', 'hop_size', 'buf_size', 'samplerate'] |
---|
349 | def __init__(self, args): |
---|
350 | self.parse_options(args, self.valid_opts) |
---|
351 | self.tempo = aubio.tempo(**self.options) |
---|
352 | super(process_beat, self).__init__(args) |
---|
353 | def __call__(self, block): |
---|
354 | return self.tempo(block) |
---|
355 | def repr_res(self, res, _frames_read, samplerate): |
---|
356 | if res[0] != 0: |
---|
357 | outstr = self.time2string(self.tempo.get_last(), samplerate) |
---|
358 | sys.stdout.write(outstr + '\n') |
---|
359 | |
---|
360 | class process_tempo(process_beat): |
---|
361 | def __init__(self, args): |
---|
362 | super(process_tempo, self).__init__(args) |
---|
363 | self.beat_locations = [] |
---|
364 | def repr_res(self, res, _frames_read, samplerate): |
---|
365 | if res[0] != 0: |
---|
366 | self.beat_locations.append(self.tempo.get_last_s()) |
---|
367 | def flush(self, frames_read, samplerate): |
---|
368 | import numpy as np |
---|
369 | if len(self.beat_locations) < 2: |
---|
370 | outstr = "unknown bpm" |
---|
371 | else: |
---|
372 | bpms = 60./ np.diff(self.beat_locations) |
---|
373 | median_bpm = np.mean(bpms) |
---|
374 | if len(self.beat_locations) < 10: |
---|
375 | outstr = "%.2f bpm (uncertain)" % median_bpm |
---|
376 | else: |
---|
377 | outstr = "%.2f bpm" % median_bpm |
---|
378 | sys.stdout.write(outstr + '\n') |
---|
379 | |
---|
380 | class process_notes(default_process): |
---|
381 | valid_opts = ['method', 'hop_size', 'buf_size', 'samplerate'] |
---|
382 | def __init__(self, args): |
---|
383 | self.parse_options(args, self.valid_opts) |
---|
384 | self.notes = aubio.notes(**self.options) |
---|
385 | super(process_notes, self).__init__(args) |
---|
386 | def __call__(self, block): |
---|
387 | return self.notes(block) |
---|
388 | def repr_res(self, res, frames_read, samplerate): |
---|
389 | if res[2] != 0: # note off |
---|
390 | fmt_out = self.time2string(frames_read, samplerate) |
---|
391 | sys.stdout.write(fmt_out + '\n') |
---|
392 | if res[0] != 0: # note on |
---|
393 | lastmidi = res[0] |
---|
394 | fmt_out = "%f\t" % lastmidi |
---|
395 | fmt_out += self.time2string(frames_read, samplerate) |
---|
396 | sys.stdout.write(fmt_out) # + '\t') |
---|
397 | def flush(self, frames_read, samplerate): |
---|
398 | eof = self.time2string(frames_read, samplerate) |
---|
399 | sys.stdout.write(eof + '\n') |
---|
400 | |
---|
401 | class process_mfcc(default_process): |
---|
402 | def __init__(self, args): |
---|
403 | valid_opts1 = ['hop_size', 'buf_size'] |
---|
404 | self.parse_options(args, valid_opts1) |
---|
405 | self.remap_pvoc_options(self.options) |
---|
406 | self.pv = aubio.pvoc(**self.options) |
---|
407 | |
---|
408 | valid_opts2 = ['buf_size', 'n_filters', 'n_coeffs', 'samplerate'] |
---|
409 | self.parse_options(args, valid_opts2) |
---|
410 | self.mfcc = aubio.mfcc(**self.options) |
---|
411 | |
---|
412 | # remember all options |
---|
413 | self.parse_options(args, list(set(valid_opts1 + valid_opts2))) |
---|
414 | |
---|
415 | super(process_mfcc, self).__init__(args) |
---|
416 | |
---|
417 | def __call__(self, block): |
---|
418 | fftgrain = self.pv(block) |
---|
419 | return self.mfcc(fftgrain) |
---|
420 | def repr_res(self, res, frames_read, samplerate): |
---|
421 | fmt_out = self.time2string(frames_read, samplerate) |
---|
422 | fmt_out += ' '.join(["% 9.7f" % f for f in res.tolist()]) |
---|
423 | sys.stdout.write(fmt_out + '\n') |
---|
424 | |
---|
425 | class process_melbands(default_process): |
---|
426 | def __init__(self, args): |
---|
427 | self.args = args |
---|
428 | valid_opts = ['hop_size', 'buf_size'] |
---|
429 | self.parse_options(args, valid_opts) |
---|
430 | self.remap_pvoc_options(self.options) |
---|
431 | self.pv = aubio.pvoc(**self.options) |
---|
432 | |
---|
433 | valid_opts = ['buf_size', 'n_filters'] |
---|
434 | self.parse_options(args, valid_opts) |
---|
435 | self.remap_pvoc_options(self.options) |
---|
436 | self.filterbank = aubio.filterbank(**self.options) |
---|
437 | self.filterbank.set_mel_coeffs_slaney(args.samplerate) |
---|
438 | |
---|
439 | super(process_melbands, self).__init__(args) |
---|
440 | def __call__(self, block): |
---|
441 | fftgrain = self.pv(block) |
---|
442 | return self.filterbank(fftgrain) |
---|
443 | def repr_res(self, res, frames_read, samplerate): |
---|
444 | fmt_out = self.time2string(frames_read, samplerate) |
---|
445 | fmt_out += ' '.join(["% 9.7f" % f for f in res.tolist()]) |
---|
446 | sys.stdout.write(fmt_out + '\n') |
---|
447 | |
---|
448 | class process_quiet(default_process): |
---|
449 | def __init__(self, args): |
---|
450 | self.args = args |
---|
451 | valid_opts = ['hop_size', 'silence'] |
---|
452 | self.parse_options(args, valid_opts) |
---|
453 | self.wassilence = 1 |
---|
454 | |
---|
455 | if args.silence is not None: |
---|
456 | self.silence = args.silence |
---|
457 | super(process_quiet, self).__init__(args) |
---|
458 | |
---|
459 | def __call__(self, block): |
---|
460 | if aubio.silence_detection(block, self.silence) == 1: |
---|
461 | if self.wassilence != 1: |
---|
462 | self.wassilence = 1 |
---|
463 | return 2 # newly found silence |
---|
464 | return 1 # silence again |
---|
465 | else: |
---|
466 | if self.wassilence != 0: |
---|
467 | self.wassilence = 0 |
---|
468 | return -1 # newly found noise |
---|
469 | return 0 # noise again |
---|
470 | |
---|
471 | def repr_res(self, res, frames_read, samplerate): |
---|
472 | fmt_out = None |
---|
473 | if res == -1: |
---|
474 | fmt_out = "NOISY: " |
---|
475 | if res == 2: |
---|
476 | fmt_out = "QUIET: " |
---|
477 | if fmt_out is not None: |
---|
478 | fmt_out += self.time2string(frames_read, samplerate) |
---|
479 | sys.stdout.write(fmt_out + '\n') |
---|
480 | |
---|
481 | class process_cut(process_onset): |
---|
482 | def __init__(self, args): |
---|
483 | super(process_cut, self).__init__(args) |
---|
484 | self.slices = [] |
---|
485 | self.options = args |
---|
486 | |
---|
487 | def __call__(self, block): |
---|
488 | ret = super(process_cut, self).__call__(block) |
---|
489 | if ret: self.slices.append(self.onset.get_last()) |
---|
490 | return ret |
---|
491 | |
---|
492 | def flush(self, frames_read, samplerate): |
---|
493 | from aubio.cut import _cut_slice |
---|
494 | _cut_slice(self.options, self.slices) |
---|
495 | duration = float (frames_read) / float(samplerate) |
---|
496 | base_info = '%(source_file)s' % {'source_file': self.options.source_uri} |
---|
497 | base_info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % \ |
---|
498 | {'duration': duration, 'samplerate': samplerate} |
---|
499 | info = "created %d slices from " % len(self.slices) |
---|
500 | info += base_info |
---|
501 | sys.stderr.write(info) |
---|
502 | |
---|
503 | def main(): |
---|
504 | parser = aubio_parser() |
---|
505 | if sys.version_info[0] != 3: |
---|
506 | # on py2, create a dummy ArgumentParser to workaround the |
---|
507 | # optional subcommand issue. See https://bugs.python.org/issue9253 |
---|
508 | # This ensures that: |
---|
509 | # - version string is shown when only '-V' is passed |
---|
510 | # - help is printed if '-V' is passed with any other argument |
---|
511 | # - any other argument get forwarded to the real parser |
---|
512 | parser_root = argparse.ArgumentParser(add_help=False) |
---|
513 | parser_root.add_argument('-V', '--version', help="show version", |
---|
514 | action="store_true", dest="show_version") |
---|
515 | args, extras = parser_root.parse_known_args() |
---|
516 | if args.show_version == False: # no -V, forward to parser |
---|
517 | args = parser.parse_args(extras, namespace=args) |
---|
518 | elif len(extras) != 0: # -V with other arguments, print help |
---|
519 | parser.print_help() |
---|
520 | sys.exit(1) |
---|
521 | else: # in py3, we can simply use parser directly |
---|
522 | args = parser.parse_args() |
---|
523 | if 'show_version' in args and args.show_version: |
---|
524 | sys.stdout.write('aubio version ' + aubio.version + '\n') |
---|
525 | sys.exit(0) |
---|
526 | elif 'verbose' in args and args.verbose > 3: |
---|
527 | sys.stderr.write('aubio version ' + aubio.version + '\n') |
---|
528 | if 'command' not in args or args.command is None or args.command in ['help']: |
---|
529 | # no command given, print help and return 1 |
---|
530 | parser.print_help() |
---|
531 | if args.command and args.command in ['help']: |
---|
532 | sys.exit(0) |
---|
533 | else: |
---|
534 | sys.exit(1) |
---|
535 | elif not args.source_uri and not args.source_uri2: |
---|
536 | sys.stderr.write("Error: a source is required\n") |
---|
537 | parser.print_help() |
---|
538 | sys.exit(1) |
---|
539 | elif args.source_uri2 is not None: |
---|
540 | args.source_uri = args.source_uri2 |
---|
541 | try: |
---|
542 | # open source_uri |
---|
543 | with aubio.source(args.source_uri, hop_size=args.hop_size, |
---|
544 | samplerate=args.samplerate) as a_source: |
---|
545 | # always update args.samplerate to native samplerate, in case |
---|
546 | # source was opened with args.samplerate=0 |
---|
547 | args.samplerate = a_source.samplerate |
---|
548 | # create the processor for this subcommand |
---|
549 | processor = args.process(args) |
---|
550 | frames_read = 0 |
---|
551 | while True: |
---|
552 | # read new block from source |
---|
553 | block, read = a_source() |
---|
554 | # execute processor on this block |
---|
555 | res = processor(block) |
---|
556 | # print results for this block |
---|
557 | if args.verbose > 0: |
---|
558 | processor.repr_res(res, frames_read, a_source.samplerate) |
---|
559 | # increment total number of frames read |
---|
560 | frames_read += read |
---|
561 | # exit loop at end of file |
---|
562 | if read < a_source.hop_size: break |
---|
563 | # flush the processor if needed |
---|
564 | processor.flush(frames_read, a_source.samplerate) |
---|
565 | if args.verbose > 1: |
---|
566 | fmt_string = "read {:.2f}s" |
---|
567 | fmt_string += " ({:d} samples in {:d} blocks of {:d})" |
---|
568 | fmt_string += " from {:s} at {:d}Hz\n" |
---|
569 | sys.stderr.write(fmt_string.format( |
---|
570 | frames_read/float(a_source.samplerate), |
---|
571 | frames_read, |
---|
572 | frames_read // a_source.hop_size + 1, |
---|
573 | a_source.hop_size, |
---|
574 | a_source.uri, |
---|
575 | a_source.samplerate)) |
---|
576 | except KeyboardInterrupt: |
---|
577 | sys.exit(1) |
---|