Prześlij pliki do "audio"
This commit is contained in:
1 parent
4feaad6760
commit
93a68999dc
1 file changed
+119
@@ -0,0 +1,119 @@
|
|||||||
|
# audio/source_manager.py
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
|
||||||
|
from audio.sources import (
|
||||||
|
RadioSource,
|
||||||
|
BluetoothSource,
|
||||||
|
AnalogSource,
|
||||||
|
SpdifSource,
|
||||||
|
CdSource,
|
||||||
|
UsbSource,
|
||||||
|
)
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class SourceManager:
|
||||||
|
"""
|
||||||
|
Zarządza wszystkimi dostępnymi źródłami audio.
|
||||||
|
|
||||||
|
- `available` – dict name → AudioSource
|
||||||
|
- `current` – aktualnie wybrany AudioSource
|
||||||
|
- Metody: `switch(name)`, `save_state()`, `load_state()`
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, audio_engine, config: dict):
|
||||||
|
self.audio = audio_engine
|
||||||
|
self.cfg_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "config", "config.json"))
|
||||||
|
self.available = {}
|
||||||
|
self.current = None
|
||||||
|
self._build_sources(config)
|
||||||
|
self.load_state()
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------
|
||||||
|
# Tworzenie instancji źródeł (na podstawie config)
|
||||||
|
# ------------------------------------------------------------------
|
||||||
|
def _build_sources(self, config):
|
||||||
|
# radio – przykładowa lista stacji (z stations.json zostanie wczytana później)
|
||||||
|
self.available["radio"] = RadioSource(
|
||||||
|
station_url="http://example.com/stream.mp3", station_name="radio"
|
||||||
|
)
|
||||||
|
# bluetooth
|
||||||
|
self.available["bluetooth"] = BluetoothSource()
|
||||||
|
# analog – w przykładowym config nie ma szczegółów, więc używamy dummy device
|
||||||
|
self.available["phono"] = AnalogSource(device="hw:0,0", name="phono")
|
||||||
|
self.available["line1"] = AnalogSource(device="hw:0,1", name="line1")
|
||||||
|
self.available["line2"] = AnalogSource(device="hw:0,2", name="line2")
|
||||||
|
# spdif
|
||||||
|
self.available["spdif"] = SpdifSource(device="hw:0,3")
|
||||||
|
# cd / usb (szkielet)
|
||||||
|
self.available["cd"] = CdSource()
|
||||||
|
self.available["usb"] = UsbSource()
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------
|
||||||
|
# Publiczne API
|
||||||
|
# ------------------------------------------------------------------
|
||||||
|
@property
|
||||||
|
def available_names(self):
|
||||||
|
return list(self.available.keys())
|
||||||
|
|
||||||
|
@property
|
||||||
|
def current_name(self):
|
||||||
|
return self.current.name if self.current else None
|
||||||
|
|
||||||
|
def switch(self, name: str):
|
||||||
|
"""Przełącza aktywne źródło audio."""
|
||||||
|
if name not in self.available:
|
||||||
|
raise ValueError(f"Nieznane źródło: {name}")
|
||||||
|
|
||||||
|
src = self.available[name]
|
||||||
|
src.activate()
|
||||||
|
src_elm = src.build_source()
|
||||||
|
self.audio.set_source(src_elm)
|
||||||
|
|
||||||
|
# Dezaktywujemy poprzednie (jeśli istnieje)
|
||||||
|
if self.current and self.current != src:
|
||||||
|
self.current.deactivate()
|
||||||
|
|
||||||
|
self.current = src
|
||||||
|
log.info("Source switched → %s", name)
|
||||||
|
self.save_state()
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------
|
||||||
|
# Zapis / odczyt stanu (vol, source, ostatnia stacja)
|
||||||
|
# ------------------------------------------------------------------
|
||||||
|
def load_state(self):
|
||||||
|
"""Wczytuje ostatni stan z config.json (jeśli istnieje)."""
|
||||||
|
try:
|
||||||
|
with open(self.cfg_path, "r", encoding="utf-8") as f:
|
||||||
|
data = json.load(f)
|
||||||
|
except (FileNotFoundError, json.JSONDecodeError):
|
||||||
|
log.warning("Nie udało się wczytać config.json – używam domyślnych wartości")
|
||||||
|
data = {}
|
||||||
|
|
||||||
|
# Odtwarzamy ostatnie źródło (lub radio jako fallback)
|
||||||
|
last_src = data.get("last_source", "radio")
|
||||||
|
if last_src in self.available:
|
||||||
|
self.switch(last_src)
|
||||||
|
|
||||||
|
# Ustawiamy głośność (domyślnie 75)
|
||||||
|
vol = data.get("volume", 75)
|
||||||
|
self.audio.set_volume(vol)
|
||||||
|
|
||||||
|
def save_state(self):
|
||||||
|
"""Zapisuje aktualny stan do pliku config.json."""
|
||||||
|
state = {
|
||||||
|
"last_source": self.current_name,
|
||||||
|
"volume": self.audio.volume,
|
||||||
|
"balance": self.audio.balance,
|
||||||
|
"loudness": self.audio.loudness,
|
||||||
|
"mono": self.audio.mono,
|
||||||
|
}
|
||||||
|
try:
|
||||||
|
with open(self.cfg_path, "w", encoding="utf-8") as f:
|
||||||
|
json.dump(state, f, indent=2)
|
||||||
|
log.debug("Zapisano stan do config.json")
|
||||||
|
except Exception as exc:
|
||||||
|
log.error("Błąd przy zapisie config.json: %s", exc)
|
||||||
Reference in new issue
Block a user