Showing the changes to python (1 of 1 file). Show all
Unified · Split
| @@ -10,19 +10,41 @@ if __name__ == '__main__': · 9 unmodified lines | |||
| 10 | sys.exit(1) | 10 | sys.exit(1) |
| 11 | source_file = sys.argv[1] | 11 | source_file = sys.argv[1] |
| 12 | duration = float(sys.argv[2]) | 12 | duration = float(sys.argv[2]) |
| 13 | sink_base, sink_ext = os.path.splitext(os.path.basename(source_file)) | 13 | source_base_name, source_ext = os.path.splitext(os.path.basename(source_file)) |
| 14 | slice_n, total_frames, read = 1, 0, 256 | 14 | |
| 15 | f = source(source_file, 0, 256) | 15 | hopsize = 256 |
| 16 | g = sink(sink_base + '-%02d' % slice_n + sink_ext, f.samplerate) | 16 | slice_n, total_frames_written, read = 0, 0, hopsize |
| 17 | while read: | 17 | |
| 18 | def new_sink_name(source_base_name, slice_n, duration = duration): | ||
| 19 | return source_base_name + '_%02.3f' % (slice_n*duration) + '.wav' | ||
| 20 | |||
| 21 | f = source(source_file, 0, hopsize) | ||
| 22 | samplerate = f.samplerate | ||
| 23 | g = sink(new_sink_name(source_base_name, slice_n), samplerate) | ||
| 24 | |||
| 25 | #print "new slice:", slice_n, 0, "+", 0, "=", 0 | ||
| 26 | while read == hopsize: | ||
| 18 | vec, read = f() | 27 | vec, read = f() |
| 19 | g(vec, read) | 28 | start_of_next_region = int(duration * samplerate * (slice_n + 1)) |
| 20 | total_frames += read | 29 | remaining = start_of_next_region - total_frames_written |
| 21 | if total_frames / float(f.samplerate) >= duration * slice_n: | 30 | # number of samples remaining is less than what we got |
| 22 | slice_n += 1 | 31 | if remaining <= read: |
| 32 | # write remaining samples from current region | ||
| 33 | g(vec[0:remaining], remaining) | ||
| 34 | # close this file | ||
| 23 | del g | 35 | del g |
| 24 | g = sink(sink_base + '-%02d' % slice_n + sink_ext, f.samplerate) | 36 | #print "new slice", slice_n, total_frames_written, "+", remaining, "=", start_of_next_region |
| 25 | total_duration = total_frames / float(f.samplerate) | 37 | slice_n += 1 |
| 26 | print 'created %(slice_n)d slices from %(source_file)s' % locals(), | 38 | # create a new file for the new region |
| 39 | g = sink(new_sink_name(source_base_name, slice_n), samplerate) | ||
| 40 | # write the remaining samples in the new file | ||
| 41 | g(vec[remaining:read], read - remaining) | ||
| 42 | else: | ||
| 43 | g(vec[0:read], read) | ||
| 44 | total_frames_written += read | ||
| 45 | total_duration = total_frames_written / float(samplerate) | ||
| 46 | slice_n += 1 | ||
| 47 | print 'created %(slice_n)s slices from %(source_base_name)s%(source_ext)s' % locals(), | ||
| 27 | print ' (total duration %(total_duration).2fs)' % locals() | 48 | print ' (total duration %(total_duration).2fs)' % locals() |
| 49 | # close source and sink files | ||
| 28 | del f, g | 50 | del f, g |