Ignore:
Timestamp:
Oct 31, 2018, 5:37:35 PM (5 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master
Children:
7a54b37
Parents:
9b23815e (diff), 78561f7 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'feature/docstrings' (see #73)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/demos/demo_filter.py

    r9b23815e r07382d8  
    11#! /usr/bin/env python
    22
     3import sys
     4import os.path
     5import aubio
    36
    4 def apply_filter(path):
    5     from aubio import source, sink, digital_filter
    6     from os.path import basename, splitext
    7 
     7def apply_filter(path, target):
    88    # open input file, get its samplerate
    9     s = source(path)
     9    s = aubio.source(path)
    1010    samplerate = s.samplerate
    1111
    1212    # create an A-weighting filter
    13     f = digital_filter(7)
     13    f = aubio.digital_filter(7)
    1414    f.set_a_weighting(samplerate)
    15     # alternatively, apply another filter
    1615
    1716    # create output file
    18     o = sink("filtered_" + splitext(basename(path))[0] + ".wav", samplerate)
     17    o = aubio.sink(target, samplerate)
    1918
    2019    total_frames = 0
    2120    while True:
     21        # read from source
    2222        samples, read = s()
     23        # filter samples
    2324        filtered_samples = f(samples)
     25        # write to sink
    2426        o(filtered_samples, read)
     27        # count frames read
    2528        total_frames += read
     29        # end of file reached
    2630        if read < s.hop_size: break
    2731
     32    # print some info
    2833    duration = total_frames / float(samplerate)
    29     print ("read {:s}".format(s.uri))
    30     print ("applied A-weighting filtered ({:d} Hz)".format(samplerate))
    31     print ("wrote {:s} ({:.2f} s)".format(o.uri, duration))
     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))
    3238
    3339if __name__ == '__main__':
    34     import sys
    35     for f in sys.argv[1:]:
    36         apply_filter(f)
     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 TracChangeset for help on using the changeset viewer.