Showing the changes to python (1 of 1 file). Show all
Unified · Split
| @@ -0,0 +1,70 @@ | ||
| 1 | +#! /usr/bin/env python | |
| 2 | + | |
| 3 | +import sys | |
| 4 | +from aubio import fvec, source, pvoc, filterbank | |
| 5 | +from numpy import vstack, zeros | |
| 6 | +from demo_waveform_plot import set_xlabels_sample2time | |
| 7 | + | |
| 8 | +win_s = 512 # fft size | |
| 9 | +hop_s = win_s / 4 # hop size | |
| 10 | + | |
| 11 | +if len(sys.argv) < 2: | |
| 12 | + print "Usage: %s <filename> [samplerate]" % sys.argv[0] | |
| 13 | + sys.exit(1) | |
| 14 | + | |
| 15 | +filename = sys.argv[1] | |
| 16 | + | |
| 17 | +samplerate = 0 | |
| 18 | +if len( sys.argv ) > 2: samplerate = int(sys.argv[2]) | |
| 19 | + | |
| 20 | +s = source(filename, samplerate, hop_s) | |
| 21 | +samplerate = s.samplerate | |
| 22 | + | |
| 23 | +pv = pvoc(win_s, hop_s) | |
| 24 | + | |
| 25 | +f = filterbank(40, win_s) | |
| 26 | +f.set_mel_coeffs_slaney(samplerate) | |
| 27 | + | |
| 28 | +energies = zeros((40,)) | |
| 29 | +o = {} | |
| 30 | + | |
| 31 | +total_frames = 0 | |
| 32 | +downsample = 2 | |
| 33 | + | |
| 34 | +while True: | |
| 35 | + samples, read = s() | |
| 36 | + fftgrain = pv(samples) | |
| 37 | + new_energies = f(fftgrain) | |
| 38 | + print '%f' % (total_frames / float(samplerate) ), | |
| 39 | + print ' '.join(['%f' % b for b in new_energies]) | |
| 40 | + energies = vstack( [energies, new_energies] ) | |
| 41 | + total_frames += read | |
| 42 | + if read < hop_s: break | |
| 43 | + | |
| 44 | +if 1: | |
| 45 | + print "done computing, now plotting" | |
| 46 | + import matplotlib.pyplot as plt | |
| 47 | + from demo_waveform_plot import get_waveform_plot | |
| 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, 10), xycoords='axes points', | |
| 65 | + horizontalalignment='right', verticalalignment='bottom', | |
| 66 | + ) | |
| 67 | + set_xlabels_sample2time( ax, all_desc_times[-1], samplerate) | |
| 68 | + #plt.ylabel('spectral descriptor value') | |
| 69 | + ax.xaxis.set_visible(True) | |
| 70 | + plt.show() | |