[doc] improve demos used in examples
authorPaul Brossier <piem@piem.org>
Wed, 31 Oct 2018 16:16:30 +0000 (17:16 +0100)
committerPaul Brossier <piem@piem.org>
Wed, 31 Oct 2018 16:16:30 +0000 (17:16 +0100)
python/demos/demo_filter.py
python/demos/demo_source_simple.py

index 10226ce..1eb5773 100755 (executable)
@@ -1,36 +1,51 @@
 #! /usr/bin/env python
 
 #! /usr/bin/env python
 
+import sys
+import os.path
+import aubio
 
 
-def apply_filter(path):
-    from aubio import source, sink, digital_filter
-    from os.path import basename, splitext
-
+def apply_filter(path, target):
     # open input file, get its samplerate
     # open input file, get its samplerate
-    s = source(path)
+    s = aubio.source(path)
     samplerate = s.samplerate
 
     # create an A-weighting filter
     samplerate = s.samplerate
 
     # create an A-weighting filter
-    f = digital_filter(7)
+    f = aubio.digital_filter(7)
     f.set_a_weighting(samplerate)
     f.set_a_weighting(samplerate)
-    # alternatively, apply another filter
 
     # create output file
 
     # create output file
-    o = sink("filtered_" + splitext(basename(path))[0] + ".wav", samplerate)
+    o = aubio.sink(target, samplerate)
 
     total_frames = 0
     while True:
 
     total_frames = 0
     while True:
+        # read from source
         samples, read = s()
         samples, read = s()
+        # filter samples
         filtered_samples = f(samples)
         filtered_samples = f(samples)
+        # write to sink
         o(filtered_samples, read)
         o(filtered_samples, read)
+        # count frames read
         total_frames += read
         total_frames += read
+        # end of file reached
         if read < s.hop_size: break
 
         if read < s.hop_size: break
 
+    # print some info
     duration = total_frames / float(samplerate)
     duration = total_frames / float(samplerate)
-    print ("read {:s}".format(s.uri))
-    print ("applied A-weighting filtered ({:d} Hz)".format(samplerate))
-    print ("wrote {:s} ({:.2f} s)".format(o.uri, duration))
+    input_str = "input: {:s} ({:.2f} s, {:d} Hz)"
+    output_str = "output: {:s}, A-weighting filtered ({:d} frames total)"
+    print (input_str.format(s.uri, duration, samplerate))
+    print (output_str.format(o.uri, total_frames))
 
 if __name__ == '__main__':
 
 if __name__ == '__main__':
-    import sys
-    for f in sys.argv[1:]:
-        apply_filter(f)
+    usage = "{:s} <input_file> [output_file]".format(sys.argv[0])
+    if not 1 < len(sys.argv) < 4:
+        print (usage)
+        sys.exit(1)
+    if len(sys.argv) < 3:
+        input_path = sys.argv[1]
+        basename = os.path.splitext(os.path.basename(input_path))[0] + ".wav"
+        output_path = "filtered_" + basename
+    else:
+        input_path, output_path = sys.argv[1:]
+    # run function
+    apply_filter(input_path, output_path)
index 46bd8be..bdf0326 100755 (executable)
@@ -1,16 +1,16 @@
 #! /usr/bin/env python
 #! /usr/bin/env python
-import sys, aubio
+import sys
+import aubio
 
 
-samplerate = 0  # use original source samplerate
+samplerate = 0 # use original source samplerate
 hop_size = 256 # number of frames to read in one block
 hop_size = 256 # number of frames to read in one block
-s = aubio.source(sys.argv[1], samplerate, hop_size)
+src = aubio.source(sys.argv[1], samplerate, hop_size)
 total_frames = 0
 
 total_frames = 0
 
-while True: # reading loop
-    samples, read = s()
-    total_frames += read
+while True:
+    samples, read = src()     # read hop_size new samples from source
+    total_frames += read      # increment total number of frames
     if read < hop_size: break # end of file reached
 
 fmt_string = "read {:d} frames at {:d}Hz from {:s}"
     if read < hop_size: break # end of file reached
 
 fmt_string = "read {:d} frames at {:d}Hz from {:s}"
-print (fmt_string.format(total_frames, s.samplerate, sys.argv[1]))
-
+print (fmt_string.format(total_frames, src.samplerate, src.uri))