[d83ac74] | 1 | #! /usr/bin/env python |
---|
| 2 | |
---|
| 3 | import sys |
---|
| 4 | from aubio import source, pvoc, mfcc |
---|
[70b2ab0] | 5 | from numpy import vstack, zeros, diff |
---|
[d83ac74] | 6 | |
---|
[9e54b83] | 7 | n_filters = 40 # must be 40 for mfcc |
---|
[d83ac74] | 8 | n_coeffs = 13 |
---|
| 9 | |
---|
| 10 | if len(sys.argv) < 2: |
---|
[70b2ab0] | 11 | print("Usage: %s <source_filename> [samplerate] [win_s] [hop_s] [mode]" % sys.argv[0]) |
---|
| 12 | print(" where [mode] can be 'delta' or 'ddelta' for first and second derivatives") |
---|
[d83ac74] | 13 | sys.exit(1) |
---|
| 14 | |
---|
| 15 | source_filename = sys.argv[1] |
---|
| 16 | |
---|
[70b2ab0] | 17 | if len(sys.argv) > 2: samplerate = int(sys.argv[2]) |
---|
| 18 | else: samplerate = 0 |
---|
| 19 | if len(sys.argv) > 3: win_s = int(sys.argv[3]) |
---|
| 20 | else: win_s = 512 |
---|
| 21 | if len(sys.argv) > 4: hop_s = int(sys.argv[4]) |
---|
| 22 | else: hop_s = win_s // 4 |
---|
| 23 | if len(sys.argv) > 5: mode = sys.argv[5] |
---|
| 24 | else: mode = "default" |
---|
| 25 | |
---|
[d83ac74] | 26 | samplerate = 0 |
---|
| 27 | if len( sys.argv ) > 2: samplerate = int(sys.argv[2]) |
---|
| 28 | |
---|
| 29 | s = source(source_filename, samplerate, hop_s) |
---|
| 30 | samplerate = s.samplerate |
---|
| 31 | p = pvoc(win_s, hop_s) |
---|
| 32 | m = mfcc(win_s, n_filters, n_coeffs, samplerate) |
---|
| 33 | |
---|
[d0bdf0c] | 34 | mfccs = zeros([n_coeffs,]) |
---|
[d83ac74] | 35 | frames_read = 0 |
---|
| 36 | while True: |
---|
| 37 | samples, read = s() |
---|
| 38 | spec = p(samples) |
---|
| 39 | mfcc_out = m(spec) |
---|
| 40 | mfccs = vstack((mfccs, mfcc_out)) |
---|
| 41 | frames_read += read |
---|
| 42 | if read < hop_s: break |
---|
| 43 | |
---|
| 44 | # do plotting |
---|
| 45 | from numpy import arange |
---|
| 46 | from demo_waveform_plot import get_waveform_plot |
---|
[6ff6d18] | 47 | from demo_waveform_plot import set_xlabels_sample2time |
---|
[d83ac74] | 48 | import matplotlib.pyplot as plt |
---|
| 49 | |
---|
| 50 | fig = plt.figure() |
---|
| 51 | plt.rc('lines',linewidth='.8') |
---|
| 52 | wave = plt.axes([0.1, 0.75, 0.8, 0.19]) |
---|
| 53 | |
---|
| 54 | get_waveform_plot( source_filename, samplerate, block_size = hop_s, ax = wave) |
---|
| 55 | wave.xaxis.set_visible(False) |
---|
| 56 | wave.yaxis.set_visible(False) |
---|
| 57 | |
---|
[70b2ab0] | 58 | # compute first and second derivatives |
---|
| 59 | if mode in ["delta", "ddelta"]: |
---|
| 60 | mfccs = diff(mfccs, axis = 0) |
---|
| 61 | if mode == "ddelta": |
---|
| 62 | mfccs = diff(mfccs, axis = 0) |
---|
| 63 | |
---|
[d83ac74] | 64 | all_times = arange(mfccs.shape[0]) * hop_s |
---|
| 65 | n_coeffs = mfccs.shape[1] |
---|
| 66 | for i in range(n_coeffs): |
---|
| 67 | ax = plt.axes ( [0.1, 0.75 - ((i+1) * 0.65 / n_coeffs), 0.8, 0.65 / n_coeffs], sharex = wave ) |
---|
| 68 | ax.xaxis.set_visible(False) |
---|
[70b2ab0] | 69 | ax.set_yticks([]) |
---|
| 70 | ax.set_ylabel('%d' % i) |
---|
[d83ac74] | 71 | ax.plot(all_times, mfccs.T[i]) |
---|
| 72 | |
---|
| 73 | # add time to the last axis |
---|
[70b2ab0] | 74 | set_xlabels_sample2time( ax, frames_read, samplerate) |
---|
[d83ac74] | 75 | |
---|
| 76 | #plt.ylabel('spectral descriptor value') |
---|
| 77 | ax.xaxis.set_visible(True) |
---|
[70b2ab0] | 78 | title = 'MFCC for %s' % source_filename |
---|
| 79 | if mode == "delta": title = mode + " " + title |
---|
| 80 | elif mode == "ddelta": title = "double-delta" + " " + title |
---|
| 81 | wave.set_title(title) |
---|
[d83ac74] | 82 | plt.show() |
---|