feat: Enhance audio normalization service to handle invalid analysis values and improve fallback logic

This commit is contained in:
JSC
2025-07-28 09:48:35 +02:00
parent 0fffce53b4
commit 71da295827
2 changed files with 53 additions and 21 deletions

View File

@@ -217,6 +217,18 @@ class SoundNormalizerService:
logger.debug("Found JSON match: %s", json_match.group()) logger.debug("Found JSON match: %s", json_match.group())
analysis_data = json.loads(json_match.group()) analysis_data = json.loads(json_match.group())
# Check for invalid values that would cause second pass to fail
invalid_values = ["-inf", "inf", "nan"]
for key in ["input_i", "input_lra", "input_tp", "input_thresh", "target_offset"]:
if str(analysis_data.get(key, "")).lower() in invalid_values:
logger.warning(
"Invalid analysis value for %s: %s. Falling back to one-pass normalization.",
key, analysis_data.get(key)
)
# Fall back to one-pass normalization
await self._normalize_audio_one_pass(input_path, output_path)
return
# Second pass: normalize with measured values # Second pass: normalize with measured values
logger.debug("Second pass: normalizing %s with measured values", input_path) logger.debug("Second pass: normalizing %s with measured values", input_path)

View File

@@ -41,7 +41,7 @@ class TestSoundScannerService:
try: try:
hash_value = scanner_service.get_file_hash(temp_path) hash_value = scanner_service.get_file_hash(temp_path)
assert len(hash_value) == 32 # MD5 hash length assert len(hash_value) == 64 # SHA-256 hash length
assert isinstance(hash_value, str) assert isinstance(hash_value, str)
finally: finally:
temp_path.unlink() temp_path.unlink()
@@ -77,9 +77,7 @@ class TestSoundScannerService:
@patch("app.services.sound_scanner.ffmpeg.probe") @patch("app.services.sound_scanner.ffmpeg.probe")
def test_get_audio_duration_success(self, mock_probe, scanner_service): def test_get_audio_duration_success(self, mock_probe, scanner_service):
"""Test successful audio duration extraction.""" """Test successful audio duration extraction."""
mock_probe.return_value = { mock_probe.return_value = {"format": {"duration": "123.456"}}
"format": {"duration": "123.456"}
}
temp_path = Path("/fake/path/test.mp3") temp_path = Path("/fake/path/test.mp3")
duration = scanner_service.get_audio_duration(temp_path) duration = scanner_service.get_audio_duration(temp_path)
@@ -137,8 +135,13 @@ class TestSoundScannerService:
try: try:
results = { results = {
"scanned": 0, "added": 0, "updated": 0, "deleted": 0, "scanned": 0,
"skipped": 0, "errors": 0, "files": [] "added": 0,
"updated": 0,
"deleted": 0,
"skipped": 0,
"errors": 0,
"files": [],
} }
await scanner_service._sync_audio_file( await scanner_service._sync_audio_file(
temp_path, "SDB", existing_sound, results temp_path, "SDB", existing_sound, results
@@ -178,8 +181,13 @@ class TestSoundScannerService:
try: try:
results = { results = {
"scanned": 0, "added": 0, "updated": 0, "deleted": 0, "scanned": 0,
"skipped": 0, "errors": 0, "files": [] "added": 0,
"updated": 0,
"deleted": 0,
"skipped": 0,
"errors": 0,
"files": [],
} }
await scanner_service._sync_audio_file(temp_path, "SDB", None, results) await scanner_service._sync_audio_file(temp_path, "SDB", None, results)
@@ -227,8 +235,13 @@ class TestSoundScannerService:
try: try:
results = { results = {
"scanned": 0, "added": 0, "updated": 0, "deleted": 0, "scanned": 0,
"skipped": 0, "errors": 0, "files": [] "added": 0,
"updated": 0,
"deleted": 0,
"skipped": 0,
"errors": 0,
"files": [],
} }
await scanner_service._sync_audio_file( await scanner_service._sync_audio_file(
temp_path, "SDB", existing_sound, results temp_path, "SDB", existing_sound, results
@@ -276,8 +289,13 @@ class TestSoundScannerService:
try: try:
results = { results = {
"scanned": 0, "added": 0, "updated": 0, "deleted": 0, "scanned": 0,
"skipped": 0, "errors": 0, "files": [] "added": 0,
"updated": 0,
"deleted": 0,
"skipped": 0,
"errors": 0,
"files": [],
} }
await scanner_service._sync_audio_file(temp_path, "CUSTOM", None, results) await scanner_service._sync_audio_file(temp_path, "CUSTOM", None, results)
@@ -293,6 +311,8 @@ class TestSoundScannerService:
assert call_args["duration"] == 60000 # Duration in ms assert call_args["duration"] == 60000 # Duration in ms
assert call_args["size"] == 2048 assert call_args["size"] == 2048
assert call_args["hash"] == "custom_hash" assert call_args["hash"] == "custom_hash"
assert call_args["is_deletable"] is False # All sounds are set to not deletable assert (
call_args["is_deletable"] is False
) # All sounds are set to not deletable
finally: finally:
temp_path.unlink() temp_path.unlink()