103 lines
3.4 KiB
Python
103 lines
3.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
# models/sound_manager.py
|
|
from pathlib import Path
|
|
from models.database import Database
|
|
from resources.code_res.game_constants import (
|
|
DEFAULT_VOL_AMBIANCE, DEFAULT_VOL_MENU,
|
|
INGAME_MUSIC_FILE, MENU_MUSIC_FILE, SOUND_MAP
|
|
)
|
|
|
|
try:
|
|
import pygame
|
|
PYGAME_AVAILABLE = True
|
|
except ImportError:
|
|
PYGAME_AVAILABLE = False
|
|
|
|
|
|
class SoundManager:
|
|
def __init__(self, sounds_dir: Path):
|
|
self.ambiance_dir = sounds_dir / "ambiance"
|
|
self.menu_dir = sounds_dir / "menu_effects"
|
|
|
|
self.ambiance_volume = self._clamp(float(Database.load_setting("vol_ambiance", str(DEFAULT_VOL_AMBIANCE))))
|
|
self.menu_volume = self._clamp(float(Database.load_setting("vol_menu", str(DEFAULT_VOL_MENU))))
|
|
|
|
self._sfx_cache = {}
|
|
self._current_music_file = None
|
|
self._ready = False
|
|
|
|
if PYGAME_AVAILABLE:
|
|
try:
|
|
pygame.mixer.init(frequency=44100, size=-16, channels=2, buffer=512)
|
|
self._ready = True
|
|
except Exception:
|
|
self._ready = False
|
|
|
|
@staticmethod
|
|
def _clamp(val: float) -> float:
|
|
return max(0.0, min(1.0, val))
|
|
|
|
def set_ambiance_volume(self, volume: float, persist: bool = True):
|
|
self.ambiance_volume = self._clamp(volume)
|
|
if self._ready:
|
|
try:
|
|
pygame.mixer.music.set_volume(self.ambiance_volume)
|
|
except Exception:
|
|
pass
|
|
if persist:
|
|
Database.save_setting("vol_ambiance", f"{self.ambiance_volume:.3f}")
|
|
|
|
def set_menu_volume(self, volume: float, persist: bool = True):
|
|
self.menu_volume = self._clamp(volume)
|
|
if persist:
|
|
Database.save_setting("vol_menu", f"{self.menu_volume:.3f}")
|
|
|
|
def play_menu_music(self):
|
|
self._switch_music(MENU_MUSIC_FILE)
|
|
|
|
def play_ingame_music(self):
|
|
self._switch_music(INGAME_MUSIC_FILE)
|
|
|
|
def _switch_music(self, filename: str):
|
|
if not self._ready:
|
|
return
|
|
if self._current_music_file == filename and pygame.mixer.music.get_busy():
|
|
pygame.mixer.music.set_volume(self.ambiance_volume)
|
|
return
|
|
|
|
track_path = self.ambiance_dir / filename
|
|
if not track_path.is_file():
|
|
return
|
|
|
|
try:
|
|
if pygame.mixer.music.get_busy():
|
|
pygame.mixer.music.fadeout(250)
|
|
pygame.mixer.music.load(str(track_path))
|
|
pygame.mixer.music.set_volume(self.ambiance_volume)
|
|
pygame.mixer.music.play(-1, fade_ms=400)
|
|
self._current_music_file = filename
|
|
except Exception:
|
|
pass
|
|
|
|
def play(self, event_name: str):
|
|
if not self._ready or event_name not in SOUND_MAP:
|
|
return
|
|
|
|
category, filename = SOUND_MAP[event_name]
|
|
vol = self.ambiance_volume if category == "ambiance" else self.menu_volume
|
|
if vol <= 0.001:
|
|
return
|
|
|
|
if event_name not in self._sfx_cache:
|
|
base_folder = self.ambiance_dir if category == "ambiance" else self.menu_dir
|
|
fpath = base_folder / filename
|
|
if fpath.is_file():
|
|
try:
|
|
self._sfx_cache[event_name] = pygame.mixer.Sound(str(fpath))
|
|
except Exception:
|
|
return
|
|
|
|
snd = self._sfx_cache.get(event_name)
|
|
if snd:
|
|
snd.set_volume(vol)
|
|
snd.play() |