shithub: aubio

Download patch

ref: c95062be03d183ed7576fba29218eeae5e179404
parent: b02d52f118afe92efebfabd3310a68546eb409ca
author: Paul Brossier <piem@piem.org>
date: Sat Sep 16 11:45:18 EDT 2017

python/lib/aubio/cmd.py: use custom parser class

--- a/python/lib/aubio/cmd.py
+++ b/python/lib/aubio/cmd.py
@@ -20,6 +20,7 @@
             action="store_true", dest="show_version")
 
     subparsers = parser.add_subparsers(title='commands', dest='command',
+            parser_class= AubioArgumentParser,
             metavar="")
 
     parser_add_subcommand_help(subparsers)
@@ -46,16 +47,16 @@
     subparser = subparsers.add_parser('onset',
             help='estimate time of onsets (beginning of sound event)',
             formatter_class = argparse.ArgumentDefaultsHelpFormatter)
-    parser_add_input(subparser)
-    parser_add_buf_hop_size(subparser)
+    subparser.add_input()
+    subparser.add_buf_hop_size()
     helpstr = "onset novelty function"
     helpstr += " <default|energy|hfc|complex|phase|specdiff|kl|mkl|specflux>"
-    parser_add_method(subparser, helpstr=helpstr)
-    parser_add_threshold(subparser)
-    parser_add_silence(subparser)
-    parser_add_minioi(subparser)
-    parser_add_time_format(subparser)
-    parser_add_verbose_help(subparser)
+    subparser.add_method(helpstr=helpstr)
+    subparser.add_threshold()
+    subparser.add_silence()
+    subparser.add_minioi()
+    subparser.add_time_format()
+    subparser.add_verbose_help()
     subparser.set_defaults(process=process_onset)
 
 def parser_add_subcommand_pitch(subparsers):
@@ -62,15 +63,15 @@
     # pitch subcommand
     subparser = subparsers.add_parser('pitch',
             help='estimate fundamental frequency (monophonic)')
-    parser_add_input(subparser)
-    parser_add_buf_hop_size(subparser, buf_size=2048)
+    subparser.add_input()
+    subparser.add_buf_hop_size(buf_size=2048)
     helpstr = "pitch detection method <default|yinfft|yin|mcomb|fcomb|schmitt>"
-    parser_add_method(subparser, helpstr=helpstr)
-    parser_add_threshold(subparser)
-    parser_add_pitch_unit(subparser)
-    parser_add_silence(subparser)
-    parser_add_time_format(subparser)
-    parser_add_verbose_help(subparser)
+    subparser.add_method(helpstr=helpstr)
+    subparser.add_threshold()
+    subparser.add_pitch_unit()
+    subparser.add_silence()
+    subparser.add_time_format()
+    subparser.add_verbose_help()
     subparser.set_defaults(process=process_pitch)
 
 def parser_add_subcommand_beat(subparsers):
@@ -77,10 +78,10 @@
     # beat subcommand
     subparser = subparsers.add_parser('beat',
             help='estimate location of beats')
-    parser_add_input(subparser)
-    parser_add_buf_hop_size(subparser, buf_size=1024, hop_size=512)
-    parser_add_time_format(subparser)
-    parser_add_verbose_help(subparser)
+    subparser.add_input()
+    subparser.add_buf_hop_size(buf_size=1024, hop_size=512)
+    subparser.add_time_format()
+    subparser.add_verbose_help()
     subparser.set_defaults(process=process_beat)
 
 def parser_add_subcommand_tempo(subparsers):
@@ -87,10 +88,10 @@
     # tempo subcommand
     subparser = subparsers.add_parser('tempo',
             help='estimate overall tempo in bpm')
-    parser_add_input(subparser)
-    parser_add_buf_hop_size(subparser, buf_size=1024, hop_size=512)
-    parser_add_time_format(subparser)
-    parser_add_verbose_help(subparser)
+    subparser.add_input()
+    subparser.add_buf_hop_size(buf_size=1024, hop_size=512)
+    subparser.add_time_format()
+    subparser.add_verbose_help()
     subparser.set_defaults(process=process_tempo)
 
 def parser_add_subcommand_notes(subparsers):
