Spaces:
Running
Running
feat: audio feature extraction ve vocal analyzer servisleri için music detection eklendi
Browse files
app/services/feature_extractor.py
CHANGED
|
@@ -25,6 +25,7 @@ logger = get_logger(__name__)
|
|
| 25 |
# ── Constants ────────────────────────────────────────────────────────────
|
| 26 |
_TARGET_SR = 22050 # Standard sample rate for analysis
|
| 27 |
_DURATION_LIMIT = 60.0 # Analyze max 1 minute (sufficient for detection)
|
|
|
|
| 28 |
_N_MFCC = 13
|
| 29 |
_N_MELS = 128
|
| 30 |
_HOP_LENGTH = 512
|
|
@@ -118,6 +119,24 @@ def extract_features(
|
|
| 118 |
target_sr = sr or _TARGET_SR
|
| 119 |
|
| 120 |
y, actual_sr = _load_audio(source, target_sr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
duration_sec = float(len(y) / actual_sr)
|
| 122 |
|
| 123 |
logger.info(
|
|
@@ -224,6 +243,7 @@ def _ffmpeg_decode(data: bytes) -> io.BytesIO:
|
|
| 224 |
def _load_audio(
|
| 225 |
source: Union[Path, bytes, io.BytesIO],
|
| 226 |
target_sr: int,
|
|
|
|
| 227 |
) -> tuple[np.ndarray, int]:
|
| 228 |
"""Load audio from any source, mono, resampled, duration-limited."""
|
| 229 |
if isinstance(source, bytes):
|
|
@@ -241,14 +261,14 @@ def _load_audio(
|
|
| 241 |
source if isinstance(source, (str, Path, io.BytesIO)) else str(source),
|
| 242 |
sr=target_sr,
|
| 243 |
mono=True,
|
| 244 |
-
duration=
|
| 245 |
)
|
| 246 |
except Exception:
|
| 247 |
# soundfile can't read webm/opus/ogg — try ffmpeg decode to WAV
|
| 248 |
if raw_bytes is None:
|
| 249 |
raise
|
| 250 |
wav_buf = _ffmpeg_decode(raw_bytes)
|
| 251 |
-
y, sr = librosa.load(wav_buf, sr=target_sr, mono=True, duration=
|
| 252 |
|
| 253 |
# Guard against silent / corrupt files
|
| 254 |
if len(y) < sr:
|
|
|
|
| 25 |
# ── Constants ────────────────────────────────────────────────────────────
|
| 26 |
_TARGET_SR = 22050 # Standard sample rate for analysis
|
| 27 |
_DURATION_LIMIT = 60.0 # Analyze max 1 minute (sufficient for detection)
|
| 28 |
+
MAX_TRACK_SEC = 360.0 # The timeline scan reads at most the first six minutes
|
| 29 |
_N_MFCC = 13
|
| 30 |
_N_MELS = 128
|
| 31 |
_HOP_LENGTH = 512
|
|
|
|
| 119 |
target_sr = sr or _TARGET_SR
|
| 120 |
|
| 121 |
y, actual_sr = _load_audio(source, target_sr)
|
| 122 |
+
return extract_features_array(y, actual_sr)
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
def load_track(
|
| 126 |
+
source: Union[Path, bytes, io.BytesIO],
|
| 127 |
+
*,
|
| 128 |
+
max_sec: float = MAX_TRACK_SEC,
|
| 129 |
+
) -> tuple[np.ndarray, int]:
|
| 130 |
+
"""The opening `max_sec` seconds of a track as mono 22.05 kHz samples.
|
| 131 |
+
|
| 132 |
+
The timeline scan cuts this into windows; the 60 s clip every other layer
|
| 133 |
+
reads still comes from decode_clip, so their input is unchanged.
|
| 134 |
+
"""
|
| 135 |
+
return _load_audio(source, _TARGET_SR, duration=max_sec)
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def extract_features_array(y: np.ndarray, actual_sr: int) -> AudioFeatures:
|
| 139 |
+
"""The same features as extract_features, from samples already in memory."""
|
| 140 |
duration_sec = float(len(y) / actual_sr)
|
| 141 |
|
| 142 |
logger.info(
|
|
|
|
| 243 |
def _load_audio(
|
| 244 |
source: Union[Path, bytes, io.BytesIO],
|
| 245 |
target_sr: int,
|
| 246 |
+
duration: float = _DURATION_LIMIT,
|
| 247 |
) -> tuple[np.ndarray, int]:
|
| 248 |
"""Load audio from any source, mono, resampled, duration-limited."""
|
| 249 |
if isinstance(source, bytes):
|
|
|
|
| 261 |
source if isinstance(source, (str, Path, io.BytesIO)) else str(source),
|
| 262 |
sr=target_sr,
|
| 263 |
mono=True,
|
| 264 |
+
duration=duration,
|
| 265 |
)
|
| 266 |
except Exception:
|
| 267 |
# soundfile can't read webm/opus/ogg — try ffmpeg decode to WAV
|
| 268 |
if raw_bytes is None:
|
| 269 |
raise
|
| 270 |
wav_buf = _ffmpeg_decode(raw_bytes)
|
| 271 |
+
y, sr = librosa.load(wav_buf, sr=target_sr, mono=True, duration=duration)
|
| 272 |
|
| 273 |
# Guard against silent / corrupt files
|
| 274 |
if len(y) < sr:
|
app/services/vocal_analyzer.py
CHANGED
|
@@ -80,6 +80,11 @@ def analyze_vocals(
|
|
| 80 |
"""
|
| 81 |
target_sr = sr or _TARGET_SR
|
| 82 |
y, actual_sr = _load_audio(source, target_sr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
duration_sec = len(y) / actual_sr
|
| 84 |
|
| 85 |
logger.info(f"Vocal analysis: {duration_sec:.1f}s audio @ {actual_sr}Hz")
|
|
|
|
| 80 |
"""
|
| 81 |
target_sr = sr or _TARGET_SR
|
| 82 |
y, actual_sr = _load_audio(source, target_sr)
|
| 83 |
+
return analyze_vocals_array(y, actual_sr)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def analyze_vocals_array(y: np.ndarray, actual_sr: int) -> VocalFeatures:
|
| 87 |
+
"""The same analysis as analyze_vocals, from samples already in memory."""
|
| 88 |
duration_sec = len(y) / actual_sr
|
| 89 |
|
| 90 |
logger.info(f"Vocal analysis: {duration_sec:.1f}s audio @ {actual_sr}Hz")
|