1 | #! /usr/bin/env python |
---|
2 | |
---|
3 | import sys |
---|
4 | from aubio import source, pvoc, mfcc |
---|
5 | from numpy import vstack, zeros |
---|
6 | |
---|
7 | win_s = 512 # fft size |
---|
8 | hop_s = win_s // 4 # hop size |
---|
9 | n_filters = 40 # must be 40 for mfcc |
---|
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 | mfccs = zeros([n_coeffs,]) |
---|
28 | frames_read = 0 |
---|
29 | while True: |
---|
30 | samples, read = s() |
---|
31 | spec = p(samples) |
---|
32 | mfcc_out = m(spec) |
---|
33 | mfccs = vstack((mfccs, mfcc_out)) |
---|
34 | frames_read += read |
---|
35 | if read < hop_s: break |
---|
36 | |
---|
37 | # do plotting |
---|
38 | from numpy import arange |
---|
39 | from demo_waveform_plot import get_waveform_plot |
---|
40 | from demo_waveform_plot import set_xlabels_sample2time |
---|
41 | import matplotlib.pyplot as plt |
---|
42 | |
---|
43 | fig = plt.figure() |
---|
44 | plt.rc('lines',linewidth='.8') |
---|
45 | wave = plt.axes([0.1, 0.75, 0.8, 0.19]) |
---|
46 | |
---|
47 | get_waveform_plot( source_filename, samplerate, block_size = hop_s, ax = wave) |
---|
48 | wave.xaxis.set_visible(False) |
---|
49 | wave.yaxis.set_visible(False) |
---|
50 | |
---|
51 | all_times = arange(mfccs.shape[0]) * hop_s |
---|
52 | n_coeffs = mfccs.shape[1] |
---|
53 | for i in range(n_coeffs): |
---|
54 | ax = plt.axes ( [0.1, 0.75 - ((i+1) * 0.65 / n_coeffs), 0.8, 0.65 / n_coeffs], sharex = wave ) |
---|
55 | ax.xaxis.set_visible(False) |
---|
56 | ax.yaxis.set_visible(False) |
---|
57 | ax.plot(all_times, mfccs.T[i]) |
---|
58 | |
---|
59 | # add time to the last axis |
---|
60 | set_xlabels_sample2time( ax, frames_read, samplerate) |
---|
61 | |
---|
62 | #plt.ylabel('spectral descriptor value') |
---|
63 | ax.xaxis.set_visible(True) |
---|
64 | wave.set_title('MFCC for %s' % source_filename) |
---|
65 | plt.show() |
---|