feat: Add Extraction model and seed main playlist functionality
This commit is contained in:
@@ -53,7 +53,7 @@ class TestSoundNormalizerService:
|
||||
)
|
||||
|
||||
normalized_path = normalizer_service._get_normalized_path(sound)
|
||||
|
||||
|
||||
assert "sounds/normalized/soundboard" in str(normalized_path)
|
||||
assert "test_audio.mp3" == normalized_path.name
|
||||
|
||||
@@ -70,7 +70,7 @@ class TestSoundNormalizerService:
|
||||
)
|
||||
|
||||
original_path = normalizer_service._get_original_path(sound)
|
||||
|
||||
|
||||
assert "sounds/originals/soundboard" in str(original_path)
|
||||
assert "test_audio.wav" == original_path.name
|
||||
|
||||
@@ -83,6 +83,7 @@ class TestSoundNormalizerService:
|
||||
|
||||
try:
|
||||
from app.utils.audio import get_file_hash
|
||||
|
||||
hash_value = get_file_hash(temp_path)
|
||||
assert len(hash_value) == 64 # SHA-256 hash length
|
||||
assert isinstance(hash_value, str)
|
||||
@@ -98,6 +99,7 @@ class TestSoundNormalizerService:
|
||||
|
||||
try:
|
||||
from app.utils.audio import get_file_size
|
||||
|
||||
size = get_file_size(temp_path)
|
||||
assert size > 0
|
||||
assert isinstance(size, int)
|
||||
@@ -111,6 +113,7 @@ class TestSoundNormalizerService:
|
||||
|
||||
temp_path = Path("/fake/path/test.mp3")
|
||||
from app.utils.audio import get_audio_duration
|
||||
|
||||
duration = get_audio_duration(temp_path)
|
||||
|
||||
assert duration == 123456 # 123.456 seconds * 1000 = 123456 ms
|
||||
@@ -123,6 +126,7 @@ class TestSoundNormalizerService:
|
||||
|
||||
temp_path = Path("/fake/path/test.mp3")
|
||||
from app.utils.audio import get_audio_duration
|
||||
|
||||
duration = get_audio_duration(temp_path)
|
||||
|
||||
assert duration == 0
|
||||
@@ -155,7 +159,7 @@ class TestSoundNormalizerService:
|
||||
sound = Sound(
|
||||
id=1,
|
||||
type="SDB",
|
||||
name="Test Sound",
|
||||
name="Test Sound",
|
||||
filename="test.mp3",
|
||||
duration=5000,
|
||||
size=1024,
|
||||
@@ -164,17 +168,25 @@ class TestSoundNormalizerService:
|
||||
)
|
||||
|
||||
# Mock file operations and ffmpeg
|
||||
with patch.object(normalizer_service, "_get_original_path") as mock_orig_path, \
|
||||
patch.object(normalizer_service, "_get_normalized_path") as mock_norm_path, \
|
||||
patch.object(normalizer_service, "_normalize_audio_two_pass") as mock_normalize, \
|
||||
patch("app.services.sound_normalizer.get_audio_duration", return_value=6000), \
|
||||
patch("app.services.sound_normalizer.get_file_size", return_value=2048), \
|
||||
patch("app.services.sound_normalizer.get_file_hash", return_value="new_hash"):
|
||||
with (
|
||||
patch.object(normalizer_service, "_get_original_path") as mock_orig_path,
|
||||
patch.object(normalizer_service, "_get_normalized_path") as mock_norm_path,
|
||||
patch.object(
|
||||
normalizer_service, "_normalize_audio_two_pass"
|
||||
) as mock_normalize,
|
||||
patch(
|
||||
"app.services.sound_normalizer.get_audio_duration", return_value=6000
|
||||
),
|
||||
patch("app.services.sound_normalizer.get_file_size", return_value=2048),
|
||||
patch(
|
||||
"app.services.sound_normalizer.get_file_hash", return_value="new_hash"
|
||||
),
|
||||
):
|
||||
|
||||
# Setup path mocks
|
||||
mock_orig_path.return_value = Path("/fake/original.mp3")
|
||||
mock_norm_path.return_value = Path("/fake/normalized.mp3")
|
||||
|
||||
|
||||
# Mock file existence
|
||||
with patch("pathlib.Path.exists", return_value=True):
|
||||
# Mock repository update
|
||||
@@ -207,7 +219,7 @@ class TestSoundNormalizerService:
|
||||
|
||||
with patch.object(normalizer_service, "_get_original_path") as mock_path:
|
||||
mock_path.return_value = Path("/fake/missing.mp3")
|
||||
|
||||
|
||||
# Mock file doesn't exist
|
||||
with patch("pathlib.Path.exists", return_value=False):
|
||||
result = await normalizer_service.normalize_sound(sound)
|
||||
@@ -230,17 +242,25 @@ class TestSoundNormalizerService:
|
||||
is_normalized=False,
|
||||
)
|
||||
|
||||
with patch.object(normalizer_service, "_get_original_path") as mock_orig_path, \
|
||||
patch.object(normalizer_service, "_get_normalized_path") as mock_norm_path, \
|
||||
patch.object(normalizer_service, "_normalize_audio_one_pass") as mock_normalize, \
|
||||
patch("app.services.sound_normalizer.get_audio_duration", return_value=5500), \
|
||||
patch("app.services.sound_normalizer.get_file_size", return_value=1500), \
|
||||
patch("app.services.sound_normalizer.get_file_hash", return_value="norm_hash"):
|
||||
with (
|
||||
patch.object(normalizer_service, "_get_original_path") as mock_orig_path,
|
||||
patch.object(normalizer_service, "_get_normalized_path") as mock_norm_path,
|
||||
patch.object(
|
||||
normalizer_service, "_normalize_audio_one_pass"
|
||||
) as mock_normalize,
|
||||
patch(
|
||||
"app.services.sound_normalizer.get_audio_duration", return_value=5500
|
||||
),
|
||||
patch("app.services.sound_normalizer.get_file_size", return_value=1500),
|
||||
patch(
|
||||
"app.services.sound_normalizer.get_file_hash", return_value="norm_hash"
|
||||
),
|
||||
):
|
||||
|
||||
# Setup path mocks
|
||||
mock_orig_path.return_value = Path("/fake/original.mp3")
|
||||
mock_norm_path.return_value = Path("/fake/normalized.mp3")
|
||||
|
||||
|
||||
# Mock file existence
|
||||
with patch("pathlib.Path.exists", return_value=True):
|
||||
# Mock repository update
|
||||
@@ -252,7 +272,7 @@ class TestSoundNormalizerService:
|
||||
assert result["normalized_duration"] == 5500
|
||||
assert result["normalized_size"] == 1500
|
||||
assert result["normalized_hash"] == "norm_hash"
|
||||
|
||||
|
||||
# Verify one-pass was used
|
||||
mock_normalize.assert_called_once()
|
||||
|
||||
@@ -270,17 +290,23 @@ class TestSoundNormalizerService:
|
||||
is_normalized=False,
|
||||
)
|
||||
|
||||
with patch.object(normalizer_service, "_get_original_path") as mock_orig_path, \
|
||||
patch.object(normalizer_service, "_get_normalized_path") as mock_norm_path:
|
||||
with (
|
||||
patch.object(normalizer_service, "_get_original_path") as mock_orig_path,
|
||||
patch.object(normalizer_service, "_get_normalized_path") as mock_norm_path,
|
||||
):
|
||||
|
||||
# Setup path mocks
|
||||
mock_orig_path.return_value = Path("/fake/original.mp3")
|
||||
mock_norm_path.return_value = Path("/fake/normalized.mp3")
|
||||
|
||||
|
||||
# Mock file existence but normalization fails
|
||||
with patch("pathlib.Path.exists", return_value=True), \
|
||||
patch.object(normalizer_service, "_normalize_audio_two_pass") as mock_normalize:
|
||||
|
||||
with (
|
||||
patch("pathlib.Path.exists", return_value=True),
|
||||
patch.object(
|
||||
normalizer_service, "_normalize_audio_two_pass"
|
||||
) as mock_normalize,
|
||||
):
|
||||
|
||||
mock_normalize.side_effect = Exception("Normalization failed")
|
||||
|
||||
result = await normalizer_service.normalize_sound(sound)
|
||||
@@ -306,7 +332,7 @@ class TestSoundNormalizerService:
|
||||
Sound(
|
||||
id=2,
|
||||
type="TTS",
|
||||
name="Sound 2",
|
||||
name="Sound 2",
|
||||
filename="sound2.wav",
|
||||
duration=3000,
|
||||
size=512,
|
||||
@@ -316,8 +342,10 @@ class TestSoundNormalizerService:
|
||||
]
|
||||
|
||||
# Mock repository calls
|
||||
normalizer_service.sound_repo.get_unnormalized_sounds = AsyncMock(return_value=sounds)
|
||||
|
||||
normalizer_service.sound_repo.get_unnormalized_sounds = AsyncMock(
|
||||
return_value=sounds
|
||||
)
|
||||
|
||||
# Mock individual normalization
|
||||
with patch.object(normalizer_service, "normalize_sound") as mock_normalize:
|
||||
mock_normalize.side_effect = [
|
||||
@@ -377,7 +405,7 @@ class TestSoundNormalizerService:
|
||||
normalizer_service.sound_repo.get_unnormalized_sounds_by_type = AsyncMock(
|
||||
return_value=sdb_sounds
|
||||
)
|
||||
|
||||
|
||||
# Mock individual normalization
|
||||
with patch.object(normalizer_service, "normalize_sound") as mock_normalize:
|
||||
mock_normalize.return_value = {
|
||||
@@ -403,7 +431,9 @@ class TestSoundNormalizerService:
|
||||
assert len(results["files"]) == 1
|
||||
|
||||
# Verify correct repository method was called
|
||||
normalizer_service.sound_repo.get_unnormalized_sounds_by_type.assert_called_once_with("SDB")
|
||||
normalizer_service.sound_repo.get_unnormalized_sounds_by_type.assert_called_once_with(
|
||||
"SDB"
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_normalize_sounds_with_errors(self, normalizer_service):
|
||||
@@ -432,8 +462,10 @@ class TestSoundNormalizerService:
|
||||
]
|
||||
|
||||
# Mock repository calls
|
||||
normalizer_service.sound_repo.get_unnormalized_sounds = AsyncMock(return_value=sounds)
|
||||
|
||||
normalizer_service.sound_repo.get_unnormalized_sounds = AsyncMock(
|
||||
return_value=sounds
|
||||
)
|
||||
|
||||
# Mock individual normalization with one success and one error
|
||||
with patch.object(normalizer_service, "normalize_sound") as mock_normalize:
|
||||
mock_normalize.side_effect = [
|
||||
@@ -481,7 +513,7 @@ class TestSoundNormalizerService:
|
||||
@pytest.mark.asyncio
|
||||
@patch("app.services.sound_normalizer.ffmpeg")
|
||||
async def test_normalize_audio_one_pass_mp3(
|
||||
self,
|
||||
self,
|
||||
mock_ffmpeg,
|
||||
normalizer_service,
|
||||
):
|
||||
@@ -500,7 +532,9 @@ class TestSoundNormalizerService:
|
||||
|
||||
# Verify ffmpeg chain was called correctly
|
||||
mock_ffmpeg.input.assert_called_once_with(str(input_path))
|
||||
mock_ffmpeg.filter.assert_called_once_with(mock_stream, "loudnorm", I=-23, TP=-2, LRA=7)
|
||||
mock_ffmpeg.filter.assert_called_once_with(
|
||||
mock_stream, "loudnorm", I=-23, TP=-2, LRA=7
|
||||
)
|
||||
mock_ffmpeg.output.assert_called_once()
|
||||
mock_ffmpeg.run.assert_called_once()
|
||||
|
||||
@@ -522,7 +556,7 @@ class TestSoundNormalizerService:
|
||||
input_path = Path("/fake/input.wav")
|
||||
output_path = Path("/fake/output.mp3")
|
||||
|
||||
# Mock ffmpeg chain
|
||||
# Mock ffmpeg chain
|
||||
mock_stream = Mock()
|
||||
mock_ffmpeg.input.return_value = mock_stream
|
||||
mock_ffmpeg.filter.return_value = mock_stream
|
||||
@@ -530,14 +564,14 @@ class TestSoundNormalizerService:
|
||||
mock_ffmpeg.overwrite_output.return_value = mock_stream
|
||||
|
||||
# Mock analysis output with valid JSON
|
||||
analysis_json = '''{
|
||||
analysis_json = """{
|
||||
"input_i": "-23.0",
|
||||
"input_lra": "11.0",
|
||||
"input_tp": "-2.0",
|
||||
"input_thresh": "-33.0",
|
||||
"target_offset": "0.0"
|
||||
}'''
|
||||
|
||||
}"""
|
||||
|
||||
mock_ffmpeg.run.side_effect = [
|
||||
(None, analysis_json.encode("utf-8")), # First pass analysis
|
||||
None, # Second pass normalization
|
||||
@@ -554,10 +588,10 @@ class TestSoundNormalizerService:
|
||||
assert first_filter_call[1]["print_format"] == "json"
|
||||
|
||||
# Verify second pass used measured values
|
||||
second_filter_call = mock_ffmpeg.filter.call_args_list[1]
|
||||
second_filter_call = mock_ffmpeg.filter.call_args_list[1]
|
||||
measured_args = second_filter_call[1]
|
||||
assert "measured_I" in measured_args
|
||||
assert "measured_LRA" in measured_args
|
||||
assert "measured_TP" in measured_args
|
||||
assert "measured_thresh" in measured_args
|
||||
assert "offset" in measured_args
|
||||
assert "offset" in measured_args
|
||||
|
||||
Reference in New Issue
Block a user