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