194 lines
9.6 KiB
Python
194 lines
9.6 KiB
Python
"""回归用例:台词必须配给「真的在说话的那个人」。
|
||
|
||
项目 9《自检-异步调度验证》现场缺陷(用户红线「配音接错人」):
|
||
镜头 02 的画面是「俊杰坐在服务台后,抬手示意紫小慧不用担心」,台词明显是俊杰说的,
|
||
但俊杰是脚本**导入之后**才补进项目的人物——导入时匹配不到说话人就把
|
||
`speaker_character_id` 留空,旧逻辑随即退回「出镜名单第一位」(紫小慧),
|
||
于是男主整段台词配成了女声,而且这条女声线还会被固化写回镜头,
|
||
用户后来在资产库里新配的男声一次都没被用上(事件 3328/3398 实测)。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
from sqlalchemy import select
|
||
|
||
from app.constants import AssetKind
|
||
from app.models import EventLog, Project
|
||
from app.services import audio, storage
|
||
|
||
SCRIPT = """# 镜头 1:稳住
|
||
画面: 俊杰坐在服务台后,微微抬手示意紫小慧不用担心
|
||
景别: 中景
|
||
运镜: 固定
|
||
时长: 2
|
||
台词: 别慌,数据都在机房云桌面里。
|
||
说话人: 俊杰
|
||
出镜角色: 紫小慧
|
||
场景: 服务大厅
|
||
|
||
# 镜头 2:崩了
|
||
画面: 紫小慧抱着一摞文件冲进服务台,俊杰在旁边让开路
|
||
景别: 中景
|
||
运镜: 跟镜
|
||
时长: 2
|
||
台词: 完了完了,三号服务器全红了!
|
||
说话人: 紫小慧
|
||
出镜角色: 紫小慧
|
||
场景: 服务大厅
|
||
"""
|
||
|
||
def _shots(db, pid):
|
||
project = db.get(Project, pid)
|
||
return sorted(project.shots or [], key=lambda row: (row.order_index, row.id))
|
||
|
||
|
||
def _cast_project(api, db):
|
||
"""照搬项目 9 的真实形状:导入时项目里只有女主角,俊杰是之后才补的人物。"""
|
||
pid = api.project(name="自检-配音接错人")
|
||
female = api.post(f"/api/projects/{pid}/characters",
|
||
json={"name": "紫小慧", "role": "主角", "gender": "female",
|
||
"appearance": "黑色长发,紫色工装外套",
|
||
"persona_prompt": "急性子的运维工程师",
|
||
"voice_profile": "明亮女声"})["result"]["id"]
|
||
female_voice = api.post(f"/api/projects/{pid}/voices",
|
||
json={"name": "紫小慧声音", "provider": "mock",
|
||
"provider_voice_id": "v-female", "gender": "female",
|
||
"is_default": True})["result"]["id"]
|
||
api.patch(f"/api/projects/{pid}/characters/{female}",
|
||
json={"default_voice_id": female_voice})
|
||
api.post(f"/api/projects/{pid}/scenes",
|
||
json={"name": "服务大厅", "description": "公司一层服务台", "prompt": "服务大厅"})
|
||
api.post(f"/api/projects/{pid}/import",
|
||
data={"script_text": SCRIPT, "default_duration": 2})
|
||
# 导入之后用户才补的男主 + 他的男声(脚本里的说话人已经永久丢了)
|
||
male = api.post(f"/api/projects/{pid}/characters",
|
||
json={"name": "俊杰", "role": "主角", "gender": "male",
|
||
"appearance": "短发,深蓝西装", "persona_prompt": "稳重的IT工程师",
|
||
"voice_profile": "低沉男声"})["result"]["id"]
|
||
male_voice = api.post(f"/api/projects/{pid}/voices",
|
||
json={"name": "俊杰声音", "provider": "mock",
|
||
"provider_voice_id": "v-male", "gender": "male"})["result"]["id"]
|
||
api.patch(f"/api/projects/{pid}/characters/{male}", json={"default_voice_id": male_voice})
|
||
db.expire_all()
|
||
rows = _shots(db, pid)
|
||
# 脚本里写了「说话人: 俊杰」,可当时项目里还没有这个人——导入直接留空,
|
||
# 原始名字就此丢失,之后无论谁来配音都只能瞎猜(这正是接错人的源头)。
|
||
assert rows[0].speaker_character_id is None, rows[0].speaker_character_id
|
||
assert rows[1].speaker_character_id == female
|
||
assert all([row.id for row in (row.characters or [])] == [female] for row in rows)
|
||
return {"project_id": pid, "female_id": female, "male_id": male,
|
||
"female_voice": female_voice, "male_voice": male_voice}
|
||
|
||
|
||
def _actions(db, pid, shot_id=None):
|
||
stmt = select(EventLog).where(EventLog.project_id == pid)
|
||
rows = db.scalars(stmt).all()
|
||
if shot_id is not None:
|
||
rows = [row for row in rows if row.shot_id == shot_id]
|
||
return [row for row in rows]
|
||
|
||
|
||
def test_speaker_is_the_character_named_first_in_the_shot(api, db):
|
||
"""台词挂在谁身上:显式说话人 → 画面描述里最早点名的人 → 出镜名单第一位。"""
|
||
ctx = _cast_project(api, db)
|
||
male_shot, female_shot = _shots(db, ctx["project_id"])
|
||
|
||
assert audio.speaker_character(db, male_shot).name == "俊杰", \
|
||
"画面第一句就是俊杰的动作,台词不该算到紫小慧头上"
|
||
assert audio.speaker_gender(db, male_shot) == "male"
|
||
assert audio.speaker_character(db, female_shot).name == "紫小慧"
|
||
assert audio.speaker_gender(db, female_shot) == "female"
|
||
|
||
|
||
def test_speaker_follows_the_earlier_name_when_the_order_swaps(api, db):
|
||
"""同一段话里两个人都被点名时,认「先被点名」的那个,不能永远认女一号。"""
|
||
ctx = _cast_project(api, db)
|
||
shot = _shots(db, ctx["project_id"])[0]
|
||
shot.description = "紫小慧冲进来喊停,俊杰却已经把机房门关上了"
|
||
db.commit()
|
||
assert audio.speaker_character(db, shot).name == "紫小慧"
|
||
shot.description = "俊杰先把机房门关上,紫小慧被拦在外面"
|
||
db.commit()
|
||
assert audio.speaker_character(db, shot).name == "俊杰"
|
||
# 谁都没被点名时退回出镜名单第一位,保持旧口径不出新问题
|
||
shot.description = "服务大厅的灯一盏盏熄灭"
|
||
db.commit()
|
||
assert audio.speaker_character(db, shot).id == ctx["female_id"]
|
||
|
||
|
||
def test_male_dialogue_no_longer_gets_the_female_lead_voice(api, db):
|
||
"""男主的台词必须匹配到男声线,且不能被项目默认(女声)兜底走。"""
|
||
ctx = _cast_project(api, db)
|
||
project = db.get(Project, ctx["project_id"])
|
||
male_shot, female_shot = _shots(db, ctx["project_id"])
|
||
|
||
voice, info = audio.resolve_voice(db, project, male_shot)
|
||
assert voice.name == "俊杰声音", info
|
||
assert info["gender"] == "male" and info["gender_mismatch"] is False, info
|
||
assert "俊杰声音" in info["reason"], info["reason"]
|
||
|
||
other, other_info = audio.resolve_voice(db, project, female_shot)
|
||
assert other.name == "紫小慧声音", other_info
|
||
assert other_info["gender_mismatch"] is False
|
||
|
||
|
||
def test_generate_voiceover_records_the_inferred_speaker(api, db):
|
||
"""推断出来的说话人要写回镜头、挂进出镜名单,并在事件里说清楚——用户能一眼改。"""
|
||
ctx = _cast_project(api, db)
|
||
project = db.get(Project, ctx["project_id"])
|
||
shot = _shots(db, ctx["project_id"])[0]
|
||
|
||
result = audio.generate_voiceover(db, project, shot)
|
||
|
||
assert result["voice"] == "俊杰声音", result
|
||
db.expire_all()
|
||
shot = _shots(db, ctx["project_id"])[0]
|
||
assert shot.speaker_character_id == ctx["male_id"], "配音完却看不出是谁在说话"
|
||
assert ctx["male_id"] in {row.id for row in (shot.characters or [])}, \
|
||
"说话人必须进本镜出镜名单,否则定妆图/口型参照都找不到他"
|
||
assert storage.first_asset(db, "shot", shot.id, AssetKind.VOICEOVER) is not None
|
||
|
||
inferred = [row for row in _actions(db, ctx["project_id"], shot.id)
|
||
if row.action == "voiceover.speaker_inferred"]
|
||
assert len(inferred) == 1, [row.action for row in _actions(db, ctx["project_id"], shot.id)]
|
||
assert inferred[0].data_json["speaker"] == "俊杰", inferred[0].data_json
|
||
assert inferred[0].data_json["voice"] == "俊杰声音"
|
||
assert "俊杰声音" in inferred[0].message
|
||
# 性别不再错配:事件流里不该再出现「说话人是男声,声线却是女声」这种告警
|
||
assert all("却是女声" not in row.message for row in
|
||
_actions(db, ctx["project_id"], shot.id)
|
||
if row.action == "voiceover.voice_fallback")
|
||
|
||
|
||
def test_inference_is_idempotent_and_leaves_explicit_speakers_alone(api, db):
|
||
"""用户/脚本已经写明说话人时绝不猜,也不再产生推断事件。"""
|
||
ctx = _cast_project(api, db)
|
||
project = db.get(Project, ctx["project_id"])
|
||
shot = _shots(db, ctx["project_id"])[0]
|
||
shot.speaker_character_id = ctx["female_id"] # 编导明确说:这句是紫小慧说的
|
||
db.commit()
|
||
|
||
assert audio.inferred_speaker(db, shot) is None
|
||
result = audio.generate_voiceover(db, project, shot)
|
||
assert result["voice"] == "紫小慧声音", result
|
||
db.expire_all()
|
||
shot = _shots(db, ctx["project_id"])[0]
|
||
assert shot.speaker_character_id == ctx["female_id"], "显式说话人被推断覆盖了"
|
||
assert "voiceover.speaker_inferred" not in [row.action for row
|
||
in _actions(db, ctx["project_id"], shot.id)]
|
||
|
||
|
||
def test_no_inference_event_when_the_cast_first_already_speaks(api, db):
|
||
"""结论和旧口径一致时不该刷事件,免得把用户淹死在噪音里。"""
|
||
ctx = _cast_project(api, db)
|
||
project = db.get(Project, ctx["project_id"])
|
||
shot = _shots(db, ctx["project_id"])[1] # 紫小慧:既是出镜第一位也是说话人
|
||
shot.speaker_character_id = None # 历史数据里说话人丢了
|
||
db.commit()
|
||
assert audio.inferred_speaker(db, shot) is None
|
||
audio.generate_voiceover(db, project, shot)
|
||
db.expire_all()
|
||
shot = _shots(db, ctx["project_id"])[1]
|
||
assert shot.speaker_character_id is None
|
||
assert "voiceover.speaker_inferred" not in [row.action for row
|
||
in _actions(db, ctx["project_id"], shot.id)]
|