1 | #!/usr/bin/python |
---|
2 | |
---|
3 | """ this file was written by Paul Brossier |
---|
4 | it is released under the GNU/GPL license. |
---|
5 | """ |
---|
6 | |
---|
7 | import sys |
---|
8 | from aubio.tasks import * |
---|
9 | |
---|
10 | usage = "usage: %s [options] -i soundfile" % sys.argv[0] |
---|
11 | |
---|
12 | |
---|
13 | def parse_args(): |
---|
14 | from optparse import OptionParser |
---|
15 | parser = OptionParser(usage=usage) |
---|
16 | parser.add_option("-i","--input", |
---|
17 | action="store", dest="filename", |
---|
18 | help="input sound file") |
---|
19 | parser.add_option("-m","--mode", |
---|
20 | action="store", dest="mode", default='mcomb', |
---|
21 | help="pitch detection mode [default=mcomb] \ |
---|
22 | mcomb|yin|fcomb|schmitt") |
---|
23 | parser.add_option("-u","--units", action="callback", |
---|
24 | callback=check_pitchm_mode, dest="omode", |
---|
25 | default=aubio_pitchm_freq, |
---|
26 | help="output pitch in units [default=Hz] \ |
---|
27 | freq|midi|cent|bin") |
---|
28 | parser.add_option("-B","--bufsize", |
---|
29 | action="store", dest="bufsize", default=None, |
---|
30 | help="buffer size [default=1024]") |
---|
31 | parser.add_option("-H","--hopsize", |
---|
32 | action="store", dest="hopsize", default=None, |
---|
33 | help="overlap size [default=512]") |
---|
34 | parser.add_option("-t","--threshold", |
---|
35 | action="store", dest="threshold", default=0.1, |
---|
36 | help="pitch threshold (for yin) [default=0.1]") |
---|
37 | parser.add_option("-s","--silence", |
---|
38 | action="store", dest="silence", default=-70, |
---|
39 | help="silence threshold [default=-70]") |
---|
40 | parser.add_option("-D","--delay", |
---|
41 | action="store", dest="delay", |
---|
42 | help="number of seconds to take back [default=system]\ |
---|
43 | default system delay is 2*hopsize/samplerate") |
---|
44 | parser.add_option("-L","--localmin", |
---|
45 | action="store_true", dest="localmin", default=False, |
---|
46 | help="use local minima after peak detection") |
---|
47 | parser.add_option("-c","--cut", |
---|
48 | action="store_true", dest="cut", default=False, |
---|
49 | help="cut input sound file at detected labels \ |
---|
50 | best used with option -L") |
---|
51 | # to be implemented |
---|
52 | parser.add_option("-n","--note", |
---|
53 | action="store_true", dest="note", default=False, |
---|
54 | help="NOT IMPLEMENTED output notes") |
---|
55 | # plotting functions |
---|
56 | parser.add_option("-p","--plot", |
---|
57 | action="store_true", dest="plot", default=False, |
---|
58 | help="NOT IMPLEMENTED draw plot") |
---|
59 | parser.add_option("-O","--outplot", |
---|
60 | action="store", dest="outplot", default=None, |
---|
61 | help="NOT IMPLEMENTED save plot to output.{ps,png}") |
---|
62 | parser.add_option("-v","--verbose", |
---|
63 | action="store_true", dest="verbose", default=False, |
---|
64 | help="make lots of noise [default]") |
---|
65 | parser.add_option("-q","--quiet", |
---|
66 | action="store_false", dest="verbose", default=False, |
---|
67 | help="be quiet") |
---|
68 | (options, args) = parser.parse_args() |
---|
69 | if not options.bufsize: |
---|
70 | if options.mode == aubio_pitch_yin: options.bufsize = 1024 |
---|
71 | if options.mode == aubio_pitch_schmitt: options.bufsize = 2048 |
---|
72 | if options.mode == aubio_pitch_mcomb: options.bufsize = 4096 |
---|
73 | if options.mode == aubio_pitch_fcomb: options.bufsize = 4096 |
---|
74 | else: options.bufsize = 2048 |
---|
75 | if not options.hopsize: |
---|
76 | options.hopsize = float(options.bufsize) / 2 |
---|
77 | if not options.filename: |
---|
78 | print "no file name given\n", usage |
---|
79 | sys.exit(1) |
---|
80 | return options, args |
---|
81 | |
---|
82 | options, args = parse_args() |
---|
83 | |
---|
84 | #print options.bufsize, options.hopsize |
---|
85 | |
---|
86 | filename = options.filename |
---|
87 | params = taskparams() |
---|
88 | params.samplerate = float(sndfile(filename).samplerate()) |
---|
89 | params.hopsize = int(options.hopsize) |
---|
90 | params.bufsize = int(options.bufsize) |
---|
91 | params.step = params.samplerate/float(params.hopsize) |
---|
92 | params.threshold = float(options.threshold) |
---|
93 | params.silence = float(options.silence) |
---|
94 | params.verbose = options.verbose |
---|
95 | #mintol = float(options.mintol)*step |
---|
96 | # default take back system delay |
---|
97 | if options.delay: delay = float(options.delay) |
---|
98 | else: delay = 2./params.step |
---|
99 | |
---|
100 | if options.note: |
---|
101 | exit("not implemented yet") |
---|
102 | |
---|
103 | |
---|
104 | wplot,oplots = [],[] |
---|
105 | modes = options.mode.split(',') |
---|
106 | for i in range(len(modes)): |
---|
107 | pitch = [] |
---|
108 | params.pitchmode = modes[i] |
---|
109 | filetask = taskpitch(filename,params=params) |
---|
110 | pitch = filetask.compute_all() |
---|
111 | #print filetask.eval(pitch[i]) |
---|
112 | filetask.plot(pitch,wplot,oplots) |
---|
113 | |
---|
114 | filetask.plotplot(wplot,oplots,outplot=options.outplot) |
---|