[98df9f4] | 1 | #! /usr/bin/python |
---|
[96fb8ad] | 2 | |
---|
| 3 | """ this file was written by Paul Brossier |
---|
| 4 | it is released under the GNU/GPL license. |
---|
| 5 | """ |
---|
| 6 | |
---|
[98df9f4] | 7 | import sys |
---|
[96fb8ad] | 8 | from aubio.aubioclass import * |
---|
| 9 | |
---|
[98df9f4] | 10 | usage = "usage: %s [options] -i soundfile" % sys.argv[0] |
---|
[96fb8ad] | 11 | |
---|
[98df9f4] | 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) |
---|
[f841b81] | 24 | elif nvalue == 'kl' : |
---|
| 25 | setattr(parser.values, option.dest, kl) |
---|
| 26 | elif nvalue == 'mkl' : |
---|
| 27 | setattr(parser.values, option.dest, mkl) |
---|
[98df9f4] | 28 | elif nvalue == 'dual' : |
---|
| 29 | setattr(parser.values, option.dest, 'dual') |
---|
[f841b81] | 30 | else: |
---|
| 31 | print "unknown detection function selected\n", usage |
---|
| 32 | sys.exit(1) |
---|
[98df9f4] | 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', |
---|
[80c0417] | 42 | help="onset detection mode [default=dual] \ |
---|
[2d04b86] | 43 | complexdomain|hfc|phase|specdiff|energy|kl|mkl|dual") |
---|
[98df9f4] | 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", |
---|
[2d04b86] | 48 | action="store", dest="hopsize", default=256, |
---|
| 49 | help="overlap size [default=256]") |
---|
[98df9f4] | 50 | parser.add_option("-t","--threshold", |
---|
[2d04b86] | 51 | action="store", dest="threshold", default=0.650, |
---|
| 52 | help="onset peak picking threshold [default=0.650]") |
---|
[98df9f4] | 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", |
---|
[80c0417] | 60 | action="store", dest="delay", |
---|
| 61 | help="number of seconds to take back [default=system]\ |
---|
| 62 | default system delay is 2*hopsize/samplerate") |
---|
[98df9f4] | 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") |
---|
[80c0417] | 70 | parser.add_option("-d","--derivate", |
---|
| 71 | action="store_true", dest="derivate", default=False, |
---|
| 72 | help="derivate onset detection function") |
---|
[13340a68] | 73 | parser.add_option("-S","--silencecut", |
---|
| 74 | action="store_true", dest="silencecut", default=False, |
---|
| 75 | help="outputs silence locations") |
---|
[98df9f4] | 76 | # to be implemented |
---|
[13340a68] | 77 | |
---|
[98df9f4] | 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") |
---|
[80c0417] | 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}") |
---|
[98df9f4] | 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()) |
---|
[382c42e] | 107 | hopsize = int(options.hopsize) |
---|
| 108 | bufsize = int(options.bufsize) |
---|
| 109 | step = float(samplerate)/float(hopsize) |
---|
[98df9f4] | 110 | threshold = float(options.threshold) |
---|
| 111 | silence = float(options.silence) |
---|
[382c42e] | 112 | mintol = float(options.mintol)*step |
---|
[80c0417] | 113 | # default take back system delay |
---|
| 114 | if options.delay: delay = float(options.delay) |
---|
| 115 | else: delay = 2./step |
---|
[98df9f4] | 116 | |
---|
| 117 | if options.beat: |
---|
| 118 | #onsets = getbeats(filename,threshold,silence,mode=options.mode) |
---|
| 119 | exit("not implemented yet") |
---|
[13340a68] | 120 | elif options.silencecut: |
---|
| 121 | onsets = getsilences(filename,hopsize=hopsize,silence=silence) |
---|
[80c0417] | 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) |
---|
[98df9f4] | 127 | else: |
---|
| 128 | onsets = getonsets(filename,threshold,silence, |
---|
| 129 | mode=options.mode,localmin=options.localmin, |
---|
[80c0417] | 130 | derivate=options.derivate, |
---|
[98df9f4] | 131 | bufsize=bufsize,hopsize=hopsize) |
---|
| 132 | |
---|
| 133 | # take back system delay |
---|
| 134 | if delay != 0: |
---|
| 135 | for i in range(len(onsets)): |
---|
[382c42e] | 136 | onsets[i] -= delay*step |
---|
[98df9f4] | 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: |
---|
[382c42e] | 150 | for i in onsets: print "%f" % (i/step) |
---|
[98df9f4] | 151 | |
---|
[80c0417] | 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 | |
---|
[98df9f4] | 157 | if options.cut: |
---|
| 158 | cutfile(filename,onsets,bufsize=bufsize,hopsize=hopsize) |
---|