[88432a9] | 1 | from aubio import source, sink |
---|
| 2 | import os |
---|
| 3 | |
---|
[f36ecea] | 4 | max_timestamp = 1e120 |
---|
| 5 | |
---|
[88432a9] | 6 | def slice_source_at_stamps(source_file, timestamps, timestamps_end = None, |
---|
| 7 | output_dir = None, |
---|
| 8 | samplerate = 0, |
---|
| 9 | hopsize = 256): |
---|
| 10 | |
---|
[aee840b] | 11 | if timestamps == None or len(timestamps) == 0: |
---|
| 12 | raise ValueError ("no timestamps given") |
---|
| 13 | |
---|
[f36ecea] | 14 | if timestamps[0] != 0: |
---|
| 15 | timestamps = [0] + timestamps |
---|
| 16 | |
---|
[4320679] | 17 | if timestamps_end != None: |
---|
| 18 | if len(timestamps_end) != len(timestamps): |
---|
| 19 | raise ValueError ("len(timestamps_end) != len(timestamps)") |
---|
[f36ecea] | 20 | else: |
---|
| 21 | timestamps_end = [t - 1 for t in timestamps[1:] ] + [ max_timestamp ] |
---|
[aee840b] | 22 | |
---|
[4320679] | 23 | regions = zip(timestamps, timestamps_end) |
---|
| 24 | #print regions |
---|
| 25 | |
---|
[88432a9] | 26 | source_base_name, source_ext = os.path.splitext(os.path.basename(source_file)) |
---|
| 27 | if output_dir != None: |
---|
| 28 | if not os.path.isdir(output_dir): |
---|
| 29 | os.makedirs(output_dir) |
---|
| 30 | source_base_name = os.path.join(output_dir, source_base_name) |
---|
| 31 | |
---|
[f36ecea] | 32 | def new_sink_name(source_base_name, timestamp, samplerate): |
---|
| 33 | timestamp_seconds = timestamp / float(samplerate) |
---|
[6fbee46] | 34 | return source_base_name + "_%011.6f" % timestamp_seconds + '.wav' |
---|
[88432a9] | 35 | |
---|
| 36 | # reopen source file |
---|
| 37 | s = source(source_file, samplerate, hopsize) |
---|
[4320679] | 38 | samplerate = s.get_samplerate() |
---|
[f36ecea] | 39 | |
---|
[4320679] | 40 | total_frames = 0 |
---|
| 41 | slices = [] |
---|
[88432a9] | 42 | |
---|
| 43 | while True: |
---|
| 44 | # get hopsize new samples from source |
---|
| 45 | vec, read = s() |
---|
[4320679] | 46 | # if the total number of frames read will exceed the next region start |
---|
| 47 | if len(regions) and total_frames + read >= regions[0][0]: |
---|
| 48 | #print "getting", regions[0], "at", total_frames |
---|
| 49 | # get next region |
---|
| 50 | start_stamp, end_stamp = regions.pop(0) |
---|
| 51 | # create a name for the sink |
---|
[f36ecea] | 52 | new_sink_path = new_sink_name(source_base_name, start_stamp, samplerate) |
---|
[4320679] | 53 | # create its sink |
---|
[88432a9] | 54 | g = sink(new_sink_path, samplerate) |
---|
[4320679] | 55 | # create a dictionary containing all this |
---|
| 56 | new_slice = {'start_stamp': start_stamp, 'end_stamp': end_stamp, 'sink': g} |
---|
| 57 | # append the dictionary to the current list of slices |
---|
| 58 | slices.append(new_slice) |
---|
| 59 | |
---|
| 60 | for current_slice in slices: |
---|
| 61 | start_stamp = current_slice['start_stamp'] |
---|
| 62 | end_stamp = current_slice['end_stamp'] |
---|
| 63 | g = current_slice['sink'] |
---|
| 64 | # sample index to start writing from new source vector |
---|
| 65 | start = max(start_stamp - total_frames, 0) |
---|
| 66 | # number of samples yet to written be until end of region |
---|
| 67 | remaining = end_stamp - total_frames + 1 |
---|
| 68 | #print current_slice, remaining, start |
---|
| 69 | # not enough frames remaining, time to split |
---|
| 70 | if remaining < read: |
---|
| 71 | if remaining > start: |
---|
| 72 | # write remaining samples from current region |
---|
| 73 | g(vec[start:remaining], remaining - start) |
---|
| 74 | #print "closing region", "remaining", remaining |
---|
| 75 | # close this file |
---|
| 76 | del g |
---|
| 77 | elif read > start: |
---|
| 78 | # write all the samples |
---|
| 79 | g(vec[start:read], read - start) |
---|
[88432a9] | 80 | total_frames += read |
---|
| 81 | if read < hopsize: break |
---|
| 82 | |
---|
[6f27719] | 83 | # close the last file |
---|
[88432a9] | 84 | del g |
---|