python/demos: add demo_pitch.py and demo_waveform_plot.py
[aubio.git] / python / demos / demo_specdesc.py
1 #! /usr/bin/env python
2
3 import sys
4 from aubio import fvec, source, pvoc, specdesc
5 from numpy import hstack
6
7 win_s = 512                 # fft size
8 hop_s = win_s / 4           # hop size
9
10 if len(sys.argv) < 2:
11     print "Usage: %s <filename> [samplerate]" % sys.argv[0]
12     sys.exit(1)
13
14 filename = sys.argv[1]
15
16 samplerate = 0
17 if len( sys.argv ) > 2: samplerate = int(sys.argv[2])
18
19 s = source(filename, samplerate, hop_s)
20 samplerate = s.samplerate
21
22 pv = pvoc(win_s, hop_s)
23
24 methods = ['default', 'energy', 'hfc', 'complex', 'phase', 'specdiff', 'kl', 'mkl',
25     'specflux', 'centroid', 'spread', 'skewness', 'kurtosis', 'slope', 'decrease',
26     'rolloff', ]
27
28 all_descs = {}
29 o = {}
30
31 for method in methods:
32     cands = []
33     all_descs[method] = fvec(0)
34     o[method] = specdesc(method, win_s)
35
36 total_frames = 0
37 downsample = 2
38
39 while True:
40     samples, read = s()
41     fftgrain = pv(samples)
42     print "%f" % ( total_frames / float(samplerate) ),
43     for method in methods:
44         specdesc_val = o[method](fftgrain)[0]
45         all_descs[method] = hstack ( [all_descs[method], specdesc_val] )
46         print "%f" % specdesc_val,
47     print
48     total_frames += read
49     if read < hop_s: break
50
51 if 1:
52     print "done computing, now plotting"
53     import matplotlib.pyplot as plt
54     from demo_waveform_plot import get_waveform_plot
55     fig = plt.figure()
56     plt.rc('lines',linewidth='.8')
57     wave = plt.axes([0.1, 0.75, 0.8, 0.19])
58     get_waveform_plot(filename, samplerate, ax = wave )
59     wave.yaxis.set_visible(False)
60     wave.xaxis.set_visible(False)
61
62     all_desc_times = [ x * hop_s  for x in range(len(all_descs["default"])) ]
63     n_methods = len(methods)
64     for i, method in enumerate(methods):
65         #ax = fig.add_subplot (n_methods, 1, i)
66         #plt2 = plt.axes([0.1, 0.1, 0.8, 0.65], sharex = plt1)
67         ax = plt.axes ( [0.1, 0.75 - ((i+1) * 0.65 / n_methods),  0.8, 0.65 / n_methods], sharex = wave )
68         ax.plot(all_desc_times, all_descs[method], '-', label = method)
69         #ax.set_ylabel(method, rotation = 0)
70         ax.xaxis.set_visible(False)
71         ax.yaxis.set_visible(False)
72         ax.axis(xmax = all_desc_times[-1], xmin = all_desc_times[0])
73         ax.annotate(method, xy=(-10, 10),  xycoords='axes points',
74                 horizontalalignment='right', verticalalignment='bottom',
75                 )
76     if all_desc_times[-1] / float(samplerate) > 60:
77         plt.xlabel('time (mm:ss)')
78         ax.set_xticklabels([ "%02d:%02d" % (t/float(samplerate)/60, (t/float(samplerate))%60) for t in ax.get_xticks()[:-1]], rotation = 50)
79     else:
80         plt.xlabel('time (ss.mm)')
81         ax.set_xticklabels([ "%02d.%02d" % (t/float(samplerate), 100*((t/float(samplerate))%1) ) for t in ax.get_xticks()[:-1]], rotation = 50)
82     #plt.ylabel('spectral descriptor value')
83     ax.xaxis.set_visible(True)
84     plt.show()