refactor: replace custom SNR calculator with librosa
Summary
app/utils/snr_frequency.py (473 lines) computes Signal-to-Noise Ratio by launching ffmpeg subprocesses and parsing stdout with regex across three fallback methods. This approach is fragile, hard to test, and tightly coupled to ffmpeg's output format.
Problem
- Three separate SNR methods all rely on parsing unstructured ffmpeg text output (e.g.,
astatsfilter,silencedetectfilter,volumedetectfilter) - Any ffmpeg version upgrade can silently break parsing without raising an exception
- The fallback chain masks failures — a wrong result is returned instead of an error
- Unit testing requires mocking subprocess calls, not the actual audio logic
- 473 lines of custom signal-processing code that the team must maintain indefinitely
Proposed Solution
Replace with librosa (Apache 2.0), the de-facto Python audio analysis library used across academia and industry.
import librosa
import numpy as np
y, sr = librosa.load(path, sr=None, mono=True)
rms = librosa.feature.rms(y=y)[0]
snr_db = 10 * np.log10(np.max(rms) / (np.min(rms) + 1e-10))
librosa handles MP3, WAV, M4A, OGG natively via soundfile/audioread — no subprocess required.
Library signals:
- License: Apache 2.0
- Weekly downloads: 4M+
- Maintenance: Active (NumFOCUS affiliated)
- Test suite: Comprehensive CI
- CVEs: None known
Impact
| Dimension | Current | After |
|---|---|---|
| LOC | 473 custom lines | ~20 lines |
| Fragility | Breaks on ffmpeg output changes | Pure Python, version-stable |
| Testability | Requires subprocess mocking | Standard unit tests |
| Accuracy | Heuristic bounds (5–60 dB hardcoded) | Mathematically correct |
| Dependencies | ffmpeg (system) + regex parsing | librosa (pip) |
Files Affected
-
app/utils/snr_frequency.py— replace entirely - Any callers in
app/tasks/data_analysis.pyandapp/tasks/file_processing.py— update import and call site only