shithub: aubio

ref: 946cad34846fd7d3cb186ddbddfee6b69a9dcf5f
dir: /python/aubioplot-onset/

View raw version
#! /usr/bin/python

import sys
import numarray
import Gnuplot, Gnuplot.funcutils
from aubio.aubioclass import *
import aubio.gnuplot
import aubio.txtfile
from aubio.onsetcompare import onset_roc


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

def parse_args():
        from optparse import OptionParser
        parser = OptionParser(usage=usage)
        parser.add_option("-v","--verbose",
                          action="store_true", dest="verbose", default=False,
                          help="make lots of noise")
        parser.add_option("-q","--quiet",
                          action="store_false", dest="verbose", default=True, 
                          help="be quiet [default]")
        parser.add_option("-t","--threshold",
                          action="store", dest="threshold", default=0.3, 
                          help="onset detection threshold [default=0.3]")
        parser.add_option("-s","--silence",
                          action="store", dest="silence", default=-70, 
                          help="silence [default=-70]")
        def check_mode(option, opt, value, parser):
                nvalue = parser.rargs[0]
                if   nvalue == 'complexdomain' : setattr(parser.values, option.dest, complexdomain)
                elif nvalue == 'hfc'           : setattr(parser.values, option.dest, hfc)
                elif nvalue == 'phase'         : setattr(parser.values, option.dest, phase)
                elif nvalue == 'specdiff'      : setattr(parser.values, option.dest, specdiff)
                elif nvalue == 'energy'        : setattr(parser.values, option.dest, energy)
                elif nvalue == 'dual'          : setattr(parser.values, option.dest, 'dual')
        parser.add_option("-m","--mode",
                          action="callback", callback=check_mode, dest="mode", default='dual', 
                          help="onsetdetection mode [default=dual]")
        parser.add_option("-o","--outplot",
                          action="store", dest="outplot", default=None, 
                          help="be quiet [default=None]")
        (options, args) = parser.parse_args()
        if not len(args): 
                 print "no file name given\n", usage
                 sys.exit(1)
        return options, args

options, args = parse_args()

filename  = args[0] #FIXME should move to optparse
threshold = float(options.threshold)
silence   = float(options.silence)

print options.mode
onsets, ofunc   = getonsetsfunc(filename,threshold,silence,mode=options.mode)

g = Gnuplot.Gnuplot(debug=1, persist=1)

if options.outplot:
        extension = options.outplot.split('.')[-1]
        if extension == 'ps': extension = 'postscript'
        g('set terminal %s' % extension)
        g('set output \'%s\'' % options.outplot)

g('set multiplot')

# onset detection function 
downtime = (512./44100.)*numarray.arange(len(ofunc))
d = Gnuplot.Data(downtime,ofunc,with='lines') 

# detected onsets
x1 = (512./44100.)*numarray.array(onsets)
y1 = max(ofunc)*numarray.ones(len(onsets))
e = Gnuplot.Data(x1,-y1,with='impulses') 
e2= Gnuplot.Data(x1,y1,with='impulses') 

# truth
import os.path
datafile = filename.replace('.wav','.txt')
if not os.path.isfile(datafile):
        print "truth file not found"
        t = Gnuplot.Data(0,0,with='impulses') 
else:
        t_onsets = aubio.txtfile.read_datafile(datafile)
        y2 = max(ofunc)*numarray.ones(len(t_onsets))
        x2 = numarray.array(t_onsets).resize(len(t_onsets))
        t = Gnuplot.Data(x2,y2,with='impulses') 
        
        eps = 4*0.012 

        orig, missed, merged, expc, bad, doubled = \
                onset_roc(x2,x1,eps)
        print  orig, missed, merged, expc, bad, doubled
        print "GD %2.8f\t"        % (100*float(orig-missed-merged)/(orig)),
        print "FP %2.8f\t"        % (100*float(bad+doubled)/(orig))       , 
        print "GD-merged %2.8f\t" % (100*float(orig-missed)/(orig))       , 
        print "FP-pruned %2.8f\t" % (100*float(bad)/(orig))                

# audio data
time,data = aubio.gnuplot.audio_to_array(filename)
f = aubio.gnuplot.make_audio_plot(time,data)

# hack to align left axis
g('set lmargin 15')

g('set size 1,0.3')
g('set origin 0,0.7')
g('set xrange [0:%f]' % max(time)) 
g('set yrange [-1:1]') 
g.ylabel('amplitude')
g.plot(f,e,t)

g('set size 1,0.7')
g('set origin 0,0')
g('set xrange [0:%f]' % (512./44100.*len(ofunc)))
g('set yrange [0:%f]' % (max(ofunc)*1.01))
g.xlabel('time')
g.ylabel('onset detection value')

g.plot(d,e2)

g('unset multiplot')