source: python/demos/demo_mel-energy.py @ 4120fbc

feature/autosinkfeature/constantqfeature/pitchshiftfeature/pydocstringsfeature/timestretchpitchshiftsamplertimestretchyinfft+
Last change on this file since 4120fbc was 4120fbc, checked in by Paul Brossier <piem@piem.org>, 3 years ago

python/demos: python3 and double precision compatibility

  • Property mode set to 100755
File size: 2.2 KB
Line 
1#! /usr/bin/env python
2
3import sys
4from aubio import source, pvoc, filterbank
5from numpy import vstack, zeros
6
7win_s = 512                 # fft size
8hop_s = win_s // 4          # hop size
9
10if len(sys.argv) < 2:
11    print("Usage: %s <filename> [samplerate]" % sys.argv[0])
12    sys.exit(1)
13
14filename = sys.argv[1]
15
16samplerate = 0
17if len( sys.argv ) > 2: samplerate = int(sys.argv[2])
18
19s = source(filename, samplerate, hop_s)
20samplerate = s.samplerate
21
22pv = pvoc(win_s, hop_s)
23
24f = filterbank(40, win_s)
25f.set_mel_coeffs_slaney(samplerate)
26
27energies = zeros((40,))
28o = {}
29
30total_frames = 0
31downsample = 2
32
33while True:
34    samples, read = s()
35    fftgrain = pv(samples)
36    new_energies = f(fftgrain)
37    timestr = '%f' % (total_frames / float(samplerate) )
38    print('{:s} {:s}'.format(timestr, ' '.join(['%f' % b for b in new_energies])))
39    energies = vstack( [energies, new_energies] )
40    total_frames += read
41    if read < hop_s: break
42
43if 1:
44    print("done computing, now plotting")
45    import matplotlib.pyplot as plt
46    from demo_waveform_plot import get_waveform_plot
47    from demo_waveform_plot import set_xlabels_sample2time
48    fig = plt.figure()
49    plt.rc('lines',linewidth='.8')
50    wave = plt.axes([0.1, 0.75, 0.8, 0.19])
51    get_waveform_plot(filename, samplerate, block_size = hop_s, ax = wave )
52    wave.yaxis.set_visible(False)
53    wave.xaxis.set_visible(False)
54
55    n_plots = len(energies.T)
56    all_desc_times = [ x * hop_s  for x in range(len(energies)) ]
57    for i, band in enumerate(energies.T):
58        ax = plt.axes ( [0.1, 0.75 - ((i+1) * 0.65 / n_plots),  0.8, 0.65 / n_plots], sharex = wave )
59        ax.plot(all_desc_times, band, '-', label = 'band %d' % i)
60        #ax.set_ylabel(method, rotation = 0)
61        ax.xaxis.set_visible(False)
62        ax.yaxis.set_visible(False)
63        ax.axis(xmax = all_desc_times[-1], xmin = all_desc_times[0])
64        ax.annotate('band %d' % i, xy=(-10, 0),  xycoords='axes points',
65                horizontalalignment='right', verticalalignment='bottom',
66                size = 'xx-small',
67                )
68    set_xlabels_sample2time( ax, all_desc_times[-1], samplerate) 
69    #plt.ylabel('spectral descriptor value')
70    ax.xaxis.set_visible(True)
71    plt.show()
Note: See TracBrowser for help on using the repository browser.