@@ -97,10 +98,10 @@
     # notes subcommand
     subparser = subparsers.add_parser('notes',
             help='estimate midi-like notes (monophonic)')
-    parser_add_input(subparser)
-    parser_add_buf_hop_size(subparser)
-    parser_add_time_format(subparser)
-    parser_add_verbose_help(subparser)
+    subparser.add_input()
+    subparser.add_buf_hop_size()
+    subparser.add_time_format()
+    subparser.add_verbose_help()
     subparser.set_defaults(process=process_notes)
 
 def parser_add_subcommand_mfcc(subparsers):
@@ -107,10 +108,10 @@
     # mfcc subcommand
     subparser = subparsers.add_parser('mfcc',
             help='extract Mel-Frequency Cepstrum Coefficients')
-    parser_add_input(subparser)
-    parser_add_buf_hop_size(subparser)
-    parser_add_time_format(subparser)
-    parser_add_verbose_help(subparser)
+    subparser.add_input()
+    subparser.add_buf_hop_size()
+    subparser.add_time_format()
+    subparser.add_verbose_help()
     subparser.set_defaults(process=process_mfcc)
 
 def parser_add_subcommand_melbands(subparsers):
@@ -117,10 +118,10 @@
     # melbands subcommand
     subparser = subparsers.add_parser('melbands',
             help='extract energies in Mel-frequency bands')
-    parser_add_input(subparser)
-    parser_add_buf_hop_size(subparser)
-    parser_add_time_format(subparser)
-    parser_add_verbose_help(subparser)
+    subparser.add_input()
+    subparser.add_buf_hop_size()
+    subparser.add_time_format()
+    subparser.add_verbose_help()
     subparser.set_defaults(process=process_melbands)
 
 def parser_add_subcommand_quiet(subparsers):
@@ -127,87 +128,89 @@
     # quiet subcommand
     subparser = subparsers.add_parser('quiet',
             help='extract timestamps of quiet and loud regions')
-    parser_add_input(subparser)
-    parser_add_hop_size(subparser)
-    parser_add_silence(subparser)
-    parser_add_time_format(subparser)
-    parser_add_verbose_help(subparser)
+    subparser.add_input()
+    subparser.add_hop_size()
+    subparser.add_silence()
+    subparser.add_time_format()
+    subparser.add_verbose_help()
     subparser.set_defaults(process=process_quiet)
 
-def parser_add_input(parser):
-    parser.add_argument("source_uri", default=None, nargs='?',
-            help="input sound file to analyse", metavar = "<source_uri>")
-    parser.add_argument("-i", "--input", dest = "source_uri2",
-            help="input sound file to analyse", metavar = "<source_uri>")
-    parser.add_argument("-r", "--samplerate",
-            metavar = "<freq>", type=int,
-            action="store", dest="samplerate", default=0,
-            help="samplerate at which the file should be represented")
+class AubioArgumentParser(argparse.ArgumentParser):
 
-def parser_add_verbose_help(parser):
-    parser.add_argument("-v","--verbose",
-            action="count", dest="verbose", default=1,
-            help="make lots of noise [default]")
-    parser.add_argument("-q","--quiet",
-            action="store_const", dest="verbose", const=0,
-            help="be quiet")
+    def add_input(self):
+        self.add_argument("source_uri", default=None, nargs='?',
+                help="input sound file to analyse", metavar = "<source_uri>")
+        self.add_argument("-i", "--input", dest = "source_uri2",
+                help="input sound file to analyse", metavar = "<source_uri>")
+        self.add_argument("-r", "--samplerate",
+                metavar = "<freq>", type=int,
+                action="store", dest="samplerate", default=0,
+                help="samplerate at which the file should be represented")
 
-def parser_add_buf_hop_size(parser, buf_size=512, hop_size=256):
-    parser_add_buf_size(parser, buf_size=buf_size)
-    parser_add_hop_size(parser, hop_size=hop_size)
+    def add_verbose_help(self):
+        self.add_argument("-v","--verbose",
+                action="count", dest="verbose", default=1,
+                help="make lots of noise [default]")
+        self.add_argument("-q","--quiet",
+                action="store_const", dest="verbose", const=0,
+                help="be quiet")
 
