source: python/demos/demo_pitchshift.py @ e8aed04c

Last change on this file since e8aed04c was 04fc360, checked in by Paul Brossier <piem@piem.org>, 8 years ago

python/demos/demo_pitchshift.py: default to 64 hop

  • Property mode set to 100755
File size: 1.9 KB
Line 
1#! /usr/bin/env python
2
3import sys
4import aubio
5
6if __name__ == '__main__':
7    if len(sys.argv) < 3:
8        print('usage: %s <inputfile> <outputfile> [transpose] [samplerate] [hop_size] [mode]' % sys.argv[0])
9        print('available modes: default, crispness:0, crispness:1, ... crispness:6')
10        sys.exit(1)
11    if len(sys.argv) > 3: transpose = float(sys.argv[3])
12    else: transpose = 12.
13    if len(sys.argv) > 4: samplerate = int(sys.argv[4])
14    else: samplerate = 0
15    if len(sys.argv) > 5: hop_size = int(sys.argv[5])
16    else: hop_size = 64
17    if len(sys.argv) > 6: mode = sys.argv[6]
18    else: mode = "default"
19
20    source_read = aubio.source(sys.argv[1], samplerate, hop_size)
21    if samplerate == 0: samplerate = source_read.samplerate
22    sink_out = aubio.sink(sys.argv[2], samplerate)
23
24    pitchshifter = aubio.pitchshift(mode, 1., hop_size, samplerate)
25    if transpose: pitchshifter.set_transpose(transpose)
26
27    total_frames, read = 0, hop_size
28    transpose_range = 23.9
29    while read:
30        vec, read = source_read()
31        # transpose the samples
32        out = pitchshifter(vec)
33        # position in the file (between 0. and 1.)
34        percent_read = total_frames / float(source_read.duration)
35        # variable transpose rate (in semitones)
36        transpose = 2 * transpose_range * percent_read - transpose_range
37        # set transpose rate
38        pitchshifter.set_transpose(transpose)
39        # print the transposition
40        #print pitchshifter.get_transpose()
41        # write the output
42        sink_out(out, read)
43        total_frames += read
44
45    # end of file, print some results
46    outstr = "wrote %.2fs" % (total_frames / float(samplerate))
47    outstr += " (%d frames in" % total_frames
48    outstr += " %d blocks" % (total_frames // source_read.hop_size)
49    outstr += " at %dHz)" % source_read.samplerate
50    outstr += " from " + source_read.uri
51    outstr += " to " + sink_out.uri
52    print(outstr)
Note: See TracBrowser for help on using the repository browser.