shithub: aubio

Download patch

ref: a5004903baa9425da41248927668bbf6f35975a8
parent: 2bf530e070a005629fe0985a4945c491c4c8889a
author: Paul Brossier <piem@piem.org>
date: Sun Apr 16 07:38:10 EDT 2017

python/lib/aubio/cut.py: upgrade to argparse

--- a/python/lib/aubio/cut.py
+++ b/python/lib/aubio/cut.py
@@ -5,77 +5,79 @@
 """
 
 import sys
+import argparse
 
 def parse_args():
-    from optparse import OptionParser
     usage = "usage: %s [options] -i soundfile" % sys.argv[0]
     usage += "\n help: %s -h" % sys.argv[0]
-    parser = OptionParser(usage=usage)
-    parser.add_option("-i", "--input", action = "store", dest = "source_file",
+    parser = argparse.ArgumentParser()
+    parser.add_argument("source_file", default=None, nargs='?',
             help="input sound file to analyse", metavar = "<source_file>")
-    parser.add_option("-O","--onset-method",
+    parser.add_argument("-i", "--input", action = "store", dest = "source_file2",
+            help="input sound file to analyse", metavar = "<source_file>")
+    parser.add_argument("-O","--onset-method",
             action="store", dest="onset_method", default='default',
             metavar = "<onset_method>",
             help="onset detection method [default=default] \
                     complexdomain|hfc|phase|specdiff|energy|kl|mkl")
     # cutting methods
-    parser.add_option("-b","--beat",
+    parser.add_argument("-b","--beat",
             action="store_true", dest="beat", default=False,
             help="use beat locations")
     """
