History

[py] improve style of cmd.py

Paul Brossier committed 8 years ago

Commit 78ebc271 parent a5c3f2a1 file · +38 −30Browse files

Showing the changes to python (1 of 1 file). Show all

Unified · Split

python/lib/aubio/cmd.py+38−30
@@ -168,10 +168,10 @@ class AubioArgumentParser(argparse.ArgumentParser): · 167 unmodified lines
168 help="samplerate at which the file should be represented")168 help="samplerate at which the file should be represented")
169 169
170 def add_verbose_help(self):170 def add_verbose_help(self):
171 self.add_argument("-v","--verbose",171 self.add_argument("-v", "--verbose",
172 action="count", dest="verbose", default=1,172 action="count", dest="verbose", default=1,
173 help="make lots of noise [default]")173 help="make lots of noise [default]")
174 self.add_argument("-q","--quiet",174 self.add_argument("-q", "--quiet",
175 action="store_const", dest="verbose", const=0,175 action="store_const", dest="verbose", const=0,
176 help="be quiet")176 help="be quiet")
177 177
@@ -180,25 +180,25 @@ class AubioArgumentParser(argparse.ArgumentParser): · 2 unmodified lines
180 self.add_hop_size(hop_size=hop_size)180 self.add_hop_size(hop_size=hop_size)
181 181
182 def add_buf_size(self, buf_size=512):182 def add_buf_size(self, buf_size=512):
183 self.add_argument("-B","--bufsize",183 self.add_argument("-B", "--bufsize",
184 action="store", dest="buf_size", default=buf_size,184 action="store", dest="buf_size", default=buf_size,
185 metavar = "<size>", type=int,185 metavar = "<size>", type=int,
186 help="buffer size [default=%d]" % buf_size)186 help="buffer size [default=%d]" % buf_size)
187 187
188 def add_hop_size(self, hop_size=256):188 def add_hop_size(self, hop_size=256):
189 self.add_argument("-H","--hopsize",189 self.add_argument("-H", "--hopsize",
190 metavar = "<size>", type=int,190 metavar = "<size>", type=int,
191 action="store", dest="hop_size", default=hop_size,191 action="store", dest="hop_size", default=hop_size,
192 help="overlap size [default=%d]" % hop_size)192 help="overlap size [default=%d]" % hop_size)
193 193
194 def add_method(self, method='default', helpstr='method'):194 def add_method(self, method='default', helpstr='method'):
195 self.add_argument("-m","--method",195 self.add_argument("-m", "--method",
196 metavar = "<method>", type=str,196 metavar = "<method>", type=str,
197 action="store", dest="method", default=method,197 action="store", dest="method", default=method,
198 help="%s [default=%s]" % (helpstr, method))198 help="%s [default=%s]" % (helpstr, method))
199 199
200 def add_threshold(self, default=None):200 def add_threshold(self, default=None):
201 self.add_argument("-t","--threshold",201 self.add_argument("-t", "--threshold",
202 metavar = "<threshold>", type=float,202 metavar = "<threshold>", type=float,
203 action="store", dest="threshold", default=default,203 action="store", dest="threshold", default=default,
204 help="threshold [default=%s]" % default)204 help="threshold [default=%s]" % default)
@@ -239,14 +239,16 @@ class AubioArgumentParser(argparse.ArgumentParser): · 34 unmodified lines
239 help=helpstr)239 help=helpstr)
240 240
241 def add_slicer_options(self):241 def add_slicer_options(self):
242 self.add_argument("-o","--output", type = str,242 self.add_argument("-o", "--output", type = str,
243 metavar = "<outputdir>",243 metavar = "<outputdir>",
244 action="store", dest="output_directory", default=None,244 action="store", dest="output_directory", default=None,
245 help="specify path where slices of the original file should be created")245 help="specify path where slices of the original file should'
246 ' be created")
246 self.add_argument("--cut-until-nsamples", type = int,247 self.add_argument("--cut-until-nsamples", type = int,
247 metavar = "<samples>",248 metavar = "<samples>",
248 action = "store", dest = "cut_until_nsamples", default = None,249 action = "store", dest = "cut_until_nsamples", default = None,
249 help="how many extra samples should be added at the end of each slice")250 help="how many extra samples should be added at the end of'
251 ' each slice")
250 self.add_argument("--cut-every-nslices", type = int,252 self.add_argument("--cut-every-nslices", type = int,
251 metavar = "<samples>",253 metavar = "<samples>",
252 action = "store", dest = "cut_every_nslices", default = None,254 action = "store", dest = "cut_every_nslices", default = None,
@@ -254,7 +256,8 @@ class AubioArgumentParser(argparse.ArgumentParser): · 1 unmodified line
254 self.add_argument("--cut-until-nslices", type = int,256 self.add_argument("--cut-until-nslices", type = int,
255 metavar = "<slices>",257 metavar = "<slices>",
256 action = "store", dest = "cut_until_nslices", default = None,258 action = "store", dest = "cut_until_nslices", default = None,
257 help="how many extra slices should be added at the end of each slice")259 help="how many extra slices should be added at the end of'
260 ' each slice")
258 self.add_argument("--create-first",261 self.add_argument("--create-first",
259 action = "store_true", dest = "create_first", default = False,262 action = "store_true", dest = "create_first", default = False,
260 help="always include first slice")263 help="always include first slice")
@@ -288,7 +291,8 @@ class default_process(object): · 27 unmodified lines
288 self.time2string = timefunc(args.time_format)291 self.time2string = timefunc(args.time_format)
289 if args.verbose > 2 and hasattr(self, 'options'):292 if args.verbose > 2 and hasattr(self, 'options'):
290 name = type(self).__name__.split('_')[1]293 name = type(self).__name__.split('_')[1]
291 optstr = ' '.join(['running', name, 'with options', repr(self.options), '\n'])294 optstr = ' '.join(['running', name, 'with options',
295 repr(self.options), '\n'])
292 sys.stderr.write(optstr)296 sys.stderr.write(optstr)
293 def flush(self, frames_read, samplerate):297 def flush(self, frames_read, samplerate):
294 # optionally called at the end of process298 # optionally called at the end of process
@@ -296,7 +300,7 @@ class default_process(object): · 1 unmodified line
296 300
297 def parse_options(self, args, valid_opts):301 def parse_options(self, args, valid_opts):
298 # get any valid options found in a dictionnary of arguments302 # get any valid options found in a dictionnary of arguments
299 options = {k :v for k,v in vars(args).items() if k in valid_opts}303 options = {k: v for k, v in vars(args).items() if k in valid_opts}
300 self.options = options304 self.options = options
301 305
302 def remap_pvoc_options(self, options):306 def remap_pvoc_options(self, options):
@@ -377,7 +381,7 @@ class process_tempo(process_beat): · 74 unmodified lines
377 if len(self.beat_locations) < 2:381 if len(self.beat_locations) < 2:
378 outstr = "unknown bpm"382 outstr = "unknown bpm"
379 else:383 else:
380 bpms = 60./ np.diff(self.beat_locations)384 bpms = 60. / np.diff(self.beat_locations)
381 median_bpm = np.mean(bpms)385 median_bpm = np.mean(bpms)
382 if len(self.beat_locations) < 10:386 if len(self.beat_locations) < 10:
383 outstr = "%.2f bpm (uncertain)" % median_bpm387 outstr = "%.2f bpm (uncertain)" % median_bpm
@@ -398,14 +402,14 @@ class process_notes(default_process): · 14 unmodified lines
398 def __call__(self, block):402 def __call__(self, block):
399 return self.notes(block)403 return self.notes(block)
400 def repr_res(self, res, frames_read, samplerate):404 def repr_res(self, res, frames_read, samplerate):
401 if res[2] != 0: # note off405 if res[2] != 0: # note off
402 fmt_out = self.time2string(frames_read, samplerate)406 fmt_out = self.time2string(frames_read, samplerate)
403 sys.stdout.write(fmt_out + '\n')407 sys.stdout.write(fmt_out + '\n')
404 if res[0] != 0: # note on408 if res[0] != 0: # note on
405 lastmidi = res[0]409 lastmidi = res[0]
406 fmt_out = "%f\t" % lastmidi410 fmt_out = "%f\t" % lastmidi
407 fmt_out += self.time2string(frames_read, samplerate)411 fmt_out += self.time2string(frames_read, samplerate)
408 sys.stdout.write(fmt_out) # + '\t')412 sys.stdout.write(fmt_out) # + '\t')
409 def flush(self, frames_read, samplerate):413 def flush(self, frames_read, samplerate):
410 eof = self.time2string(frames_read, samplerate)414 eof = self.time2string(frames_read, samplerate)
411 sys.stdout.write(eof + '\n')415 sys.stdout.write(eof + '\n')
@@ -472,13 +476,13 @@ class process_quiet(default_process): · 60 unmodified lines
472 if aubio.silence_detection(block, self.silence) == 1:476 if aubio.silence_detection(block, self.silence) == 1:
473 if self.wassilence != 1:477 if self.wassilence != 1:
474 self.wassilence = 1478 self.wassilence = 1
475 return 2 # newly found silence479 return 2 # newly found silence
476 return 1 # silence again480 return 1 # silence again
477 else:481 else:
478 if self.wassilence != 0:482 if self.wassilence != 0:
479 self.wassilence = 0483 self.wassilence = 0
480 return -1 # newly found noise484 return -1 # newly found noise
481 return 0 # noise again485 return 0 # noise again
482 486
483 def repr_res(self, res, frames_read, samplerate):487 def repr_res(self, res, frames_read, samplerate):
484 fmt_out = None488 fmt_out = None
@@ -498,16 +502,18 @@ class process_cut(process_onset): · 13 unmodified lines
498 502
499 def __call__(self, block):503 def __call__(self, block):
500 ret = super(process_cut, self).__call__(block)504 ret = super(process_cut, self).__call__(block)
501 if ret: self.slices.append(self.onset.get_last())505 if ret:
506 self.slices.append(self.onset.get_last())
502 return ret507 return ret
503 508
504 def flush(self, frames_read, samplerate):509 def flush(self, frames_read, samplerate):
505 from aubio.cut import _cut_slice510 from aubio.cut import _cut_slice
506 _cut_slice(self.options, self.slices)511 _cut_slice(self.options, self.slices)
507 duration = float (frames_read) / float(samplerate)512 duration = float(frames_read) / float(samplerate)
508 base_info = '%(source_file)s' % {'source_file': self.options.source_uri}513 base_info = '%(source_file)s' % \
514 {'source_file': self.options.source_uri}
509 base_info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % \515 base_info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % \
510 {'duration': duration, 'samplerate': samplerate}516 {'duration': duration, 'samplerate': samplerate}
511 info = "created %d slices from " % len(self.slices)517 info = "created %d slices from " % len(self.slices)
512 info += base_info518 info += base_info
513 sys.stderr.write(info)519 sys.stderr.write(info)
@@ -525,19 +531,20 @@ def main(): · 11 unmodified lines
525 parser_root.add_argument('-V', '--version', help="show version",531 parser_root.add_argument('-V', '--version', help="show version",
526 action="store_true", dest="show_version")532 action="store_true", dest="show_version")
527 args, extras = parser_root.parse_known_args()533 args, extras = parser_root.parse_known_args()
528 if args.show_version == False: # no -V, forward to parser534 if not args.show_version: # no -V, forward to parser
529 args = parser.parse_args(extras, namespace=args)535 args = parser.parse_args(extras, namespace=args)
530 elif len(extras) != 0: # -V with other arguments, print help536 elif len(extras) != 0: # -V with other arguments, print help
531 parser.print_help()537 parser.print_help()
532 sys.exit(1)538 sys.exit(1)
533 else: # in py3, we can simply use parser directly539 else: # in py3, we can simply use parser directly
534 args = parser.parse_args()540 args = parser.parse_args()
535 if 'show_version' in args and args.show_version:541 if 'show_version' in args and args.show_version:
536 sys.stdout.write('aubio version ' + aubio.version + '\n')542 sys.stdout.write('aubio version ' + aubio.version + '\n')
537 sys.exit(0)543 sys.exit(0)
538 elif 'verbose' in args and args.verbose > 3:544 elif 'verbose' in args and args.verbose > 3:
539 sys.stderr.write('aubio version ' + aubio.version + '\n')545 sys.stderr.write('aubio version ' + aubio.version + '\n')
540 if 'command' not in args or args.command is None or args.command in ['help']:546 if 'command' not in args or args.command is None \
547 or args.command in ['help']:
541 # no command given, print help and return 1548 # no command given, print help and return 1
542 parser.print_help()549 parser.print_help()
543 if args.command and args.command in ['help']:550 if args.command and args.command in ['help']:
@@ -571,7 +578,8 @@ def main(): · 27 unmodified lines
571 # increment total number of frames read578 # increment total number of frames read
572 frames_read += read579 frames_read += read
573 # exit loop at end of file580 # exit loop at end of file
574 if read < a_source.hop_size: break581 if read < a_source.hop_size:
582 break
575 # flush the processor if needed583 # flush the processor if needed
576 processor.flush(frames_read, a_source.samplerate)584 processor.flush(frames_read, a_source.samplerate)
577 if args.verbose > 1:585 if args.verbose > 1:
@@ -579,7 +587,7 @@ def main(): · 1 unmodified line
579 fmt_string += " ({:d} samples in {:d} blocks of {:d})"587 fmt_string += " ({:d} samples in {:d} blocks of {:d})"
580 fmt_string += " from {:s} at {:d}Hz\n"588 fmt_string += " from {:s} at {:d}Hz\n"
581 sys.stderr.write(fmt_string.format(589 sys.stderr.write(fmt_string.format(
582 frames_read/float(a_source.samplerate),590 frames_read / float(a_source.samplerate),
583 frames_read,591 frames_read,
584 frames_read // a_source.hop_size + 1,592 frames_read // a_source.hop_size + 1,
585 a_source.hop_size,593 a_source.hop_size,

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t