source: python/demos/demo_filter.py @ 7b8e51c

feature/cnnfeature/crepefix/ffmpeg5
Last change on this file since 7b8e51c 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
RevLine 
[0b3c17b]1#! /usr/bin/env python
2
[78561f7]3import sys
4import os.path
5import aubio
[0b3c17b]6
[8ebcd3d]7
[78561f7]8def apply_filter(path, target):
[01f7598]9    # open input file, get its samplerate
[78561f7]10    s = aubio.source(path)
[01f7598]11    samplerate = s.samplerate
12
13    # create an A-weighting filter
[78561f7]14    f = aubio.digital_filter(7)
[01f7598]15    f.set_a_weighting(samplerate)
[0b3c17b]16
[01f7598]17    # create output file
[78561f7]18    o = aubio.sink(target, samplerate)
[01f7598]19
20    total_frames = 0
[0b3c17b]21    while True:
[78561f7]22        # read from source
[0b3c17b]23        samples, read = s()
[78561f7]24        # filter samples
[0b3c17b]25        filtered_samples = f(samples)
[78561f7]26        # write to sink
[01f7598]27        o(filtered_samples, read)
[78561f7]28        # count frames read
[0b3c17b]29        total_frames += read
[78561f7]30        # end of file reached
[8ebcd3d]31        if read < s.hop_size:
32            break
[01f7598]33
[78561f7]34    # print some info
[01f7598]35    duration = total_frames / float(samplerate)
[78561f7]36    input_str = "input: {:s} ({:.2f} s, {:d} Hz)"
37    output_str = "output: {:s}, A-weighting filtered ({:d} frames total)"
[8ebcd3d]38    print(input_str.format(s.uri, duration, samplerate))
39    print(output_str.format(o.uri, total_frames))
[0b3c17b]40
41if __name__ == '__main__':
[78561f7]42    usage = "{:s} <input_file> [output_file]".format(sys.argv[0])
43    if not 1 < len(sys.argv) < 4:
[8ebcd3d]44        print(usage)
[78561f7]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.