Showing the changes to python (1 of 1 file). Show all
Unified · Split
| @@ -7,7 +7,7 @@ · 6 unmodified lines | |||
| 7 | import sys | 7 | import sys |
| 8 | import argparse | 8 | import argparse |
| 9 | 9 | ||
| 10 | def parse_args(): | 10 | def aubio_cut_parser(): |
| 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 = argparse.ArgumentParser() | 13 | parser = argparse.ArgumentParser() |
| @@ -132,28 +132,23 @@ def parse_args(): · 118 unmodified lines | |||
| 132 | parser.add_argument("-q","--quiet", | 132 | parser.add_argument("-q","--quiet", |
| 133 | action="store_false", dest="verbose", default=True, | 133 | action="store_false", dest="verbose", default=True, |
| 134 | help="be quiet") | 134 | help="be quiet") |
| 135 | args = parser.parse_args() | 135 | return parser |
| 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 | ||
| 143 | 136 | ||
| 144 | def main(): | ||
| 145 | options = parse_args() | ||
| 146 | 137 | ||
| 138 | def _cut_analyze(options): | ||
| 147 | source_file = options.source_file | 139 | source_file = options.source_file |
| 148 | hopsize = options.hopsize | 140 | hopsize = options.hopsize |
| 149 | bufsize = options.bufsize | 141 | bufsize = options.bufsize |
| 150 | samplerate = options.samplerate | 142 | samplerate = options.samplerate |
| 151 | source_file = options.source_file | 143 | source_file = options.source_file |
| 152 | 144 | ||
| 145 | # analyze pass | ||
| 153 | from aubio import onset, tempo, source | 146 | from aubio import onset, tempo, source |
| 154 | 147 | ||
| 155 | s = source(source_file, samplerate, hopsize) | 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 | if options.beat: | 153 | if options.beat: |
| 159 | o = tempo(options.onset_method, bufsize, hopsize, samplerate=samplerate) | 154 | o = tempo(options.onset_method, bufsize, hopsize, samplerate=samplerate) |
| @@ -170,7 +165,6 @@ def main(): · 10 unmodified lines | |||
| 170 | 165 | ||
| 171 | timestamps = [] | 166 | timestamps = [] |
| 172 | total_frames = 0 | 167 | total_frames = 0 |
| 173 | # analyze pass | ||
| 174 | while True: | 168 | while True: |
| 175 | samples, read = s() | 169 | samples, read = s() |
| 176 | if o(samples): | 170 | if o(samples): |
| @@ -179,15 +173,12 @@ def main(): · 2 unmodified lines | |||
| 179 | total_frames += read | 173 | total_frames += read |
| 180 | if read < hopsize: break | 174 | if read < hopsize: break |
| 181 | del s | 175 | del s |
| 182 | # print some info | 176 | return timestamps, total_frames |
| 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) | ||
| 188 | 177 | ||
| 178 | def _cut_slice(options, timestamps): | ||
| 189 | # cutting pass | 179 | # cutting pass |
| 190 | if options.cut and nstamps > 0: | 180 | nstamps = len(timestamps) |
| 181 | if nstamps > 0: | ||
| 191 | # generate output files | 182 | # generate output files |
| 192 | from aubio.slicing import slice_source_at_stamps | 183 | from aubio.slicing import slice_source_at_stamps |
| 193 | timestamps_end = None | 184 | timestamps_end = None |
| @@ -202,12 +193,36 @@ def main(): · 8 unmodified lines | |||
| 202 | if options.cut_until_nslices: | 193 | if options.cut_until_nslices: |
| 203 | timestamps_end = [t for t in timestamps[1 + options.cut_until_nslices:]] | 194 | timestamps_end = [t for t in timestamps[1 + options.cut_until_nslices:]] |
| 204 | timestamps_end += [ 1e120 ] * (options.cut_until_nslices + 1) | 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 | output_dir = options.output_directory, | 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 | 224 | if options.cut: |
| 210 | duration = float (total_frames) / float(samplerate) | 225 | _cut_slice(options, timestamps) |
| 211 | info = 'created %(nstamps)d slices from %(source_file)s' % locals() | 226 | info = "created %d slices from " % len(timestamps) |
| 212 | info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % locals() | 227 | info += base_info |
| 213 | sys.stderr.write(info) | 228 | sys.stderr.write(info) |