[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) |
---|
| 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', |
---|
[80c0417] | 35 | help="onset detection mode [default=dual] \ |
---|
[98df9f4] | 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", |
---|
[80c0417] | 53 | action="store", dest="delay", |
---|
| 54 | help="number of seconds to take back [default=system]\ |
---|
| 55 | default system delay is 2*hopsize/samplerate") |
---|
[98df9f4] | 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") |
---|
[80c0417] | 63 | parser.add_option("-d","--derivate", |
---|
| 64 | action="store_true", dest="derivate", default=False, |
---|
| 65 | help="derivate onset detection function") |
---|
[13340a68] | 66 | parser.add_option("-S","--silencecut", |
---|
| 67 | action="store_true", dest="silencecut", default=False, |
---|
| 68 | help="outputs silence locations") |
---|
[98df9f4] | 69 | # to be implemented |
---|
[13340a68] | 70 | |
---|
[98df9f4] | 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") |
---|
[80c0417] | 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}") |
---|
[98df9f4] | 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()) |
---|
[382c42e] | 100 | hopsize = int(options.hopsize) |
---|
| 101 | bufsize = int(options.bufsize) |
---|
| 102 | step = float(samplerate)/float(hopsize) |
---|
[98df9f4] | 103 | threshold = float(options.threshold) |
---|
| 104 | silence = float(options.silence) |
---|
[382c42e] | 105 | mintol = float(options.mintol)*step |
---|
[80c0417] | 106 | # default take back system delay |
---|
| 107 | if options.delay: delay = float(options.delay) |
---|
| 108 | else: delay = 2./step |
---|
[98df9f4] | 109 | |
---|
| 110 | if options.beat: |
---|
| 111 | #onsets = getbeats(filename,threshold,silence,mode=options.mode) |
---|
| 112 | exit("not implemented yet") |
---|
[13340a68] | 113 | elif options.silencecut: |
---|
| 114 | onsets = getsilences(filename,hopsize=hopsize,silence=silence) |
---|
[80c0417] | 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) |
---|
[98df9f4] | 120 | else: |
---|
| 121 | onsets = getonsets(filename,threshold,silence, |
---|
| 122 | mode=options.mode,localmin=options.localmin, |
---|
[80c0417] | 123 | derivate=options.derivate, |
---|
[98df9f4] | 124 | bufsize=bufsize,hopsize=hopsize) |
---|
| 125 | |
---|
| 126 | # take back system delay |
---|
| 127 | if delay != 0: |
---|
| 128 | for i in range(len(onsets)): |
---|
[382c42e] | 129 | onsets[i] -= delay*step |
---|
[98df9f4] | 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: |
---|
[382c42e] | 143 | for i in onsets: print "%f" % (i/step) |
---|
[98df9f4] | 144 | |
---|
[80c0417] | 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 | |
---|
[98df9f4] | 150 | if options.cut: |
---|
| 151 | cutfile(filename,onsets,bufsize=bufsize,hopsize=hopsize) |
---|