shithub: aubio

ref: e968939e0135dcc1d09d611d875dc07b0605e862
dir: /python/aubiocut/

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.tasks 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='dual', 
                          help="onset detection mode [default=dual] \
                          complexdomain|hfc|phase|specdiff|energy|kl|mkl|dual")
        parser.add_option("-B","--bufsize",
                          action="store", dest="bufsize", default=512, 
                          help="buffer size [default=512]")
        parser.add_option("-H","--hopsize",
                          action="store", dest="hopsize", default=256, 
                          help="overlap size [default=256]")
        parser.add_option("-t","--threshold",
                          action="store", dest="threshold", default=0.650, 
                          help="onset peak picking threshold [default=0.650]")
        parser.add_option("-s","--silence",
                          action="store", dest="silence", default=-70, 
                          help="silence threshold [default=-70]")
        parser.add_option("-M","--mintol",
                          action="store", dest="mintol", default=0.048, 
                          help="minimum inter onset interval [default=0.048]")
        parser.add_option("-D","--delay",
                          action="store", dest="delay",  
                          help="number of seconds to take back [default=system]\
                          default system delay is 3*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")
        parser.add_option("-d","--derivate",
                          action="store_true", dest="derivate", default=False, 
                          help="derivate onset detection function")
        parser.add_option("-S","--silencecut",
                          action="store_true", dest="silencecut", default=False,
                          help="outputs silence locations")
        parser.add_option("-z","--zerocross",
                          action="store", dest="zerothres", default=0.008, 
                          help="zero-crossing threshold for slicing [default=0.00008]")
        # plotting functions
        parser.add_option("-p","--plot",
                          action="store_true", dest="plot", default=False, 
                          help="draw plot")
        parser.add_option("-O","--outplot",
                          action="store", dest="outplot", default=None, 
                          help="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")
        # to be implemented
        parser.add_option("-b","--beat",
                          action="store_true", dest="beat", default=False,
                          help="output beat locations")
        (options, args) = parser.parse_args()
        if not options.filename: 
                 print "no file name given\n", usage
                 sys.exit(1)
        return options, args

options, args = parse_args()

filename   = options.filename
params = taskparams()
params.hopsize    = int(options.hopsize)
params.bufsize    = int(options.bufsize)
params.threshold  = float(options.threshold)
params.zerothres  = float(options.zerothres)
params.silence    = float(options.silence)
params.mintol     = float(options.mintol)
# default take back system delay
if options.delay: delay = float(options.delay)
else:             delay = 3./params.step


lonsets, lofunc = [], []
modes = options.mode.split(',')
for i in range(len(modes)):

	params.onsetmode = modes[i] 
	dotask = taskonset
	if options.beat:
		dotask = taskbeat
	elif options.silencecut:
		dotask = tasksilence
	elif options.plot: 
		params.storefunc=True
	else:              
		params.storefunc=False
	filetask = dotask(filename,params=params)
	onsets = filetask.compute_all()
	ofunc = filetask.ofunc
        #onsets, ofunc = getonsets(filename,threshold,silence,
        #        mode=mode[i],localmin=options.localmin,
        #        derivate=options.derivate,
        #        bufsize=bufsize,hopsize=hopsize,storefunc=True)

        # take back system delay
        if delay != 0:
                for each in range(len(onsets)):
			onsets[each] = onsets[each][0] - delay*params.step

        # prune doubled 
	params.mintol *= params.step
        if params.mintol > 0:
                last = -2*params.mintol
                newonsets = []
                for new in onsets:
                        if (new - last > params.mintol): 
                                newonsets.append(new)
                        last = new
                onsets = newonsets

        lonsets.append(onsets)
        lofunc.append(ofunc)

	# print times in second
	if options.verbose:
		#print modes[i]
		for i in range(len(onsets)):
				print onsets[i]*params.step

	if options.plot:
		filetask.plot(onsets, ofunc)
		filetask.plotplot(outplot=options.outplot)

if options.cut:
        a = taskcut(filename,onsets,params=params)
	a.compute_all()