Showing the changes to python/scripts (1 of 1 file). Show all
Unified · Split
| @@ -8,6 +8,7 @@ import sys · 7 unmodified lines | |||
| 8 | #from aubio.task import * | 8 | #from aubio.task import * |
| 9 | 9 | ||
| 10 | usage = "usage: %s [options] -i soundfile" % sys.argv[0] | 10 | usage = "usage: %s [options] -i soundfile" % sys.argv[0] |
| 11 | usage += "\nhelp: %s -h" % sys.argv[0] | ||
| 11 | 12 | ||
| 12 | def parse_args(): | 13 | def parse_args(): |
| 13 | from optparse import OptionParser | 14 | from optparse import OptionParser |
| @@ -133,31 +134,64 @@ if __name__ == '__main__': · 119 unmodified lines | |||
| 133 | o = onset(options.onset_method, bufsize, hopsize) | 134 | o = onset(options.onset_method, bufsize, hopsize) |
| 134 | o.set_threshold(options.threshold) | 135 | o.set_threshold(options.threshold) |
| 135 | 136 | ||
| 136 | slice_number = 0 | ||
| 137 | #if options.cut: | ||
| 138 | this_slice = sink('/tmp/t-%02d.wav' % slice_number, samplerate) | ||
| 139 | |||
| 140 | timestamps = [] | 137 | timestamps = [] |
| 141 | block_read = 0 | 138 | total_frames = 0 |
| 139 | # analyze pass | ||
| 142 | while True: | 140 | while True: |
| 143 | samples, read = s() | 141 | samples, read = s() |
| 144 | #ring_buffer = hstack([ring_buffer, samples]) | 142 | if o(samples): |
| 145 | is_onset = o(samples) | 143 | this_onset = o.get_last_onset() |
| 146 | if is_onset: | 144 | if options.verbose: print "%.4f" % o.get_last_onset_s() |
| 147 | this_onset = (block_read - 4. + is_onset[0]) * hopsize | ||
| 148 | if options.verbose: | ||
| 149 | print "%.4f" % ( this_onset / samplerate ) | ||
| 150 | timestamps.append (this_onset) | 145 | timestamps.append (this_onset) |
| 151 | del this_slice | 146 | total_frames += read |
| 152 | slice_number += 1 | ||
| 153 | this_slice = sink('/tmp/t-%02d.wav' % slice_number, samplerate) | ||
| 154 | this_slice(samples, read) | ||
| 155 | block_read += 1 | ||
| 156 | if read < hopsize: break | 147 | if read < hopsize: break |
| 157 | 148 | ||
| 158 | # print some info | 149 | # print some info |
| 159 | duration = float ( block_read * hopsize + read ) / samplerate | ||
| 160 | nstamps = len(timestamps) | 150 | nstamps = len(timestamps) |
| 151 | duration = float (total_frames) / float(samplerate) | ||
| 161 | info = 'found %(nstamps)d timestamps in %(source_file)s' % locals() | 152 | info = 'found %(nstamps)d timestamps in %(source_file)s' % locals() |
| 162 | info += ' (read %(duration).2fs at %(samplerate)dHz)\n' % locals() | 153 | info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % locals() |
| 163 | sys.stderr.write(info) | 154 | sys.stderr.write(info) |
| 155 | |||
| 156 | # cutting pass | ||
| 157 | if options.cut and nstamps > 0: | ||
| 158 | # generate output filenames | ||
| 159 | import os | ||
| 160 | source_base_name, source_ext = os.path.splitext(os.path.basename(source_file)) | ||
| 161 | def new_sink_name(source_base_name, timestamp): | ||
| 162 | return source_base_name + '_%02.3f' % (timestamp) + '.wav' | ||
| 163 | # reopen source file | ||
| 164 | del s | ||
| 165 | s = source(source_file, samplerate, hopsize) | ||
| 166 | # create first sink at 0 | ||
| 167 | g = sink(new_sink_name(source_base_name, 0.), samplerate) | ||
| 168 | total_frames = 0 | ||
| 169 | # get next region | ||
| 170 | next_onset = int(timestamps.pop(0)) | ||
| 171 | while True: | ||
| 172 | vec, read = s() | ||
| 173 | remaining = next_onset - total_frames | ||
| 174 | if remaining <= read: | ||
| 175 | # write remaining samples from current region | ||
| 176 | g(vec[0:remaining], remaining) | ||
| 177 | # close this file | ||
| 178 | del g | ||
| 179 | # create a new file for the new region | ||
| 180 | g = sink(new_sink_name(source_base_name, next_onset / float(samplerate)), samplerate) | ||
| 181 | # write the remaining samples in the new file | ||
| 182 | g(vec[remaining:read], read - remaining) | ||
| 183 | #print "new slice", total_frames_written, "+", remaining, "=", start_of_next_region | ||
| 184 | if len(timestamps): | ||
| 185 | next_onset = int(timestamps.pop(0)) | ||
| 186 | else: | ||
| 187 | next_onset = 1e120 | ||
| 188 | else: | ||
| 189 | g(vec[0:read], read) | ||
| 190 | total_frames += read | ||
| 191 | if read < hopsize: break | ||
| 192 | |||
| 193 | # print some info | ||
| 194 | duration = float (total_frames) / float(samplerate) | ||
| 195 | info = 'created %(nstamps)d slices from %(source_file)s' % locals() | ||
| 196 | info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % locals() | ||
| 197 | sys.stderr.write(info) | ||