import os
import random
path_timit = '/Users/musy/code/TIMIT'
ending = 'WAV'
speakers = {}
for root, dirs, files in os.walk(path_timit, topdown=False):
allowed_files = (filename for filename in files if ending in filename)
for f in allowed_files:
speaker_name = root.split('/')[-1]
if speaker_name in speakers:
speakers.get(speaker_name).get('sound_files').append(os.path.join(root, f))
else:
speakers[speaker_name] = {'sound_files': [os.path.join(root, f)]}
def select_random_speaker(sex=None):
if sex is None:
name = speakers.keys()[random.randint(1, 630)]
return name, speakers[name]
speaker = select_random_speaker()
speaker
('MRLR0', {'sound_files': ['/Users/musy/code/TIMIT/TRAIN/DR2/MRLR0/SA1.WAV', '/Users/musy/code/TIMIT/TRAIN/DR2/MRLR0/SA2.WAV', '/Users/musy/code/TIMIT/TRAIN/DR2/MRLR0/SI1196.WAV', '/Users/musy/code/TIMIT/TRAIN/DR2/MRLR0/SI1826.WAV', '/Users/musy/code/TIMIT/TRAIN/DR2/MRLR0/SI566.WAV', '/Users/musy/code/TIMIT/TRAIN/DR2/MRLR0/SX116.WAV', '/Users/musy/code/TIMIT/TRAIN/DR2/MRLR0/SX206.WAV', '/Users/musy/code/TIMIT/TRAIN/DR2/MRLR0/SX26.WAV', '/Users/musy/code/TIMIT/TRAIN/DR2/MRLR0/SX296.WAV', '/Users/musy/code/TIMIT/TRAIN/DR2/MRLR0/SX386.WAV']})
%pylab inline
import wave
import sys
wav = speaker[1].get('sound_files')[10]
#wav = '/Users/musy/Desktop/SA1.WAV'
Populating the interactive namespace from numpy and matplotlib
WARNING: pylab import has clobbered these variables: ['random', 'f'] `%matplotlib` prevents importing * from pylab and numpy
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-3-808b59ee49ce> in <module>() 3 import sys 4 ----> 5 wav = speaker[1].get('sound_files')[10] 6 #wav = '/Users/musy/Desktop/SA1.WAV' IndexError: list index out of range
spf = wave.open(wav,'r')
print spf.getframerate(), spf.getnchannels(), spf.getnframes(), spf.getsampwidth(), spf.getcomptype(), spf.getcompname()
16000 1 53863 2 NONE not compressed
spf = wave.open(wav,'r')
#Extract Raw Audio from Wav File
signal = spf.readframes(-1)
signal = np.fromstring(signal, 'Int16') #data in signal
#If Stereo
if spf.getnchannels() == 2:
print 'Just mono files'
sys.exit(0)
plt.figure(1)
plt.title('Signal Wave...')
plt.plot(signal)
[<matplotlib.lines.Line2D at 0x1088c3f90>]
from IPython.display import Audio
Audio(wav)
from scipy.io import wavfile
fpaths = '/Users/musy/Desktop/SA1.WAV'
fs, d = wavfile.read(fpaths)
fs, d.shape
(16000, (53863,))
Audio(d, rate=fs)
signal
array([ 0, -9, 1, ..., 1, -4, 10], dtype=int16)
signal.shape
(53863,)
import librosa
/Users/musy/anaconda/lib/python2.7/site-packages/librosa/core.py:26: UserWarning: Could not import scikits.samplerate. Falling back to scipy.signal 'Falling back to scipy.signal')
d.shape
(53863,)
mfccs = librosa.feature.mfcc(d, sr=fs, n_mfcc=13)
mfccs
array([[ 387.51507236, 379.77619144, 341.15045949, ..., 376.94548475, 343.51885482, 325.78432135], [ 37.2375711 , 35.41415219, 23.50698514, ..., 37.52370773, 16.00121179, 6.07766739], [ -17.91177917, -14.55876741, 3.78041357, ..., 16.984449 , 24.23021741, 19.76567323], ..., [ -5.47597033, -5.03441319, 1.47626728, ..., -2.14819731, 2.63307088, 4.9081712 ], [ 3.14102086, 3.38416625, 6.07586444, ..., -7.96843808, -3.39845053, 1.98235916], [ 3.90795751, 4.26453884, 2.41527374, ..., -6.97284058, 3.56820032, 4.16169082]])
from librosa.feature import melspectrogram
from librosa.display import specshow
S = melspectrogram(d, sr=fs, n_fft=1024)
logS = librosa.logamplitude(S)
specshow(logS, sr=fs, x_axis='time', y_axis='mel')
<matplotlib.image.AxesImage at 0x10da4d150>