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