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