89 lines
3.2 KiB
Python
89 lines
3.2 KiB
Python
"""V2作者音频与采样选择合同;旧毫秒合同继续独立解释。"""
|
||
|
||
from typing import Literal
|
||
from pydantic import Field, model_validator
|
||
from app.domain.timeline import Strict, Audio
|
||
|
||
ID = r"^[0-9a-f]{32}$"
|
||
SAMPLE_RATE = 48000
|
||
MAX_SAMPLES = 360000 * 2000
|
||
|
||
|
||
class AudioSelectionV2(Strict):
|
||
asset_id: str = Field(min_length=1, max_length=64)
|
||
source_start_sample: int = Field(default=0, ge=0, le=MAX_SAMPLES)
|
||
source_end_sample: int = Field(gt=0, le=MAX_SAMPLES)
|
||
offset_sample: int = Field(default=0, ge=0, le=MAX_SAMPLES)
|
||
sample_rate: Literal[48000] = 48000
|
||
short_policy: Literal["silence"] = "silence"
|
||
|
||
@model_validator(mode="after")
|
||
def interval(self):
|
||
if self.source_end_sample <= self.source_start_sample:
|
||
raise ValueError("音频选用样本区间无效")
|
||
return self
|
||
|
||
|
||
class AudioPosition(Strict):
|
||
anchor: Literal["shot", "scene", "absolute"] = "shot"
|
||
shot_id: str | None = Field(default=None, pattern=ID)
|
||
scene_id: str | None = Field(default=None, pattern=ID)
|
||
offset_sample: int = Field(default=0, ge=0, le=MAX_SAMPLES)
|
||
source_start_sample: int = Field(default=0, ge=0, le=MAX_SAMPLES)
|
||
source_end_sample: int | None = Field(default=None, gt=0, le=MAX_SAMPLES)
|
||
gain_db: float = Field(default=0.0, ge=-60, le=12, allow_inf_nan=False)
|
||
fade_in_samples: int = Field(default=0, ge=0, le=MAX_SAMPLES)
|
||
fade_out_samples: int = Field(default=0, ge=0, le=MAX_SAMPLES)
|
||
|
||
@model_validator(mode="after")
|
||
def valid_position(self):
|
||
if (
|
||
self.anchor == "shot"
|
||
and (not self.shot_id or self.scene_id)
|
||
or self.anchor == "scene"
|
||
and (not self.scene_id or self.shot_id)
|
||
or self.anchor == "absolute"
|
||
and (self.shot_id or self.scene_id)
|
||
):
|
||
raise ValueError("音频锚点字段不匹配")
|
||
if (
|
||
self.source_end_sample is not None
|
||
and self.source_end_sample <= self.source_start_sample
|
||
):
|
||
raise ValueError("音频选用样本区间无效")
|
||
return self
|
||
|
||
|
||
class AudioSegmentV2(AudioPosition):
|
||
id: str = Field(pattern=ID)
|
||
text: str = Field(default="", max_length=2000)
|
||
voice: str | None = None
|
||
speed: float | None = Field(default=None, ge=0.5, le=2, allow_inf_nan=False)
|
||
|
||
|
||
class AudioLayerV2(AudioPosition):
|
||
id: str = Field(pattern=ID)
|
||
role: Literal["bgm", "sfx"] = "bgm"
|
||
asset_id: str = Field(min_length=1, max_length=64)
|
||
|
||
|
||
class EditorAudioV2(Strict):
|
||
mode: Literal["silent", "manual_track", "generated_narration"] = "silent"
|
||
voice: str = ""
|
||
speed: float = Field(default=1.0, ge=0.5, le=2, allow_inf_nan=False)
|
||
track: AudioSelectionV2 | Audio | None = None
|
||
segments: list[AudioSegmentV2] = Field(default_factory=list, max_length=1000)
|
||
layers: list[AudioLayerV2] = Field(default_factory=list, max_length=32)
|
||
ducking: bool = True
|
||
|
||
@model_validator(mode="after")
|
||
def unique_layers(self):
|
||
ids = [s.id for s in self.segments] + [s.id for s in self.layers]
|
||
if len(ids) != len(set(ids)):
|
||
raise ValueError("音频段与图层ID不能重复")
|
||
return self
|
||
|
||
|
||
class EditorAudioV3(EditorAudioV2):
|
||
mix_mode: Literal["composed", "manual_mix"] = "composed"
|