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