<jupyter_start><jupyter_text>Functions for converting files to MFCC<jupyter_code>def files_to_mfcc(file_paths):
    mfcc_array = []
    for fp in file_paths:
        X,sr = librosa.load(fp)
        mfccs = librosa.feature.mfcc(y=X, sr=sr, n_mfcc=40)
        mfcc_array.append(mfccs)
    return mfcc_array
sound_dir = ["BD15C", "BD6A", "BD6B", "BD15L"]
converted_array = [] 

for d in sound_dir:
    files = os.listdir(d)
    files = [d + "/" + s for s in files]
    converted_array.append(files_to_mfcc(files))
min_global = 100
max_global = 0
for i in range(4):
    max_local = 0
    min_local = 100
    for j in converted_array[i]:
        if min_local > j.shape[1]:
            min_local = j.shape[1]
        if max_local < j.shape[1]:
            max_local = j.shape[1]
    if min_global > min_local:
        min_global = min_local
    if max_global < max_local:
        max_global = max_local
    print min_local, max_local
print min_global, max_global
len(converted_array[1])
splits = np.array_split(converted_array[1][1],min_global, axis=1)
print converted_array[1][1].shape
print splits[1][:,-2]
fig, axes = plt.subplots(4, 5, figsize=(10,10))
for i in range(4):
    for j in range(5):
        rand_i = np.random.randint(20)
        axes[i,j].imshow(converted_array[i][rand_i])
def extract_feature(file_name):
    X, sample_rate = librosa.load(file_name)
    stft = np.abs(librosa.stft(X))
    mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T,axis=0)
    chroma = np.mean(librosa.feature.chroma_stft(S=stft, sr=sample_rate).T,axis=0)
    mel = np.mean(librosa.feature.melspectrogram(X, sr=sample_rate).T,axis=0)
    contrast = np.mean(librosa.feature.spectral_contrast(S=stft, sr=sample_rate).T,axis=0)
    tonnetz = np.mean(librosa.feature.tonnetz(y=librosa.effects.harmonic(X),
    sr=sample_rate).T,axis=0)
    return mfccs,chroma,mel,contrast,tonnetz

def parse_audio_files(parent_dir,sub_dirs,file_ext="*.wav"):
    features, labels = np.empty((0,193)), np.empty(0)
    for label, sub_dir in enumerate(sub_dirs):
        for fn in glob.glob(os.path.join(parent_dir, sub_dir, file_ext)):
            try:
              mfccs, chroma, mel, contrast,tonnetz = extract_feature(fn)
            except Exception as e:
              print "Error encountered while parsing file: ", fn
              continue
            ext_features = np.hstack([mfccs,chroma,mel,contrast,tonnetz])
            features = np.vstack([features,ext_features])
            labels = np.append(labels, fn.split('/')[2].split('-')[1])
    return np.array(features), np.array(labels, dtype = np.int)

def one_hot_encode(labels):
    n_labels = len(labels)
    n_unique_labels = len(np.unique(labels))
    one_hot_encode = np.zeros((n_labels,n_unique_labels))
    one_hot_encode[np.arange(n_labels), labels] = 1
    return one_hot_encode
sound_file_paths = ["BD15C/5801100W.wav","BD6A/61072013.wav","BD6B/5903700S.wav","BD15C/7500300H.wav"]
mfccs, chroma, mel, contrast, tonnetz = extract_feature(sound_file_paths[0])
plt.figure(figsize = (20,8))
plt.show(plt.imshow(mel.reshape(1,mel.shape[0])))
plt.show(plt.imshow(mfccs.reshape(1,mfccs.shape[0])))
mfccs, chroma, mel, contrast, tonnetz = extract_feature(sound_file_paths[1])
plt.figure(figsize = (20,8))
plt.show(plt.imshow(mel.reshape(1,mel.shape[0])))
plt.show(plt.imshow(mfccs.reshape(1,mfccs.shape[0])))
mfccs, chroma, mel, contrast, tonnetz = extract_feature(sound_file_paths[2])
plt.figure(figsize = (20,8))
plt.show(plt.imshow(mel.reshape(1,mel.shape[0])))
plt.show(plt.imshow(mfccs.reshape(1,mfccs.shape[0])))
mfccs, chroma, mel, contrast, tonnetz = extract_feature(sound_file_paths[3])
plt.figure(figsize = (20,8))
plt.show(plt.imshow(mel.reshape(1,mel.shape[0])))
plt.show(plt.imshow(mfccs.reshape(1,mfccs.shape[0])))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15013123359580052, 'fraction_numerical': 0.031758530183727034, 'mean_word_length': 4.263812154696133, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '277513', 'license': 'MIT', 'n_tokens_mistral': 1600, 'n_tokens_neox': 1512, 'n_words': 234, 'path': 'Helpers/Convert Raw WAV Files to MFCC.ipynb', 'repo_name': 'arkar-wpi/cs525-project', 'size': '3810'}