Showing the changes to python/lib (1 of 1 file). Show all
Unified · Split
| @@ -7,7 +7,7 @@ · 6 unmodified lines | ||
| 7 | 7 | import sys |
| 8 | 8 | import argparse |
| 9 | 9 | |
| 10 | −def parse_args(): | |
| 10 | +def aubio_cut_parser(): | |
| 11 | 11 | usage = "usage: %s [options] -i soundfile" % sys.argv[0] |
| 12 | 12 | usage += "\n help: %s -h" % sys.argv[0] |
| 13 | 13 | parser = argparse.ArgumentParser() |
| @@ -132,28 +132,23 @@ def parse_args(): · 118 unmodified lines | ||
| 132 | 132 | parser.add_argument("-q","--quiet", |
| 133 | 133 | action="store_false", dest="verbose", default=True, |
| 134 | 134 | help="be quiet") |
| 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 | |
| 135 | + return parser | |
| 143 | 136 | |
| 144 | −def main(): | |
| 145 | − options = parse_args() | |
| 146 | 137 | |
| 138 | +def _cut_analyze(options): | |
| 147 | 139 | source_file = options.source_file |
| 148 | 140 | hopsize = options.hopsize |
| 149 | 141 | bufsize = options.bufsize |
| 150 | 142 | samplerate = options.samplerate |
| 151 | 143 | source_file = options.source_file |
| 152 | 144 | |
| 145 | + # analyze pass | |
| 153 | 146 | from aubio import onset, tempo, source |
| 154 | 147 | |
| 155 | 148 | s = source(source_file, samplerate, hopsize) |
| 156 | − if samplerate == 0: samplerate = s.get_samplerate() | |
| 149 | + if samplerate == 0: | |
| 150 | + samplerate = s.get_samplerate() | |
| 151 | + options.samplerate = samplerate | |
| 157 | 152 | |
| 158 | 153 | if options.beat: |
| 159 | 154 | o = tempo(options.onset_method, bufsize, hopsize, samplerate=samplerate) |
| @@ -170,7 +165,6 @@ def main(): · 10 unmodified lines | ||
| 170 | 165 | |
| 171 | 166 | timestamps = [] |
| 172 | 167 | total_frames = 0 |
| 173 | − # analyze pass | |
| 174 | 168 | while True: |
| 175 | 169 | samples, read = s() |
| 176 | 170 | if o(samples): |
| @@ -179,15 +173,12 @@ def main(): · 2 unmodified lines | ||
| 179 | 173 | total_frames += read |
| 180 | 174 | if read < hopsize: break |
| 181 | 175 | del s |
| 182 | − # print some info | |
| 183 | − nstamps = len(timestamps) | |
| 184 | − duration = float (total_frames) / float(samplerate) | |
| 185 | − info = 'found %(nstamps)d timestamps in %(source_file)s' % locals() | |
| 186 | − info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % locals() | |
| 187 | − sys.stderr.write(info) | |
| 176 | + return timestamps, total_frames | |
| 188 | 177 | |
| 178 | +def _cut_slice(options, timestamps): | |
| 189 | 179 | # cutting pass |
| 190 | − if options.cut and nstamps > 0: | |
| 180 | + nstamps = len(timestamps) | |
| 181 | + if nstamps > 0: | |
| 191 | 182 | # generate output files |
| 192 | 183 | from aubio.slicing import slice_source_at_stamps |
| 193 | 184 | timestamps_end = None |
| @@ -202,12 +193,36 @@ def main(): · 8 unmodified lines | ||
| 202 | 193 | if options.cut_until_nslices: |
| 203 | 194 | timestamps_end = [t for t in timestamps[1 + options.cut_until_nslices:]] |
| 204 | 195 | timestamps_end += [ 1e120 ] * (options.cut_until_nslices + 1) |
| 205 | − slice_source_at_stamps(source_file, timestamps, timestamps_end = timestamps_end, | |
| 196 | + slice_source_at_stamps(options.source_file, | |
| 197 | + timestamps, timestamps_end = timestamps_end, | |
| 206 | 198 | output_dir = options.output_directory, |
| 207 | − samplerate = samplerate) | |
| 199 | + samplerate = options.samplerate) | |
| 200 | + | |
| 201 | +def main(): | |
| 202 | + parser = aubio_cut_parser() | |
| 203 | + options = parser.parse_args() | |
| 204 | + if not options.source_file and not options.source_file2: | |
| 205 | + sys.stderr.write("Error: no file name given\n") | |
| 206 | + parser.print_help() | |
| 207 | + sys.exit(1) | |
| 208 | + elif options.source_file2 is not None: | |
| 209 | + options.source_file = options.source_file2 | |
| 210 | + | |
| 211 | + # analysis | |
| 212 | + timestamps, total_frames = _cut_analyze(options) | |
| 213 | + | |
| 214 | + # print some info | |
| 215 | + duration = float (total_frames) / float(options.samplerate) | |
| 216 | + base_info = '%(source_file)s' % {'source_file': options.source_file} | |
| 217 | + base_info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % \ | |
| 218 | + {'duration': duration, 'samplerate': options.samplerate} | |
| 219 | + | |
| 220 | + info = "found %d timestamps in " % len(timestamps) | |
| 221 | + info += base_info | |
| 222 | + sys.stderr.write(info) | |
| 208 | 223 | |
| 209 | − # print some info | |
| 210 | − duration = float (total_frames) / float(samplerate) | |
| 211 | − info = 'created %(nstamps)d slices from %(source_file)s' % locals() | |
| 212 | − info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % locals() | |
| 224 | + if options.cut: | |
| 225 | + _cut_slice(options, timestamps) | |
| 226 | + info = "created %d slices from " % len(timestamps) | |
| 227 | + info += base_info | |
| 213 | 228 | sys.stderr.write(info) |