demos/demo_sink_create_woodblock.py: generate a woodblock like sound
authorPaul Brossier <piem@piem.org>
Sun, 10 Feb 2013 05:13:56 +0000 (00:13 -0500)
committerPaul Brossier <piem@piem.org>
Sun, 10 Feb 2013 05:13:56 +0000 (00:13 -0500)
python/demos/demo_sink_create_woodblock.py [new file with mode: 0755]

diff --git a/python/demos/demo_sink_create_woodblock.py b/python/demos/demo_sink_create_woodblock.py
new file mode 100755 (executable)
index 0000000..9fd86ae
--- /dev/null
@@ -0,0 +1,38 @@
+#! /usr/bin/env python
+
+import sys
+from math import sin, pi
+from aubio import sink
+from numpy import array
+
+if len(sys.argv) != 2:
+    print 'usage: %s <outputfile>' % sys.argv[0]
+    sys.exit(1)
+
+samplerate = 44100      # in Hz
+pitch = 2200            # in Hz
+blocksize = 256         # in samples
+duration = 0.02         # in seconds
+
+twopi = pi * 2.
+
+duration = int ( 44100 * duration ) # convert to samples
+attack = 3
+
+period = int ( float(samplerate) /  pitch )
+sinetone = [ 0.7 * sin(twopi * i/ period) for i in range(period) ] 
+sinetone *= int ( duration / period )
+sinetone = array(sinetone, dtype = 'float32')
+
+from math import exp, e
+for i in range(len(sinetone)):
+    sinetone[i] *= exp( - e * float(i) / len(sinetone))
+for i in range(attack):
+    sinetone[i] *= exp( e * (float(i) / attack - 1 ) )
+
+my_sink = sink(sys.argv[1], 44100)
+
+i = 0
+while i + blocksize < duration:
+    my_sink(sinetone[i:i+blocksize], blocksize)
+    i += blocksize