Skip to content

Instantly share code, notes, and snippets.

@manashmandal
Last active October 18, 2018 15:14
Show Gist options
  • Save manashmandal/e24dbe21f2addc3e02748ec81db27999 to your computer and use it in GitHub Desktop.
Save manashmandal/e24dbe21f2addc3e02748ec81db27999 to your computer and use it in GitHub Desktop.
import numpy as np
import librosa
def wav2mfcc(file_path, max_pad_len=11):
wave, sr = librosa.load(file_path, mono=True, sr=None)
wave = wave[::3]
mfcc = librosa.feature.mfcc(wave, sr=16000)
pad_width = max_pad_len - mfcc.shape[1]
mfcc = np.pad(mfcc, pad_width=((0, 0), (0, pad_width)), mode='constant')
return mfcc
@manbharae
Copy link

When I run this I get error 👎

`

ValueError Traceback (most recent call last)
in ()
----> 1 wav2mfcc('./data/hold/hm1_004.mp3')

in wav2mfcc(file_path, max_pad_len)
8 mfcc = librosa.feature.mfcc(wave, sr=16000)
9 pad_width = max_pad_len - mfcc.shape[1]
---> 10 mfcc = np.pad(mfcc, pad_width=((0, 0), (0, pad_width)), mode='constant')
11 # print mfcc
12 return mfcc

/usr/local/lib/python2.7/dist-packages/numpy/lib/arraypad.pyc in pad(array, pad_width, mode, **kwargs)
1293
1294 narray = np.array(array)
-> 1295 pad_width = _validate_lengths(narray, pad_width)
1296
1297 allowedkwargs = {

/usr/local/lib/python2.7/dist-packages/numpy/lib/arraypad.pyc in _validate_lengths(narray, number_elements)
1084 if (chk[0] < 0) or (chk[1] < 0):
1085 fmt = "%s cannot contain negative values."
-> 1086 raise ValueError(fmt % (number_elements,))
1087 return normshp
1088

ValueError: ((0, 0), (0, -17)) cannot contain negative values.`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment