Showing the changes to python (1 of 1 file). Show all
Unified · Split
| @@ -5,143 +5,146 @@ · 4 unmodified lines | |||
| 5 | """ | 5 | """ |
| 6 | 6 | ||
| 7 | import sys | 7 | import sys |
| 8 | import argparse | ||
| 8 | 9 | ||
| 9 | def parse_args(): | 10 | def parse_args(): |
| 10 | from optparse import OptionParser | ||
| 11 | usage = "usage: %s [options] -i soundfile" % sys.argv[0] | 11 | usage = "usage: %s [options] -i soundfile" % sys.argv[0] |
| 12 | usage += "\n help: %s -h" % sys.argv[0] | 12 | usage += "\n help: %s -h" % sys.argv[0] |
| 13 | parser = OptionParser(usage=usage) | 13 | parser = argparse.ArgumentParser() |
| 14 | parser.add_option("-i", "--input", action = "store", dest = "source_file", | 14 | parser.add_argument("source_file", default=None, nargs='?', |
| 15 | help="input sound file to analyse", metavar = "<source_file>") | 15 | 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", | ||
| 17 | action="store", dest="onset_method", default='default', | 19 | action="store", dest="onset_method", default='default', |
| 18 | metavar = "<onset_method>", | 20 | metavar = "<onset_method>", |
| 19 | help="onset detection method [default=default] \ | 21 | help="onset detection method [default=default] \ |
| 20 | complexdomain|hfc|phase|specdiff|energy|kl|mkl") | 22 | complexdomain|hfc|phase|specdiff|energy|kl|mkl") |
| 21 | # cutting methods | 23 | # cutting methods |
| 22 | parser.add_option("-b","--beat", | 24 | parser.add_argument("-b","--beat", |
| 23 | action="store_true", dest="beat", default=False, | 25 | action="store_true", dest="beat", default=False, |
| 24 | help="use beat locations") | 26 | help="use beat locations") |
| 25 | """ | 27 | """ |
| 26 | parser.add_option("-S","--silencecut", | 28 | parser.add_argument("-S","--silencecut", |
| 27 | action="store_true", dest="silencecut", default=False, | 29 | action="store_true", dest="silencecut", default=False, |
| 28 | help="use silence locations") | 30 | help="use silence locations") |
| 29 | parser.add_option("-s","--silence", | 31 | parser.add_argument("-s","--silence", |
| 30 | metavar = "<value>", | 32 | metavar = "<value>", |
| 31 | action="store", dest="silence", default=-70, | 33 | action="store", dest="silence", default=-70, |
| 32 | help="silence threshold [default=-70]") | 34 | help="silence threshold [default=-70]") |
| 33 | """ | 35 | """ |
| 34 | # algorithm parameters | 36 | # algorithm parameters |
| 35 | parser.add_option("-r", "--samplerate", | 37 | parser.add_argument("-r", "--samplerate", |
| 36 | metavar = "<freq>", type='int', | 38 | metavar = "<freq>", type=int, |
| 37 | action="store", dest="samplerate", default=0, | 39 | action="store", dest="samplerate", default=0, |
| 38 | help="samplerate at which the file should be represented") | 40 | help="samplerate at which the file should be represented") |
| 39 | parser.add_option("-B","--bufsize", | 41 | parser.add_argument("-B","--bufsize", |
| 40 | action="store", dest="bufsize", default=512, | 42 | action="store", dest="bufsize", default=512, |
| 41 | metavar = "<size>", type='int', | 43 | metavar = "<size>", type=int, |
| 42 | help="buffer size [default=512]") | 44 | help="buffer size [default=512]") |
| 43 | parser.add_option("-H","--hopsize", | 45 | parser.add_argument("-H","--hopsize", |
| 44 | metavar = "<size>", type='int', | 46 | metavar = "<size>", type=int, |
| 45 | action="store", dest="hopsize", default=256, | 47 | action="store", dest="hopsize", default=256, |
| 46 | help="overlap size [default=256]") | 48 | help="overlap size [default=256]") |
| 47 | parser.add_option("-t","--onset-threshold", | 49 | parser.add_argument("-t","--onset-threshold", |
| 48 | metavar = "<value>", type="float", | 50 | metavar = "<value>", type=float, |
| 49 | action="store", dest="threshold", default=0.3, | 51 | action="store", dest="threshold", default=0.3, |
| 50 | help="onset peak picking threshold [default=0.3]") | 52 | help="onset peak picking threshold [default=0.3]") |
| 51 | parser.add_option("-c","--cut", | 53 | parser.add_argument("-c","--cut", |
| 52 | action="store_true", dest="cut", default=False, | 54 | action="store_true", dest="cut", default=False, |
| 53 | help="cut input sound file at detected labels \ | 55 | help="cut input sound file at detected labels \ |
| 54 | best used with option -L") | 56 | best used with option -L") |
| 55 | 57 | ||
| 56 | # minioi | 58 | # minioi |
| 57 | parser.add_option("-M","--minioi", | 59 | parser.add_argument("-M","--minioi", |
| 58 | metavar = "<value>", type='string', | 60 | metavar = "<value>", type=str, |
| 59 | action="store", dest="minioi", default="12ms", | 61 | action="store", dest="minioi", default="12ms", |
| 60 | help="minimum inter onset interval [default=12ms]") | 62 | help="minimum inter onset interval [default=12ms]") |
| 61 | 63 | ||
| 62 | """ | 64 | """ |
| 63 | parser.add_option("-D","--delay", | 65 | parser.add_argument("-D","--delay", |
| 64 | action = "store", dest = "delay", type = "float", | 66 | action = "store", dest = "delay", type = float, |
| 65 | metavar = "<seconds>", default=0, | 67 | metavar = "<seconds>", default=0, |
| 66 | help="number of seconds to take back [default=system]\ | 68 | help="number of seconds to take back [default=system]\ |
| 67 | default system delay is 3*hopsize/samplerate") | 69 | default system delay is 3*hopsize/samplerate") |
| 68 | parser.add_option("-C","--dcthreshold", | 70 | parser.add_argument("-C","--dcthreshold", |
| 69 | metavar = "<value>", | 71 | metavar = "<value>", |
| 70 | action="store", dest="dcthreshold", default=1., | 72 | action="store", dest="dcthreshold", default=1., |
| 71 | help="onset peak picking DC component [default=1.]") | 73 | help="onset peak picking DC component [default=1.]") |
| 72 | parser.add_option("-L","--localmin", | 74 | parser.add_argument("-L","--localmin", |
| 73 | action="store_true", dest="localmin", default=False, | 75 | action="store_true", dest="localmin", default=False, |
| 74 | help="use local minima after peak detection") | 76 | help="use local minima after peak detection") |
| 75 | parser.add_option("-d","--derivate", | 77 | parser.add_argument("-d","--derivate", |
| 76 | action="store_true", dest="derivate", default=False, | 78 | action="store_true", dest="derivate", default=False, |
| 77 | help="derivate onset detection function") | 79 | help="derivate onset detection function") |
| 78 | parser.add_option("-z","--zerocross", | 80 | parser.add_argument("-z","--zerocross", |
| 79 | metavar = "<value>", | 81 | metavar = "<value>", |
| 80 | action="store", dest="zerothres", default=0.008, | 82 | action="store", dest="zerothres", default=0.008, |
| 81 | help="zero-crossing threshold for slicing [default=0.00008]") | 83 | help="zero-crossing threshold for slicing [default=0.00008]") |
| 82 | """ | 84 | """ |
| 83 | # plotting functions | 85 | # plotting functions |
| 84 | """ | 86 | """ |
| 85 | parser.add_option("-p","--plot", | 87 | parser.add_argument("-p","--plot", |
| 86 | action="store_true", dest="plot", default=False, | 88 | action="store_true", dest="plot", default=False, |
| 87 | help="draw plot") | 89 | help="draw plot") |
| 88 | parser.add_option("-x","--xsize", | 90 | parser.add_argument("-x","--xsize", |
| 89 | metavar = "<size>", | 91 | metavar = "<size>", |
| 90 | action="store", dest="xsize", default=1., | 92 | action="store", dest="xsize", default=1., |
| 91 | type='float', help="define xsize for plot") | 93 | type=float, help="define xsize for plot") |
| 92 | parser.add_option("-y","--ysize", | 94 | parser.add_argument("-y","--ysize", |
| 93 | metavar = "<size>", | 95 | metavar = "<size>", |
| 94 | action="store", dest="ysize", default=1., | 96 | action="store", dest="ysize", default=1., |
| 95 | type='float', help="define ysize for plot") | 97 | type=float, help="define ysize for plot") |
| 96 | parser.add_option("-f","--function", | 98 | parser.add_argument("-f","--function", |
| 97 | action="store_true", dest="func", default=False, | 99 | action="store_true", dest="func", default=False, |
| 98 | help="print detection function") | 100 | help="print detection function") |
| 99 | parser.add_option("-n","--no-onsets", | 101 | parser.add_argument("-n","--no-onsets", |
| 100 | action="store_true", dest="nplot", default=False, | 102 | action="store_true", dest="nplot", default=False, |
| 101 | help="do not plot detected onsets") | 103 | help="do not plot detected onsets") |
| 102 | parser.add_option("-O","--outplot", | 104 | parser.add_argument("-O","--outplot", |
| 103 | metavar = "<output_image>", | 105 | metavar = "<output_image>", |
| 104 | action="store", dest="outplot", default=None, | 106 | action="store", dest="outplot", default=None, |
| 105 | help="save plot to output.{ps,png}") | 107 | help="save plot to output.{ps,png}") |
| 106 | parser.add_option("-F","--spectrogram", | 108 | parser.add_argument("-F","--spectrogram", |
| 107 | action="store_true", dest="spectro", default=False, | 109 | action="store_true", dest="spectro", default=False, |
| 108 | help="add spectrogram to the plot") | 110 | help="add spectrogram to the plot") |
| 109 | """ | 111 | """ |
| 110 | parser.add_option("-o","--output", type = str, | 112 | parser.add_argument("-o","--output", type = str, |
| 111 | metavar = "<outputdir>", | 113 | metavar = "<outputdir>", |
| 112 | action="store", dest="output_directory", default=None, | 114 | action="store", dest="output_directory", default=None, |
| 113 | help="specify path where slices of the original file should be created") | 115 | 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, |
| 115 | metavar = "<samples>", | 117 | metavar = "<samples>", |
| 116 | action = "store", dest = "cut_until_nsamples", default = None, | 118 | action = "store", dest = "cut_until_nsamples", default = None, |
| 117 | help="how many extra samples should be added at the end of each slice") | 119 | 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, |
| 119 | metavar = "<samples>", | 121 | metavar = "<samples>", |
| 120 | action = "store", dest = "cut_every_nslices", default = None, | 122 | action = "store", dest = "cut_every_nslices", default = None, |
| 121 | help="how many slices should be groupped together at each cut") | 123 | 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, |
| 123 | metavar = "<slices>", | 125 | metavar = "<slices>", |
| 124 | action = "store", dest = "cut_until_nslices", default = None, | 126 | action = "store", dest = "cut_until_nslices", default = None, |
| 125 | help="how many extra slices should be added at the end of each slice") | 127 | help="how many extra slices should be added at the end of each slice") |
| 126 | 128 | ||
| 127 | parser.add_option("-v","--verbose", | 129 | parser.add_argument("-v","--verbose", |
| 128 | action="store_true", dest="verbose", default=True, | 130 | action="store_true", dest="verbose", default=True, |
| 129 | help="make lots of noise [default]") | 131 | help="make lots of noise [default]") |
| 130 | parser.add_option("-q","--quiet", | 132 | parser.add_argument("-q","--quiet", |
| 131 | action="store_false", dest="verbose", default=True, | 133 | action="store_false", dest="verbose", default=True, |
| 132 | help="be quiet") | 134 | help="be quiet") |
| 133 | (options, args) = parser.parse_args() | 135 | args = parser.parse_args() |
| 134 | if not options.source_file: | 136 | if not args.source_file and not args.source_file2: |
| 135 | if len(args) == 1: | 137 | sys.stderr.write("Error: no file name given\n") |
| 136 | options.source_file = args[0] | 138 | parser.print_help() |
| 137 | else: | 139 | sys.exit(1) |
| 138 | print ("no file name given\n" + usage) | 140 | elif args.source_file2 is not None: |
| 139 | sys.exit(1) | 141 | args.source_file = args.source_file2 |
| 140 | return options, args | 142 | return args |
| 141 | 143 | ||
| 142 | def main(): | 144 | def main(): |
| 143 | options, args = parse_args() | 145 | options = parse_args() |
| 144 | 146 | ||
| 147 | source_file = options.source_file | ||
| 145 | hopsize = options.hopsize | 148 | hopsize = options.hopsize |
| 146 | bufsize = options.bufsize | 149 | bufsize = options.bufsize |
| 147 | samplerate = options.samplerate | 150 | samplerate = options.samplerate |