[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, |
---|
[c9c012e] | 9 | output_dir=None, samplerate=0, hopsize=256, |
---|
| 10 | create_first=False): |
---|
[5c6a8587] | 11 | """Slice a sound file at given timestamps. |
---|
| 12 | |
---|
| 13 | This function reads `source_file` and creates slices, new smaller |
---|
| 14 | files each starting at `t` in `timestamps`, a list of integer |
---|
| 15 | corresponding to time locations in `source_file`, in samples. |
---|
| 16 | |
---|
| 17 | If `timestamps_end` is unspecified, the slices will end at |
---|
| 18 | `timestamps_end[n] = timestamps[n+1]-1`, or the end of file. |
---|
| 19 | Otherwise, `timestamps_end` should be a list with the same length |
---|
| 20 | as `timestamps` containing the locations of the end of each slice. |
---|
| 21 | |
---|
| 22 | If `output_dir` is unspecified, the new slices will be written in |
---|
| 23 | the current directory. If `output_dir` is a string, new slices |
---|
| 24 | will be written in `output_dir`, after creating the directory if |
---|
| 25 | required. |
---|
| 26 | |
---|
| 27 | The default `samplerate` is 0, meaning the original sampling rate |
---|
| 28 | of `source_file` will be used. When using a sampling rate |
---|
| 29 | different to the one of the original files, `timestamps` and |
---|
[97a8bef] | 30 | `timestamps_end` should be expressed in the re-sampled signal. |
---|
[5c6a8587] | 31 | |
---|
| 32 | The `hopsize` parameter simply tells :class:`source` to use this |
---|
| 33 | hopsize and does not change the output slices. |
---|
| 34 | |
---|
| 35 | If `create_first` is True and `timestamps` does not start with `0`, the |
---|
| 36 | first slice from `0` to `timestamps[0] - 1` will be automatically added. |
---|
| 37 | |
---|
| 38 | Parameters |
---|
| 39 | ---------- |
---|
| 40 | source_file : str |
---|
| 41 | path of the resource to slice |
---|
| 42 | timestamps : :obj:`list` of :obj:`int` |
---|
| 43 | time stamps at which to slice, in samples |
---|
| 44 | timestamps_end : :obj:`list` of :obj:`int` (optional) |
---|
| 45 | time stamps at which to end the slices |
---|
| 46 | output_dir : str (optional) |
---|
| 47 | output directory to write the slices to |
---|
| 48 | samplerate : int (optional) |
---|
| 49 | samplerate to read the file at |
---|
| 50 | hopsize : int (optional) |
---|
| 51 | number of samples read from source per iteration |
---|
| 52 | create_first : bool (optional) |
---|
| 53 | always create the slice at the start of the file |
---|
| 54 | |
---|
| 55 | Examples |
---|
| 56 | -------- |
---|
[f9400d0] | 57 | Create two slices: the first slice starts at the beginning of the |
---|
| 58 | input file `loop.wav` and lasts exactly one second, starting at |
---|
| 59 | sample `0` and ending at sample `44099`; the second slice starts |
---|
| 60 | at sample `44100` and lasts until the end of the input file: |
---|
[5c6a8587] | 61 | |
---|
| 62 | >>> aubio.slice_source_at_stamps('loop.wav', [0, 44100]) |
---|
| 63 | |
---|
| 64 | Create one slice, from 1 second to 2 seconds: |
---|
| 65 | |
---|
[f9400d0] | 66 | >>> aubio.slice_source_at_stamps('loop.wav', [44100], [44100 * 2 - 1]) |
---|
[97a8bef] | 67 | |
---|
| 68 | Notes |
---|
| 69 | ----- |
---|
| 70 | Slices may be overlapping. If `timestamps_end` is `1` element |
---|
| 71 | shorter than `timestamps`, the last slice will end at the end of |
---|
| 72 | the file. |
---|
[5c6a8587] | 73 | """ |
---|
[88432a9] | 74 | |
---|
[8b56b18] | 75 | if timestamps is None or len(timestamps) == 0: |
---|
| 76 | raise ValueError("no timestamps given") |
---|
[aee840b] | 77 | |
---|
[c9c012e] | 78 | if timestamps[0] != 0 and create_first: |
---|
[f36ecea] | 79 | timestamps = [0] + timestamps |
---|
[dc654f8] | 80 | if timestamps_end is not None: |
---|
[35a44e9] | 81 | timestamps_end = [timestamps[1] - 1] + timestamps_end |
---|
[f36ecea] | 82 | |
---|
[dc654f8] | 83 | if timestamps_end is not None: |
---|
[a88594d] | 84 | if len(timestamps_end) == len(timestamps) - 1: |
---|
| 85 | timestamps_end = timestamps_end + [_max_timestamp] |
---|
| 86 | elif len(timestamps_end) != len(timestamps): |
---|
[8b56b18] | 87 | raise ValueError("len(timestamps_end) != len(timestamps)") |
---|
[f36ecea] | 88 | else: |
---|
[8b56b18] | 89 | timestamps_end = [t - 1 for t in timestamps[1:]] + [_max_timestamp] |
---|
[aee840b] | 90 | |
---|
[0e59ae0] | 91 | regions = list(zip(timestamps, timestamps_end)) |
---|
[4320679] | 92 | #print regions |
---|
| 93 | |
---|
[8b56b18] | 94 | source_base_name, _ = os.path.splitext(os.path.basename(source_file)) |
---|
[dc654f8] | 95 | if output_dir is not None: |
---|
[88432a9] | 96 | if not os.path.isdir(output_dir): |
---|
| 97 | os.makedirs(output_dir) |
---|
| 98 | source_base_name = os.path.join(output_dir, source_base_name) |
---|
| 99 | |
---|
[f36ecea] | 100 | def new_sink_name(source_base_name, timestamp, samplerate): |
---|
[8b56b18] | 101 | """ create a sink based on a timestamp in samples, converted in seconds """ |
---|
[f36ecea] | 102 | timestamp_seconds = timestamp / float(samplerate) |
---|
[6fbee46] | 103 | return source_base_name + "_%011.6f" % timestamp_seconds + '.wav' |
---|
[88432a9] | 104 | |
---|
[8b56b18] | 105 | # open source file |
---|
| 106 | _source = source(source_file, samplerate, hopsize) |
---|
[1b62ee9] | 107 | samplerate = _source.samplerate |
---|
[f36ecea] | 108 | |
---|
[4320679] | 109 | total_frames = 0 |
---|
| 110 | slices = [] |
---|
[88432a9] | 111 | |
---|
| 112 | while True: |
---|
| 113 | # get hopsize new samples from source |
---|
[8b56b18] | 114 | vec, read = _source.do_multi() |
---|
[4320679] | 115 | # if the total number of frames read will exceed the next region start |
---|
[60c8a73] | 116 | while len(regions) and total_frames + read >= regions[0][0]: |
---|
[4320679] | 117 | #print "getting", regions[0], "at", total_frames |
---|
| 118 | # get next region |
---|
| 119 | start_stamp, end_stamp = regions.pop(0) |
---|
| 120 | # create a name for the sink |
---|
[f36ecea] | 121 | new_sink_path = new_sink_name(source_base_name, start_stamp, samplerate) |
---|
[4320679] | 122 | # create its sink |
---|
[8b56b18] | 123 | _sink = sink(new_sink_path, samplerate, _source.channels) |
---|
[4320679] | 124 | # create a dictionary containing all this |
---|
[8b56b18] | 125 | new_slice = {'start_stamp': start_stamp, 'end_stamp': end_stamp, 'sink': _sink} |
---|
[4320679] | 126 | # append the dictionary to the current list of slices |
---|
| 127 | slices.append(new_slice) |
---|
| 128 | |
---|
| 129 | for current_slice in slices: |
---|
| 130 | start_stamp = current_slice['start_stamp'] |
---|
| 131 | end_stamp = current_slice['end_stamp'] |
---|
[8b56b18] | 132 | _sink = current_slice['sink'] |
---|
[4320679] | 133 | # sample index to start writing from new source vector |
---|
| 134 | start = max(start_stamp - total_frames, 0) |
---|
| 135 | # number of samples yet to written be until end of region |
---|
| 136 | remaining = end_stamp - total_frames + 1 |
---|
| 137 | #print current_slice, remaining, start |
---|
| 138 | # not enough frames remaining, time to split |
---|
| 139 | if remaining < read: |
---|
| 140 | if remaining > start: |
---|
| 141 | # write remaining samples from current region |
---|
[8b56b18] | 142 | _sink.do_multi(vec[:, start:remaining], remaining - start) |
---|
[c9c012e] | 143 | #print("closing region", "remaining", remaining) |
---|
[4320679] | 144 | # close this file |
---|
[8b56b18] | 145 | _sink.close() |
---|
[4320679] | 146 | elif read > start: |
---|
| 147 | # write all the samples |
---|
[8b56b18] | 148 | _sink.do_multi(vec[:, start:read], read - start) |
---|
[88432a9] | 149 | total_frames += read |
---|
[04b31af] | 150 | # remove old slices |
---|
| 151 | slices = list(filter(lambda s: s['end_stamp'] > total_frames, |
---|
| 152 | slices)) |
---|
[8b56b18] | 153 | if read < hopsize: |
---|
| 154 | break |
---|