Showing the changes to python (2 of 2 files). Show all
Unified · Split
| @@ -223,3 +223,75 @@ def plot_onsets(filename, onsets, ofunc, samplerate=44100., hopsize=512, outplot · 222 unmodified lines | ||
| 223 | 223 | g.plot(d,e2) |
| 224 | 224 | |
| 225 | 225 | g('unset multiplot') |
| 226 | + | |
| 227 | + | |
| 228 | +def plot_pitch(filename, pitch, samplerate=44100., hopsize=512, outplot=None): | |
| 229 | + import aubio.txtfile | |
| 230 | + import os.path | |
| 231 | + import numarray | |
| 232 | + | |
| 233 | + # onset detection function | |
| 234 | + downtime = (hopsize/samplerate)*numarray.arange(len(pitch)) | |
| 235 | + d = Gnuplot.Data(downtime,pitch,with='lines') | |
| 236 | + | |
| 237 | + # check if datafile exists truth | |
| 238 | + datafile = filename.replace('.wav','.txt') | |
| 239 | + if not os.path.isfile(datafile): | |
| 240 | + title = "truth file not found" | |
| 241 | + t = Gnuplot.Data(0,0,with='impulses') | |
| 242 | + else: | |
| 243 | + title = "truth file plotting not implemented yet" | |
| 244 | + t = Gnuplot.Data(0,0,with='impulses') | |
| 245 | + #times,pitch = aubio.txtfile.read_datafile(datafile) | |
| 246 | + #t = Gnuplot.Data(times,pitch,with='lines') | |
| 247 | + | |
| 248 | + #orig, missed, merged, expc, bad, doubled = \ | |
| 249 | + # onset_roc(x2,x1,tol) | |
| 250 | + #title = "GD %2.3f%% FP %2.3f%%" % \ | |
| 251 | + # ((100*float(orig-missed-merged)/(orig)), | |
| 252 | + # (100*float(bad+doubled)/(orig))) | |
| 253 | + #print orig, missed, merged, expc, bad, doubled | |
| 254 | + #print "GD %2.8f\t" % (100*float(orig-missed-merged)/(orig)), | |
| 255 | + #print "FP %2.8f\t" % (100*float(bad+doubled)/(orig)) , | |
| 256 | + #print "GD-merged %2.8f\t" % (100*float(orig-missed)/(orig)) , | |
| 257 | + #print "FP-pruned %2.8f\t" % (100*float(bad)/(orig)) | |
| 258 | + | |
| 259 | + # audio data | |
| 260 | + time,data = audio_to_array(filename) | |
| 261 | + f = make_audio_plot(time,data) | |
| 262 | + | |
| 263 | + # prepare the plot | |
| 264 | + g = Gnuplot.Gnuplot(debug=1, persist=1) | |
| 265 | + if outplot: | |
| 266 | + extension = outplot.split('.')[-1] | |
| 267 | + if extension == 'ps': extension = 'postscript' | |
| 268 | + g('set terminal %s' % extension) | |
| 269 | + g('set output \'%s\'' % outplot) | |
| 270 | + | |
| 271 | + g('set title \'%s %s\'' % (filename,title)) | |
| 272 | + | |
| 273 | + g('set multiplot') | |
| 274 | + | |
| 275 | + # hack to align left axis | |
| 276 | + g('set lmargin 15') | |
| 277 | + | |
| 278 | + # plot waveform and onsets | |
| 279 | + g('set size 1,0.3') | |
| 280 | + g('set origin 0,0.7') | |
| 281 | + g('set xrange [0:%f]' % max(time)) | |
| 282 | + g('set yrange [-1:1]') | |
| 283 | + g.ylabel('amplitude') | |
| 284 | + g.plot(f) | |
| 285 | + | |
| 286 | + g('unset title') | |
| 287 | + | |
| 288 | + # plot onset detection function | |
| 289 | + g('set size 1,0.7') | |
| 290 | + g('set origin 0,0') | |
| 291 | + g('set xrange [0:%f]' % (hopsize/samplerate*len(pitch))) | |
| 292 | + g('set yrange [0:%f]' % (max(pitch)*1.01)) | |
| 293 | + g.xlabel('time') | |
| 294 | + g.ylabel('frequency (Hz)') | |
| 295 | + g.plot(d,t) | |
| 296 | + | |
| 297 | + g('unset multiplot') | |
| @@ -1,24 +1,127 @@ | ||
| 1 | 1 | #!/usr/bin/python |
| 2 | 2 | |
| 3 | −def do(filein): | |
| 4 | − from aubio import aubioclass | |
| 5 | − hopsize = 512 | |
| 6 | − bufsize = 4096 | |
| 7 | − frameread = 0 | |
| 8 | − filei = aubioclass.sndfile(filein) | |
| 9 | − srate = filei.samplerate() | |
| 10 | − channels = filei.channels() | |
| 11 | − myvec = aubioclass.fvec(hopsize,channels) | |
| 12 | − readsize = filei.read(hopsize,myvec) | |
| 13 | − ppick = aubioclass.pitchpick(bufsize,hopsize,channels,myvec,srate) | |
| 14 | − while(readsize==hopsize): | |
| 15 | − readsize = filei.read(hopsize,myvec) | |
| 16 | − val = ppick.do(myvec) | |
| 17 | − freq = aubioclass.bintofreq(val,srate,bufsize) | |
| 18 | − now = (frameread)*hopsize/(srate+0.) | |
| 19 | − print "%.3f %.2f" % (now,freq) | |
| 20 | − frameread += 1 | |
| 21 | − | |
| 22 | −if __name__ == "__main__": | |
| 23 | − import sys | |
| 24 | − do(sys.argv[1]) | |
| 3 | +""" this file was written by Paul Brossier | |
| 4 | + it is released under the GNU/GPL license. | |
| 5 | +""" | |
| 6 | + | |
| 7 | + | |
| 8 | +import sys | |
| 9 | +import numarray | |
| 10 | +from aubio.aubioclass import * | |
| 11 | + | |
| 12 | +usage = "usage: %s [options] -i soundfile" % sys.argv[0] | |
| 13 | + | |
| 14 | +def check_mode(option, opt, value, parser): | |
| 15 | + nvalue = parser.rargs[0] | |
| 16 | + if nvalue == 'mcomb' : | |
| 17 | + setattr(parser.values, option.dest, aubio_mcomb) | |
| 18 | + elif nvalue == 'yin' : | |
| 19 | + setattr(parser.values, option.dest, aubio_yin) | |
| 20 | + elif nvalue == 'fcomb' : | |
| 21 | + setattr(parser.values, option.dest, aubio_fcomb) | |
| 22 | + elif nvalue == 'schmitt' : | |
| 23 | + setattr(parser.values, option.dest, aubio_schmitt) | |
| 24 | + | |
| 25 | + | |
| 26 | +def parse_args(): | |
| 27 | + from optparse import OptionParser | |
| 28 | + parser = OptionParser(usage=usage) | |
| 29 | + parser.add_option("-i","--input", | |
| 30 | + action="store", dest="filename", | |
| 31 | + help="input sound file") | |
| 32 | + parser.add_option("-m","--mode", action="callback", | |
| 33 | + callback=check_mode, dest="mode", default=aubio_schmitt, | |
| 34 | + help="pitch detection mode [default=dual] \ | |
| 35 | + mcomb|yin|fcomb|schmitt") | |
| 36 | + parser.add_option("-B","--bufsize", | |
| 37 | + action="store", dest="bufsize", default=1024, | |
| 38 | + help="buffer size [default=1024]") | |
| 39 | + parser.add_option("-H","--hopsize", | |
| 40 | + action="store", dest="hopsize", default=512, | |
| 41 | + help="overlap size [default=512]") | |
| 42 | + parser.add_option("-t","--threshold", | |
| 43 | + action="store", dest="threshold", default=0.1, | |
| 44 | + help="pitch threshold (for yin) [default=0.1]") | |
| 45 | + parser.add_option("-s","--silence", | |
| 46 | + action="store", dest="silence", default=-70, | |
| 47 | + help="silence threshold [default=-70]") | |
| 48 | + parser.add_option("-D","--delay", | |
| 49 | + action="store", dest="delay", | |
| 50 | + help="number of seconds to take back [default=system]\ | |
| 51 | + default system delay is 2*hopsize/samplerate") | |
| 52 | + parser.add_option("-L","--localmin", | |
| 53 | + action="store_true", dest="localmin", default=False, | |
| 54 | + help="use local minima after peak detection") | |
| 55 | + parser.add_option("-c","--cut", | |
| 56 | + action="store_true", dest="cut", default=False, | |
| 57 | + help="cut input sound file at detected labels \ | |
| 58 | + best used with option -L") | |
| 59 | + # to be implemented | |
| 60 | + parser.add_option("-n","--note", | |
| 61 | + action="store_true", dest="note", default=False, | |
| 62 | + help="NOT IMPLEMENTED output notes") | |
| 63 | + # plotting functions | |
| 64 | + parser.add_option("-p","--plot", | |
| 65 | + action="store_true", dest="plot", default=False, | |
| 66 | + help="NOT IMPLEMENTED draw plot") | |
| 67 | + parser.add_option("-O","--outplot", | |
| 68 | + action="store", dest="outplot", default=None, | |
| 69 | + help="NOT IMPLEMENTED save plot to output.{ps,png}") | |
| 70 | + parser.add_option("-v","--verbose", | |
| 71 | + action="store_true", dest="verbose", default=False, | |
| 72 | + help="make lots of noise [default]") | |
| 73 | + parser.add_option("-q","--quiet", | |
| 74 | + action="store_false", dest="verbose", default=False, | |
| 75 | + help="be quiet") | |
| 76 | + (options, args) = parser.parse_args() | |
| 77 | + if not options.filename: | |
| 78 | + print "no file name given\n", usage | |
| 79 | + sys.exit(1) | |
| 80 | + return options, args | |
| 81 | + | |
| 82 | +options, args = parse_args() | |
| 83 | + | |
| 84 | +filename = options.filename | |
| 85 | +samplerate = float(sndfile(filename).samplerate()) | |
| 86 | +hopsize = int(options.hopsize) | |
| 87 | +bufsize = int(options.bufsize) | |
| 88 | +step = float(samplerate)/float(hopsize) | |
| 89 | +threshold = float(options.threshold) | |
| 90 | +silence = float(options.silence) | |
| 91 | +#mintol = float(options.mintol)*step | |
| 92 | +# default take back system delay | |
| 93 | +if options.delay: delay = float(options.delay) | |
| 94 | +else: delay = 2./step | |
| 95 | + | |
| 96 | +if options.note: | |
| 97 | + exit("not implemented yet") | |
| 98 | +else: | |
| 99 | + pitch = getpitch(filename, #threshold,silence, | |
| 100 | + mode=options.mode, | |
| 101 | + bufsize=bufsize,hopsize=hopsize) | |
| 102 | + | |
| 103 | +## take back system delay | |
| 104 | +#if delay != 0: | |
| 105 | +# for i in range(len(onsets)): | |
| 106 | +# onsets[i] -= delay*step | |
| 107 | +# | |
| 108 | +## prune doubled | |
| 109 | +#if mintol > 0: | |
| 110 | +# last = -2*mintol | |
| 111 | +# newonsets = [] | |
| 112 | +# for new in onsets: | |
| 113 | +# if (new - last > mintol): | |
| 114 | +# newonsets.append(new) | |
| 115 | +# last = new | |
| 116 | +# onsets = newonsets | |
| 117 | + | |
| 118 | +# print times in second | |
| 119 | +if options.verbose: | |
| 120 | + for i in range(len(pitch)): | |
| 121 | + print "%f\t%f" % (i/step,pitch[i]) | |
| 122 | + | |
| 123 | +if options.plot: | |
| 124 | + from aubio.gnuplot import plot_pitch | |
| 125 | + plot_pitch(filename, pitch, | |
| 126 | + samplerate=samplerate, hopsize=hopsize, outplot=options.outplot) | |
| 127 | + | |