| @@ -1,6 +1,8 @@ |
| 1 | from aubio import source, sink | 1 | from aubio import source, sink |
| 2 | import os | 2 | import os |
| 3 | | 3 | |
| | 4 | max_timestamp = 1e120 |
| | 5 | |
| 4 | def slice_source_at_stamps(source_file, timestamps, timestamps_end = None, | 6 | def slice_source_at_stamps(source_file, timestamps, timestamps_end = None, |
| 5 | output_dir = None, | 7 | output_dir = None, |
| 6 | samplerate = 0, | 8 | samplerate = 0, |
| @@ -9,8 +11,13 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end = None, · 2 unmodified lines |
| 9 | if timestamps == None or len(timestamps) == 0: | 11 | if timestamps == None or len(timestamps) == 0: |
| 10 | raise ValueError ("no timestamps given") | 12 | raise ValueError ("no timestamps given") |
| 11 | | 13 | |
| | 14 | if timestamps[0] != 0: |
| | 15 | timestamps = [0] + timestamps |
| | 16 | |
| 12 | if timestamps_end != None and len(timestamps_end) != len(timestamps): | 17 | if timestamps_end != None and len(timestamps_end) != len(timestamps): |
| 13 | raise ValueError ("len(timestamps_end) != len(timestamps)") | 18 | raise ValueError ("len(timestamps_end) != len(timestamps)") |
| | 19 | else: |
| | 20 | timestamps_end = [t - 1 for t in timestamps[1:] ] + [ max_timestamp ] |
| 14 | | 21 | |
| 15 | source_base_name, source_ext = os.path.splitext(os.path.basename(source_file)) | 22 | source_base_name, source_ext = os.path.splitext(os.path.basename(source_file)) |
| 16 | if output_dir != None: | 23 | if output_dir != None: |
| @@ -18,22 +25,29 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end = None, · 1 unmodified line |
| 18 | os.makedirs(output_dir) | 25 | os.makedirs(output_dir) |
| 19 | source_base_name = os.path.join(output_dir, source_base_name) | 26 | source_base_name = os.path.join(output_dir, source_base_name) |
| 20 | | 27 | |
| 21 | def new_sink_name(source_base_name, timestamp): | 28 | def new_sink_name(source_base_name, timestamp, samplerate): |
| 22 | return source_base_name + '_%02.3f' % (timestamp) + '.wav' | 29 | timestamp_seconds = timestamp / float(samplerate) |
| | 30 | #print source_base_name + '_%02.3f' % (timestamp_seconds) + '.wav' |
| | 31 | return source_base_name + '_%02.3f' % (timestamp_seconds) + '.wav' |
| 23 | | 32 | |
| 24 | # reopen source file | 33 | # reopen source file |
| 25 | s = source(source_file, samplerate, hopsize) | 34 | s = source(source_file, samplerate, hopsize) |
| 26 | if samplerate == 0: samplerate = s.get_samplerate() | 35 | if samplerate == 0: samplerate = s.get_samplerate() |
| 27 | # create first sink at 0 | | |
| 28 | g = sink(new_sink_name(source_base_name, 0.), samplerate) | | |
| 29 | total_frames = 0 | 36 | total_frames = 0 |
| 30 | # get next region | 37 | # get next region |
| 31 | next_stamp = int(timestamps.pop(0)) | 38 | start_stamp = int(timestamps.pop(0)) |
| | 39 | end_stamp = int(timestamps_end.pop(0)) |
| | 40 | |
| | 41 | # create first sink |
| | 42 | new_sink_path = new_sink_name(source_base_name, start_stamp, samplerate) |
| | 43 | #print "new slice", total_frames, "+", remaining, "=", end_stamp |
| | 44 | g = sink(new_sink_path, samplerate) |
| 32 | | 45 | |
| 33 | while True: | 46 | while True: |
| 34 | # get hopsize new samples from source | 47 | # get hopsize new samples from source |
| 35 | vec, read = s() | 48 | vec, read = s() |
| 36 | remaining = next_stamp - total_frames | 49 | # number of samples until end of region |
| | 50 | remaining = end_stamp - total_frames |
| 37 | # not enough frames remaining, time to split | 51 | # not enough frames remaining, time to split |
| 38 | if remaining < read: | 52 | if remaining < read: |
| 39 | if remaining != 0: | 53 | if remaining != 0: |
| @@ -41,17 +55,17 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end = None, · 1 unmodified line |
| 41 | g(vec[0:remaining], remaining) | 55 | g(vec[0:remaining], remaining) |
| 42 | # close this file | 56 | # close this file |
| 43 | del g | 57 | del g |
| | 58 | # get the next region |
| | 59 | start_stamp = int(timestamps.pop(0)) |
| | 60 | end_stamp = int(timestamps_end.pop(0)) |
| 44 | # create a new file for the new region | 61 | # create a new file for the new region |
| 45 | new_sink_path = new_sink_name(source_base_name, next_stamp / float(samplerate)) | 62 | new_sink_path = new_sink_name(source_base_name, start_stamp, samplerate) |
| 46 | #print "new slice", total_frames, "+", remaining, "=", next_stamp | 63 | #print "new slice", total_frames, "+", remaining, "=", end_stamp |
| 47 | g = sink(new_sink_path, samplerate) | 64 | g = sink(new_sink_path, samplerate) |
| 48 | # write the remaining samples in the new file | 65 | # write the remaining samples in the new file |
| 49 | g(vec[remaining:read], read - remaining) | 66 | g(vec[remaining:read], read - remaining) |
| 50 | if len(timestamps): | 67 | elif read > 0: |
| 51 | next_stamp = int(timestamps.pop(0)) | 68 | # write all the samples |
| 52 | else: | | |
| 53 | next_stamp = 1e120 | | |
| 54 | else: | | |
| 55 | g(vec[0:read], read) | 69 | g(vec[0:read], read) |
| 56 | total_frames += read | 70 | total_frames += read |
| 57 | if read < hopsize: break | 71 | if read < hopsize: break |