ref: 78561f7924523d31ef3a4fb6c101e7c93e491a34
parent: dc3f68dc15da93014965dfbaaf9a42a8a9f650f8
author: Paul Brossier <piem@piem.org>
date: Wed Oct 31 13:16:30 EDT 2018
[doc] improve demos used in examples
--- a/python/demos/demo_filter.py
+++ b/python/demos/demo_filter.py
@@ -1,36 +1,51 @@
#! /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
- s = source(path)
+ s = aubio.source(path)
samplerate = s.samplerate
# create an A-weighting filter
- f = digital_filter(7)
+ f = aubio.digital_filter(7)
f.set_a_weighting(samplerate)
- # alternatively, apply another filter
# create output file
- o = sink("filtered_" + splitext(basename(path))[0] + ".wav", samplerate)
+ o = aubio.sink(target, samplerate)
total_frames = 0
while True:
+ # read from source
samples, read = s()
+ # filter samples
filtered_samples = f(samples)
+ # write to sink
o(filtered_samples, read)
+ # count frames read
total_frames += read
+ # end of file reached
if read < s.hop_size: break
+ # print some info
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__':
- 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)
--- a/python/demos/demo_source_simple.py
+++ b/python/demos/demo_source_simple.py
@@ -1,16 +1,16 @@
#! /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
-s = aubio.source(sys.argv[1], samplerate, hop_size)
+src = aubio.source(sys.argv[1], samplerate, hop_size)
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}"
-print (fmt_string.format(total_frames, s.samplerate, sys.argv[1]))
-
+print (fmt_string.format(total_frames, src.samplerate, src.uri))