Showing the changes to python (1 of 1 file). Show all
Unified · Split
| @@ -5,83 +5,86 @@ · 4 unmodified lines | |||
| 5 | """ | 5 | """ |
| 6 | 6 | ||
| 7 | import sys | 7 | import sys |
| 8 | import warnings | ||
| 8 | from aubio.cmd import AubioArgumentParser | 9 | from aubio.cmd import AubioArgumentParser |
| 10 | from aubio.slicing import slice_source_at_stamps | ||
| 11 | |||
| 9 | 12 | ||
| 10 | def aubio_cut_parser(): | 13 | def aubio_cut_parser(): |
| 11 | parser = AubioArgumentParser() | 14 | parser = AubioArgumentParser() |
| 12 | parser.add_input() | 15 | parser.add_input() |
| 13 | parser.add_argument("-O","--onset-method", | 16 | parser.add_argument("-O", "--onset-method", |
| 14 | action="store", dest="onset_method", default='default', | 17 | action="store", dest="onset_method", default='default', |
| 15 | metavar = "<onset_method>", | 18 | metavar = "<onset_method>", |
| 16 | help="onset detection method [default=default] \ | 19 | help="onset detection method [default=default] \ |
| 17 | complexdomain|hfc|phase|specdiff|energy|kl|mkl") | 20 | complexdomain|hfc|phase|specdiff|energy|kl|mkl") |
| 18 | # cutting methods | 21 | # cutting methods |
| 19 | parser.add_argument("-b","--beat", | 22 | parser.add_argument("-b", "--beat", |
| 20 | action="store_true", dest="beat", default=False, | 23 | action="store_true", dest="beat", default=False, |
| 21 | help="slice at beat locations") | 24 | help="slice at beat locations") |
| 22 | """ | 25 | """ |
| 23 | parser.add_argument("-S","--silencecut", | 26 | parser.add_argument("-S", "--silencecut", |
| 24 | action="store_true", dest="silencecut", default=False, | 27 | action="store_true", dest="silencecut", default=False, |
| 25 | help="use silence locations") | 28 | help="use silence locations") |
| 26 | parser.add_argument("-s","--silence", | 29 | parser.add_argument("-s", "--silence", |
| 27 | metavar = "<value>", | 30 | metavar = "<value>", |
| 28 | action="store", dest="silence", default=-70, | 31 | action="store", dest="silence", default=-70, |
| 29 | help="silence threshold [default=-70]") | 32 | help="silence threshold [default=-70]") |
| 30 | """ | 33 | """ |
| 31 | # algorithm parameters | 34 | # algorithm parameters |
| 32 | parser.add_buf_hop_size() | 35 | parser.add_buf_hop_size() |
| 33 | parser.add_argument("-t","--threshold", "--onset-threshold", | 36 | parser.add_argument("-t", "--threshold", "--onset-threshold", |
| 34 | metavar = "<threshold>", type=float, | 37 | metavar = "<threshold>", type=float, |
| 35 | action="store", dest="threshold", default=0.3, | 38 | action="store", dest="threshold", default=0.3, |
| 36 | help="onset peak picking threshold [default=0.3]") | 39 | help="onset peak picking threshold [default=0.3]") |
| 37 | parser.add_argument("-c","--cut", | 40 | parser.add_argument("-c", "--cut", |
| 38 | action="store_true", dest="cut", default=False, | 41 | action="store_true", dest="cut", default=False, |
| 39 | help="cut input sound file at detected labels") | 42 | help="cut input sound file at detected labels") |
| 40 | parser.add_minioi() | 43 | parser.add_minioi() |
| 41 | 44 | ||
| 42 | """ | 45 | """ |
| 43 | parser.add_argument("-D","--delay", | 46 | parser.add_argument("-D", "--delay", |
| 44 | action = "store", dest = "delay", type = float, | 47 | action = "store", dest = "delay", type = float, |
| 45 | metavar = "<seconds>", default=0, | 48 | metavar = "<seconds>", default=0, |
| 46 | help="number of seconds to take back [default=system]\ | 49 | help="number of seconds to take back [default=system]\ |
| 47 | default system delay is 3*hopsize/samplerate") | 50 | default system delay is 3*hopsize/samplerate") |
| 48 | parser.add_argument("-C","--dcthreshold", | 51 | parser.add_argument("-C", "--dcthreshold", |
| 49 | metavar = "<value>", | 52 | metavar = "<value>", |
| 50 | action="store", dest="dcthreshold", default=1., | 53 | action="store", dest="dcthreshold", default=1., |
| 51 | help="onset peak picking DC component [default=1.]") | 54 | help="onset peak picking DC component [default=1.]") |
| 52 | parser.add_argument("-L","--localmin", | 55 | parser.add_argument("-L", "--localmin", |
| 53 | action="store_true", dest="localmin", default=False, | 56 | action="store_true", dest="localmin", default=False, |
| 54 | help="use local minima after peak detection") | 57 | help="use local minima after peak detection") |
| 55 | parser.add_argument("-d","--derivate", | 58 | parser.add_argument("-d", "--derivate", |
| 56 | action="store_true", dest="derivate", default=False, | 59 | action="store_true", dest="derivate", default=False, |
| 57 | help="derivate onset detection function") | 60 | help="derivate onset detection function") |
| 58 | parser.add_argument("-z","--zerocross", | 61 | parser.add_argument("-z", "--zerocross", |
| 59 | metavar = "<value>", | 62 | metavar = "<value>", |
| 60 | action="store", dest="zerothres", default=0.008, | 63 | action="store", dest="zerothres", default=0.008, |
| 61 | help="zero-crossing threshold for slicing [default=0.00008]") | 64 | help="zero-crossing threshold for slicing [default=0.00008]") |
| 62 | # plotting functions | 65 | # plotting functions |
| 63 | parser.add_argument("-p","--plot", | 66 | parser.add_argument("-p", "--plot", |
| 64 | action="store_true", dest="plot", default=False, | 67 | action="store_true", dest="plot", default=False, |
| 65 | help="draw plot") | 68 | help="draw plot") |
| 66 | parser.add_argument("-x","--xsize", | 69 | parser.add_argument("-x", "--xsize", |
| 67 | metavar = "<size>", | 70 | metavar = "<size>", |
| 68 | action="store", dest="xsize", default=1., | 71 | action="store", dest="xsize", default=1., |
| 69 | type=float, help="define xsize for plot") | 72 | type=float, help="define xsize for plot") |
| 70 | parser.add_argument("-y","--ysize", | 73 | parser.add_argument("-y", "--ysize", |
| 71 | metavar = "<size>", | 74 | metavar = "<size>", |
| 72 | action="store", dest="ysize", default=1., | 75 | action="store", dest="ysize", default=1., |
| 73 | type=float, help="define ysize for plot") | 76 | type=float, help="define ysize for plot") |
| 74 | parser.add_argument("-f","--function", | 77 | parser.add_argument("-f", "--function", |
| 75 | action="store_true", dest="func", default=False, | 78 | action="store_true", dest="func", default=False, |
| 76 | help="print detection function") | 79 | help="print detection function") |
| 77 | parser.add_argument("-n","--no-onsets", | 80 | parser.add_argument("-n", "--no-onsets", |
| 78 | action="store_true", dest="nplot", default=False, | 81 | action="store_true", dest="nplot", default=False, |
| 79 | help="do not plot detected onsets") | 82 | help="do not plot detected onsets") |
| 80 | parser.add_argument("-O","--outplot", | 83 | parser.add_argument("-O", "--outplot", |
| 81 | metavar = "<output_image>", | 84 | metavar = "<output_image>", |
| 82 | action="store", dest="outplot", default=None, | 85 | action="store", dest="outplot", default=None, |
| 83 | help="save plot to output.{ps,png}") | 86 | help="save plot to output.{ps,png}") |
| 84 | parser.add_argument("-F","--spectrogram", | 87 | parser.add_argument("-F", "--spectrogram", |
| 85 | action="store_true", dest="spectro", default=False, | 88 | action="store_true", dest="spectro", default=False, |
| 86 | help="add spectrogram to the plot") | 89 | help="add spectrogram to the plot") |
| 87 | """ | 90 | """ |
| @@ -105,9 +108,11 @@ def _cut_analyze(options): · 17 unmodified lines | |||
| 105 | options.samplerate = samplerate | 108 | options.samplerate = samplerate |
| 106 | 109 | ||
| 107 | if options.beat: | 110 | if options.beat: |
| 108 | o = tempo(options.onset_method, bufsize, hopsize, samplerate=samplerate) | 111 | o = tempo(options.onset_method, bufsize, hopsize, |
| 112 | samplerate=samplerate) | ||
| 109 | else: | 113 | else: |
| 110 | o = onset(options.onset_method, bufsize, hopsize, samplerate=samplerate) | 114 | o = onset(options.onset_method, bufsize, hopsize, |
| 115 | samplerate=samplerate) | ||
| 111 | if options.minioi: | 116 | if options.minioi: |
| 112 | if options.minioi.endswith('ms'): | 117 | if options.minioi.endswith('ms'): |
| 113 | o.set_minioi_ms(int(options.minioi[:-2])) | 118 | o.set_minioi_ms(int(options.minioi[:-2])) |
| @@ -122,31 +127,36 @@ def _cut_analyze(options): · 8 unmodified lines | |||
| 122 | while True: | 127 | while True: |
| 123 | samples, read = s() | 128 | samples, read = s() |
| 124 | if o(samples): | 129 | if o(samples): |
| 125 | timestamps.append (o.get_last()) | 130 | timestamps.append(o.get_last()) |
| 126 | if options.verbose: print ("%.4f" % o.get_last_s()) | 131 | if options.verbose: |
| 132 | print("%.4f" % o.get_last_s()) | ||
| 127 | total_frames += read | 133 | total_frames += read |
| 128 | if read < hopsize: break | 134 | if read < hopsize: |
| 135 | break | ||
| 129 | del s | 136 | del s |
| 130 | return timestamps, total_frames | 137 | return timestamps, total_frames |
| 131 | 138 | ||
| 139 | |||
| 132 | def _cut_slice(options, timestamps): | 140 | def _cut_slice(options, timestamps): |
| 133 | # cutting pass | 141 | # cutting pass |
| 134 | nstamps = len(timestamps) | 142 | nstamps = len(timestamps) |
| 135 | if nstamps > 0: | 143 | if nstamps > 0: |
| 136 | # generate output files | 144 | # generate output files |
| 137 | from aubio.slicing import slice_source_at_stamps | ||
| 138 | timestamps_end = None | 145 | timestamps_end = None |
| 139 | if options.cut_every_nslices: | 146 | if options.cut_every_nslices: |
| 140 | timestamps = timestamps[::options.cut_every_nslices] | 147 | timestamps = timestamps[::options.cut_every_nslices] |
| 141 | nstamps = len(timestamps) | 148 | nstamps = len(timestamps) |
| 142 | if options.cut_until_nslices and options.cut_until_nsamples: | 149 | if options.cut_until_nslices and options.cut_until_nsamples: |
| 143 | print ("warning: using cut_until_nslices, but cut_until_nsamples is set") | 150 | msg = "using cut_until_nslices, but cut_until_nsamples is set" |
| 151 | warnings.warn(msg) | ||
| 144 | if options.cut_until_nsamples: | 152 | if options.cut_until_nsamples: |
| 145 | timestamps_end = [t + options.cut_until_nsamples for t in timestamps[1:]] | 153 | lag = options.cut_until_nsamples |
| 146 | timestamps_end += [ 1e120 ] | 154 | timestamps_end = [t + lag for t in timestamps[1:]] |
| 155 | timestamps_end += [1e120] | ||
| 147 | if options.cut_until_nslices: | 156 | if options.cut_until_nslices: |
| 148 | timestamps_end = [t for t in timestamps[1 + options.cut_until_nslices:]] | 157 | slice_lag = options.cut_until_nslices |
| 149 | timestamps_end += [ 1e120 ] * (options.cut_until_nslices + 1) | 158 | timestamps_end = [t for t in timestamps[1 + slice_lag:]] |
| 159 | timestamps_end += [1e120] * (options.cut_until_nslices + 1) | ||
| 150 | slice_source_at_stamps(options.source_uri, | 160 | slice_source_at_stamps(options.source_uri, |
| 151 | timestamps, timestamps_end = timestamps_end, | 161 | timestamps, timestamps_end = timestamps_end, |
| 152 | output_dir = options.output_directory, | 162 | output_dir = options.output_directory, |
| @@ -167,7 +177,7 @@ def main(): · 14 unmodified lines | |||
| 167 | timestamps, total_frames = _cut_analyze(options) | 177 | timestamps, total_frames = _cut_analyze(options) |
| 168 | 178 | ||
| 169 | # print some info | 179 | # print some info |
| 170 | duration = float (total_frames) / float(options.samplerate) | 180 | duration = float(total_frames) / float(options.samplerate) |
| 171 | base_info = '%(source_uri)s' % {'source_uri': options.source_uri} | 181 | base_info = '%(source_uri)s' % {'source_uri': options.source_uri} |
| 172 | base_info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % \ | 182 | base_info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % \ |
| 173 | {'duration': duration, 'samplerate': options.samplerate} | 183 | {'duration': duration, 'samplerate': options.samplerate} |