Changeset a5004903 for python


Ignore:
Timestamp:
Apr 16, 2017, 11:38:10 AM (8 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master
Children:
9956027
Parents:
2bf530e0
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/lib/aubio/cut.py

    r2bf530e0 ra5004903  
    66
    77import sys
     8import argparse
    89
    910def parse_args():
    10     from optparse import OptionParser
    1111    usage = "usage: %s [options] -i soundfile" % sys.argv[0]
    1212    usage += "\n help: %s -h" % sys.argv[0]
    13     parser = OptionParser(usage=usage)
    14     parser.add_option("-i", "--input", action = "store", dest = "source_file",
     13    parser = argparse.ArgumentParser()
     14    parser.add_argument("source_file", default=None, nargs='?',
    1515            help="input sound file to analyse", metavar = "<source_file>")
    16     parser.add_option("-O","--onset-method",
     16    parser.add_argument("-i", "--input", action = "store", dest = "source_file2",
     17            help="input sound file to analyse", metavar = "<source_file>")
     18    parser.add_argument("-O","--onset-method",
    1719            action="store", dest="onset_method", default='default',
    1820            metavar = "<onset_method>",
     
    2022                    complexdomain|hfc|phase|specdiff|energy|kl|mkl")
    2123    # cutting methods
    22     parser.add_option("-b","--beat",
     24    parser.add_argument("-b","--beat",
    2325            action="store_true", dest="beat", default=False,
    2426            help="use beat locations")
    2527    """
    26     parser.add_option("-S","--silencecut",
     28    parser.add_argument("-S","--silencecut",
    2729            action="store_true", dest="silencecut", default=False,
    2830            help="use silence locations")
    29     parser.add_option("-s","--silence",
     31    parser.add_argument("-s","--silence",
    3032            metavar = "<value>",
    3133            action="store", dest="silence", default=-70,
     
    3335            """
    3436    # algorithm parameters
    35     parser.add_option("-r", "--samplerate",
    36             metavar = "<freq>", type='int',
     37    parser.add_argument("-r", "--samplerate",
     38            metavar = "<freq>", type=int,
    3739            action="store", dest="samplerate", default=0,
    3840            help="samplerate at which the file should be represented")
    39     parser.add_option("-B","--bufsize",
     41    parser.add_argument("-B","--bufsize",
    4042            action="store", dest="bufsize", default=512,
    41             metavar = "<size>", type='int',
     43            metavar = "<size>", type=int,
    4244            help="buffer size [default=512]")
    43     parser.add_option("-H","--hopsize",
    44             metavar = "<size>", type='int',
     45    parser.add_argument("-H","--hopsize",
     46            metavar = "<size>", type=int,
    4547            action="store", dest="hopsize", default=256,
    4648            help="overlap size [default=256]")
    47     parser.add_option("-t","--onset-threshold",
    48             metavar = "<value>", type="float",
     49    parser.add_argument("-t","--onset-threshold",
     50            metavar = "<value>", type=float,
    4951            action="store", dest="threshold", default=0.3,
    5052            help="onset peak picking threshold [default=0.3]")
    51     parser.add_option("-c","--cut",
     53    parser.add_argument("-c","--cut",
    5254            action="store_true", dest="cut", default=False,
    5355            help="cut input sound file at detected labels \
     
    5557
    5658    # minioi
    57     parser.add_option("-M","--minioi",
    58             metavar = "<value>", type='string',
     59    parser.add_argument("-M","--minioi",
     60            metavar = "<value>", type=str,
    5961            action="store", dest="minioi", default="12ms",
    6062            help="minimum inter onset interval [default=12ms]")
    6163
    6264    """
    63     parser.add_option("-D","--delay",
    64             action = "store", dest = "delay", type = "float",
     65    parser.add_argument("-D","--delay",
     66            action = "store", dest = "delay", type = float,
    6567            metavar = "<seconds>", default=0,
    6668            help="number of seconds to take back [default=system]\
    6769                    default system delay is 3*hopsize/samplerate")
    68     parser.add_option("-C","--dcthreshold",
     70    parser.add_argument("-C","--dcthreshold",
    6971            metavar = "<value>",
    7072            action="store", dest="dcthreshold", default=1.,
    7173            help="onset peak picking DC component [default=1.]")
    72     parser.add_option("-L","--localmin",
     74    parser.add_argument("-L","--localmin",
    7375            action="store_true", dest="localmin", default=False,
    7476            help="use local minima after peak detection")
    75     parser.add_option("-d","--derivate",
     77    parser.add_argument("-d","--derivate",
    7678            action="store_true", dest="derivate", default=False,
    7779            help="derivate onset detection function")
    78     parser.add_option("-z","--zerocross",
     80    parser.add_argument("-z","--zerocross",
    7981            metavar = "<value>",
    8082            action="store", dest="zerothres", default=0.008,
     
    8385    # plotting functions
    8486    """
    85     parser.add_option("-p","--plot",
     87    parser.add_argument("-p","--plot",
    8688            action="store_true", dest="plot", default=False,
    8789            help="draw plot")
    88     parser.add_option("-x","--xsize",
     90    parser.add_argument("-x","--xsize",
    8991            metavar = "<size>",
    9092            action="store", dest="xsize", default=1.,
    91             type='float', help="define xsize for plot")
    92     parser.add_option("-y","--ysize",
     93            type=float, help="define xsize for plot")
     94    parser.add_argument("-y","--ysize",
    9395            metavar = "<size>",
    9496            action="store", dest="ysize", default=1.,
    95             type='float', help="define ysize for plot")
    96     parser.add_option("-f","--function",
     97            type=float, help="define ysize for plot")
     98    parser.add_argument("-f","--function",
    9799            action="store_true", dest="func", default=False,
    98100            help="print detection function")
    99     parser.add_option("-n","--no-onsets",
     101    parser.add_argument("-n","--no-onsets",
    100102            action="store_true", dest="nplot", default=False,
    101103            help="do not plot detected onsets")
    102     parser.add_option("-O","--outplot",
     104    parser.add_argument("-O","--outplot",
    103105            metavar = "<output_image>",
    104106            action="store", dest="outplot", default=None,
    105107            help="save plot to output.{ps,png}")
    106     parser.add_option("-F","--spectrogram",
     108    parser.add_argument("-F","--spectrogram",
    107109            action="store_true", dest="spectro", default=False,
    108110            help="add spectrogram to the plot")
    109111    """
    110     parser.add_option("-o","--output", type = str,
     112    parser.add_argument("-o","--output", type = str,
    111113            metavar = "<outputdir>",
    112114            action="store", dest="output_directory", default=None,
    113115            help="specify path where slices of the original file should be created")
    114     parser.add_option("--cut-until-nsamples", type = int,
     116    parser.add_argument("--cut-until-nsamples", type = int,
    115117            metavar = "<samples>",
    116118            action = "store", dest = "cut_until_nsamples", default = None,
    117119            help="how many extra samples should be added at the end of each slice")
    118     parser.add_option("--cut-every-nslices", type = int,
     120    parser.add_argument("--cut-every-nslices", type = int,
    119121            metavar = "<samples>",
    120122            action = "store", dest = "cut_every_nslices", default = None,
    121123            help="how many slices should be groupped together at each cut")
    122     parser.add_option("--cut-until-nslices", type = int,
     124    parser.add_argument("--cut-until-nslices", type = int,
    123125            metavar = "<slices>",
    124126            action = "store", dest = "cut_until_nslices", default = None,
    125127            help="how many extra slices should be added at the end of each slice")
    126128
    127     parser.add_option("-v","--verbose",
     129    parser.add_argument("-v","--verbose",
    128130            action="store_true", dest="verbose", default=True,
    129131            help="make lots of noise [default]")
    130     parser.add_option("-q","--quiet",
     132    parser.add_argument("-q","--quiet",
    131133            action="store_false", dest="verbose", default=True,
    132134            help="be quiet")
    133     (options, args) = parser.parse_args()
    134     if not options.source_file:
    135         if len(args) == 1:
    136             options.source_file = args[0]
    137         else:
    138             print ("no file name given\n" + usage)
    139             sys.exit(1)
    140     return options, args
     135    args = parser.parse_args()
     136    if not args.source_file and not args.source_file2:
     137        sys.stderr.write("Error: no file name given\n")
     138        parser.print_help()
     139        sys.exit(1)
     140    elif args.source_file2 is not None:
     141        args.source_file = args.source_file2
     142    return args
    141143
    142144def main():
    143     options, args = parse_args()
    144 
     145    options = parse_args()
     146
     147    source_file = options.source_file
    145148    hopsize = options.hopsize
    146149    bufsize = options.bufsize
Note: See TracChangeset for help on using the changeset viewer.