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 | |
---|
8 | import sys |
---|
9 | from aubio.aubioclass import * |
---|
10 | |
---|
11 | usage = "usage: %s [options] -i soundfile" % sys.argv[0] |
---|
12 | |
---|
13 | |
---|
14 | def parse_args(): |
---|
15 | from optparse import OptionParser |
---|
16 | parser = OptionParser(usage=usage) |
---|
17 | parser.add_option("-i","--input", |
---|
18 | action="store", dest="filename", |
---|
19 | help="input sound file") |
---|
20 | parser.add_option("-m","--mode", action="callback", |
---|
21 | callback=check_pitch_mode, dest="mode", |
---|
22 | default=aubio_pitch_mcomb, |
---|
23 | help="pitch detection mode [default=mcomb] \ |
---|
24 | mcomb|yin|fcomb|schmitt") |
---|
25 | parser.add_option("-u","--units", action="callback", |
---|
26 | callback=check_pitchm_mode, dest="omode", |
---|
27 | default=aubio_pitchm_freq, |
---|
28 | help="output pitch in units [default=Hz] \ |
---|
29 | freq|midi|cent|bin") |
---|
30 | parser.add_option("-B","--bufsize", |
---|
31 | action="store", dest="bufsize", default=None, |
---|
32 | help="buffer size [default=1024]") |
---|
33 | parser.add_option("-H","--hopsize", |
---|
34 | action="store", dest="hopsize", default=None, |
---|
35 | help="overlap size [default=512]") |
---|
36 | parser.add_option("-t","--threshold", |
---|
37 | action="store", dest="threshold", default=0.1, |
---|
38 | help="pitch threshold (for yin) [default=0.1]") |
---|
39 | parser.add_option("-s","--silence", |
---|
40 | action="store", dest="silence", default=-70, |
---|
41 | help="silence threshold [default=-70]") |
---|
42 | parser.add_option("-D","--delay", |
---|
43 | action="store", dest="delay", |
---|
44 | help="number of seconds to take back [default=system]\ |
---|
45 | default system delay is 2*hopsize/samplerate") |
---|
46 | parser.add_option("-L","--localmin", |
---|
47 | action="store_true", dest="localmin", default=False, |
---|
48 | help="use local minima after peak detection") |
---|
49 | parser.add_option("-c","--cut", |
---|
50 | action="store_true", dest="cut", default=False, |
---|
51 | help="cut input sound file at detected labels \ |
---|
52 | best used with option -L") |
---|
53 | # to be implemented |
---|
54 | parser.add_option("-n","--note", |
---|
55 | action="store_true", dest="note", default=False, |
---|
56 | help="NOT IMPLEMENTED output notes") |
---|
57 | # plotting functions |
---|
58 | parser.add_option("-p","--plot", |
---|
59 | action="store_true", dest="plot", default=False, |
---|
60 | help="NOT IMPLEMENTED draw plot") |
---|
61 | parser.add_option("-O","--outplot", |
---|
62 | action="store", dest="outplot", default=None, |
---|
63 | help="NOT IMPLEMENTED save plot to output.{ps,png}") |
---|
64 | parser.add_option("-v","--verbose", |
---|
65 | action="store_true", dest="verbose", default=False, |
---|
66 | help="make lots of noise [default]") |
---|
67 | parser.add_option("-q","--quiet", |
---|
68 | action="store_false", dest="verbose", default=False, |
---|
69 | help="be quiet") |
---|
70 | (options, args) = parser.parse_args() |
---|
71 | if not options.bufsize: |
---|
72 | if options.mode == aubio_pitch_yin: options.bufsize = 1024 |
---|
73 | if options.mode == aubio_pitch_schmitt: options.bufsize = 2048 |
---|
74 | if options.mode == aubio_pitch_mcomb: options.bufsize = 4096 |
---|
75 | if options.mode == aubio_pitch_fcomb: options.bufsize = 4096 |
---|
76 | if not options.hopsize: |
---|
77 | options.hopsize = float(options.bufsize) / 2 |
---|
78 | if not options.filename: |
---|
79 | print "no file name given\n", usage |
---|
80 | sys.exit(1) |
---|
81 | return options, args |
---|
82 | |
---|
83 | options, args = parse_args() |
---|
84 | |
---|
85 | #print options.bufsize, options.hopsize |
---|
86 | |
---|
87 | filename = options.filename |
---|
88 | samplerate = float(sndfile(filename).samplerate()) |
---|
89 | hopsize = int(options.hopsize) |
---|
90 | bufsize = int(options.bufsize) |
---|
91 | step = float(samplerate)/float(hopsize) |
---|
92 | threshold = float(options.threshold) |
---|
93 | 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./step |
---|
98 | |
---|
99 | if options.note: |
---|
100 | exit("not implemented yet") |
---|
101 | else: |
---|
102 | pitch = getpitch(filename, #threshold, |
---|
103 | mode=options.mode, |
---|
104 | omode=options.omode, |
---|
105 | bufsize=bufsize,hopsize=hopsize, |
---|
106 | silence=silence) |
---|
107 | |
---|
108 | ## take back system delay |
---|
109 | #if delay != 0: |
---|
110 | # for i in range(len(onsets)): |
---|
111 | # onsets[i] -= delay*step |
---|
112 | # |
---|
113 | ## prune doubled |
---|
114 | #if mintol > 0: |
---|
115 | # last = -2*mintol |
---|
116 | # newonsets = [] |
---|
117 | # for new in onsets: |
---|
118 | # if (new - last > mintol): |
---|
119 | # newonsets.append(new) |
---|
120 | # last = new |
---|
121 | # onsets = newonsets |
---|
122 | |
---|
123 | # print times in second |
---|
124 | if options.verbose: |
---|
125 | for i in range(len(pitch)): |
---|
126 | print "%f\t%f" % (i/step,pitch[i]) |
---|
127 | |
---|
128 | if options.plot: |
---|
129 | from aubio.gnuplot import plot_pitch |
---|
130 | plot_pitch(filename, pitch, |
---|
131 | samplerate=samplerate, hopsize=hopsize, outplot=options.outplot) |
---|
132 | |
---|