[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 | import sys |
---|
[5e491b3b] | 8 | from aubio.tasks import * |
---|
[d4a0cc4] | 9 | |
---|
| 10 | usage = "usage: %s [options] -i soundfile" % sys.argv[0] |
---|
| 11 | |
---|
| 12 | |
---|
| 13 | def parse_args(): |
---|
| 14 | from optparse import OptionParser |
---|
| 15 | parser = OptionParser(usage=usage) |
---|
| 16 | parser.add_option("-i","--input", |
---|
| 17 | action="store", dest="filename", |
---|
| 18 | help="input sound file") |
---|
| 19 | parser.add_option("-m","--mode", action="callback", |
---|
[5cf415f] | 20 | callback=check_pitch_mode, dest="mode", |
---|
[bfe76d4] | 21 | default=[aubio_pitch_mcomb], |
---|
[97b8c3d] | 22 | help="pitch detection mode [default=mcomb] \ |
---|
[d4a0cc4] | 23 | mcomb|yin|fcomb|schmitt") |
---|
[aa17581] | 24 | parser.add_option("-u","--units", action="callback", |
---|
| 25 | callback=check_pitchm_mode, dest="omode", |
---|
| 26 | default=aubio_pitchm_freq, |
---|
| 27 | help="output pitch in units [default=Hz] \ |
---|
| 28 | freq|midi|cent|bin") |
---|
[d4a0cc4] | 29 | parser.add_option("-B","--bufsize", |
---|
[aa17581] | 30 | action="store", dest="bufsize", default=None, |
---|
[d4a0cc4] | 31 | help="buffer size [default=1024]") |
---|
| 32 | parser.add_option("-H","--hopsize", |
---|
[aa17581] | 33 | action="store", dest="hopsize", default=None, |
---|
[d4a0cc4] | 34 | help="overlap size [default=512]") |
---|
| 35 | parser.add_option("-t","--threshold", |
---|
| 36 | action="store", dest="threshold", default=0.1, |
---|
| 37 | help="pitch threshold (for yin) [default=0.1]") |
---|
| 38 | parser.add_option("-s","--silence", |
---|
| 39 | action="store", dest="silence", default=-70, |
---|
| 40 | help="silence threshold [default=-70]") |
---|
| 41 | parser.add_option("-D","--delay", |
---|
| 42 | action="store", dest="delay", |
---|
| 43 | help="number of seconds to take back [default=system]\ |
---|
| 44 | default system delay is 2*hopsize/samplerate") |
---|
| 45 | parser.add_option("-L","--localmin", |
---|
| 46 | action="store_true", dest="localmin", default=False, |
---|
| 47 | help="use local minima after peak detection") |
---|
| 48 | parser.add_option("-c","--cut", |
---|
| 49 | action="store_true", dest="cut", default=False, |
---|
| 50 | help="cut input sound file at detected labels \ |
---|
| 51 | best used with option -L") |
---|
| 52 | # to be implemented |
---|
| 53 | parser.add_option("-n","--note", |
---|
| 54 | action="store_true", dest="note", default=False, |
---|
| 55 | help="NOT IMPLEMENTED output notes") |
---|
| 56 | # plotting functions |
---|
| 57 | parser.add_option("-p","--plot", |
---|
| 58 | action="store_true", dest="plot", default=False, |
---|
| 59 | help="NOT IMPLEMENTED draw plot") |
---|
| 60 | parser.add_option("-O","--outplot", |
---|
| 61 | action="store", dest="outplot", default=None, |
---|
| 62 | help="NOT IMPLEMENTED save plot to output.{ps,png}") |
---|
| 63 | parser.add_option("-v","--verbose", |
---|
| 64 | action="store_true", dest="verbose", default=False, |
---|
| 65 | help="make lots of noise [default]") |
---|
| 66 | parser.add_option("-q","--quiet", |
---|
| 67 | action="store_false", dest="verbose", default=False, |
---|
| 68 | help="be quiet") |
---|
| 69 | (options, args) = parser.parse_args() |
---|
[aa17581] | 70 | if not options.bufsize: |
---|
| 71 | if options.mode == aubio_pitch_yin: options.bufsize = 1024 |
---|
| 72 | if options.mode == aubio_pitch_schmitt: options.bufsize = 2048 |
---|
| 73 | if options.mode == aubio_pitch_mcomb: options.bufsize = 4096 |
---|
| 74 | if options.mode == aubio_pitch_fcomb: options.bufsize = 4096 |
---|
[5e491b3b] | 75 | else: options.bufsize = 2048 |
---|
[aa17581] | 76 | if not options.hopsize: |
---|
| 77 | options.hopsize = float(options.bufsize) / 2 |
---|
[d4a0cc4] | 78 | if not options.filename: |
---|
[aa17581] | 79 | print "no file name given\n", usage |
---|
| 80 | sys.exit(1) |
---|
[d4a0cc4] | 81 | return options, args |
---|
| 82 | |
---|
| 83 | options, args = parse_args() |
---|
| 84 | |
---|
[aa17581] | 85 | #print options.bufsize, options.hopsize |
---|
| 86 | |
---|
[d4a0cc4] | 87 | filename = options.filename |
---|
| 88 | samplerate = float(sndfile(filename).samplerate()) |
---|
| 89 | hopsize = int(options.hopsize) |
---|
| 90 | bufsize = int(options.bufsize) |
---|
| 91 | step = float(samplerate)/float(hopsize) |
---|
| 92 | threshold = float(options.threshold) |
---|
| 93 | silence = float(options.silence) |
---|
[5e491b3b] | 94 | mode = options.mode |
---|
[d4a0cc4] | 95 | #mintol = float(options.mintol)*step |
---|
| 96 | # default take back system delay |
---|
| 97 | if options.delay: delay = float(options.delay) |
---|
| 98 | else: delay = 2./step |
---|
| 99 | |
---|
| 100 | if options.note: |
---|
| 101 | exit("not implemented yet") |
---|
| 102 | else: |
---|
[5e491b3b] | 103 | pitch = [] |
---|
| 104 | for i in range(len(mode)): |
---|
| 105 | pitch.append(getpitch(filename, #threshold, |
---|
| 106 | mode=mode[i], |
---|
| 107 | omode=options.omode, |
---|
| 108 | bufsize=bufsize,hopsize=hopsize, |
---|
| 109 | silence=silence)) |
---|
[a49b0bc] | 110 | for j in range(len(pitch[i])): |
---|
| 111 | if pitch[i][j] > 1500 or pitch[i][j] < 40: |
---|
| 112 | pitch[i][j] = 0.; |
---|
[d4a0cc4] | 113 | |
---|
| 114 | ## take back system delay |
---|
| 115 | #if delay != 0: |
---|
| 116 | # for i in range(len(onsets)): |
---|
| 117 | # onsets[i] -= delay*step |
---|
| 118 | # |
---|
| 119 | ## prune doubled |
---|
| 120 | #if mintol > 0: |
---|
| 121 | # last = -2*mintol |
---|
| 122 | # newonsets = [] |
---|
| 123 | # for new in onsets: |
---|
| 124 | # if (new - last > mintol): |
---|
| 125 | # newonsets.append(new) |
---|
| 126 | # last = new |
---|
| 127 | # onsets = newonsets |
---|
| 128 | |
---|
| 129 | # print times in second |
---|
| 130 | if options.verbose: |
---|
[5e491b3b] | 131 | for j in range(len(pitch[0])): |
---|
| 132 | print "%f\t" % (j/step), |
---|
| 133 | for i in range(len(pitch)): |
---|
| 134 | print "%f\t" % pitch[i][j], |
---|
| 135 | print |
---|
[d4a0cc4] | 136 | |
---|
| 137 | if options.plot: |
---|
| 138 | from aubio.gnuplot import plot_pitch |
---|
| 139 | plot_pitch(filename, pitch, |
---|
| 140 | samplerate=samplerate, hopsize=hopsize, outplot=options.outplot) |
---|