-def parser_add_buf_size(parser, buf_size=512):
-    parser.add_argument("-B","--bufsize",
-            action="store", dest="buf_size", default=buf_size,
-            metavar = "<size>", type=int,
-            help="buffer size [default=%d]" % buf_size)
+    def add_buf_hop_size(self, buf_size=512, hop_size=256):
+        self.add_buf_size(buf_size=buf_size)
+        self.add_hop_size(hop_size=hop_size)
 
-def parser_add_hop_size(parser, hop_size=256):
-    parser.add_argument("-H","--hopsize",
-            metavar = "<size>", type=int,
-            action="store", dest="hop_size", default=hop_size,
-            help="overlap size [default=%d]" % hop_size)
+    def add_buf_size(self, buf_size=512):
+        self.add_argument("-B","--bufsize",
+                action="store", dest="buf_size", default=buf_size,
+                metavar = "<size>", type=int,
+                help="buffer size [default=%d]" % buf_size)
 
-def parser_add_method(parser, method='default', helpstr='method'):
-    parser.add_argument("-m","--method",
-            metavar = "<method>", type=str,
-            action="store", dest="method", default=method,
-            help="%s [default=%s]" % (helpstr, method))
+    def add_hop_size(self, hop_size=256):
+        self.add_argument("-H","--hopsize",
+                metavar = "<size>", type=int,
+                action="store", dest="hop_size", default=hop_size,
+                help="overlap size [default=%d]" % hop_size)
 
-def parser_add_threshold(parser, default=None):
-    parser.add_argument("-t","--threshold",
-            metavar = "<threshold>", type=float,
-            action="store", dest="threshold", default=default,
-            help="threshold [default=%s]" % default)
+    def add_method(self, method='default', helpstr='method'):
+        self.add_argument("-m","--method",
+                metavar = "<method>", type=str,
+                action="store", dest="method", default=method,
+                help="%s [default=%s]" % (helpstr, method))
 
-def parser_add_silence(parser):
-    parser.add_argument("-s", "--silence",
-            metavar = "<value>", type=float,
-            action="store", dest="silence", default=-70,
-            help="silence threshold")
+    def add_threshold(self, default=None):
+        self.add_argument("-t","--threshold",
+                metavar = "<threshold>", type=float,
+                action="store", dest="threshold", default=default,
+                help="threshold [default=%s]" % default)
 
-def parser_add_minioi(parser):
-    parser.add_argument("-M", "--minioi",
-            metavar = "<value>", type=str,
-            action="store", dest="minioi", default="12ms",
-            help="minimum Inter-Onset Interval")
+    def add_silence(self):
+        self.add_argument("-s", "--silence",
+                metavar = "<value>", type=float,
+                action="store", dest="silence", default=-70,
+                help="silence threshold")
 
-def parser_add_pitch_unit(parser, default="Hz"):
-    help_str = "frequency unit, should be one of Hz, midi, bin, cent"
-    help_str += " [default=%s]" % default
-    parser.add_argument("-u", "--pitch-unit",
-            metavar = "<value>", type=str,
-            action="store", dest="pitch_unit", default=default,
-            help=help_str)
+    def add_minioi(self, default="12ms"):
+        self.add_argument("-M", "--minioi",
+                metavar = "<value>", type=str,
+                action="store", dest="minioi", default=default,
+                help="minimum Inter-Onset Interval [default=%s]" % default)
 
-def parser_add_time_format(parser):
-    helpstr = "select time values output format (samples, ms, seconds)"
-    helpstr += " [default=seconds]"
-    parser.add_argument("-T", "--time-format",
-             metavar='format',
-             dest="time_format",
-             default=None,
-             help=helpstr)
+    def add_pitch_unit(self, default="Hz"):
+        help_str = "frequency unit, should be one of Hz, midi, bin, cent"
+        help_str += " [default=%s]" % default
+        self.add_argument("-u", "--pitch-unit",
+                metavar = "<value>", type=str,
+                action="store", dest="pitch_unit", default=default,
+                help=help_str)
+
+    def add_time_format(self):
+        helpstr = "select time values output format (samples, ms, seconds)"
+        helpstr += " [default=seconds]"
+        self.add_argument("-T", "--time-format",
+                 metavar='format',
+                 dest="time_format",
+                 default=None,
+                 help=helpstr)
 
 # some utilities