source: python/lib/aubio/slicing.py @ 0e59ae0

feature/autosinkfeature/constantqfeature/pitchshiftfeature/pydocstringsfeature/timestretchpitchshiftsamplertimestretchyinfft+
Last change on this file since 0e59ae0 was 0e59ae0, checked in by Paul Brossier <piem@piem.org>, 5 years ago

python/lib/aubio/slicing.py: make sure we get a list of regions (python3, see #33)

  • Property mode set to 100644
File size: 3.2 KB
Line 
1from aubio import source, sink
2import os
3
4max_timestamp = 1e120
5
6def slice_source_at_stamps(source_file, timestamps, timestamps_end = None,
7        output_dir = None,
8        samplerate = 0,
9        hopsize = 256):
10
11    if timestamps == None or len(timestamps) == 0:
12        raise ValueError ("no timestamps given")
13
14    if timestamps[0] != 0:
15        timestamps = [0] + timestamps
16        if timestamps_end != None:
17            timestamps_end = [timestamps[1] - 1] + timestamps_end
18
19    if timestamps_end != None:
20        if len(timestamps_end) != len(timestamps):
21            raise ValueError ("len(timestamps_end) != len(timestamps)")
22    else:
23        timestamps_end = [t - 1 for t in timestamps[1:] ] + [ max_timestamp ]
24
25    regions = list(zip(timestamps, timestamps_end))
26    #print regions
27
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
34    def new_sink_name(source_base_name, timestamp, samplerate):
35        timestamp_seconds = timestamp / float(samplerate)
36        return source_base_name + "_%011.6f" % timestamp_seconds + '.wav'
37
38    # reopen source file
39    s = source(source_file, samplerate, hopsize)
40    samplerate = s.get_samplerate()
41
42    total_frames = 0
43    slices = []
44
45    while True:
46        # get hopsize new samples from source
47        vec, read = s.do_multi()
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
54            new_sink_path = new_sink_name(source_base_name, start_stamp, samplerate)
55            # create its sink
56            g = sink(new_sink_path, samplerate, s.channels)
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
75                    g.do_multi(vec[:,start:remaining], remaining - start)
76                    #print "closing region", "remaining", remaining
77                    # close this file
78                    g.close()
79            elif read > start:
80                # write all the samples
81                g.do_multi(vec[:,start:read], read - start)
82        total_frames += read
83        if read < hopsize: break
Note: See TracBrowser for help on using the repository browser.