Add audio sources as first-class entities, add mapped CSS type, simplify target editor for mapped sources
- Audio sources moved to separate tab with dedicated CRUD API, store, and editor modal - New "mapped" color strip source type: assigns different CSS sources to distinct LED sub-ranges (zones) - Mapped stream runtime with per-zone sub-streams, auto-sizing, hot-update support - Target editor auto-collapses segments UI when mapped CSS is selected - Delete protection for CSS sources referenced by mapped zones - Compact header/footer layout Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
113
server/src/wled_controller/storage/audio_source.py
Normal file
113
server/src/wled_controller/storage/audio_source.py
Normal file
@@ -0,0 +1,113 @@
|
||||
"""Audio source data model with inheritance-based source types.
|
||||
|
||||
An AudioSource represents a reusable audio input configuration:
|
||||
MultichannelAudioSource — wraps a physical audio device (index + loopback flag)
|
||||
MonoAudioSource — extracts a single channel from a multichannel source
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
|
||||
@dataclass
|
||||
class AudioSource:
|
||||
"""Base class for audio source configurations."""
|
||||
|
||||
id: str
|
||||
name: str
|
||||
source_type: str # "multichannel" | "mono"
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
description: Optional[str] = None
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
"""Convert source to dictionary. Subclasses extend this."""
|
||||
return {
|
||||
"id": self.id,
|
||||
"name": self.name,
|
||||
"source_type": self.source_type,
|
||||
"created_at": self.created_at.isoformat(),
|
||||
"updated_at": self.updated_at.isoformat(),
|
||||
"description": self.description,
|
||||
# Subclass fields default to None for forward compat
|
||||
"device_index": None,
|
||||
"is_loopback": None,
|
||||
"audio_source_id": None,
|
||||
"channel": None,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def from_dict(data: dict) -> "AudioSource":
|
||||
"""Factory: dispatch to the correct subclass based on source_type."""
|
||||
source_type: str = data.get("source_type", "multichannel") or "multichannel"
|
||||
sid: str = data["id"]
|
||||
name: str = data["name"]
|
||||
description: str | None = data.get("description")
|
||||
|
||||
raw_created = data.get("created_at")
|
||||
created_at: datetime = (
|
||||
datetime.fromisoformat(raw_created)
|
||||
if isinstance(raw_created, str)
|
||||
else raw_created if isinstance(raw_created, datetime)
|
||||
else datetime.utcnow()
|
||||
)
|
||||
raw_updated = data.get("updated_at")
|
||||
updated_at: datetime = (
|
||||
datetime.fromisoformat(raw_updated)
|
||||
if isinstance(raw_updated, str)
|
||||
else raw_updated if isinstance(raw_updated, datetime)
|
||||
else datetime.utcnow()
|
||||
)
|
||||
|
||||
if source_type == "mono":
|
||||
return MonoAudioSource(
|
||||
id=sid, name=name, source_type="mono",
|
||||
created_at=created_at, updated_at=updated_at, description=description,
|
||||
audio_source_id=data.get("audio_source_id") or "",
|
||||
channel=data.get("channel") or "mono",
|
||||
)
|
||||
|
||||
# Default: multichannel
|
||||
return MultichannelAudioSource(
|
||||
id=sid, name=name, source_type="multichannel",
|
||||
created_at=created_at, updated_at=updated_at, description=description,
|
||||
device_index=int(data.get("device_index", -1)),
|
||||
is_loopback=bool(data.get("is_loopback", True)),
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class MultichannelAudioSource(AudioSource):
|
||||
"""Audio source wrapping a physical audio device.
|
||||
|
||||
Captures all channels from the device. For WASAPI loopback devices
|
||||
(system audio output), set is_loopback=True.
|
||||
"""
|
||||
|
||||
device_index: int = -1 # -1 = default device
|
||||
is_loopback: bool = True # True = WASAPI loopback (system audio)
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
d = super().to_dict()
|
||||
d["device_index"] = self.device_index
|
||||
d["is_loopback"] = self.is_loopback
|
||||
return d
|
||||
|
||||
|
||||
@dataclass
|
||||
class MonoAudioSource(AudioSource):
|
||||
"""Audio source that extracts a single channel from a multichannel source.
|
||||
|
||||
References a MultichannelAudioSource and selects which channel to use:
|
||||
mono (L+R mix), left, or right.
|
||||
"""
|
||||
|
||||
audio_source_id: str = "" # references a MultichannelAudioSource
|
||||
channel: str = "mono" # mono | left | right
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
d = super().to_dict()
|
||||
d["audio_source_id"] = self.audio_source_id
|
||||
d["channel"] = self.channel
|
||||
return d
|
||||
Reference in New Issue
Block a user