Changeset 78561f7 for python


Ignore:
Timestamp:
Oct 31, 2018, 5:16:30 PM (6 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:
07382d8
Parents:
dc3f68d
Message:

[doc] improve demos used in examples

Location:
python/demos
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/demos/demo_filter.py

    rdc3f68d r78561f7  
    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)
  • python/demos/demo_source_simple.py

    rdc3f68d r78561f7  
    11#! /usr/bin/env python
    2 import sys, aubio
     2import sys
     3import aubio
    34
    4 samplerate = 0  # use original source samplerate
     5samplerate = 0 # use original source samplerate
    56hop_size = 256 # number of frames to read in one block
    6 s = aubio.source(sys.argv[1], samplerate, hop_size)
     7src = aubio.source(sys.argv[1], samplerate, hop_size)
    78total_frames = 0
    89
    9 while True: # reading loop
    10     samples, read = s()
    11     total_frames += read
     10while True:
     11    samples, read = src()     # read hop_size new samples from source
     12    total_frames += read      # increment total number of frames
    1213    if read < hop_size: break # end of file reached
    1314
    1415fmt_string = "read {:d} frames at {:d}Hz from {:s}"
    15 print (fmt_string.format(total_frames, s.samplerate, sys.argv[1]))
    16 
     16print (fmt_string.format(total_frames, src.samplerate, src.uri))
Note: See TracChangeset for help on using the changeset viewer.