2026-11-14 04:25:51 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
# models/sound_manager.py
|
2026-11-16 20:38:05 +00:00
|
|
|
import os
|
|
|
|
|
import sys
|
2026-11-14 04:25:51 +00:00
|
|
|
from pathlib import Path
|
2026-11-16 20:38:05 +00:00
|
|
|
|
2026-11-14 04:25:51 +00:00
|
|
|
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
|
|
|
|
|
)
|
|
|
|
|
|
2026-11-16 20:38:05 +00:00
|
|
|
if sys.platform.startswith("linux"):
|
|
|
|
|
# Verhindert Audio-Stottern / Knacksen in VM- & Container-Umgebungen (insb. ChromeOS Crostini)
|
|
|
|
|
os.environ.setdefault("PULSE_LATENCY_MSEC", "80")
|
|
|
|
|
|
2026-11-14 04:25:51 +00:00
|
|
|
try:
|
|
|
|
|
import pygame
|
|
|
|
|
PYGAME_AVAILABLE = True
|
|
|
|
|
except ImportError:
|
|
|
|
|
PYGAME_AVAILABLE = False
|
|
|
|
|
|
|
|
|
|
|
2026-11-16 20:38:05 +00:00
|
|
|
def _is_crostini_env() -> bool:
|
|
|
|
|
if not sys.platform.startswith("linux"):
|
|
|
|
|
return False
|
|
|
|
|
return (
|
|
|
|
|
Path("/dev/.cros_milestone").exists()
|
|
|
|
|
or "sommelier" in os.environ.get("XDG_RUNTIME_DIR", "").lower()
|
|
|
|
|
or Path("/opt/google/cros-containers").exists()
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-11-14 04:25:51 +00:00
|
|
|
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:
|
2026-11-16 20:38:05 +00:00
|
|
|
self._init_mixer()
|
|
|
|
|
|
|
|
|
|
def _init_mixer(self):
|
|
|
|
|
is_linux = sys.platform.startswith("linux")
|
|
|
|
|
is_cros = _is_crostini_env()
|
|
|
|
|
|
|
|
|
|
# ChromeOS CRAS arbeitet nativ mit 48 kHz und benötigt über die VM-Grenze einen großen Puffer
|
|
|
|
|
freq = 48000 if is_linux else 44100
|
|
|
|
|
buf_size = 8192 if is_cros else (4096 if is_linux else 1024)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
if pygame.mixer.get_init():
|
|
|
|
|
pygame.mixer.quit()
|
|
|
|
|
pygame.mixer.pre_init(frequency=freq, size=-16, channels=2, buffer=buf_size)
|
|
|
|
|
pygame.mixer.init(frequency=freq, size=-16, channels=2, buffer=buf_size)
|
|
|
|
|
self._ready = True
|
|
|
|
|
except Exception:
|
2026-11-14 04:25:51 +00:00
|
|
|
try:
|
2026-11-16 20:38:05 +00:00
|
|
|
pygame.mixer.init()
|
2026-11-14 04:25:51 +00:00
|
|
|
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()
|