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

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5
Last change on this file since 78561f7 was 78561f7, checked in by Paul Brossier <piem@piem.org>, 5 years ago

[doc] improve demos used in examples

  • 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
[78561f7]7def apply_filter(path, target):
[01f7598]8    # open input file, get its samplerate
[78561f7]9    s = aubio.source(path)
[01f7598]10    samplerate = s.samplerate
11
12    # create an A-weighting filter
[78561f7]13    f = aubio.digital_filter(7)
[01f7598]14    f.set_a_weighting(samplerate)
[0b3c17b]15
[01f7598]16    # create output file
[78561f7]17    o = aubio.sink(target, samplerate)
[01f7598]18
19    total_frames = 0
[0b3c17b]20    while True:
[78561f7]21        # read from source
[0b3c17b]22        samples, read = s()
[78561f7]23        # filter samples
[0b3c17b]24        filtered_samples = f(samples)
[78561f7]25        # write to sink
[01f7598]26        o(filtered_samples, read)
[78561f7]27        # count frames read
[0b3c17b]28        total_frames += read
[78561f7]29        # end of file reached
[0b3c17b]30        if read < s.hop_size: break
[01f7598]31
[78561f7]32    # print some info
[01f7598]33    duration = total_frames / float(samplerate)
[78561f7]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))
[0b3c17b]38
39if __name__ == '__main__':
[78561f7]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.