source: python/demos/demo_filter.py @ 78561f7

feature/autosinkfeature/constantqfeature/pitchshiftfeature/pydocstringsfeature/timestretch
Last change on this file since 78561f7 was 78561f7, checked in by Paul Brossier <piem@piem.org>, 2 years ago

[doc] improve demos used in examples

  • Property mode set to 100755
File size: 1.4 KB
Line 
1#! /usr/bin/env python
2
3import sys
4import os.path
5import aubio
6
7def apply_filter(path, target):
8    # open input file, get its samplerate
9    s = aubio.source(path)
10    samplerate = s.samplerate
11
12    # create an A-weighting filter
13    f = aubio.digital_filter(7)
14    f.set_a_weighting(samplerate)
15
16    # create output file
17    o = aubio.sink(target, samplerate)
18
19    total_frames = 0
20    while True:
21        # read from source
22        samples, read = s()
23        # filter samples
24        filtered_samples = f(samples)
25        # write to sink
26        o(filtered_samples, read)
27        # count frames read
28        total_frames += read
29        # end of file reached
30        if read < s.hop_size: break
31
32    # print some info
33    duration = total_frames / float(samplerate)
34    input_str = "input: {:s} ({:.2f} s, {:d} Hz)"
35    output_str = "output: {:s}, A-weighting filtered ({:d} frames total)"
36    print (input_str.format(s.uri, duration, samplerate))
37    print (output_str.format(o.uri, total_frames))
38
39if __name__ == '__main__':
40    usage = "{:s} <input_file> [output_file]".format(sys.argv[0])
41    if not 1 < len(sys.argv) < 4:
42        print (usage)
43        sys.exit(1)
44    if len(sys.argv) < 3:
45        input_path = sys.argv[1]
46        basename = os.path.splitext(os.path.basename(input_path))[0] + ".wav"
47        output_path = "filtered_" + basename
48    else:
49        input_path, output_path = sys.argv[1:]
50    # run function
51    apply_filter(input_path, output_path)
Note: See TracBrowser for help on using the repository browser.