From 71da295827eb9942f38b01aac95b5ae1ba9bac98 Mon Sep 17 00:00:00 2001 From: JSC Date: Mon, 28 Jul 2025 09:48:35 +0200 Subject: [PATCH] feat: Enhance audio normalization service to handle invalid analysis values and improve fallback logic --- app/services/sound_normalizer.py | 12 ++++++ tests/services/test_sound_scanner.py | 62 ++++++++++++++++++---------- 2 files changed, 53 insertions(+), 21 deletions(-) diff --git a/app/services/sound_normalizer.py b/app/services/sound_normalizer.py index 45de40c..1cb4ebf 100644 --- a/app/services/sound_normalizer.py +++ b/app/services/sound_normalizer.py @@ -216,6 +216,18 @@ class SoundNormalizerService: logger.debug("Found JSON match: %s", 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 logger.debug("Second pass: normalizing %s with measured values", input_path) diff --git a/tests/services/test_sound_scanner.py b/tests/services/test_sound_scanner.py index c65c253..e7b2507 100644 --- a/tests/services/test_sound_scanner.py +++ b/tests/services/test_sound_scanner.py @@ -41,7 +41,7 @@ class TestSoundScannerService: try: 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) finally: temp_path.unlink() @@ -77,9 +77,7 @@ class TestSoundScannerService: @patch("app.services.sound_scanner.ffmpeg.probe") def test_get_audio_duration_success(self, mock_probe, scanner_service): """Test successful audio duration extraction.""" - mock_probe.return_value = { - "format": {"duration": "123.456"} - } + mock_probe.return_value = {"format": {"duration": "123.456"}} temp_path = Path("/fake/path/test.mp3") duration = scanner_service.get_audio_duration(temp_path) @@ -137,8 +135,13 @@ class TestSoundScannerService: try: results = { - "scanned": 0, "added": 0, "updated": 0, "deleted": 0, - "skipped": 0, "errors": 0, "files": [] + "scanned": 0, + "added": 0, + "updated": 0, + "deleted": 0, + "skipped": 0, + "errors": 0, + "files": [], } await scanner_service._sync_audio_file( temp_path, "SDB", existing_sound, results @@ -153,7 +156,7 @@ class TestSoundScannerService: finally: temp_path.unlink() - @pytest.mark.asyncio + @pytest.mark.asyncio async def test_sync_audio_file_new(self, scanner_service): """Test syncing a new audio file.""" created_sound = Sound( @@ -178,8 +181,13 @@ class TestSoundScannerService: try: results = { - "scanned": 0, "added": 0, "updated": 0, "deleted": 0, - "skipped": 0, "errors": 0, "files": [] + "scanned": 0, + "added": 0, + "updated": 0, + "deleted": 0, + "skipped": 0, + "errors": 0, + "files": [], } await scanner_service._sync_audio_file(temp_path, "SDB", None, results) @@ -188,7 +196,7 @@ class TestSoundScannerService: assert results["updated"] == 0 assert len(results["files"]) == 1 assert results["files"][0]["status"] == "added" - + # Verify sound_repo.create was called with correct data call_args = scanner_service.sound_repo.create.call_args[0][0] assert call_args["type"] == "SDB" @@ -200,7 +208,7 @@ class TestSoundScannerService: finally: temp_path.unlink() - @pytest.mark.asyncio + @pytest.mark.asyncio async def test_sync_audio_file_updated(self, scanner_service): """Test syncing a file that was modified (different hash).""" # Existing sound with different hash than file @@ -210,7 +218,7 @@ class TestSoundScannerService: name="Old Sound", filename="test.mp3", duration=60000, # Old duration - size=512, # Old size + size=512, # Old size hash="old_hash", # Old hash ) @@ -227,8 +235,13 @@ class TestSoundScannerService: try: results = { - "scanned": 0, "added": 0, "updated": 0, "deleted": 0, - "skipped": 0, "errors": 0, "files": [] + "scanned": 0, + "added": 0, + "updated": 0, + "deleted": 0, + "skipped": 0, + "errors": 0, + "files": [], } await scanner_service._sync_audio_file( temp_path, "SDB", existing_sound, results @@ -240,7 +253,7 @@ class TestSoundScannerService: assert len(results["files"]) == 1 assert results["files"][0]["status"] == "updated" assert results["files"][0]["reason"] == "file was modified" - + # Verify sound_repo.update was called with correct data call_args = scanner_service.sound_repo.update.call_args[0][1] # update_data assert call_args["duration"] == 120000 @@ -251,7 +264,7 @@ class TestSoundScannerService: finally: temp_path.unlink() - @pytest.mark.asyncio + @pytest.mark.asyncio async def test_sync_audio_file_custom_type(self, scanner_service): """Test syncing file with custom type.""" created_sound = Sound( @@ -276,8 +289,13 @@ class TestSoundScannerService: try: results = { - "scanned": 0, "added": 0, "updated": 0, "deleted": 0, - "skipped": 0, "errors": 0, "files": [] + "scanned": 0, + "added": 0, + "updated": 0, + "deleted": 0, + "skipped": 0, + "errors": 0, + "files": [], } await scanner_service._sync_audio_file(temp_path, "CUSTOM", None, results) @@ -285,7 +303,7 @@ class TestSoundScannerService: assert results["skipped"] == 0 assert len(results["files"]) == 1 assert results["files"][0]["status"] == "added" - + # Verify sound_repo.create was called with correct data for custom type call_args = scanner_service.sound_repo.create.call_args[0][0] assert call_args["type"] == "CUSTOM" @@ -293,6 +311,8 @@ class TestSoundScannerService: assert call_args["duration"] == 60000 # Duration in ms assert call_args["size"] == 2048 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: - temp_path.unlink() \ No newline at end of file + temp_path.unlink()