shithub: aubio

ref: 1d4fc4a1aca4ffd957827d557776b23526e340a0
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='mcomb',
                          help="pitch detection mode [default=mcomb] \
                          mcomb|yin|fcomb|schmitt")
        parser.add_option("-u","--units", action="callback", 
                          callback=check_pitchm_mode, dest="omode",
                          default=aubio_pitchm_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=1024]")
        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 to take back [default=system]\
                          default system delay is 2*hopsize/samplerate")
        parser.add_option("-L","--localmin",
                          action="store_true", dest="localmin", default=False, 
                          help="use local minima after peak detection")
        parser.add_option("-c","--cut",
                          action="store_true", dest="cut", default=False,
                          help="cut input sound file at detected labels \
                          best used with option -L")
        # 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("-p","--plot",
                          action="store_true", dest="plot", default=False, 
                          help="NOT IMPLEMENTED draw plot")
        parser.add_option("-O","--outplot",
                          action="store", dest="outplot", default=None, 
                          help="NOT IMPLEMENTED save plot to output.{ps,png}")
        parser.add_option("-v","--verbose",
                          action="store_true", dest="verbose", default=False,
                          help="make lots of noise [default]")
        parser.add_option("-q","--quiet",
                          action="store_false", dest="verbose", default=False, 
                          help="be quiet")
        (options, args) = parser.parse_args()
        if not options.bufsize:
                if options.mode == aubio_pitch_yin:     options.bufsize = 1024
                if options.mode == aubio_pitch_schmitt: options.bufsize = 2048
                if options.mode == aubio_pitch_mcomb:   options.bufsize = 4096
                if options.mode == aubio_pitch_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.threshold  = float(options.threshold)
params.silence    = float(options.silence)
params.verbose    = options.verbose
#mintol     = float(options.mintol)*step
# default take back system delay
if options.delay: delay = float(options.delay)
else:             delay = 2./params.step

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


wplot,oplots = [],[]
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)

if options.plot: 
	filetask.plotplot(wplot,oplots,outplot=options.outplot)