source: python/demos/demo_slicing.py @ 5c6b264

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

demos/demo_slicing.py: slice exactly at region boundary

  • Property mode set to 100755
File size: 1.9 KB
Line 
1#! /usr/bin/env python
2
3import sys
4import os.path
5from aubio import source, sink
6
7if __name__ == '__main__':
8    if len(sys.argv) < 3:
9        print 'usage: %s <inputfile> <duration>' % sys.argv[0]
10        sys.exit(1)
11    source_file = sys.argv[1]
12    duration = float(sys.argv[2])
13    source_base_name, source_ext = os.path.splitext(os.path.basename(source_file))
14
15    hopsize = 256
16    slice_n, total_frames_written, read = 0, 0, hopsize
17
18    def new_sink_name(source_base_name, slice_n, duration = duration):
19        return source_base_name + '_%02.3f' % (slice_n*duration) + '.wav'
20
21    f = source(source_file, 0, hopsize)
22    samplerate = f.samplerate
23    g = sink(new_sink_name(source_base_name, slice_n), samplerate)
24
25    #print "new slice:", slice_n, 0, "+", 0, "=", 0
26    while read == hopsize:
27        vec, read = f()
28        start_of_next_region = int(duration * samplerate * (slice_n + 1))
29        remaining = start_of_next_region - total_frames_written
30        # number of samples remaining is less than what we got
31        if remaining <= read:
32            # write remaining samples from current region
33            g(vec[0:remaining], remaining)
34            # close this file
35            del g
36            #print "new slice", slice_n, total_frames_written, "+", remaining, "=", start_of_next_region
37            slice_n += 1
38            # create a new file for the new region
39            g = sink(new_sink_name(source_base_name, slice_n), samplerate)
40            # write the remaining samples in the new file
41            g(vec[remaining:read], read - remaining)
42        else:
43            g(vec[0:read], read)
44        total_frames_written += read
45    total_duration = total_frames_written / float(samplerate)
46    slice_n += 1
47    print 'created %(slice_n)s slices from %(source_base_name)s%(source_ext)s' % locals(),
48    print ' (total duration %(total_duration).2fs)' % locals()
49    # close source and sink files
50    del f, g
Note: See TracBrowser for help on using the repository browser.