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