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.aubioclass import * |
---|
9 | |
---|
10 | usage = "usage: %s [options] -i soundfile" % sys.argv[0] |
---|
11 | |
---|
12 | def check_mode(option, opt, value, parser): |
---|
13 | nvalue = parser.rargs[0] |
---|
14 | if nvalue == 'complexdomain' : |
---|
15 | setattr(parser.values, option.dest, complexdomain) |
---|
16 | elif nvalue == 'hfc' : |
---|
17 | setattr(parser.values, option.dest, hfc) |
---|
18 | elif nvalue == 'phase' : |
---|
19 | setattr(parser.values, option.dest, phase) |
---|
20 | elif nvalue == 'specdiff' : |
---|
21 | setattr(parser.values, option.dest, specdiff) |
---|
22 | elif nvalue == 'energy' : |
---|
23 | setattr(parser.values, option.dest, energy) |
---|
24 | elif nvalue == 'kl' : |
---|
25 | setattr(parser.values, option.dest, kl) |
---|
26 | elif nvalue == 'mkl' : |
---|
27 | setattr(parser.values, option.dest, mkl) |
---|
28 | elif nvalue == 'dual' : |
---|
29 | setattr(parser.values, option.dest, 'dual') |
---|
30 | else: |
---|
31 | print "unknown detection function selected\n", usage |
---|
32 | sys.exit(1) |
---|
33 | |
---|
34 | def parse_args(): |
---|
35 | from optparse import OptionParser |
---|
36 | parser = OptionParser(usage=usage) |
---|
37 | parser.add_option("-i","--input", |
---|
38 | action="store", dest="filename", |
---|
39 | help="input sound file") |
---|
40 | parser.add_option("-m","--mode", action="callback", |
---|
41 | callback=check_mode, dest="mode", default='dual', |
---|
42 | help="onset detection mode [default=dual] \ |
---|
43 | complexdomain|hfc|phase|specdiff|energy|kl|mkl|dual") |
---|
44 | parser.add_option("-B","--bufsize", |
---|
45 | action="store", dest="bufsize", default=1024, |
---|
46 | help="buffer size [default=1024]") |
---|
47 | parser.add_option("-H","--hopsize", |
---|
48 | action="store", dest="hopsize", default=256, |
---|
49 | help="overlap size [default=256]") |
---|
50 | parser.add_option("-t","--threshold", |
---|
51 | action="store", dest="threshold", default=0.650, |
---|
52 | help="onset peak picking threshold [default=0.650]") |
---|
53 | parser.add_option("-s","--silence", |
---|
54 | action="store", dest="silence", default=-70, |
---|
55 | help="silence threshold [default=-70]") |
---|
56 | parser.add_option("-M","--mintol", |
---|
57 | action="store", dest="mintol", default=0.048, |
---|
58 | help="minimum inter onset interval [default=0.048]") |
---|
59 | parser.add_option("-D","--delay", |
---|
60 | action="store", dest="delay", |
---|
61 | help="number of seconds to take back [default=system]\ |
---|
62 | default system delay is 2*hopsize/samplerate") |
---|
63 | parser.add_option("-L","--localmin", |
---|
64 | action="store_true", dest="localmin", default=False, |
---|
65 | help="use local minima after peak detection") |
---|
66 | parser.add_option("-c","--cut", |
---|
67 | action="store_true", dest="cut", default=False, |
---|
68 | help="cut input sound file at detected labels \ |
---|
69 | best used with option -L") |
---|
70 | parser.add_option("-d","--derivate", |
---|
71 | action="store_true", dest="derivate", default=False, |
---|
72 | help="derivate onset detection function") |
---|
73 | parser.add_option("-S","--silencecut", |
---|
74 | action="store_true", dest="silencecut", default=False, |
---|
75 | help="outputs silence locations") |
---|
76 | # to be implemented |
---|
77 | |
---|
78 | parser.add_option("-z","--zerocross", |
---|
79 | action="store_true", dest="zerocross", default=False, |
---|
80 | help="NOT IMPLEMENTED zero crossing matching") |
---|
81 | parser.add_option("-b","--beat", |
---|
82 | action="store_true", dest="beat", default=False, |
---|
83 | help="NOT IMPLEMENTED output beat locations") |
---|
84 | # plotting functions |
---|
85 | parser.add_option("-p","--plot", |
---|
86 | action="store_true", dest="plot", default=False, |
---|
87 | help="draw plot") |
---|
88 | parser.add_option("-O","--outplot", |
---|
89 | action="store", dest="outplot", default=None, |
---|
90 | help="save plot to output.{ps,png}") |
---|
91 | parser.add_option("-v","--verbose", |
---|
92 | action="store_true", dest="verbose", default=False, |
---|
93 | help="make lots of noise [default]") |
---|
94 | parser.add_option("-q","--quiet", |
---|
95 | action="store_false", dest="verbose", default=False, |
---|
96 | help="be quiet") |
---|
97 | (options, args) = parser.parse_args() |
---|
98 | if not options.filename: |
---|
99 | print "no file name given\n", usage |
---|
100 | sys.exit(1) |
---|
101 | return options, args |
---|
102 | |
---|
103 | options, args = parse_args() |
---|
104 | |
---|
105 | filename = options.filename |
---|
106 | samplerate = float(sndfile(filename).samplerate()) |
---|
107 | hopsize = int(options.hopsize) |
---|
108 | bufsize = int(options.bufsize) |
---|
109 | step = float(samplerate)/float(hopsize) |
---|
110 | threshold = float(options.threshold) |
---|
111 | silence = float(options.silence) |
---|
112 | mintol = float(options.mintol)*step |
---|
113 | # default take back system delay |
---|
114 | if options.delay: delay = float(options.delay) |
---|
115 | else: delay = 2./step |
---|
116 | |
---|
117 | if options.beat: |
---|
118 | #onsets = getbeats(filename,threshold,silence,mode=options.mode) |
---|
119 | exit("not implemented yet") |
---|
120 | elif options.silencecut: |
---|
121 | onsets = getsilences(filename,hopsize=hopsize,silence=silence) |
---|
122 | elif options.plot: |
---|
123 | onsets, ofunc = getonsets(filename,threshold,silence, |
---|
124 | mode=options.mode,localmin=options.localmin, |
---|
125 | derivate=options.derivate, |
---|
126 | bufsize=bufsize,hopsize=hopsize,storefunc=True) |
---|
127 | else: |
---|
128 | onsets = getonsets(filename,threshold,silence, |
---|
129 | mode=options.mode,localmin=options.localmin, |
---|
130 | derivate=options.derivate, |
---|
131 | bufsize=bufsize,hopsize=hopsize) |
---|
132 | |
---|
133 | # take back system delay |
---|
134 | if delay != 0: |
---|
135 | for i in range(len(onsets)): |
---|
136 | onsets[i] -= delay*step |
---|
137 | |
---|
138 | # prune doubled |
---|
139 | if mintol > 0: |
---|
140 | last = -2*mintol |
---|
141 | newonsets = [] |
---|
142 | for new in onsets: |
---|
143 | if (new - last > mintol): |
---|
144 | newonsets.append(new) |
---|
145 | last = new |
---|
146 | onsets = newonsets |
---|
147 | |
---|
148 | # print times in second |
---|
149 | if options.verbose: |
---|
150 | for i in onsets: print "%f" % (i/step) |
---|
151 | |
---|
152 | if options.plot: |
---|
153 | from aubio.gnuplot import plot_onsets |
---|
154 | plot_onsets(filename, onsets, ofunc, |
---|
155 | samplerate=samplerate, hopsize=hopsize, outplot=options.outplot) |
---|
156 | |
---|
157 | if options.cut: |
---|
158 | cutfile(filename,onsets,bufsize=bufsize,hopsize=hopsize) |
---|