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 @@
1#! /usr/bin/env python1#! /usr/bin/env python
2 2
3import sys
4import os.path
5import aubio
3 6
4def apply_filter(path):7def 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 samplerate8 # open input file, get its samplerate
9 s = source(path)9 s = aubio.source(path)
10 samplerate = s.samplerate10 samplerate = s.samplerate
11 11
12 # create an A-weighting filter12 # 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 file16 # create output file
18 o = sink("filtered_" + splitext(basename(path))[0] + ".wav", samplerate)17 o = aubio.sink(target, samplerate)
19 18
20 total_frames = 019 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 += read28 total_frames += read
29 # end of file reached
26 if read < s.hop_size: break30 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
33if __name__ == '__main__':39if __name__ == '__main__':
34 import sys40 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)
python/demos/demo_source_simple.py+8−8
@@ -1,16 +1,16 @@
1#! /usr/bin/env python1#! /usr/bin/env python
2import sys, aubio2import sys
3import aubio
3 4
4samplerate = 0 # use original source samplerate5samplerate = 0 # use original source samplerate
5hop_size = 256 # number of frames to read in one block6hop_size = 256 # number of frames to read in one block
6s = aubio.source(sys.argv[1], samplerate, hop_size)7src = aubio.source(sys.argv[1], samplerate, hop_size)
7total_frames = 08total_frames = 0
8 9
9while True: # reading loop10while True:
10 samples, read = s()11 samples, read = src() # read hop_size new samples from source
11 total_frames += read12 total_frames += read # increment total number of frames
12 if read < hop_size: break # end of file reached13 if read < hop_size: break # end of file reached
13 14
14fmt_string = "read {:d} frames at {:d}Hz from {:s}"15fmt_string = "read {:d} frames at {:d}Hz from {:s}"
15print (fmt_string.format(total_frames, s.samplerate, sys.argv[1]))16print (fmt_string.format(total_frames, src.samplerate, src.uri))
16

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