Update audio_features.py
Browse files- audio_features.py +6 -9
audio_features.py
CHANGED
|
@@ -4,10 +4,10 @@ import librosa
|
|
| 4 |
SR = 16000
|
| 5 |
N_COEFFS = 20
|
| 6 |
|
| 7 |
-
def extract_mfcc(y, sr
|
| 8 |
return librosa.feature.mfcc(y=y, sr=sr, n_mfcc=n_mfcc)
|
| 9 |
|
| 10 |
-
def extract_lfcc(y, sr
|
| 11 |
S = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=n_lfcc, fmin=0, fmax=sr/2)
|
| 12 |
return librosa.power_to_db(S)
|
| 13 |
|
|
@@ -15,7 +15,6 @@ def extract_features_with_time_series(file_path, sr=SR, n_coeffs=N_COEFFS):
|
|
| 15 |
try:
|
| 16 |
y, _ = librosa.load(file_path, sr=sr, mono=True)
|
| 17 |
y, _ = librosa.effects.trim(y)
|
| 18 |
-
|
| 19 |
if np.max(np.abs(y)) > 0:
|
| 20 |
y = y / np.max(np.abs(y))
|
| 21 |
|
|
@@ -28,11 +27,9 @@ def extract_features_with_time_series(file_path, sr=SR, n_coeffs=N_COEFFS):
|
|
| 28 |
|
| 29 |
features_to_stack = [mfccs, lfccs, chroma, spec_centroid, spec_bandwidth, zcr]
|
| 30 |
max_len = max(f.shape[1] for f in features_to_stack)
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
stacked_features = np.vstack(padded_features).astype(np.float32)
|
| 34 |
return stacked_features.T
|
| 35 |
-
|
| 36 |
except Exception as e:
|
| 37 |
-
print(f"[extract_features] Error
|
| 38 |
-
return None
|
|
|
|
| 4 |
SR = 16000
|
| 5 |
N_COEFFS = 20
|
| 6 |
|
| 7 |
+
def extract_mfcc(y, sr, n_mfcc=N_COEFFS):
|
| 8 |
return librosa.feature.mfcc(y=y, sr=sr, n_mfcc=n_mfcc)
|
| 9 |
|
| 10 |
+
def extract_lfcc(y, sr, n_lfcc=N_COEFFS):
|
| 11 |
S = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=n_lfcc, fmin=0, fmax=sr/2)
|
| 12 |
return librosa.power_to_db(S)
|
| 13 |
|
|
|
|
| 15 |
try:
|
| 16 |
y, _ = librosa.load(file_path, sr=sr, mono=True)
|
| 17 |
y, _ = librosa.effects.trim(y)
|
|
|
|
| 18 |
if np.max(np.abs(y)) > 0:
|
| 19 |
y = y / np.max(np.abs(y))
|
| 20 |
|
|
|
|
| 27 |
|
| 28 |
features_to_stack = [mfccs, lfccs, chroma, spec_centroid, spec_bandwidth, zcr]
|
| 29 |
max_len = max(f.shape[1] for f in features_to_stack)
|
| 30 |
+
padded = [librosa.util.fix_length(f, size=max_len, axis=1) for f in features_to_stack]
|
| 31 |
+
stacked_features = np.vstack(padded).astype(np.float32)
|
|
|
|
| 32 |
return stacked_features.T
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
+
print(f"[extract_features] Error: {e}")
|
| 35 |
+
return None
|