source: python/demos/demo_filter.py @ 42dbcf4

feature/crepe
Last change on this file since 42dbcf4 was 8ebcd3d, checked in by Paul Brossier <piem@piem.org>, 6 years ago

[py] improve style for demo_filter.py

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