shithub: aubio

ref: 59c046dc1d15355548e24d6e29bae383782e656a
dir: /python/aubiopitch/

View raw version
#!/usr/bin/python

""" this file was written by Paul Brossier 
  it is released under the GNU/GPL license.
"""

import sys
from aubio.task import *

usage = "usage: %s [options] -i soundfile" % sys.argv[0]


def parse_args():
  from optparse import OptionParser
  parser = OptionParser(usage=usage)
  parser.add_option("-i","--input",
      action="store", dest="filename", 
      help="input sound file")
  parser.add_option("-m","--mode", 
      action="store", dest="mode", default='yinfft',
      help="pitch detection mode [default=mcomb] \
      mcomb|yin|fcomb|schmitt")
  parser.add_option("-u","--units",
      action="store", dest="omode", default="freq",
      help="output pitch in units [default=Hz] \
      freq|midi|cent|bin")
  parser.add_option("-B","--bufsize",
      action="store", dest="bufsize", default=None, 
      help="buffer size [default=2048]")
  parser.add_option("-H","--hopsize",
      action="store", dest="hopsize", default=None, 
      help="overlap size [default=512]")
  parser.add_option("-t","--threshold",
      action="store", dest="threshold", default=0.1, 
      help="pitch threshold (for yin) [default=0.1]")
  parser.add_option("-s","--silence",
      action="store", dest="silence", default=-70, 
      help="silence threshold [default=-70]")
  parser.add_option("-D","--delay",
      action="store", dest="delay",  
      help="number of seconds frames to take back [default=0]")
  parser.add_option("-S","--smoothing",
      action="store", dest="smoothing", default=False, 
      help="use a median filter of N frames [default=0]")
  parser.add_option("-M","--maximum",
      action="store", dest="pitchmax", default=False, 
      help="maximum pitch value to look for (Hz) [default=20000]")
  parser.add_option("-l","--minimum",
      action="store", dest="pitchmin", default=False, 
      help="minimum pitch value to look for (Hz) [default=20]")
  # to be implemented
  parser.add_option("-n","--note",
      action="store_true", dest="note", default=False,
      help="NOT IMPLEMENTED output notes")
  # plotting functions
  parser.add_option("-T","--plottruth",
      action="store_true", dest="plottruth", default=False, 
      help="draw plot of the ground truth pitch track")
  parser.add_option("-p","--plot",
      action="store_true", dest="plot", default=False, 
      help="draw plot of the pitch track")
  parser.add_option("-x","--xsize",
      action="store", dest="xsize", default=1., 
      type='float', help="define xsize for plot")
  parser.add_option("-y","--ysize",
      action="store", dest="ysize", default=1., 
      type='float', help="define ysize for plot")
  parser.add_option("-O","--outplot",
      action="store", dest="outplot", default=None, 
      help="save the plot to output.{ps,png,svg} instead of displaying it")
  parser.add_option("-v","--verbose",
      action="store_true", dest="verbose", default=True,
      help="make lots of noise")
  parser.add_option("-q","--quiet",
      action="store_false", dest="verbose", default=True, 
      help="be quiet")
  (options, args) = parser.parse_args()
  if not options.bufsize:
    if options.mode == "yin":     options.bufsize = 1024
    if options.mode == "schmitt": options.bufsize = 2048
    if options.mode == "mcomb":   options.bufsize = 4096
    if options.mode == "fcomb":   options.bufsize = 4096 
    else: options.bufsize = 2048
  if not options.hopsize:
    options.hopsize = float(options.bufsize) / 2
  if not options.filename: 
    print "no file name given\n", usage
    sys.exit(1)
  return options, args

options, args = parse_args()

#print options.bufsize, options.hopsize

filename   = options.filename
params = taskparams()
params.samplerate = float(sndfile(filename).samplerate())
params.hopsize    = int(options.hopsize)
params.bufsize    = int(options.bufsize)
params.step       = params.samplerate/float(params.hopsize)
params.yinthresh  = float(options.threshold)
params.silence    = float(options.silence)
params.verbose    = options.verbose
if options.smoothing: params.pitchsmooth = int(options.smoothing)
if options.pitchmax:  params.pitchmax    = int(options.pitchmax)
if options.pitchmin:  params.pitchmin    = int(options.pitchmin)
#mintol     = float(options.mintol)*step
# default take back system delay
if options.delay: params.pitchdelay = float(options.delay)

if options.note:
        exit("not implemented yet")

wplot,oplots,titles = [],[],[]
modes = options.mode.split(',')
for i in range(len(modes)):
	pitch = []
	params.pitchmode  = modes[i]
	filetask = taskpitch(filename,params=params)
	pitch = filetask.compute_all()
	#print filetask.eval(pitch[i]) 
	if options.plot: filetask.plot(pitch,wplot,oplots,titles)

if options.outplot:
  extension = options.outplot.split('.')[-1] 
  outplot = '.'.join(options.outplot.split('.')[:-1])
else:
  extension,outplot = None,None
if options.plot: 
	filetask.plotplot(wplot,oplots,titles,outplot=outplot,extension=extension,
  xsize=options.xsize,ysize=options.ysize,truth=options.plottruth)