-    parser.add_option("-S","--silencecut",
+    parser.add_argument("-S","--silencecut",
             action="store_true", dest="silencecut", default=False,
             help="use silence locations")
-    parser.add_option("-s","--silence",
+    parser.add_argument("-s","--silence",
             metavar = "<value>",
             action="store", dest="silence", default=-70,
             help="silence threshold [default=-70]")
             """
     # algorithm parameters
-    parser.add_option("-r", "--samplerate",
-            metavar = "<freq>", type='int',
+    parser.add_argument("-r", "--samplerate",
+            metavar = "<freq>", type=int,
             action="store", dest="samplerate", default=0,
             help="samplerate at which the file should be represented")
-    parser.add_option("-B","--bufsize",
+    parser.add_argument("-B","--bufsize",
             action="store", dest="bufsize", default=512,
-            metavar = "<size>", type='int',
+            metavar = "<size>", type=int,
             help="buffer size [default=512]")
-    parser.add_option("-H","--hopsize",
-            metavar = "<size>", type='int',
+    parser.add_argument("-H","--hopsize",
+            metavar = "<size>", type=int,
             action="store", dest="hopsize", default=256,
             help="overlap size [default=256]")
-    parser.add_option("-t","--onset-threshold",
-            metavar = "<value>", type="float",
+    parser.add_argument("-t","--onset-threshold",
+            metavar = "<value>", type=float,
             action="store", dest="threshold", default=0.3,
             help="onset peak picking threshold [default=0.3]")
-    parser.add_option("-c","--cut",
+    parser.add_argument("-c","--cut",
             action="store_true", dest="cut", default=False,
             help="cut input sound file at detected labels \
                     best used with option -L")
 
     # minioi
-    parser.add_option("-M","--minioi",
-            metavar = "<value>", type='string',
+    parser.add_argument("-M","--minioi",
+            metavar = "<value>", type=str,
             action="store", dest="minioi", default="12ms",
             help="minimum inter onset interval [default=12ms]")
 
     """
-    parser.add_option("-D","--delay",
-            action = "store", dest = "delay", type = "float",
+    parser.add_argument("-D","--delay",
+            action = "store", dest = "delay", type = float,
             metavar = "<seconds>", default=0,
             help="number of seconds to take back [default=system]\
                     default system delay is 3*hopsize/samplerate")
-    parser.add_option("-C","--dcthreshold",
+    parser.add_argument("-C","--dcthreshold",
             metavar = "<value>",
             action="store", dest="dcthreshold", default=1.,
             help="onset peak picking DC component [default=1.]")
-    parser.add_option("-L","--localmin",
+    parser.add_argument("-L","--localmin",
             action="store_true", dest="localmin", default=False,
             help="use local minima after peak detection")
-    parser.add_option("-d","--derivate",
+    parser.add_argument("-d","--derivate",
             action="store_true", dest="derivate", default=False,
             help="derivate onset detection function")
-    parser.add_option("-z","--zerocross",
+    parser.add_argument("-z","--zerocross",
             metavar = "<value>",
             action="store", dest="zerothres", default=0.008,
             help="zero-crossing threshold for slicing [default=0.00008]")
@@ -82,66 +84,67 @@
             """
     # plotting functions
     """
-    parser.add_option("-p","--plot",
+    parser.add_argument("-p","--plot",
             action="store_true", dest="plot", default=False,
             help="draw plot")
-    parser.add_option("-x","--xsize",
+    parser.add_argument("-x","--xsize",
             metavar = "<size>",
             action="store", dest="xsize", default=1.,
-            type='float', help="define xsize for plot")
-    parser.add_option("-y","--ysize",
+            type=float, help="define xsize for plot")
+    parser.add_argument("-y","--ysize",
             metavar = "<size>",
             action="store", dest="ysize", default=1.,
-            type='float', help="define ysize for plot")
-    parser.add_option("-f","--function",
+            type=float, help="define ysize for plot")
+    parser.add_argument("-f","--function",
             action="store_true", dest="func", default=False,
             help="print detection function")
-    parser.add_option("-n","--no-onsets",
+    parser.add_argument("-n","--no-onsets",
             action="store_true", dest="nplot", default=False,
             help="do not plot detected onsets")
-    parser.add_option("-O","--outplot",
+    parser.add_argument("-O","--outplot",
             metavar = "<output_image>",
             action="store", dest="outplot", default=None,
             help="save plot to output.{ps,png}")
-    parser.add_option("-F","--spectrogram",
+    parser.add_argument("-F","--spectrogram",
             action="store_true", dest="spectro", default=False,
             help="add spectrogram to the plot")
     """
-    parser.add_option("-o","--output", type = str,
+    parser.add_argument("-o","--output", type = str,
             metavar = "<outputdir>",
             action="store", dest="output_directory", default=None,
             help="specify path where slices of the original file should be created")
-    parser.add_option("--cut-until-nsamples", type = int,
+    parser.add_argument("--cut-until-nsamples", type = int,
             metavar = "<samples>",
             action = "store", dest = "cut_until_nsamples", default = None,
             help="how many extra samples should be added at the end of each slice")
-    parser.add_option("--cut-every-nslices", type = int,
+    parser.add_argument("--cut-every-nslices", type = int,
             metavar = "<samples>",
             action = "store", dest = "cut_every_nslices", default = None,
             help="how many slices should be groupped together at each cut")
-    parser.add_option("--cut-until-nslices", type = int,
+    parser.add_argument("--cut-until-nslices", type = int,
             metavar = "<slices>",
             action = "store", dest = "cut_until_nslices", default = None,
             help="how many extra slices should be added at the end of each slice")
 
-    parser.add_option("-v","--verbose",
+    parser.add_argument("-v","--verbose",
             action="store_true", dest="verbose", default=True,
             help="make lots of noise [default]")
-    parser.add_option("-q","--quiet",
+    parser.add_argument("-q","--quiet",
             action="store_false", dest="verbose", default=True,
             help="be quiet")
-    (options, args) = parser.parse_args()
-    if not options.source_file:
-        if len(args) == 1:
-            options.source_file = args[0]
-        else:
-            print ("no file name given\n" + usage)
-            sys.exit(1)
-    return options, args
+    args = parser.parse_args()
+    if not args.source_file and not args.source_file2:
+        sys.stderr.write("Error: no file name given\n")
+        parser.print_help()
+        sys.exit(1)
+    elif args.source_file2 is not None:
+        args.source_file = args.source_file2
+    return args
 
 def main():
-    options, args = parse_args()
+    options = parse_args()
 
+    source_file = options.source_file
     hopsize = options.hopsize
     bufsize = options.bufsize
     samplerate = options.samplerate