Showing the changes to python (2 of 2 files). Show all
Unified · Split
| @@ -1,36 +1,51 @@ | |||
| 1 | #! /usr/bin/env python | 1 | #! /usr/bin/env python |
| 2 | 2 | ||
| 3 | import sys | ||
| 4 | import os.path | ||
| 5 | import aubio | ||
| 3 | 6 | ||
| 4 | def apply_filter(path): | 7 | def apply_filter(path, target): |
| 5 | from aubio import source, sink, digital_filter | ||
| 6 | from os.path import basename, splitext | ||
| 7 | |||
| 8 | # open input file, get its samplerate | 8 | # open input file, get its samplerate |
| 9 | s = source(path) | 9 | s = aubio.source(path) |
| 10 | samplerate = s.samplerate | 10 | samplerate = s.samplerate |
| 11 | 11 | ||
| 12 | # create an A-weighting filter | 12 | # create an A-weighting filter |
| 13 | f = digital_filter(7) | 13 | f = aubio.digital_filter(7) |
| 14 | f.set_a_weighting(samplerate) | 14 | f.set_a_weighting(samplerate) |
| 15 | # alternatively, apply another filter | ||
| 16 | 15 | ||
| 17 | # create output file | 16 | # create output file |
| 18 | o = sink("filtered_" + splitext(basename(path))[0] + ".wav", samplerate) | 17 | o = aubio.sink(target, samplerate) |
| 19 | 18 | ||
| 20 | total_frames = 0 | 19 | total_frames = 0 |
| 21 | while True: | 20 | while True: |
| 21 | # read from source | ||
| 22 | samples, read = s() | 22 | samples, read = s() |
| 23 | # filter samples | ||
| 23 | filtered_samples = f(samples) | 24 | filtered_samples = f(samples) |
| 25 | # write to sink | ||
| 24 | o(filtered_samples, read) | 26 | o(filtered_samples, read) |
| 27 | # count frames read | ||
| 25 | total_frames += read | 28 | total_frames += read |
| 29 | # end of file reached | ||
| 26 | if read < s.hop_size: break | 30 | if read < s.hop_size: break |
| 27 | 31 | ||
| 32 | # print some info | ||
| 28 | duration = total_frames / float(samplerate) | 33 | duration = total_frames / float(samplerate) |
| 29 | print ("read {:s}".format(s.uri)) | 34 | input_str = "input: {:s} ({:.2f} s, {:d} Hz)" |
| 30 | print ("applied A-weighting filtered ({:d} Hz)".format(samplerate)) | 35 | output_str = "output: {:s}, A-weighting filtered ({:d} frames total)" |
| 31 | print ("wrote {:s} ({:.2f} s)".format(o.uri, duration)) | 36 | print (input_str.format(s.uri, duration, samplerate)) |
| 37 | print (output_str.format(o.uri, total_frames)) | ||
| 32 | 38 | ||
| 33 | if __name__ == '__main__': | 39 | if __name__ == '__main__': |
| 34 | import sys | 40 | usage = "{:s} <input_file> [output_file]".format(sys.argv[0]) |
| 35 | for f in sys.argv[1:]: | 41 | if not 1 < len(sys.argv) < 4: |
| 36 | apply_filter(f) | 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) | ||
| @@ -1,16 +1,16 @@ | |||
| 1 | #! /usr/bin/env python | 1 | #! /usr/bin/env python |
| 2 | import sys, aubio | 2 | import sys |
| 3 | import aubio | ||
| 3 | 4 | ||
| 4 | samplerate = 0 # use original source samplerate | 5 | samplerate = 0 # use original source samplerate |
| 5 | hop_size = 256 # number of frames to read in one block | 6 | hop_size = 256 # number of frames to read in one block |
| 6 | s = aubio.source(sys.argv[1], samplerate, hop_size) | 7 | src = aubio.source(sys.argv[1], samplerate, hop_size) |
| 7 | total_frames = 0 | 8 | total_frames = 0 |
| 8 | 9 | ||
| 9 | while True: # reading loop | 10 | while True: |
| 10 | samples, read = s() | 11 | samples, read = src() # read hop_size new samples from source |
| 11 | total_frames += read | 12 | total_frames += read # increment total number of frames |
| 12 | if read < hop_size: break # end of file reached | 13 | if read < hop_size: break # end of file reached |
| 13 | 14 | ||
| 14 | fmt_string = "read {:d} frames at {:d}Hz from {:s}" | 15 | fmt_string = "read {:d} frames at {:d}Hz from {:s}" |
| 15 | print (fmt_string.format(total_frames, s.samplerate, sys.argv[1])) | 16 | print (fmt_string.format(total_frames, src.samplerate, src.uri)) |
| 16 | |||