feat: Enhance audio normalization service to handle invalid analysis values and improve fallback logic
This commit is contained in:
@@ -217,6 +217,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)
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
Reference in New Issue
Block a user