python/demos/demo_pitchshift.py: add simple pitch demo with a ramp
authorPaul Brossier <piem@piem.org>
Fri, 16 Sep 2016 23:47:46 +0000 (01:47 +0200)
committerPaul Brossier <piem@piem.org>
Fri, 16 Sep 2016 23:47:46 +0000 (01:47 +0200)
python/demos/demo_pitchshift.py [new file with mode: 0755]

diff --git a/python/demos/demo_pitchshift.py b/python/demos/demo_pitchshift.py
new file mode 100755 (executable)
index 0000000..a7cd06b
--- /dev/null
@@ -0,0 +1,49 @@
+#! /usr/bin/env python
+
+import sys
+import aubio
+
+if __name__ == '__main__':
+    if len(sys.argv) < 3:
+        print('usage: %s <inputfile> <outputfile> [transpose] [samplerate] [hop_size]' % sys.argv[0])
+        sys.exit(1)
+    if len(sys.argv) > 3: transpose = float(sys.argv[3])
+    else: transpose = 12.
+    if len(sys.argv) > 4: samplerate = int(sys.argv[4])
+    else: samplerate = 0
+    if len(sys.argv) > 5: hop_size = int(sys.argv[5])
+    else: hop_size = 256
+
+    source_read = aubio.source(sys.argv[1], samplerate, hop_size)
+    if samplerate == 0: samplerate = source_read.samplerate
+    sink_out = aubio.sink(sys.argv[2], samplerate)
+
+    pitchshifter = aubio.pitchshift("default", 1., hop_size, samplerate)
+    if transpose: pitchshifter.set_transpose(transpose)
+
+    total_frames, read = 0, hop_size
+    transpose_range = 23.9
+    while read:
+        vec, read = source_read()
+        # transpose the samples
+        out = pitchshifter(vec)
+        # position in the file (between 0. and 1.)
+        percent_read = total_frames / float(source_read.duration)
+        # variable transpose rate (in semitones)
+        transpose = 2 * transpose_range * percent_read - transpose_range
+        # set transpose rate
+        pitchshifter.set_transpose(transpose)
+        # print the transposition
+        #print pitchshifter.get_transpose()
+        # write the output
+        sink_out(out, read)
+        total_frames += read
+
+    # end of file, print some results
+    outstr = "wrote %.2fs" % (total_frames / float(samplerate))
+    outstr += " (%d frames in" % total_frames
+    outstr += " %d blocks" % (total_frames // source_read.hop_size)
+    outstr += " at %dHz)" % source_read.samplerate
+    outstr += " from " + source_read.uri
+    outstr += " to " + sink_out.uri
+    print(outstr)