[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 |
---|
| 8 | import numarray |
---|
[96fb8ad] | 9 | from aubio.aubioclass import * |
---|
| 10 | |
---|
[98df9f4] | 11 | usage = "usage: %s [options] -i soundfile" % sys.argv[0] |
---|
[96fb8ad] | 12 | |
---|
[98df9f4] | 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', |
---|
[80c0417] | 36 | help="onset detection mode [default=dual] \ |
---|
[98df9f4] | 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", |
---|
[80c0417] | 54 | action="store", dest="delay", |
---|
| 55 | help="number of seconds to take back [default=system]\ |
---|
| 56 | default system delay is 2*hopsize/samplerate") |
---|
[98df9f4] | 57 | parser.add_option("-L","--localmin", |
---|
| 58 | action="store_true", dest="localmin", default=False, |
---|
| 59 | help="use local minima after peak detection") |
---|
| 60 | parser.add_option("-c","--cut", |
---|
| 61 | action="store_true", dest="cut", default=False, |
---|
| 62 | help="cut input sound file at detected labels \ |
---|
| 63 | best used with option -L") |
---|
[80c0417] | 64 | parser.add_option("-d","--derivate", |
---|
| 65 | action="store_true", dest="derivate", default=False, |
---|
| 66 | help="derivate onset detection function") |
---|
[98df9f4] | 67 | # to be implemented |
---|
| 68 | parser.add_option("-z","--zerocross", |
---|
| 69 | action="store_true", dest="zerocross", default=False, |
---|
| 70 | help="NOT IMPLEMENTED zero crossing matching") |
---|
| 71 | parser.add_option("-b","--beat", |
---|
| 72 | action="store_true", dest="beat", default=False, |
---|
| 73 | help="NOT IMPLEMENTED output beat locations") |
---|
[80c0417] | 74 | # plotting functions |
---|
| 75 | parser.add_option("-p","--plot", |
---|
| 76 | action="store_true", dest="plot", default=False, |
---|
| 77 | help="draw plot") |
---|
| 78 | parser.add_option("-O","--outplot", |
---|
| 79 | action="store", dest="outplot", default=None, |
---|
| 80 | help="save plot to output.{ps,png}") |
---|
[98df9f4] | 81 | parser.add_option("-v","--verbose", |
---|
| 82 | action="store_true", dest="verbose", default=False, |
---|
| 83 | help="make lots of noise [default]") |
---|
| 84 | parser.add_option("-q","--quiet", |
---|
| 85 | action="store_false", dest="verbose", default=False, |
---|
| 86 | help="be quiet") |
---|
| 87 | (options, args) = parser.parse_args() |
---|
| 88 | if not options.filename: |
---|
| 89 | print "no file name given\n", usage |
---|
| 90 | sys.exit(1) |
---|
| 91 | return options, args |
---|
| 92 | |
---|
| 93 | options, args = parse_args() |
---|
| 94 | |
---|
| 95 | filename = options.filename |
---|
| 96 | samplerate = float(sndfile(filename).samplerate()) |
---|
[382c42e] | 97 | hopsize = int(options.hopsize) |
---|
| 98 | bufsize = int(options.bufsize) |
---|
| 99 | step = float(samplerate)/float(hopsize) |
---|
[98df9f4] | 100 | threshold = float(options.threshold) |
---|
| 101 | silence = float(options.silence) |
---|
[382c42e] | 102 | mintol = float(options.mintol)*step |
---|
[80c0417] | 103 | # default take back system delay |
---|
| 104 | if options.delay: delay = float(options.delay) |
---|
| 105 | else: delay = 2./step |
---|
[98df9f4] | 106 | |
---|
| 107 | if options.beat: |
---|
| 108 | #onsets = getbeats(filename,threshold,silence,mode=options.mode) |
---|
| 109 | exit("not implemented yet") |
---|
[80c0417] | 110 | elif options.plot: |
---|
| 111 | onsets, ofunc = getonsets(filename,threshold,silence, |
---|
| 112 | mode=options.mode,localmin=options.localmin, |
---|
| 113 | derivate=options.derivate, |
---|
| 114 | bufsize=bufsize,hopsize=hopsize,storefunc=True) |
---|
[98df9f4] | 115 | else: |
---|
| 116 | onsets = getonsets(filename,threshold,silence, |
---|
| 117 | mode=options.mode,localmin=options.localmin, |
---|
[80c0417] | 118 | derivate=options.derivate, |
---|
[98df9f4] | 119 | bufsize=bufsize,hopsize=hopsize) |
---|
| 120 | |
---|
| 121 | # take back system delay |
---|
| 122 | if delay != 0: |
---|
| 123 | for i in range(len(onsets)): |
---|
[382c42e] | 124 | onsets[i] -= delay*step |
---|
[98df9f4] | 125 | |
---|
| 126 | # prune doubled |
---|
| 127 | if mintol > 0: |
---|
| 128 | last = -2*mintol |
---|
| 129 | newonsets = [] |
---|
| 130 | for new in onsets: |
---|
| 131 | if (new - last > mintol): |
---|
| 132 | newonsets.append(new) |
---|
| 133 | last = new |
---|
| 134 | onsets = newonsets |
---|
| 135 | |
---|
| 136 | # print times in second |
---|
| 137 | if options.verbose: |
---|
[382c42e] | 138 | for i in onsets: print "%f" % (i/step) |
---|
[98df9f4] | 139 | |
---|
[80c0417] | 140 | if options.plot: |
---|
| 141 | from aubio.gnuplot import plot_onsets |
---|
| 142 | plot_onsets(filename, onsets, ofunc, |
---|
| 143 | samplerate=samplerate, hopsize=hopsize, outplot=options.outplot) |
---|
| 144 | |
---|
[98df9f4] | 145 | if options.cut: |
---|
| 146 | cutfile(filename,onsets,bufsize=bufsize,hopsize=hopsize) |
---|