History

[doc] improve demos used in examples

Paul Brossier committed 8 years ago

Commit 78561f71 parent dc3f68d2 files · +37 −22Browse files

Showing the changes to python (2 of 2 files). Show all

Unified · Split

2 files
  1. python/demos/demo_filter.py +29 −14
  2. python/demos/demo_source_simple.py +8 −8
python/demos/demo_filter.py+29−14
@@ -1,36 +1,51 @@
11 #! /usr/bin/env python
22
3+import sys
4+import os.path
5+import aubio
36
4−def apply_filter(path):
5− from aubio import source, sink, digital_filter
6− from os.path import basename, splitext
7−
7+def 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
3339 if __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+8−8
@@ -1,16 +1,16 @@
11 #! /usr/bin/env python
2−import sys, aubio
2+import sys
3+import aubio
34
4−samplerate = 0 # use original source samplerate
5+samplerate = 0 # use original source samplerate
56 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)
78 total_frames = 0
89
9−while True: # reading loop
10− samples, read = s()
11− total_frames += read
10+while 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
1415 fmt_string = "read {:d} frames at {:d}Hz from {:s}"
15−print (fmt_string.format(total_frames, s.samplerate, sys.argv[1]))
16−
16+print (fmt_string.format(total_frames, src.samplerate, src.uri))

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t