[96fb8ad] | 1 | #!/usr/bin/python |
---|
| 2 | |
---|
[d4a0cc4] | 3 | """ this file was written by Paul Brossier |
---|
| 4 | it is released under the GNU/GPL license. |
---|
| 5 | """ |
---|
| 6 | |
---|
| 7 | |
---|
| 8 | import sys |
---|
| 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 == 'mcomb' : |
---|
| 16 | setattr(parser.values, option.dest, aubio_mcomb) |
---|
| 17 | elif nvalue == 'yin' : |
---|
| 18 | setattr(parser.values, option.dest, aubio_yin) |
---|
| 19 | elif nvalue == 'fcomb' : |
---|
| 20 | setattr(parser.values, option.dest, aubio_fcomb) |
---|
| 21 | elif nvalue == 'schmitt' : |
---|
| 22 | setattr(parser.values, option.dest, aubio_schmitt) |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | def parse_args(): |
---|
| 26 | from optparse import OptionParser |
---|
| 27 | parser = OptionParser(usage=usage) |
---|
| 28 | parser.add_option("-i","--input", |
---|
| 29 | action="store", dest="filename", |
---|
| 30 | help="input sound file") |
---|
| 31 | parser.add_option("-m","--mode", action="callback", |
---|
[97b8c3d] | 32 | callback=check_mode, dest="mode", default=aubio_mcomb, |
---|
| 33 | help="pitch detection mode [default=mcomb] \ |
---|
[d4a0cc4] | 34 | mcomb|yin|fcomb|schmitt") |
---|
| 35 | parser.add_option("-B","--bufsize", |
---|
| 36 | action="store", dest="bufsize", default=1024, |
---|
| 37 | help="buffer size [default=1024]") |
---|
| 38 | parser.add_option("-H","--hopsize", |
---|
| 39 | action="store", dest="hopsize", default=512, |
---|
| 40 | help="overlap size [default=512]") |
---|
| 41 | parser.add_option("-t","--threshold", |
---|
| 42 | action="store", dest="threshold", default=0.1, |
---|
| 43 | help="pitch threshold (for yin) [default=0.1]") |
---|
| 44 | parser.add_option("-s","--silence", |
---|
| 45 | action="store", dest="silence", default=-70, |
---|
| 46 | help="silence threshold [default=-70]") |
---|
| 47 | parser.add_option("-D","--delay", |
---|
| 48 | action="store", dest="delay", |
---|
| 49 | help="number of seconds to take back [default=system]\ |
---|
| 50 | default system delay is 2*hopsize/samplerate") |
---|
| 51 | parser.add_option("-L","--localmin", |
---|
| 52 | action="store_true", dest="localmin", default=False, |
---|
| 53 | help="use local minima after peak detection") |
---|
| 54 | parser.add_option("-c","--cut", |
---|
| 55 | action="store_true", dest="cut", default=False, |
---|
| 56 | help="cut input sound file at detected labels \ |
---|
| 57 | best used with option -L") |
---|
| 58 | # to be implemented |
---|
| 59 | parser.add_option("-n","--note", |
---|
| 60 | action="store_true", dest="note", default=False, |
---|
| 61 | help="NOT IMPLEMENTED output notes") |
---|
| 62 | # plotting functions |
---|
| 63 | parser.add_option("-p","--plot", |
---|
| 64 | action="store_true", dest="plot", default=False, |
---|
| 65 | help="NOT IMPLEMENTED draw plot") |
---|
| 66 | parser.add_option("-O","--outplot", |
---|
| 67 | action="store", dest="outplot", default=None, |
---|
| 68 | help="NOT IMPLEMENTED 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.note: |
---|
| 96 | exit("not implemented yet") |
---|
| 97 | else: |
---|
| 98 | pitch = getpitch(filename, #threshold,silence, |
---|
| 99 | mode=options.mode, |
---|
| 100 | bufsize=bufsize,hopsize=hopsize) |
---|
| 101 | |
---|
| 102 | ## take back system delay |
---|
| 103 | #if delay != 0: |
---|
| 104 | # for i in range(len(onsets)): |
---|
| 105 | # onsets[i] -= delay*step |
---|
| 106 | # |
---|
| 107 | ## prune doubled |
---|
| 108 | #if mintol > 0: |
---|
| 109 | # last = -2*mintol |
---|
| 110 | # newonsets = [] |
---|
| 111 | # for new in onsets: |
---|
| 112 | # if (new - last > mintol): |
---|
| 113 | # newonsets.append(new) |
---|
| 114 | # last = new |
---|
| 115 | # onsets = newonsets |
---|
| 116 | |
---|
| 117 | # print times in second |
---|
| 118 | if options.verbose: |
---|
| 119 | for i in range(len(pitch)): |
---|
| 120 | print "%f\t%f" % (i/step,pitch[i]) |
---|
| 121 | |
---|
| 122 | if options.plot: |
---|
| 123 | from aubio.gnuplot import plot_pitch |
---|
| 124 | plot_pitch(filename, pitch, |
---|
| 125 | samplerate=samplerate, hopsize=hopsize, outplot=options.outplot) |
---|
| 126 | |
---|