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

@@ -216,6 +216,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
@@ -153,7 +156,7 @@ class TestSoundScannerService:
finally: finally:
temp_path.unlink() temp_path.unlink()
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_sync_audio_file_new(self, scanner_service): async def test_sync_audio_file_new(self, scanner_service):
"""Test syncing a new audio file.""" """Test syncing a new audio file."""
created_sound = Sound( created_sound = Sound(
@@ -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)
@@ -188,7 +196,7 @@ class TestSoundScannerService:
assert results["updated"] == 0 assert results["updated"] == 0
assert len(results["files"]) == 1 assert len(results["files"]) == 1
assert results["files"][0]["status"] == "added" assert results["files"][0]["status"] == "added"
# Verify sound_repo.create was called with correct data # Verify sound_repo.create was called with correct data
call_args = scanner_service.sound_repo.create.call_args[0][0] call_args = scanner_service.sound_repo.create.call_args[0][0]
assert call_args["type"] == "SDB" assert call_args["type"] == "SDB"
@@ -200,7 +208,7 @@ class TestSoundScannerService:
finally: finally:
temp_path.unlink() temp_path.unlink()
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_sync_audio_file_updated(self, scanner_service): async def test_sync_audio_file_updated(self, scanner_service):
"""Test syncing a file that was modified (different hash).""" """Test syncing a file that was modified (different hash)."""
# Existing sound with different hash than file # Existing sound with different hash than file
@@ -210,7 +218,7 @@ class TestSoundScannerService:
name="Old Sound", name="Old Sound",
filename="test.mp3", filename="test.mp3",
duration=60000, # Old duration duration=60000, # Old duration
size=512, # Old size size=512, # Old size
hash="old_hash", # Old hash hash="old_hash", # Old hash
) )
@@ -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
@@ -240,7 +253,7 @@ class TestSoundScannerService:
assert len(results["files"]) == 1 assert len(results["files"]) == 1
assert results["files"][0]["status"] == "updated" assert results["files"][0]["status"] == "updated"
assert results["files"][0]["reason"] == "file was modified" assert results["files"][0]["reason"] == "file was modified"
# Verify sound_repo.update was called with correct data # Verify sound_repo.update was called with correct data
call_args = scanner_service.sound_repo.update.call_args[0][1] # update_data call_args = scanner_service.sound_repo.update.call_args[0][1] # update_data
assert call_args["duration"] == 120000 assert call_args["duration"] == 120000
@@ -251,7 +264,7 @@ class TestSoundScannerService:
finally: finally:
temp_path.unlink() temp_path.unlink()
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_sync_audio_file_custom_type(self, scanner_service): async def test_sync_audio_file_custom_type(self, scanner_service):
"""Test syncing file with custom type.""" """Test syncing file with custom type."""
created_sound = Sound( created_sound = Sound(
@@ -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)
@@ -285,7 +303,7 @@ class TestSoundScannerService:
assert results["skipped"] == 0 assert results["skipped"] == 0
assert len(results["files"]) == 1 assert len(results["files"]) == 1
assert results["files"][0]["status"] == "added" assert results["files"][0]["status"] == "added"
# Verify sound_repo.create was called with correct data for custom type # Verify sound_repo.create was called with correct data for custom type
call_args = scanner_service.sound_repo.create.call_args[0][0] call_args = scanner_service.sound_repo.create.call_args[0][0]
assert call_args["type"] == "CUSTOM" assert call_args["type"] == "CUSTOM"
@@ -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()