demos/demo_slicing.py: add simple slicing example
authorPaul Brossier <piem@piem.org>
Wed, 13 Feb 2013 17:12:52 +0000 (12:12 -0500)
committerPaul Brossier <piem@piem.org>
Wed, 13 Feb 2013 17:12:52 +0000 (12:12 -0500)
python/demos/demo_slicing.py [new file with mode: 0755]

diff --git a/python/demos/demo_slicing.py b/python/demos/demo_slicing.py
new file mode 100755 (executable)
index 0000000..3f2e2d2
--- /dev/null
@@ -0,0 +1,28 @@
+#! /usr/bin/env python
+
+import sys
+import os.path
+from aubio import source, sink
+
+if __name__ == '__main__':
+    if len(sys.argv) < 3:
+        print 'usage: %s <inputfile> <duration>' % sys.argv[0]
+        sys.exit(1)
+    source_file = sys.argv[1]
+    duration = float(sys.argv[2])
+    sink_base, sink_ext = os.path.splitext(os.path.basename(source_file))
+    slice_n, total_frames, read = 1, 0, 256
+    f = source(source_file, 0, 256)
+    g = sink(sink_base + '-%02d' % slice_n + sink_ext, f.samplerate)
+    while read:
+        vec, read = f()
+        g(vec, read)
+        total_frames += read
+        if total_frames / float(f.samplerate) >= duration * slice_n: 
+            slice_n += 1
+            del g
+            g = sink(sink_base + '-%02d' % slice_n + sink_ext, f.samplerate)
+    total_duration = total_frames / float(f.samplerate)
+    print 'created %(slice_n)d slices from %(source_file)s' % locals(),
+    print ' (total duration %(total_duration).2fs)' % locals()
+    del f, g