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 = float(samplerate)/float(hopsize) |
---|
92 | params.threshold = float(options.threshold) |
---|
93 | params.silence = float(options.silence) |
---|
94 | #mintol = float(options.mintol)*step |
---|
95 | # default take back system delay |
---|
96 | if options.delay: delay = float(options.delay) |
---|
97 | else: delay = 2./params.step |
---|
98 | |
---|
99 | if options.note: |
---|
100 | exit("not implemented yet") |
---|
101 | |
---|
102 | |
---|
103 | pitch = [] |
---|
104 | modes = options.mode.split(',') |
---|
105 | for i in range(len(modes)): |
---|
106 | params.pitchmode = modes[i] |
---|
107 | dotask = taskpitch |
---|
108 | #pitch.append(getpitch(filename, #threshold, |
---|
109 | # mode=mode[i], |
---|
110 | # omode=options.omode, |
---|
111 | # bufsize=bufsize,hopsize=hopsize, |
---|
112 | # silence=silence)) |
---|
113 | filetask = dotask(filename,params=params) |
---|
114 | pitch.append(filetask.compute_all()) |
---|
115 | for j in range(len(pitch[i])): |
---|
116 | if pitch[i][j] > 10000 or pitch[i][j] < 40: |
---|
117 | pitch[i][j] = 0.; |
---|
118 | |
---|
119 | if options.verbose: |
---|
120 | for j in range(len(pitch[i])): |
---|
121 | print "%f\t" % (j/params.step), |
---|
122 | print "%f\t" % pitch[i][j], |
---|
123 | print |
---|
124 | |
---|
125 | if options.plot: |
---|
126 | filetask.plot(pitch,outplot=options.outplot) |
---|