feat(stream): integrate sound normalization into stream processing service

This commit is contained in:
JSC
2025-07-07 15:29:50 +02:00
parent fe628b99d4
commit c44b055f83
2 changed files with 50 additions and 9 deletions

View File

@@ -27,8 +27,17 @@ class SoundNormalizerService:
".aac", ".aac",
".opus", ".opus",
} }
SOUNDS_DIR = "sounds/soundboard" # Sound directories by type
NORMALIZED_DIR = "sounds/normalized/soundboard" 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 = { LOUDNORM_PARAMS = {
"integrated": -16, "integrated": -16,
@@ -62,9 +71,17 @@ class SoundNormalizerService:
"error": f"Sound with ID {sound_id} not found", "error": f"Sound with ID {sound_id} not found",
} }
source_path = ( # Get directories based on sound type
Path(SoundNormalizerService.SOUNDS_DIR) / sound.filename 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(): if not source_path.exists():
return { return {
"success": False, "success": False,
@@ -74,10 +91,7 @@ class SoundNormalizerService:
# Always output as WAV regardless of input format # Always output as WAV regardless of input format
filename_without_ext = Path(sound.filename).stem filename_without_ext = Path(sound.filename).stem
normalized_filename = f"{filename_without_ext}.wav" normalized_filename = f"{filename_without_ext}.wav"
normalized_path = ( normalized_path = Path(normalized_dir) / normalized_filename
Path(SoundNormalizerService.NORMALIZED_DIR)
/ normalized_filename
)
normalized_path.parent.mkdir(parents=True, exist_ok=True) normalized_path.parent.mkdir(parents=True, exist_ok=True)

View File

@@ -454,6 +454,9 @@ class StreamProcessingService:
# Add sound to main playlist # Add sound to main playlist
cls._add_sound_to_main_playlist(sound) cls._add_sound_to_main_playlist(sound)
# Normalize the sound
cls._normalize_sound(sound)
return sound, None return sound, None
except Exception as e: except Exception as e:
@@ -541,6 +544,30 @@ class StreamProcessingService:
except Exception as e: except Exception as e:
logger.error(f"Failed to add sound {sound.id} to main playlist: {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 @classmethod
def _calculate_file_hash(cls, file_path: str) -> str: def _calculate_file_hash(cls, file_path: str) -> str:
"""Calculate SHA256 hash of file.""" """Calculate SHA256 hash of file."""