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