from extract_mfcc import extract_mfcc
from keras.layers import Input, Dense
from keras.models import Model
import librosa
import numpy as np
import os
audio_path = '<PATH TO TRAINING FILES HERE>'
dir = os.listdir(audio_path)
x_values = []
for files in dir:
if files[0] != '.':
mfcc = extract_mfcc(f'{audio_path}/{files}')
x_values.append(mfcc)
x_values = np.array(x_values)
num_mfcc_features = x_values.shape[1]
# Normalize the data (assuming mean and std normalization)
mean = np.mean(x_values, axis=0)
std = np.std(x_values, axis=0)
X_train_normalized = (x_values - mean) / std
# Defining an autoencoder
input_shape = (num_mfcc_features,)
encoding_dim = 32
input_layer = Input(shape=input_shape)
encoded = Dense(128, activation='relu')(input_layer)
encoded = Dense(encoding_dim, activation='relu', name='encoding_layer')(encoded)
decoded = Dense(128, activation='relu')(encoded)
decoded = Dense(num_mfcc_features, activation='linear')(decoded)
autoencoder = Model(input_layer, decoded)
autoencoder.compile(optimizer='adam', loss='mean_squared_error')
# Extract the encoder part of the autoencoder
encoder = Model(inputs=autoencoder.input, outputs=autoencoder.get_layer('encoding_layer').output)
encoded_data = encoder.predict(x_values)
np.save('LRDB.npy', encoded_data)
encoder.save('encoder_model.h5')