|
'''Copyright 2024 Ashok Kumar
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.'''
|
|
|
|
|
|
import os
|
|
import matplotlib.pyplot as plt
|
|
plt.style.use('dark_background')
|
|
import seaborn as sns
|
|
import numpy as np
|
|
import librosa
|
|
from IPython.display import Audio
|
|
import pandas as pd
|
|
|
|
|
|
|
|
def extract_features(file_path):
|
|
|
|
audio, sample_rate = librosa.load(file_path)
|
|
|
|
mfccs = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=40)
|
|
|
|
flattened_features = np.mean(mfccs.T, axis=0)
|
|
|
|
return flattened_features
|
|
|
|
|
|
def load_data_and_extract_features(data_dir):
|
|
labels = []
|
|
features = []
|
|
|
|
for filename in os.listdir(data_dir):
|
|
if filename.endswith('.wav'):
|
|
file_path = os.path.join(data_dir, filename)
|
|
|
|
label = filename.split('-')[0]
|
|
labels.append(label)
|
|
|
|
feature = extract_features(file_path)
|
|
features.append(feature)
|
|
|
|
return np.array(features), np.array(labels)
|
|
|