source: python/aubioplot-onset @ 382c42e

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 382c42e was 71f98f1, checked in by Paul Brossier <piem@altern.org>, 20 years ago

add aubioplot-onset, minor fixes on aubiocut and aubioclass.py
add aubioplot-onset, minor fixes on aubiocut and aubioclass.py

  • Property mode set to 100755
File size: 4.4 KB
RevLine 
[71f98f1]1#! /usr/bin/python
2
3import sys
4import numarray
5import Gnuplot, Gnuplot.funcutils
6from aubio.aubioclass import *
7import aubio.gnuplot
8import aubio.txtfile
9from aubio.onsetcompare import onset_roc
10
11
12usage = "usage: %s [options] soundfile" % sys.argv[0]
13
14def parse_args():
15        from optparse import OptionParser
16        parser = OptionParser(usage=usage)
17        parser.add_option("-v","--verbose",
18                          action="store_true", dest="verbose", default=False,
19                          help="make lots of noise")
20        parser.add_option("-q","--quiet",
21                          action="store_false", dest="verbose", default=True,
22                          help="be quiet [default]")
23        parser.add_option("-t","--threshold",
24                          action="store", dest="threshold", default=0.3,
25                          help="onset detection threshold [default=0.3]")
26        parser.add_option("-s","--silence",
27                          action="store", dest="silence", default=-70,
28                          help="silence [default=-70]")
29        def check_mode(option, opt, value, parser):
30                nvalue = parser.rargs[0]
31                if   nvalue == 'complexdomain' : setattr(parser.values, option.dest, complexdomain)
32                elif nvalue == 'hfc'           : setattr(parser.values, option.dest, hfc)
33                elif nvalue == 'phase'         : setattr(parser.values, option.dest, phase)
34                elif nvalue == 'specdiff'      : setattr(parser.values, option.dest, specdiff)
35                elif nvalue == 'energy'        : setattr(parser.values, option.dest, energy)
36                elif nvalue == 'dual'          : setattr(parser.values, option.dest, 'dual')
37        parser.add_option("-m","--mode",
38                          action="callback", callback=check_mode, dest="mode", default='dual',
39                          help="onsetdetection mode [default=dual]")
40        parser.add_option("-o","--outplot",
41                          action="store", dest="outplot", default=None,
42                          help="be quiet [default=None]")
43        (options, args) = parser.parse_args()
44        if not len(args):
45                 print "no file name given\n", usage
46                 sys.exit(1)
47        return options, args
48
49options, args = parse_args()
50
51filename  = args[0] #FIXME should move to optparse
52threshold = float(options.threshold)
53silence   = float(options.silence)
54
55print options.mode
56onsets, ofunc   = getonsetsfunc(filename,threshold,silence,mode=options.mode)
57
58g = Gnuplot.Gnuplot(debug=1, persist=1)
59
60if options.outplot:
61        extension = options.outplot.split('.')[-1]
62        if extension == 'ps': extension = 'postscript'
63        g('set terminal %s' % extension)
64        g('set output \'%s\'' % options.outplot)
65
66g('set multiplot')
67
68# onset detection function
69downtime = (512./44100.)*numarray.arange(len(ofunc))
70d = Gnuplot.Data(downtime,ofunc,with='lines')
71
72# detected onsets
73x1 = (512./44100.)*numarray.array(onsets)
74y1 = max(ofunc)*numarray.ones(len(onsets))
75e = Gnuplot.Data(x1,-y1,with='impulses')
76e2= Gnuplot.Data(x1,y1,with='impulses')
77
78# truth
79import os.path
80datafile = filename.replace('.wav','.txt')
81if not os.path.isfile(datafile):
82        print "truth file not found"
83        t = Gnuplot.Data(0,0,with='impulses')
84else:
85        t_onsets = aubio.txtfile.read_datafile(datafile)
86        y2 = max(ofunc)*numarray.ones(len(t_onsets))
87        x2 = numarray.array(t_onsets).resize(len(t_onsets))
88        t = Gnuplot.Data(x2,y2,with='impulses')
89       
90        eps = 4*0.012
91
92        orig, missed, merged, expc, bad, doubled = \
93                onset_roc(x2,x1,eps)
94        print  orig, missed, merged, expc, bad, doubled
95        print "GD %2.8f\t"        % (100*float(orig-missed-merged)/(orig)),
96        print "FP %2.8f\t"        % (100*float(bad+doubled)/(orig))       ,
97        print "GD-merged %2.8f\t" % (100*float(orig-missed)/(orig))       ,
98        print "FP-pruned %2.8f\t" % (100*float(bad)/(orig))               
99
100# audio data
101time,data = aubio.gnuplot.audio_to_array(filename)
102f = aubio.gnuplot.make_audio_plot(time,data)
103
104# hack to align left axis
105g('set lmargin 15')
106
107g('set size 1,0.3')
108g('set origin 0,0.7')
109g('set xrange [0:%f]' % max(time))
110g('set yrange [-1:1]')
111g.ylabel('amplitude')
112g.plot(f,e,t)
113
114g('set size 1,0.7')
115g('set origin 0,0')
116g('set xrange [0:%f]' % (512./44100.*len(ofunc)))
117g('set yrange [0:%f]' % (max(ofunc)*1.01))
118g.xlabel('time')
119g.ylabel('onset detection value')
120
121g.plot(d,e2)
122
123g('unset multiplot')
124
Note: See TracBrowser for help on using the repository browser.