Spaces:
Build error
Build error
feat: add spectral rolloff and MFCC delta features to audio extraction
Browse files
app/services/feature_extractor.py
CHANGED
|
@@ -189,6 +189,11 @@ def _extract_spectral(y: np.ndarray, sr: int) -> dict:
|
|
| 189 |
y=y, sr=sr, hop_length=_HOP_LENGTH
|
| 190 |
)[0]
|
| 191 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
# Spectral contrast — valley-to-peak in frequency bands
|
| 193 |
contrast = librosa.feature.spectral_contrast(
|
| 194 |
y=y, sr=sr, hop_length=_HOP_LENGTH, n_bands=6
|
|
@@ -208,6 +213,10 @@ def _extract_spectral(y: np.ndarray, sr: int) -> dict:
|
|
| 208 |
)
|
| 209 |
mel_db = librosa.power_to_db(mel, ref=np.max)
|
| 210 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
return {
|
| 212 |
"centroid_mean": float(np.mean(centroid)),
|
| 213 |
"centroid_std": float(np.std(centroid)),
|
|
@@ -215,12 +224,13 @@ def _extract_spectral(y: np.ndarray, sr: int) -> dict:
|
|
| 215 |
"flatness_std": float(np.std(flatness)),
|
| 216 |
"bandwidth_mean": float(np.mean(bandwidth)),
|
| 217 |
"bandwidth_std": float(np.std(bandwidth)),
|
|
|
|
|
|
|
| 218 |
"contrast_mean": float(np.mean(contrast)),
|
| 219 |
"contrast_std": float(np.std(contrast)),
|
| 220 |
"mfcc_variance": float(np.mean(np.var(mfcc, axis=1))),
|
| 221 |
-
"mfcc_delta_var": float(np.mean(np.var(
|
| 222 |
-
|
| 223 |
-
))),
|
| 224 |
"rms_mean": float(np.mean(rms)),
|
| 225 |
"rms_std": float(np.std(rms)),
|
| 226 |
"mel_flatness": float(np.mean(np.std(mel_db, axis=0))),
|
|
|
|
| 189 |
y=y, sr=sr, hop_length=_HOP_LENGTH
|
| 190 |
)[0]
|
| 191 |
|
| 192 |
+
# Spectral rolloff — frequency below which 85% of energy
|
| 193 |
+
rolloff = librosa.feature.spectral_rolloff(
|
| 194 |
+
y=y, sr=sr, hop_length=_HOP_LENGTH, roll_percent=0.85
|
| 195 |
+
)[0]
|
| 196 |
+
|
| 197 |
# Spectral contrast — valley-to-peak in frequency bands
|
| 198 |
contrast = librosa.feature.spectral_contrast(
|
| 199 |
y=y, sr=sr, hop_length=_HOP_LENGTH, n_bands=6
|
|
|
|
| 213 |
)
|
| 214 |
mel_db = librosa.power_to_db(mel, ref=np.max)
|
| 215 |
|
| 216 |
+
# MFCC delta (first derivative) and delta-delta (acceleration)
|
| 217 |
+
mfcc_delta = librosa.feature.delta(mfcc)
|
| 218 |
+
mfcc_delta2 = librosa.feature.delta(mfcc, order=2)
|
| 219 |
+
|
| 220 |
return {
|
| 221 |
"centroid_mean": float(np.mean(centroid)),
|
| 222 |
"centroid_std": float(np.std(centroid)),
|
|
|
|
| 224 |
"flatness_std": float(np.std(flatness)),
|
| 225 |
"bandwidth_mean": float(np.mean(bandwidth)),
|
| 226 |
"bandwidth_std": float(np.std(bandwidth)),
|
| 227 |
+
"rolloff_mean": float(np.mean(rolloff)),
|
| 228 |
+
"rolloff_std": float(np.std(rolloff)),
|
| 229 |
"contrast_mean": float(np.mean(contrast)),
|
| 230 |
"contrast_std": float(np.std(contrast)),
|
| 231 |
"mfcc_variance": float(np.mean(np.var(mfcc, axis=1))),
|
| 232 |
+
"mfcc_delta_var": float(np.mean(np.var(mfcc_delta, axis=1))),
|
| 233 |
+
"mfcc_delta2_var": float(np.mean(np.var(mfcc_delta2, axis=1))),
|
|
|
|
| 234 |
"rms_mean": float(np.mean(rms)),
|
| 235 |
"rms_std": float(np.std(rms)),
|
| 236 |
"mel_flatness": float(np.mean(np.std(mel_db, axis=0))),
|