-
Notifications
You must be signed in to change notification settings - Fork 1
/
monngodb1.py
107 lines (83 loc) · 4.17 KB
/
monngodb1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import os
import librosa
import numpy as np
from pymongo import MongoClient
# Connect to MongoDB
client = MongoClient('mongodb://localhost:27017/') # Update connection string as needed
db = client['project']
collection = db['tashi']
# Path to the folder containing all the folders with audio files
root_folder = '/home/laibu/Downloads/fma_large'
# Initialize lists to store features for all audio files
all_mfccs = []
all_centroids = []
all_zero_crossing_rates = []
# Define a fixed length for MFCC arrays
max_frames = 500
# Number of files to process per folder
files_per_folder = 350
for folder_name in sorted(os.listdir(root_folder)): # Iterate over all folders
folder_path = os.path.join(root_folder, folder_name)
# Skip if it's not a directory
if not os.path.isdir(folder_path):
continue
# Counter for the number of files processed in the current folder
files_processed = 0
# Print the folder path
print(f"Processing folder: {folder_path}")
# Loop through each audio file in the current subfolder
for audio_file in os.listdir(folder_path):
if audio_file.endswith('.mp3'):
audio_path = os.path.join(folder_path, audio_file)
# Print the audio file path
print(f"Processing audio file: {audio_path}")
try:
# Load audio file
y, sr = librosa.load(audio_path)
# Extract MFCCs
mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
# Pad or truncate MFCCs to ensure fixed length
if mfccs.shape[1] < max_frames:
# Pad with zeros
mfccs = np.pad(mfccs, ((0, 0), (0, max_frames - mfccs.shape[1])), mode='constant')
elif mfccs.shape[1] > max_frames:
# Truncate
mfccs = mfccs[:, :max_frames]
# Calculate spectral centroid
centroid = librosa.feature.spectral_centroid(y=y, sr=sr)
centroid = centroid[:, :max_frames] # Truncate to match MFCCs length
centroid_mean = np.mean(centroid) # Calculate the mean value of the centroid
centroid = np.full_like(mfccs, centroid_mean) # Create an array with the same shape as MFCCs, filled with the centroid mean
# Calculate zero-crossing rate
zero_crossing_rate = librosa.feature.zero_crossing_rate(y)
zero_crossing_rate = zero_crossing_rate[:, :max_frames] # Truncate to match MFCCs length
zero_crossing_rate_mean = np.mean(zero_crossing_rate) # Calculate the mean value of the zero-crossing rate
zero_crossing_rate = np.full_like(mfccs, zero_crossing_rate_mean) # Create an array with the same shape as MFCCs, filled with the zero-crossing rate mean
# Append MFCCs, spectral centroid, and zero-crossing rate to the lists
all_mfccs.append(mfccs)
all_centroids.append(centroid)
all_zero_crossing_rates.append(zero_crossing_rate)
# Prepare document to insert into MongoDB
document = {
"folder": folder_name,
"audio_file": audio_file,
"mfccs": mfccs.tolist(),
"spectral_centroid": centroid.tolist(),
"zero_crossing_rate": zero_crossing_rate.tolist()
}
# Insert document into MongoDB collection
collection.insert_one(document)
# Increment the counter for files processed in the current folder
files_processed += 1
# Break if processed the required number of files per folder
if files_processed >= files_per_folder:
break
except Exception as e:
print(f"Error processing audio file: {audio_path}")
print(e)
# Convert the lists of features to numpy arrays
all_mfccs = np.array(all_mfccs)
all_centroids = np.array(all_centroids)
all_zero_crossing_rates = np.array(all_zero_crossing_rates)
# Get the number of frames (columns) in the MFCCs array
num_frames = all_mfccs.shape[2]