source: python/lib/aubio/slicing.py @ 6fbee46

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 6fbee46 was 6fbee46, checked in by Paul Brossier <piem@piem.org>, 11 years ago

python/lib/aubio/slicing.py: fix zero padding in slice names

  • Property mode set to 100644
File size: 3.1 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
17    if timestamps_end != None:
18        if len(timestamps_end) != len(timestamps):
19            raise ValueError ("len(timestamps_end) != len(timestamps)")
20    else:
21        timestamps_end = [t - 1 for t in timestamps[1:] ] + [ max_timestamp ]
22
23    regions = zip(timestamps, timestamps_end)
24    #print regions
25
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
32    def new_sink_name(source_base_name, timestamp, samplerate):
33        timestamp_seconds = timestamp / float(samplerate)
34        return source_base_name + "_%011.6f" % timestamp_seconds + '.wav'
35
36    # reopen source file
37    s = source(source_file, samplerate, hopsize)
38    samplerate = s.get_samplerate()
39
40    total_frames = 0
41    slices = []
42
43    while True:
44        # get hopsize new samples from source
45        vec, read = s()
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
52            new_sink_path = new_sink_name(source_base_name, start_stamp, samplerate)
53            # create its sink
54            g = sink(new_sink_path, samplerate)
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)
80        total_frames += read
81        if read < hopsize: break
82
83    # close the last file
84    del g
Note: See TracBrowser for help on using the repository browser.