From: Paul Brossier Date: Wed, 31 Oct 2018 16:16:30 +0000 (+0100) Subject: [doc] improve demos used in examples X-Git-Tag: 0.4.8~90^2 X-Git-Url: https://git.aubio.org/?p=aubio.git;a=commitdiff_plain;h=78561f7924523d31ef3a4fb6c101e7c93e491a34 [doc] improve demos used in examples --- diff --git a/python/demos/demo_filter.py b/python/demos/demo_filter.py index 10226cea..1eb57730 100755 --- 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} [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) diff --git a/python/demos/demo_source_simple.py b/python/demos/demo_source_simple.py index 46bd8bee..bdf03267 100755 --- 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))