From c44b055f83a3aa7bf9948e827777242de65039a6 Mon Sep 17 00:00:00 2001 From: JSC Date: Mon, 7 Jul 2025 15:29:50 +0200 Subject: [PATCH] feat(stream): integrate sound normalization into stream processing service --- app/services/sound_normalizer_service.py | 32 ++++++++++++++++------- app/services/stream_processing_service.py | 27 +++++++++++++++++++ 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/app/services/sound_normalizer_service.py b/app/services/sound_normalizer_service.py index 6973cfe..00e9588 100644 --- a/app/services/sound_normalizer_service.py +++ b/app/services/sound_normalizer_service.py @@ -27,8 +27,17 @@ class SoundNormalizerService: ".aac", ".opus", } - SOUNDS_DIR = "sounds/soundboard" - NORMALIZED_DIR = "sounds/normalized/soundboard" + # Sound directories by type + SOUND_DIRS = { + "SDB": "sounds/soundboard", + "SAY": "sounds/say", + "STR": "sounds/stream" + } + NORMALIZED_DIRS = { + "SDB": "sounds/normalized/soundboard", + "SAY": "sounds/normalized/say", + "STR": "sounds/normalized/stream" + } LOUDNORM_PARAMS = { "integrated": -16, @@ -62,9 +71,17 @@ class SoundNormalizerService: "error": f"Sound with ID {sound_id} not found", } - source_path = ( - Path(SoundNormalizerService.SOUNDS_DIR) / sound.filename - ) + # Get directories based on sound type + sound_dir = SoundNormalizerService.SOUND_DIRS.get(sound.type) + normalized_dir = SoundNormalizerService.NORMALIZED_DIRS.get(sound.type) + + if not sound_dir or not normalized_dir: + return { + "success": False, + "error": f"Unsupported sound type: {sound.type}", + } + + source_path = Path(sound_dir) / sound.filename if not source_path.exists(): return { "success": False, @@ -74,10 +91,7 @@ class SoundNormalizerService: # Always output as WAV regardless of input format filename_without_ext = Path(sound.filename).stem normalized_filename = f"{filename_without_ext}.wav" - normalized_path = ( - Path(SoundNormalizerService.NORMALIZED_DIR) - / normalized_filename - ) + normalized_path = Path(normalized_dir) / normalized_filename normalized_path.parent.mkdir(parents=True, exist_ok=True) diff --git a/app/services/stream_processing_service.py b/app/services/stream_processing_service.py index 747196f..fe912ec 100644 --- a/app/services/stream_processing_service.py +++ b/app/services/stream_processing_service.py @@ -453,6 +453,9 @@ class StreamProcessingService: # Add sound to main playlist cls._add_sound_to_main_playlist(sound) + + # Normalize the sound + cls._normalize_sound(sound) return sound, None @@ -541,6 +544,30 @@ class StreamProcessingService: except Exception as e: logger.error(f"Failed to add sound {sound.id} to main playlist: {e}") + @classmethod + def _normalize_sound(cls, sound: Sound) -> None: + """Normalize a stream sound using the sound normalizer service.""" + try: + from app.services.sound_normalizer_service import SoundNormalizerService + + logger.info(f"Starting normalization of stream sound {sound.id}: {sound.name}") + + # Normalize the sound (overwrite=True since it's a new sound) + result = SoundNormalizerService.normalize_sound( + sound.id, + overwrite=True, + two_pass=True + ) + + if result.get("success"): + logger.info(f"Successfully normalized stream sound {sound.id}") + else: + error_msg = result.get("error", "Unknown normalization error") + logger.warning(f"Failed to normalize stream sound {sound.id}: {error_msg}") + + except Exception as e: + logger.error(f"Error normalizing stream sound {sound.id}: {e}") + @classmethod def _calculate_file_hash(cls, file_path: str) -> str: """Calculate SHA256 hash of file."""