[d83ac74] | 1 | #! /usr/bin/env python |
---|
| 2 | |
---|
| 3 | import sys |
---|
| 4 | from aubio import source, pvoc, mfcc |
---|
| 5 | from numpy import array, vstack, zeros |
---|
| 6 | |
---|
| 7 | win_s = 512 # fft size |
---|
| 8 | hop_s = win_s / 4 # hop size |
---|
| 9 | n_filters = 40 |
---|
| 10 | n_coeffs = 13 |
---|
| 11 | samplerate = 44100 |
---|
| 12 | |
---|
| 13 | if len(sys.argv) < 2: |
---|
| 14 | print "Usage: %s <source_filename>" % sys.argv[0] |
---|
| 15 | sys.exit(1) |
---|
| 16 | |
---|
| 17 | source_filename = sys.argv[1] |
---|
| 18 | |
---|
| 19 | samplerate = 0 |
---|
| 20 | if len( sys.argv ) > 2: samplerate = int(sys.argv[2]) |
---|
| 21 | |
---|
| 22 | s = source(source_filename, samplerate, hop_s) |
---|
| 23 | samplerate = s.samplerate |
---|
| 24 | p = pvoc(win_s, hop_s) |
---|
| 25 | m = mfcc(win_s, n_filters, n_coeffs, samplerate) |
---|
| 26 | |
---|
| 27 | desc = [] |
---|
| 28 | tdesc = [] |
---|
| 29 | |
---|
| 30 | mfccs = zeros([13,]) |
---|
| 31 | frames_read = 0 |
---|
| 32 | while True: |
---|
| 33 | samples, read = s() |
---|
| 34 | spec = p(samples) |
---|
| 35 | mfcc_out = m(spec) |
---|
| 36 | mfccs = vstack((mfccs, mfcc_out)) |
---|
| 37 | frames_read += read |
---|
| 38 | if read < hop_s: break |
---|
| 39 | |
---|
| 40 | # do plotting |
---|
| 41 | from numpy import arange |
---|
| 42 | from demo_waveform_plot import get_waveform_plot |
---|
[6ff6d18] | 43 | from demo_waveform_plot import set_xlabels_sample2time |
---|
[d83ac74] | 44 | import matplotlib.pyplot as plt |
---|
| 45 | |
---|
| 46 | fig = plt.figure() |
---|
| 47 | plt.rc('lines',linewidth='.8') |
---|
| 48 | wave = plt.axes([0.1, 0.75, 0.8, 0.19]) |
---|
| 49 | |
---|
| 50 | get_waveform_plot( source_filename, samplerate, block_size = hop_s, ax = wave) |
---|
| 51 | wave.xaxis.set_visible(False) |
---|
| 52 | wave.yaxis.set_visible(False) |
---|
| 53 | |
---|
| 54 | all_times = arange(mfccs.shape[0]) * hop_s |
---|
| 55 | n_coeffs = mfccs.shape[1] |
---|
| 56 | for i in range(n_coeffs): |
---|
| 57 | ax = plt.axes ( [0.1, 0.75 - ((i+1) * 0.65 / n_coeffs), 0.8, 0.65 / n_coeffs], sharex = wave ) |
---|
| 58 | ax.xaxis.set_visible(False) |
---|
| 59 | ax.yaxis.set_visible(False) |
---|
| 60 | ax.plot(all_times, mfccs.T[i]) |
---|
| 61 | |
---|
| 62 | # add time to the last axis |
---|
| 63 | set_xlabels_sample2time( ax, frames_read, samplerate) |
---|
| 64 | |
---|
| 65 | #plt.ylabel('spectral descriptor value') |
---|
| 66 | ax.xaxis.set_visible(True) |
---|
| 67 | wave.set_title('MFCC for %s' % source_filename) |
---|
| 68 | plt.show() |
---|