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