Add songforge as independent repo with path layout.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1 @@
|
||||
from .main import main
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""铁律组装与校验:tags / 前奏注入 / 密度闸门。"""
|
||||
import re
|
||||
|
||||
from ..core.errors import RuleViolation
|
||||
from ..lyrics import meter
|
||||
from ..rules import ironlaw
|
||||
from .common import chill_intro_bars, resolve_profile
|
||||
|
||||
|
||||
def split_sections(lyrics_raw):
|
||||
"""把裸歌词切成 [(tag, text)],识别不出标记时整段当 [verse]。"""
|
||||
secs = []
|
||||
cur_tag, buf = None, []
|
||||
for ln in lyrics_raw.split("\n"):
|
||||
s = ln.strip()
|
||||
if s.startswith("[") and s.endswith("]"):
|
||||
if cur_tag and buf:
|
||||
secs.append((cur_tag, "\n".join(buf).strip()))
|
||||
cur_tag, buf = s, []
|
||||
elif s:
|
||||
buf.append(s)
|
||||
if cur_tag and buf:
|
||||
secs.append((cur_tag, "\n".join(buf).strip()))
|
||||
if not secs:
|
||||
secs = [("[verse]", lyrics_raw.strip())]
|
||||
return secs
|
||||
|
||||
|
||||
def vocal_sections(lyrics_raw):
|
||||
"""只取真正有人唱的段:丢掉所有只挂 [inst] 的纯器乐占位。
|
||||
|
||||
chill 重排时器乐占位要全部重新生成,先从原始歌词剥掉,
|
||||
否则同一段器乐会被写两遍,第二遍反而把人声提前拉回来。
|
||||
"""
|
||||
out = []
|
||||
for tag, text in split_sections(lyrics_raw):
|
||||
t = text.strip()
|
||||
if not t or re.fullmatch(r"\[?inst\]?|\[Instrumental[^\]]*\]", t, re.I):
|
||||
continue
|
||||
out.append((tag, text))
|
||||
return out or [("[verse]", "")]
|
||||
|
||||
|
||||
def duration_allowed(cfg, dur):
|
||||
"""时长闸门:主区间 + 显式白名单区间。
|
||||
|
||||
主区间 150~200s 来自 35 首已验证实歌,不能为短时长题材放宽。
|
||||
短视频/口水歌这类确实需要 100~130s,但没有实歌背书,
|
||||
所以走 config.song.duration_extra_ranges 显式列出。
|
||||
"""
|
||||
lo, hi = cfg["song"]["duration_range"]
|
||||
if lo <= dur <= hi:
|
||||
return True
|
||||
for a, b in cfg["song"].get("duration_extra_ranges") or []:
|
||||
if a <= dur <= b:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def _extra_hint(cfg, dur):
|
||||
"""报错时列出当前生效的额外区间,省一次查配置。"""
|
||||
ex = cfg["song"].get("duration_extra_ranges") or []
|
||||
if not ex:
|
||||
return ""
|
||||
return "(额外放行区间:%s)" % ", ".join("%d~%ds" % (a, b) for a, b in ex)
|
||||
|
||||
|
||||
def assemble(args, cfg, st, lyrics_raw, title):
|
||||
"""套用铁律:生成 tags、补器乐前奏、清洗中文标记。"""
|
||||
chill = bool(getattr(args, "chill_vocal", False))
|
||||
inst = bool(getattr(args, "instrumental", False))
|
||||
lean = bool(getattr(args, "lean_tags", False))
|
||||
tags = ironlaw.build_tags(
|
||||
style=st.get("style", ""), instruments=st.get("instruments", ""),
|
||||
mood=st.get("mood", ""), extra=st.get("extra", ""),
|
||||
chill_vocal=chill, instrumental=inst, lean=lean)
|
||||
ibars = chill_intro_bars(cfg, chill)
|
||||
# 纯器乐:没有可唱正文,歌词体直接由结构标记铺出来,
|
||||
# 密度闸门对纯器乐不适用(见 do_validate)。
|
||||
if inst and not lyrics_raw.lstrip().lower().startswith("[intro]"):
|
||||
lyrics = ironlaw.build_inst_lyrics(
|
||||
st.get("intro_detail") or st.get("instruments"),
|
||||
bars=ibars)
|
||||
return tags, lyrics
|
||||
if chill:
|
||||
# chill 走独立结构体:器乐段整体排在人声段之前。
|
||||
# 实测这只降低人声密度,并不推迟起点(chill 3 第一句仍在 0 秒),
|
||||
# 标签侧的时间/时机约束同样无效。详见 ironlaw 顶部实测结论。
|
||||
cfg_lyrics = meter.pick(cfg["lyrics"], resolve_profile(args))
|
||||
cap = meter.capacity(st["duration"], st["bpm"], cfg_lyrics)
|
||||
lyrics = ironlaw.build_chill_lyrics(
|
||||
vocal_sections(lyrics_raw),
|
||||
st.get("intro_detail") or st.get("instruments"),
|
||||
bars=ibars, total_bars=cap["total_bars"])
|
||||
return tags, lyrics
|
||||
lyrics = lyrics_raw
|
||||
if not lyrics.lstrip().lower().startswith("[intro]"):
|
||||
lyrics = ironlaw.build_lyrics(
|
||||
split_sections(lyrics_raw),
|
||||
st.get("intro_detail") or st.get("instruments"),
|
||||
bars=ibars)
|
||||
else:
|
||||
# 已有 [intro] 头的歌词文件:强制把前奏小节数对齐到标签写的值。
|
||||
# 标签写 16 小节而歌词头写 8 小节时,check_rules 会判两处不一致
|
||||
# ——这是故意留的闸门,不是 bug。
|
||||
lyrics = ironlaw.force_intro_bars(lyrics, ibars)
|
||||
# 前奏括号里的配器也要过 rhythm 过滤(2026-10-05 补)。
|
||||
# 之前这条分支只改小节数,作者手写的 [Instrumental: ...] 直接
|
||||
# 原样送进模型。ckfunk 批次的 8 首歌词前奏括号里就带着
|
||||
# "half time feel" 这类非节奏组描述,而作者也可能写 pad、
|
||||
# riser、sweep——intro_rhythm_only() 存在的全部理由就是把这些
|
||||
# 空场信号从前奏里剔掉,只留节奏组。手写歌词不等于免检。
|
||||
lyrics = ironlaw.filter_intro_bracket(lyrics)
|
||||
lyrics = "\n".join(ironlaw.strip_cn_instrumental(lyrics.splitlines())) + "\n"
|
||||
return tags, lyrics
|
||||
|
||||
|
||||
def do_validate(lyrics, tags, cfg, st, lexicon, strict=True,
|
||||
instrumental=False, profile=None, lean=False):
|
||||
errs = []
|
||||
errs += ironlaw.check_rules(lyrics, tags, lean=lean)
|
||||
errs += ironlaw.check_lexicon(lyrics, lexicon)
|
||||
cfg_lyrics = meter.pick(cfg["lyrics"], profile)
|
||||
if instrumental:
|
||||
# 纯器乐没有可唱正文,"歌词过少/过多/每句音节"三条全部不适用。
|
||||
# 只保留结构与词典校验,密度统计仅作诊断记录。
|
||||
derr = []
|
||||
stats = meter.analyse(lyrics, st["duration"], st["bpm"], cfg_lyrics)
|
||||
else:
|
||||
derr, stats = meter.check(lyrics, st["duration"], st["bpm"], cfg_lyrics)
|
||||
errs += derr
|
||||
if strict and errs:
|
||||
raise RuleViolation("校验未通过:\n - " + "\n - ".join(errs))
|
||||
return errs, stats
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- commands
|
||||
|
||||
+429
@@ -0,0 +1,429 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""CLI 命令实现:doctor / styles / target / check / gen / runs / show。"""
|
||||
import datetime
|
||||
import json
|
||||
import os
|
||||
import random
|
||||
|
||||
from ..config import loader
|
||||
from ..core import catalog as catmod
|
||||
from ..core import paths as pmod
|
||||
from ..core import workflow as wfmod
|
||||
from ..core.engine import Engine
|
||||
from ..core.errors import ConfigError, ServerError
|
||||
from ..lyrics import meter
|
||||
from ..log.logger import Logger, Run
|
||||
from ..rules import ironlaw
|
||||
from .assemble import (_extra_hint, assemble, do_validate, duration_allowed)
|
||||
from .common import (ROOT, build_cfg, chill_intro_bars, gen_day_root, now_id,
|
||||
read_lyrics, resolve_profile, runs_root, safe_name,
|
||||
session_logger, style_of)
|
||||
|
||||
|
||||
def cmd_doctor(args):
|
||||
cfg = build_cfg(args)
|
||||
run, log = session_logger(args, cfg, "doctor")
|
||||
layout = pmod.path_layout(cfg)
|
||||
root = layout["comfy_root"]
|
||||
log.info("doctor 开始", server=cfg["engine"]["server"])
|
||||
print("== 路径 ==")
|
||||
print(" ComfyUI 根 %s" % root)
|
||||
print(" workflows %s" % ROOT)
|
||||
print(" runs %s (按日 YYYY-MM-DD/)" % layout["runs"])
|
||||
print(" tmp %s" % layout["tmp"])
|
||||
print(" lyrics %s" % layout["lyrics"])
|
||||
print(" baseline %s" % layout["baseline"])
|
||||
print(" catalog %s" % layout["catalog"])
|
||||
print(" qc_cache %s" % layout["qc_cache"])
|
||||
print(" 音频输出 %s" % layout["audio_out"])
|
||||
local_cfg = os.path.join(ROOT, "songforge", "config", "local.json")
|
||||
print(" local.json %s" % ("有" if os.path.isfile(local_cfg) else "无(可用 local.json.example)"))
|
||||
|
||||
print("== 根目录卫生 ==")
|
||||
pkg = layout["pkg"]
|
||||
junk = []
|
||||
for name in pmod.ROOT_JUNK_NAMES:
|
||||
p = os.path.join(pkg, name)
|
||||
if os.path.isfile(p):
|
||||
junk.append(name)
|
||||
for name in ("qc_baseline.json", "qc_baseline_old.json", "style_ref.json"):
|
||||
if os.path.isfile(os.path.join(pkg, name)):
|
||||
junk.append(name + "(应在 config/baseline/)")
|
||||
if junk:
|
||||
print(" [WARN] 包根有过程/错位文件: %s" % ", ".join(junk))
|
||||
else:
|
||||
print(" [ok] 包根无过程垃圾")
|
||||
|
||||
print("== ComfyUI ==")
|
||||
ok, info = Engine(cfg["engine"]["server"]).health()
|
||||
if not ok:
|
||||
print(" [FAIL] %s" % info)
|
||||
print(" 排查:确认 ComfyUI 已启动且端口 8188 可访问")
|
||||
log.error("ComfyUI 不可达", detail=str(info))
|
||||
log.close()
|
||||
return 2
|
||||
print(" [ok] %s 显存 %.1fGB 队列 运行%d/等待%d"
|
||||
% (info["device"], info["vram_total_gb"],
|
||||
info["queue_running"], info["queue_pending"]))
|
||||
print(" [ok] ComfyUI %s" % info["comfyui_version"])
|
||||
|
||||
print("== 模型 ==")
|
||||
ck = pmod.model_path(root, "checkpoints", cfg["model"]["checkpoint"])
|
||||
lora = pmod.model_path(root, "loras", cfg["model"]["lora"])
|
||||
tpl = os.path.join(ROOT, cfg["paths"]["workflow_template"])
|
||||
rc = 0
|
||||
for label, p in (("checkpoint", ck), ("lora", lora),
|
||||
("template", tpl)):
|
||||
good = os.path.exists(p)
|
||||
print(" [%s] %-11s %s" % ("ok" if good else "FAIL", label, p))
|
||||
if not good:
|
||||
rc = 2
|
||||
if cfg["model"]["checkpoint"] in cfg["model"]["denied_checkpoints"]:
|
||||
print(" [FAIL] 当前 checkpoint 在禁用名单里:%s"
|
||||
% cfg["model"]["checkpoint"])
|
||||
rc = 2
|
||||
else:
|
||||
print(" [ok] 模型锁定 %s + %s"
|
||||
% (cfg["model"]["checkpoint"], cfg["model"]["lora"]))
|
||||
|
||||
print("== 铁律 ==")
|
||||
for k in ("swlvoice", "8 bar instrumental intro hook", "no opening vocals"):
|
||||
good = k in ironlaw.build_tags("test")
|
||||
print(" [%s] %s" % ("ok" if good else "FAIL", k))
|
||||
if not good:
|
||||
rc = 2
|
||||
|
||||
print("== 采样 ==")
|
||||
bad = (cfg["sampler"]["shift"] < 5.0
|
||||
or cfg["sampler"]["steps"] < 24
|
||||
or cfg["sampler"]["cfg"] < 1.5
|
||||
or not (0.60 <= cfg["encode"]["temperature"] <= 0.70))
|
||||
print(" [%s] shift=%s steps=%s cfg=%s temperature=%s"
|
||||
% ("FAIL" if bad else "ok", cfg["sampler"]["shift"],
|
||||
cfg["sampler"]["steps"], cfg["sampler"]["cfg"],
|
||||
cfg["encode"]["temperature"]))
|
||||
if bad:
|
||||
print(" 偏离已验证参数:shift 5.0~5.5 / steps 24~28 / cfg 2.0"
|
||||
" / temperature 0.62~0.68")
|
||||
rc = 2
|
||||
|
||||
print("== 时长区间 ==")
|
||||
lo, hi = cfg["song"]["duration_range"]
|
||||
good = lo <= 150 and hi >= 200
|
||||
print(" [%s] 允许 %d~%d 秒(理想 150~200)"
|
||||
% ("ok" if good else "WARN", lo, hi))
|
||||
|
||||
print("== 配置 ==")
|
||||
print(" 服务器=%s" % cfg["engine"]["server"])
|
||||
for key in ("runs", "tmp", "tmp_today", "lyrics", "qc_cache",
|
||||
"style_cache", "index", "baseline"):
|
||||
pmod.ensure_dir(layout[key])
|
||||
log.info("doctor 结束", rc=rc, log_dir=run.dir)
|
||||
log.close()
|
||||
return rc
|
||||
|
||||
|
||||
def cmd_styles(args):
|
||||
data = loader.styles()
|
||||
print("%-12s %5s %-11s %5s %s" % ("风格", "BPM", "调性", "时长", "说明"))
|
||||
for k, v in sorted(data.items()):
|
||||
if k.startswith("_"):
|
||||
continue
|
||||
print("%-12s %5s %-11s %5s %s"
|
||||
% (k, v["bpm"], v["key"], v["duration"], v["style"]))
|
||||
return 0
|
||||
|
||||
|
||||
def cmd_target(args):
|
||||
cfg = build_cfg(args)
|
||||
if args.style:
|
||||
st = style_of(cfg, args.style, args)
|
||||
else:
|
||||
st = {"bpm": args.bpm or 94, "duration": args.dur or 165}
|
||||
if getattr(args, "bpm", None):
|
||||
st["bpm"] = args.bpm
|
||||
if getattr(args, "dur", None):
|
||||
st["duration"] = args.dur
|
||||
prof = resolve_profile(args)
|
||||
inst = bool(getattr(args, "instrumental", False))
|
||||
cfg_lyrics = meter.pick(cfg["lyrics"], prof)
|
||||
if args.style:
|
||||
print("风格 %s" % args.style)
|
||||
print("时长 %ds BPM %d" % (st["duration"], st["bpm"]))
|
||||
if inst:
|
||||
stt = meter.analyse("", st["duration"], st["bpm"], cfg_lyrics)
|
||||
print(" [纯器乐] 无人声,整首 %.0f 小节全部留给器乐"
|
||||
% stt["total_bars"])
|
||||
print(" 器乐结构段:intro / breakdown / build / drop / "
|
||||
"post-drop / final drop / outro")
|
||||
print(" 可唱歌词目标:0 音节")
|
||||
return 0
|
||||
stt = meter.analyse("", st["duration"], st["bpm"], cfg_lyrics)
|
||||
print(" 总小节 %.0f 前奏 %d 小节 可唱 %.0f 小节"
|
||||
% (stt["total_bars"], cfg_lyrics["intro_bars"], stt["sing_bars"]))
|
||||
if prof:
|
||||
print(" [密度档] %s(可唱小节占比 %.0f%%)"
|
||||
% (prof, cfg_lyrics["sing_bar_ratio"] * 100))
|
||||
mid, lo, hi = meter.target_words(st["duration"], st["bpm"], cfg_lyrics)
|
||||
print(" 理论音节容量 %d ~ %d" % (stt["lo"], stt["hi"]))
|
||||
print(" 建议正文字数:%d 字左右(放行区间 %d ~ %d)" % (mid, lo, hi))
|
||||
print(" 平均每句:%d ~ %d 音节(硬上限 %d)"
|
||||
% (cfg_lyrics["ideal_line_syllable"][0],
|
||||
cfg_lyrics["ideal_line_syllable"][1],
|
||||
cfg_lyrics["line_syllable_max"]))
|
||||
return 0
|
||||
|
||||
|
||||
def cmd_check(args):
|
||||
cfg = build_cfg(args)
|
||||
run, log = session_logger(args, cfg, "check")
|
||||
lexicon = loader.lexicon()
|
||||
raw = read_lyrics(args)
|
||||
st = style_of(cfg, args.style, args)
|
||||
tags, lyrics = assemble(args, cfg, st, raw, args.title or "check")
|
||||
inst = bool(getattr(args, "instrumental", False))
|
||||
prof = resolve_profile(args)
|
||||
errs, stats = do_validate(lyrics, tags, cfg, st, lexicon, strict=False,
|
||||
instrumental=inst, profile=prof,
|
||||
lean=bool(getattr(args, "lean_tags", False)))
|
||||
run.save_text("lyrics.txt", lyrics)
|
||||
run.save_text("tags.txt", tags)
|
||||
print("== 密度 ==")
|
||||
print(" 时长 %ds BPM %d 调性 %s" % (st["duration"], st["bpm"], st["key"]))
|
||||
if inst:
|
||||
print(" [纯器乐] 无可唱歌词,密度闸门不适用")
|
||||
elif prof:
|
||||
print(" [密度档] %s(可唱小节占比 %.0f%%)"
|
||||
% (prof, meter.pick(cfg["lyrics"], prof)["sing_bar_ratio"] * 100))
|
||||
print(" 正文字节 %d 音节 / %d 句 / 平均每句 %.1f 音节"
|
||||
% (stats["syllables"], stats["lines"], stats["per_line"]))
|
||||
if inst:
|
||||
print(" 密度闸门 N/A(纯器乐无歌词,放行区间不适用)")
|
||||
else:
|
||||
print(" 理论容量 %d ~ %d 放行区间 %d ~ %d"
|
||||
% (stats["lo"], stats["hi"], stats["win_lo"], stats["win_hi"]))
|
||||
print("== 结果 ==")
|
||||
if errs:
|
||||
for e in errs:
|
||||
print(" [FAIL] %s" % e)
|
||||
log.warn("check 未通过", errors=len(errs), syllables=stats["syllables"])
|
||||
log.close()
|
||||
return 1
|
||||
print(" [OK] 通过,可提交生成")
|
||||
log.info("check 通过", syllables=stats["syllables"],
|
||||
win="%d-%d" % (stats.get("win_lo", 0), stats.get("win_hi", 0)),
|
||||
log_dir=run.dir)
|
||||
log.close()
|
||||
return 0
|
||||
|
||||
|
||||
def cmd_gen(args):
|
||||
cfg = build_cfg(args)
|
||||
lexicon = loader.lexicon()
|
||||
title = args.title or "untitled"
|
||||
st = style_of(cfg, args.style, args)
|
||||
raw = read_lyrics(args)
|
||||
|
||||
lo, hi = cfg["song"]["duration_range"]
|
||||
if not duration_allowed(cfg, st["duration"]):
|
||||
raise ConfigError("时长 %ds 超出允许区间 %d~%ds%s"
|
||||
% (st["duration"], lo, hi,
|
||||
_extra_hint(cfg, st["duration"])))
|
||||
|
||||
tags, lyrics = assemble(args, cfg, st, raw, title)
|
||||
inst = bool(getattr(args, "instrumental", False))
|
||||
prof = resolve_profile(args)
|
||||
ibars = chill_intro_bars(
|
||||
cfg, bool(getattr(args, "chill_vocal", False)))
|
||||
errs, stats = do_validate(lyrics, tags, cfg, st, lexicon, strict=True,
|
||||
instrumental=inst, profile=prof,
|
||||
lean=bool(getattr(args, "lean_tags", False)))
|
||||
|
||||
day_root = gen_day_root(cfg)
|
||||
pmod.ensure_dir(day_root)
|
||||
run_id = now_id("gen", day_root)
|
||||
run = Run(run_id, day_root)
|
||||
log = Logger(run, level=args.log_level, quiet=args.quiet)
|
||||
|
||||
log.info("开始生成", title=title, style=args.style, bpm=st["bpm"],
|
||||
dur=st["duration"], key=st["key"], seeds=args.seeds)
|
||||
# 纯器乐与英文低声吟唱都强制走英文 language:
|
||||
# 1) chill-vocal 的歌词正文本来就是英文;
|
||||
# 2) instrumental 没有歌词,但 language 节点仍会影响音色/咬字倾向,
|
||||
# 不统一会让 instrumental vs chill 的 A/B 多出一个隐藏变量
|
||||
# (早期 InstPure 就是因为漏传 --language en 走了默认中文,
|
||||
# 跟 chill 版对比时无法归因)。
|
||||
lang = "en" if (getattr(args, "chill_vocal", False)
|
||||
or inst) else cfg["song"]["language"]
|
||||
if getattr(args, "language", None):
|
||||
lang = args.language
|
||||
log.info("密度校验通过", syllables=stats["syllables"],
|
||||
lines=stats["lines"], per_line=round(stats["per_line"], 2),
|
||||
win="N/A" if inst else "%d-%d" % (stats["win_lo"],
|
||||
stats["win_hi"]),
|
||||
cap_ratio=round(stats["fill"], 3),
|
||||
instrumental=inst, density_profile=prof or "default")
|
||||
|
||||
template = wfmod.load_template(cfg["paths"]["workflow_template"])
|
||||
prefix = cfg["paths"]["output_prefix"]
|
||||
fmt = cfg["paths"]["output_format"]
|
||||
seed_base = args.seed if args.seed else random.randint(1, 2 ** 31 - 1)
|
||||
seeds = [seed_base + i for i in range(args.seeds)] if args.seeds > 1 \
|
||||
else [seed_base]
|
||||
# 永久命名规则:歌名-【seed号】-【特征】-日期戳
|
||||
# 特征从风格名中提取,日期戳格式 YYMMDD
|
||||
date_stamp = datetime.datetime.now().strftime("%y%m%d")
|
||||
style_tag = args.style if len(args.style) <= 12 else args.style[:12]
|
||||
safe_title = safe_name(title)
|
||||
|
||||
run.save_text("lyrics.txt", lyrics)
|
||||
run.save_text("tags.txt", tags)
|
||||
|
||||
built = []
|
||||
for i, sd in enumerate(seeds):
|
||||
# 永久命名规则:歌名-【sN】-【风格特征】-YYMMDD
|
||||
sub = "%s/%s-s%d-%s-%s" % (prefix, safe_title, i + 1, style_tag, date_stamp)
|
||||
wf = wfmod.build_workflow(
|
||||
template, title=title, tags=tags, lyrics=lyrics, seed=sd,
|
||||
bpm=st["bpm"], duration=st["duration"], keyscale=st["key"],
|
||||
sampler=cfg["sampler"], encode=cfg["encode"], model=cfg["model"],
|
||||
output_prefix=sub, output_format=fmt,
|
||||
language=lang,
|
||||
timesignature=cfg["song"]["time_signature"])
|
||||
wf_path = run.path("wf_%s_s%d.json" % (safe_title, i + 1))
|
||||
with open(wf_path, "w", encoding="utf-8") as f:
|
||||
json.dump(wf, f, ensure_ascii=False, indent=1)
|
||||
built.append((sd, wf, wf_path, sub))
|
||||
log.info("workflow 已写入", file=os.path.basename(wf_path), seed=sd)
|
||||
|
||||
if args.dry_run:
|
||||
api = wfmod.render(built[0][1])
|
||||
run.save_json("payload_preview.json", api)
|
||||
print("\n[dry-run] 未提交 ComfyUI。已产出:")
|
||||
print(" 歌词 %d 音节 / %d 句 / 平均每句 %.1f 音节"
|
||||
% (stats["syllables"], stats["lines"], stats["per_line"]))
|
||||
print(" 时长 %ds BPM %d %s" % (st["duration"], st["bpm"], st["key"]))
|
||||
print(" workflow %s" % built[0][2])
|
||||
print(" API 预览 %s" % run.path("payload_preview.json"))
|
||||
print(" 实际输出将写到 %s"
|
||||
% pmod.music_dir(cfg, pmod.comfy_root(cfg=cfg)))
|
||||
# dry-run 也写入索引,便于目录浏览
|
||||
man_dry = dict(
|
||||
run_id=run.run_id, title=title, style=args.style,
|
||||
bpm=st["bpm"], key=st["key"], duration=st["duration"],
|
||||
results=[dict(seed=seeds[0], files=[])],
|
||||
created=datetime.datetime.now().isoformat(timespec="seconds"),
|
||||
dry_run=True)
|
||||
run.save_json("manifest.json", man_dry)
|
||||
catmod.append_entry(cfg, catmod.entry_from_gen(run.dir, man_dry))
|
||||
log.info("dry-run 结束", seeds=seeds)
|
||||
log.close()
|
||||
return 0
|
||||
|
||||
eng = Engine(cfg["engine"]["server"], timeout=cfg["engine"]["timeout_sec"],
|
||||
poll_interval=cfg["engine"]["poll_interval_sec"],
|
||||
submit_retries=cfg["engine"]["submit_retries"], logger=log)
|
||||
ok, info = eng.health()
|
||||
if not ok:
|
||||
raise ServerError("ComfyUI 不可达: %s" % info)
|
||||
|
||||
results = []
|
||||
for sd, wf, wf_path, sub in built:
|
||||
pid = eng.submit_retry(wfmod.render(wf))
|
||||
log.info("已提交", seed=sd, prompt_id=pid)
|
||||
|
||||
def prog(tick, secs, sd=sd):
|
||||
log.debug("等待中", seed=sd, tick=tick, elapsed=secs)
|
||||
|
||||
outs, rec = eng.wait(pid, on_progress=prog)
|
||||
log.info("完成", seed=sd, files=outs)
|
||||
results.append(dict(seed=sd, prompt_id=pid, files=outs,
|
||||
workflow=os.path.basename(wf_path)))
|
||||
|
||||
cro = pmod.comfy_root(cfg=cfg)
|
||||
manifest = dict(
|
||||
run_id=run.run_id, title=title, style=args.style,
|
||||
bpm=st["bpm"], key=st["key"], duration=st["duration"],
|
||||
language=lang,
|
||||
chill_vocal=bool(getattr(args, "chill_vocal", False)),
|
||||
instrumental=inst,
|
||||
density_profile=prof or "default",
|
||||
intro_bars=ibars,
|
||||
sampler=cfg["sampler"], encode=cfg["encode"], model=cfg["model"],
|
||||
density=stats, results=results,
|
||||
output_dir=pmod.music_dir(cfg, cro),
|
||||
created=datetime.datetime.now().isoformat(timespec="seconds"),
|
||||
)
|
||||
mpath = run.save_json("manifest.json", manifest)
|
||||
catmod.append_entry(cfg, catmod.entry_from_gen(run.dir, manifest))
|
||||
log.info("全部完成", run=run.run_id, manifest=os.path.basename(mpath))
|
||||
|
||||
print("\n生成完成 run_id=%s" % run.run_id)
|
||||
print(" 时长 %ds BPM %d %s 种子 %s"
|
||||
% (st["duration"], st["bpm"], st["key"], seeds))
|
||||
if inst:
|
||||
print(" [纯器乐] 无可唱歌词,密度闸门不适用")
|
||||
else:
|
||||
print(" 字数 %d 音节 放行区间 %d~%d 平均每句 %.1f 音节"
|
||||
% (stats["syllables"], stats["win_lo"], stats["win_hi"],
|
||||
stats["per_line"]))
|
||||
for r in results:
|
||||
print(" seed=%s -> %s" % (r["seed"], ", ".join(r["files"] or ["无输出"])))
|
||||
print(" 产物目录: %s" % run.dir)
|
||||
print(" 音频输出: %s" % manifest["output_dir"])
|
||||
log.close()
|
||||
return 0
|
||||
|
||||
|
||||
def cmd_runs(args):
|
||||
cfg = loader.load({})
|
||||
rows = catmod.list_entries(cfg, day=getattr(args, "day", None),
|
||||
limit=args.limit)
|
||||
if not rows:
|
||||
# 无索引时回退扫盘
|
||||
path, n = catmod.rebuild(cfg)
|
||||
print("索引为空,已 rebuild -> %s (%d 条)" % (path, n))
|
||||
rows = catmod.list_entries(cfg, day=getattr(args, "day", None),
|
||||
limit=args.limit)
|
||||
if not rows:
|
||||
print("还没有任何运行记录")
|
||||
return 0
|
||||
print("%-26s %-12s %-10s %5s %5s %-6s %s"
|
||||
% ("run_id", "日期", "标题", "BPM", "时长", "QC", "风格"))
|
||||
for d in rows:
|
||||
print("%-26s %-12s %-10s %5s %5s %-6s %s"
|
||||
% (d.get("run_id", "?"), d.get("date") or "-",
|
||||
(d.get("title") or "?")[:10], d.get("bpm"),
|
||||
d.get("duration"), d.get("qc_verdict") or "-",
|
||||
d.get("style") or ""))
|
||||
return 0
|
||||
|
||||
|
||||
def cmd_show(args):
|
||||
cfg = loader.load({})
|
||||
rundir = catmod.find_run_dir(runs_root(cfg), args.run_id)
|
||||
if not rundir:
|
||||
print("找不到 run: %s" % args.run_id)
|
||||
return 2
|
||||
m = os.path.join(rundir, "manifest.json")
|
||||
with open(m, encoding="utf-8") as f:
|
||||
print(json.dumps(json.load(f), ensure_ascii=False, indent=1))
|
||||
return 0
|
||||
|
||||
|
||||
def cmd_catalog(args):
|
||||
cfg = loader.load({})
|
||||
if args.rebuild:
|
||||
path, n = catmod.rebuild(cfg)
|
||||
print("rebuild %s (%d 条)" % (path, n))
|
||||
return 0
|
||||
rows = catmod.list_entries(cfg, day=args.day, limit=args.limit)
|
||||
if not rows:
|
||||
print("索引为空。可先: songforge catalog --rebuild")
|
||||
return 0
|
||||
for d in rows:
|
||||
print("%s %s %s bpm=%s dur=%s qc=%s %s"
|
||||
% (d.get("date"), d.get("run_id"), d.get("title"),
|
||||
d.get("bpm"), d.get("duration"),
|
||||
d.get("qc_verdict") or "-", d.get("style")))
|
||||
return 0
|
||||
+161
@@ -0,0 +1,161 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""CLI 共用:配置组装、风格解析、运行 ID、时长闸门。"""
|
||||
import datetime
|
||||
import os
|
||||
import re
|
||||
|
||||
from ..config import loader
|
||||
from ..core import paths as pmod
|
||||
from ..core import workflow as wfmod
|
||||
from ..core.errors import ConfigError
|
||||
from ..log.logger import Logger, Run
|
||||
|
||||
ROOT = wfmod.ROOT
|
||||
|
||||
# chill 吟唱(第三张专辑)的前奏小节数。
|
||||
#
|
||||
# 这里曾是 16:想靠"加长前奏 + 前 20 秒纯器乐"把人声压下去。
|
||||
# 实测 InstLowChill2 完全无效——vocals stem 仍然从 0.0 秒开始唱。
|
||||
# 原因见 ironlaw.RULE_CHILL_INTRO 的注释:ACE-Step 按歌词体的结构标记顺序
|
||||
# 分配段落,不按时长,标签里写多少小节都不起作用。
|
||||
# 现在三种模式统一 8 小节,和标签、歌词头三处保持一致。
|
||||
# 结构重排只降低人声密度,不推迟起点(见 ironlaw 顶部实测结论)。
|
||||
CHILL_INTRO_BARS = 8
|
||||
|
||||
|
||||
def safe_name(s):
|
||||
s = re.sub(r'[\\/:*?"<>|]', "_", s).strip()
|
||||
return s or "untitled"
|
||||
|
||||
|
||||
def now_id(prefix, root=None):
|
||||
"""生成运行 ID,并把 runs/<run_id>/ 目录原子占位,返回 run_id。
|
||||
|
||||
旧实现只到秒,连跑三首会落进同一个 runs 目录,lyrics.txt / tags.txt /
|
||||
manifest.json 互相覆盖,运行记录直接失效。这里做三层保险:
|
||||
1. 毫秒时间戳,保证连续调用几乎不会撞;
|
||||
2. os.makedirs(exist_ok=False) 原子占位,同毫秒内也只有一个能拿到;
|
||||
3. 真撞上就加序号自增,绝不覆盖已有目录。
|
||||
"""
|
||||
base = "%s-%s" % (
|
||||
prefix,
|
||||
datetime.datetime.now().strftime("%Y%m%d-%H%M%S-%f")[:-3],
|
||||
)
|
||||
if not root:
|
||||
return base
|
||||
for n in range(0, 100):
|
||||
cand = base if n == 0 else "%s-%d" % (base, n + 1)
|
||||
try:
|
||||
os.makedirs(os.path.join(root, cand), exist_ok=False)
|
||||
return cand
|
||||
except FileExistsError:
|
||||
continue
|
||||
raise RuntimeError("无法为 %s 分配运行目录(root=%s)" % (prefix, root))
|
||||
|
||||
|
||||
def build_cfg(args):
|
||||
ov = {}
|
||||
if getattr(args, "server", None):
|
||||
ov["engine.server"] = args.server
|
||||
if getattr(args, "shift", None) is not None:
|
||||
ov["sampler.shift"] = args.shift
|
||||
if getattr(args, "steps", None) is not None:
|
||||
ov["sampler.steps"] = args.steps
|
||||
if getattr(args, "cfg", None) is not None:
|
||||
ov["sampler.cfg"] = args.cfg
|
||||
if getattr(args, "temperature", None) is not None:
|
||||
ov["encode.temperature"] = args.temperature
|
||||
if getattr(args, "enc_cfg", None) is not None:
|
||||
ov["encode.cfg_scale"] = args.enc_cfg
|
||||
if getattr(args, "template", None):
|
||||
ov["paths.workflow_template"] = args.template
|
||||
if getattr(args, "runs_dir", None):
|
||||
ov["paths.runs_dir"] = args.runs_dir
|
||||
if getattr(args, "language", None):
|
||||
ov["song.language"] = args.language
|
||||
return loader.load(ov)
|
||||
|
||||
|
||||
def style_of(cfg, name, args):
|
||||
"""命令行覆盖优先于风格字典。"""
|
||||
st = dict(loader.styles().get(name) or {})
|
||||
if not st:
|
||||
raise ConfigError("未知风格 '%s',用 `songforge styles` 查看全部" % name)
|
||||
if getattr(args, "bpm", None):
|
||||
st["bpm"] = args.bpm
|
||||
if getattr(args, "key", None):
|
||||
st["key"] = args.key
|
||||
if getattr(args, "dur", None):
|
||||
st["duration"] = args.dur
|
||||
if getattr(args, "instruments", None):
|
||||
st["instruments"] = args.instruments
|
||||
if getattr(args, "mood", None):
|
||||
st["mood"] = args.mood
|
||||
return st
|
||||
|
||||
|
||||
def resolve_profile(args):
|
||||
"""决定用哪个密度档。
|
||||
|
||||
chill-vocal(英文低声吟唱)默认走 sparse:那一档的可唱小节只占 25%,
|
||||
允许整首只有几组 ooh ooh / mmm。之前它借用 default 档,而 default 档是
|
||||
按"人声填满全曲"反推的,会把 84 音节的低音吟唱判成"歌词过少",
|
||||
逼着写满 254 音节——那正是"只有人声没有音乐"的直接来源。
|
||||
"""
|
||||
prof = getattr(args, "density_profile", None)
|
||||
if prof:
|
||||
return prof
|
||||
if getattr(args, "chill_vocal", False):
|
||||
return "sparse"
|
||||
return None
|
||||
|
||||
|
||||
def read_lyrics(args):
|
||||
if args.lyrics_file:
|
||||
with open(args.lyrics_file, encoding="utf-8") as f:
|
||||
return f.read()
|
||||
if args.lyrics:
|
||||
return args.lyrics.replace("\\n", "\n")
|
||||
if getattr(args, "instrumental", False):
|
||||
# 纯器乐没有可唱正文,由 assemble 用结构标记自动铺一段。
|
||||
return ""
|
||||
raise ConfigError("必须提供 --lyrics-file 或 --lyrics")
|
||||
|
||||
|
||||
def chill_intro_bars(cfg, chill):
|
||||
"""chill 吟唱的前奏长度。
|
||||
|
||||
这里曾试过 16 小节:想靠"加长前奏 + 前 20 秒纯器乐"把人声压下去。
|
||||
实测两版(InstLowChill2 / 3)证明标签侧写多少小节都不起作用,
|
||||
ACE-Step 按歌词体结构标记顺序分配段落,不按时长描述。
|
||||
现在前奏回到 8 小节,和标签、歌词头三处保持一致。
|
||||
|
||||
注意:不要再把这个函数当成"推迟人声"的手段。实测(chill 3)
|
||||
即使器乐段整体排在人声段之前,第一句真唱仍然落在 0 秒。
|
||||
人声起点无法靠 prompt 控制,只能靠 sparse 密度档把真唱压到稀疏,
|
||||
并用音频 QC 验证。
|
||||
"""
|
||||
return CHILL_INTRO_BARS if chill else int(cfg["rules"]["intro_bars"])
|
||||
|
||||
|
||||
def session_logger(args, cfg, prefix):
|
||||
"""非 gen 命令的会话日志:落到 data/tmp/YYYY-MM-DD/<prefix>-ts/。"""
|
||||
layout = pmod.path_layout(cfg)
|
||||
day_root = layout["tmp_today"]
|
||||
pmod.ensure_dir(day_root)
|
||||
rid = now_id(prefix, day_root)
|
||||
run = Run(rid, day_root)
|
||||
log = Logger(run,
|
||||
level=getattr(args, "log_level", "info"),
|
||||
quiet=getattr(args, "quiet", False))
|
||||
return run, log
|
||||
|
||||
|
||||
def runs_root(cfg):
|
||||
return pmod.under_workflows(cfg["paths"]["runs_dir"])
|
||||
|
||||
|
||||
def gen_day_root(cfg):
|
||||
"""data/runs/YYYY-MM-DD/"""
|
||||
return pmod.day_subdir(runs_root(cfg))
|
||||
|
||||
+164
@@ -0,0 +1,164 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""songforge CLI 驱动(薄调度层)。
|
||||
|
||||
命令:
|
||||
doctor / styles / target / check / gen / runs / show / catalog / qc / selftest
|
||||
|
||||
设计约束:所有业务参数来自配置/命令行,本文件不写死任何模型或采样常量。
|
||||
实现见 common / assemble / commands / selftest / qc。
|
||||
"""
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
from ..core.errors import (LyricDensityError, RuleViolation, SongForgeError)
|
||||
from . import commands as cmds
|
||||
from . import selftest as stmod
|
||||
|
||||
|
||||
def build_parser():
|
||||
p = argparse.ArgumentParser(
|
||||
prog="songforge",
|
||||
description="ACE-Step 生歌引擎。配置优先级:命令行 > 环境变量 > "
|
||||
"local.json > default.json")
|
||||
p.add_argument("--server", help="ComfyUI 地址")
|
||||
p.add_argument("--log-level", default="info",
|
||||
choices=["debug", "info", "warn", "error"])
|
||||
p.add_argument("--quiet", action="store_true")
|
||||
sub = p.add_subparsers(dest="cmd")
|
||||
|
||||
d = sub.add_parser("doctor", help="环境自检")
|
||||
d.add_argument("--shift", type=float)
|
||||
d.add_argument("--steps", type=int)
|
||||
d.add_argument("--cfg", type=float)
|
||||
d.add_argument("--template")
|
||||
d.add_argument("--runs-dir", dest="runs_dir")
|
||||
d.set_defaults(func=cmds.cmd_doctor)
|
||||
|
||||
s = sub.add_parser("styles", help="列出风格字典")
|
||||
s.set_defaults(func=cmds.cmd_styles)
|
||||
|
||||
st = sub.add_parser("selftest", help="全链路自检(不连 ComfyUI 生成)")
|
||||
st.set_defaults(func=stmod.cmd_selftest)
|
||||
|
||||
t = sub.add_parser("target", help="死心塌地字数目标")
|
||||
t.add_argument("--style", help="用风格字典的 bpm/时长;可再被 --bpm/--dur 覆盖")
|
||||
t.add_argument("--bpm", type=int)
|
||||
t.add_argument("--dur", type=int)
|
||||
t.add_argument("--key")
|
||||
t.add_argument("--chill-vocal", dest="chill_vocal", action="store_true",
|
||||
help="英文低声吟唱:自动按 sparse 档(音乐主导)算目标字数")
|
||||
t.add_argument("--instrumental", action="store_true",
|
||||
help="纯器乐:算纯器乐结构下的器乐容量(无可唱歌词)")
|
||||
t.add_argument("--density-profile", dest="density_profile",
|
||||
help="密度档:default(人声填满)/ sparse(音乐主导、人声点缀)")
|
||||
t.set_defaults(func=cmds.cmd_target)
|
||||
|
||||
c = sub.add_parser("check", help="纯本地校验,不连 ComfyUI")
|
||||
c.add_argument("--style", required=True)
|
||||
c.add_argument("--lyrics-file", dest="lyrics_file")
|
||||
c.add_argument("--lyrics")
|
||||
c.add_argument("--title")
|
||||
c.add_argument("--bpm", type=int)
|
||||
c.add_argument("--key")
|
||||
c.add_argument("--dur", type=int)
|
||||
c.add_argument("--instruments")
|
||||
c.add_argument("--mood")
|
||||
c.add_argument("--shift", type=float)
|
||||
c.add_argument("--steps", type=int)
|
||||
c.add_argument("--cfg", type=float)
|
||||
c.add_argument("--temperature", type=float)
|
||||
c.add_argument("--enc-cfg", dest="enc_cfg", type=float)
|
||||
c.add_argument("--template")
|
||||
c.add_argument("--runs-dir", dest="runs_dir")
|
||||
c.add_argument("--language")
|
||||
c.add_argument("--chill-vocal", dest="chill_vocal", action="store_true",
|
||||
help="英文低声吟唱模式:放开 humming/melisma,"
|
||||
"人声退到点缀位,突出音乐本身")
|
||||
c.add_argument("--instrumental", action="store_true",
|
||||
help="纯器乐:整首无人声,只测 prompt 对乐器的控制力。"
|
||||
"不需要 --lyrics-file")
|
||||
c.add_argument("--density-profile", dest="density_profile",
|
||||
help="密度档:default(人声填满)/ sparse(音乐主导、人声点缀)")
|
||||
c.add_argument("--lean-tags", dest="lean_tags", action="store_true",
|
||||
help="精简正向标签:保留'前奏器乐+第一拍鼓'硬约束,"
|
||||
"但不注入 RULE_NEG 那 20+ 条人声否定句")
|
||||
c.set_defaults(func=cmds.cmd_check)
|
||||
|
||||
g = sub.add_parser("gen", help="生成歌曲")
|
||||
g.add_argument("--style", required=True)
|
||||
g.add_argument("--lyrics-file", dest="lyrics_file", required=False)
|
||||
g.add_argument("--lyrics")
|
||||
g.add_argument("--title", required=True)
|
||||
g.add_argument("--bpm", type=int)
|
||||
g.add_argument("--key")
|
||||
g.add_argument("--dur", type=int)
|
||||
g.add_argument("--instruments")
|
||||
g.add_argument("--mood")
|
||||
g.add_argument("--seeds", type=int, default=1)
|
||||
g.add_argument("--seed", type=int)
|
||||
g.add_argument("--shift", type=float)
|
||||
g.add_argument("--steps", type=int)
|
||||
g.add_argument("--cfg", type=float)
|
||||
g.add_argument("--temperature", type=float)
|
||||
g.add_argument("--enc-cfg", dest="enc_cfg", type=float)
|
||||
g.add_argument("--template")
|
||||
g.add_argument("--runs-dir", dest="runs_dir")
|
||||
g.add_argument("--language")
|
||||
g.add_argument("--chill-vocal", dest="chill_vocal", action="store_true",
|
||||
help="英文低声吟唱模式:放开 humming/melisma,"
|
||||
"人声退到点缀位,突出音乐本身(第三张专辑用)")
|
||||
g.add_argument("--instrumental", action="store_true",
|
||||
help="纯器乐:整首无人声,只测 prompt 对乐器的控制力。"
|
||||
"不需要 --lyrics-file")
|
||||
g.add_argument("--density-profile", dest="density_profile",
|
||||
help="密度档:default(人声填满)/ sparse(音乐主导、人声点缀)")
|
||||
g.add_argument("--lean-tags", dest="lean_tags", action="store_true",
|
||||
help="精简正向标签:保留'前奏器乐+第一拍鼓'硬约束,"
|
||||
"但不注入 RULE_NEG 那 20+ 条人声否定句")
|
||||
g.add_argument("--dry-run", dest="dry_run", action="store_true",
|
||||
help="只写 workflow 与 API 预览,不提交 ComfyUI")
|
||||
g.set_defaults(func=cmds.cmd_gen)
|
||||
|
||||
r = sub.add_parser("runs", help="列出历史运行")
|
||||
r.add_argument("--limit", type=int, default=20)
|
||||
r.add_argument("--day", help="只列某一天 YYYY-MM-DD")
|
||||
r.set_defaults(func=cmds.cmd_runs)
|
||||
|
||||
w = sub.add_parser("show", help="查看某次运行")
|
||||
w.add_argument("run_id")
|
||||
w.set_defaults(func=cmds.cmd_show)
|
||||
|
||||
cat = sub.add_parser("catalog", help="全局索引(歌名/风格/BPM/QC)")
|
||||
cat.add_argument("--limit", type=int, default=50)
|
||||
cat.add_argument("--day", help="只列某一天 YYYY-MM-DD")
|
||||
cat.add_argument("--rebuild", action="store_true",
|
||||
help="从 data/runs 扫描重建 catalog.jsonl")
|
||||
cat.set_defaults(func=cmds.cmd_catalog)
|
||||
|
||||
from .qc import add_qc
|
||||
add_qc(sub)
|
||||
return p
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
argv = argv if argv is not None else sys.argv[1:]
|
||||
parser = build_parser()
|
||||
args = parser.parse_args(argv)
|
||||
if not getattr(args, "func", None):
|
||||
parser.print_help()
|
||||
return 1
|
||||
try:
|
||||
return args.func(args) or 0
|
||||
except (RuleViolation, LyricDensityError) as e:
|
||||
print("\n[校验不通过]\n%s" % e)
|
||||
return 1
|
||||
except SongForgeError as e:
|
||||
detail = getattr(e, "detail", "")
|
||||
print("\n[错误] %s" % e)
|
||||
if detail:
|
||||
print(" 详情: %s" % detail)
|
||||
return 2
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,423 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""songforge qc —— 生成后验收。
|
||||
|
||||
songforge qc --run <run_id> 验收某次运行产出的所有 FLAC
|
||||
songforge qc --run <run_id> --baseline 额外跑 8 首已认可老歌作对照
|
||||
songforge qc --file <flac> --dur 165 直接验收单个文件
|
||||
songforge qc --baseline-only --save-baseline
|
||||
校准一次并把软阈值固化到
|
||||
songforge/qc_baseline.json
|
||||
|
||||
设计约束:主 CLI 是零重依赖的(只吃标准库),librosa/torch/demucs/whisper
|
||||
全在 ComfyUI venv 里。所以本命令只做三件事:
|
||||
1. 组 job(纯 dict/json)
|
||||
2. 用 venv python 起一个子进程跑 qc_worker.py
|
||||
3. 渲染报告 + 落盘 qc.json
|
||||
测量逻辑一行都不在这里,避免主链路被音频栈拖死。
|
||||
|
||||
坑位记录:
|
||||
- 不要试图在主进程 import librosa。主入口跑在系统 Python(3.10,无 librosa),
|
||||
硬 import 会让整个 songforge 直接不可用。
|
||||
- worker 输出的 JSON 前面有进度打印,用 ###JSON### 哨兵切分,
|
||||
不要直接 json.loads 整个 stdout。
|
||||
- venv python 路径要能自动定位:先看 SONGFORGE_QC_PYTHON,再找
|
||||
ComfyUI 根同级/上级常见的 comfyui-venv,找不到就明确报错而不是静默跳过。
|
||||
- 老歌基线必须带歌词正文(qc_baseline_old.json),否则 coverage 恒为 0,
|
||||
派生出的"召回 >= 0%"是假放行,比没有基线更危险。
|
||||
"""
|
||||
import argparse
|
||||
import glob
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
from ..config import loader
|
||||
from ..core import catalog as catmod
|
||||
from ..core import paths as pmod
|
||||
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
WORKER = os.path.join(ROOT, "songforge", "tools", "qc_worker.py")
|
||||
|
||||
|
||||
def _cfg():
|
||||
return loader.load({})
|
||||
|
||||
|
||||
def _baseline_paths(cfg=None):
|
||||
cfg = cfg or _cfg()
|
||||
base = pmod.under_workflows(
|
||||
cfg["paths"].get("baseline_dir") or "songforge/config/baseline")
|
||||
return (os.path.join(base, "qc_baseline_old.json"),
|
||||
os.path.join(base, "qc_baseline.json"),
|
||||
os.path.join(base, "style_ref.json"))
|
||||
|
||||
|
||||
def _comfy_root(cfg=None):
|
||||
return pmod.comfy_root(cfg=cfg or _cfg())
|
||||
|
||||
|
||||
def find_qc_python(cfg=None):
|
||||
"""定位带 librosa/torch/demucs 的那个 python。"""
|
||||
cfg = cfg or _cfg()
|
||||
configured = (cfg.get("paths") or {}).get("qc_python")
|
||||
if configured and os.path.isfile(configured):
|
||||
return os.path.abspath(configured)
|
||||
|
||||
env = os.environ.get("SONGFORGE_QC_PYTHON")
|
||||
if env and os.path.isfile(env):
|
||||
return os.path.abspath(env)
|
||||
|
||||
cands = []
|
||||
drive = os.environ.get("SystemDrive", "C:")
|
||||
cands += [
|
||||
r"C:\comfyui-venv\Scripts\python.exe",
|
||||
os.path.join(drive, r"\comfyui-venv\Scripts\python.exe"),
|
||||
]
|
||||
root = _comfy_root(cfg)
|
||||
for base in (os.path.dirname(root), os.path.dirname(os.path.dirname(root)),
|
||||
os.path.join(root, "venv"), root):
|
||||
cands.append(os.path.join(base, "Scripts", "python.exe"))
|
||||
for p in os.environ.get("PATH", "").split(os.pathsep):
|
||||
if p.lower().endswith("scripts") and os.path.isdir(p):
|
||||
cands.append(os.path.join(p, "python.exe"))
|
||||
|
||||
for c in cands:
|
||||
if os.path.isfile(c):
|
||||
try:
|
||||
r = subprocess.run(
|
||||
[c, "-c", "import librosa,torch,demucs,soundfile"],
|
||||
capture_output=True, timeout=180)
|
||||
if r.returncode == 0:
|
||||
return os.path.abspath(c)
|
||||
except (OSError, subprocess.SubprocessError):
|
||||
continue
|
||||
return None
|
||||
|
||||
|
||||
def _normalize_run_id(run):
|
||||
run = (run or "").strip().rstrip("/\\")
|
||||
if not run:
|
||||
return run
|
||||
cand = run
|
||||
if cand.endswith("manifest.json"):
|
||||
cand = os.path.dirname(cand)
|
||||
if os.path.isdir(cand) and os.path.isfile(os.path.join(cand, "manifest.json")):
|
||||
return os.path.basename(os.path.abspath(cand))
|
||||
return os.path.basename(run.replace("/", os.sep))
|
||||
|
||||
|
||||
def _read_manifest(run_id):
|
||||
cfg = _cfg()
|
||||
rid = _normalize_run_id(run_id)
|
||||
runs = pmod.under_workflows(cfg["paths"]["runs_dir"])
|
||||
rundir = catmod.find_run_dir(runs, rid)
|
||||
if not rundir:
|
||||
# 兼容旧路径 songforge/runs/<id>
|
||||
legacy = os.path.join(ROOT, "songforge", "runs", rid, "manifest.json")
|
||||
if os.path.isfile(legacy):
|
||||
with open(legacy, encoding="utf-8") as f:
|
||||
return json.load(f), os.path.dirname(legacy)
|
||||
return None, None
|
||||
with open(os.path.join(rundir, "manifest.json"), encoding="utf-8") as f:
|
||||
return json.load(f), rundir
|
||||
|
||||
|
||||
def _baseline_items():
|
||||
base_old, _, _ = _baseline_paths()
|
||||
if not os.path.isfile(base_old):
|
||||
print("[错误] 缺少 %s" % base_old)
|
||||
print(" 先跑 tools/make_baseline.py 从 remake_v5.py 抽歌词基线。")
|
||||
return []
|
||||
with open(base_old, encoding="utf-8") as f:
|
||||
return json.load(f).get("items", [])
|
||||
|
||||
|
||||
def _old_files(music):
|
||||
out = []
|
||||
for it in _baseline_items():
|
||||
p = os.path.join(music, it["file"])
|
||||
if os.path.isfile(p):
|
||||
out.append(dict(path=p, title=it["title"], group="old",
|
||||
lyrics=it["lyrics"], bpm=it.get("bpm"),
|
||||
duration=it.get("duration")))
|
||||
else:
|
||||
print(" [skip] 基线缺失 %s" % it["file"])
|
||||
return out
|
||||
|
||||
|
||||
def cmd_qc(args):
|
||||
cfg = _cfg()
|
||||
base_old, base_json, style_ref = _baseline_paths(cfg)
|
||||
music = args.music_dir or pmod.music_dir(cfg)
|
||||
ref = args.ref or os.path.join(
|
||||
_comfy_root(cfg), "input", "voice_samples", "materials_D60927.wav")
|
||||
|
||||
files = []
|
||||
rundir = None
|
||||
if args.run:
|
||||
man, rundir = _read_manifest(args.run)
|
||||
if not man:
|
||||
print("找不到 run: %s" % args.run)
|
||||
return 2
|
||||
names = []
|
||||
for r in man.get("results", []):
|
||||
for fn in r.get("files") or []:
|
||||
names.append(fn)
|
||||
if not names:
|
||||
names = glob.glob("%s_s*_00001.flac"
|
||||
% (man.get("title") or args.run))
|
||||
lyr = ""
|
||||
lp = os.path.join(rundir, "lyrics.txt")
|
||||
if os.path.isfile(lp):
|
||||
with open(lp, encoding="utf-8") as f:
|
||||
lyr = f.read()
|
||||
for fn in names:
|
||||
base = os.path.basename(fn)
|
||||
hit = None
|
||||
for cand in (fn, os.path.join("MyMusic", fn),
|
||||
os.path.join(music, base)):
|
||||
if os.path.isabs(cand) and os.path.isfile(cand):
|
||||
hit = cand
|
||||
break
|
||||
if os.path.isfile(cand):
|
||||
hit = cand
|
||||
break
|
||||
if hit is None:
|
||||
hit = os.path.join(music, base)
|
||||
files.append(dict(
|
||||
path=hit, title=man.get("title"), style=man.get("style", ""),
|
||||
group="new", duration=man.get("duration"),
|
||||
bpm=man.get("bpm"), intro_bars=man.get("intro_bars", 8),
|
||||
lang=man.get("language", "zh"),
|
||||
lyrics=lyr))
|
||||
if not files:
|
||||
print("run %s 没有可验收的音频" % args.run)
|
||||
return 2
|
||||
|
||||
for fn in args.file or []:
|
||||
files.append(dict(path=fn, title=os.path.splitext(
|
||||
os.path.basename(fn))[0], group="new",
|
||||
duration=args.dur, bpm=args.bpm,
|
||||
intro_bars=args.intro_bars, lang=args.lang, lyrics=""))
|
||||
|
||||
if args.baseline or args.baseline_only:
|
||||
files = _old_files(music) + files
|
||||
if not files:
|
||||
print("没有要验收的文件")
|
||||
return 2
|
||||
|
||||
missing = [f["path"] for f in files if not os.path.isfile(f["path"])]
|
||||
if missing and not args.allow_missing:
|
||||
for m in missing:
|
||||
print(" [FAIL] 文件不存在 %s" % m)
|
||||
return 2
|
||||
|
||||
py = find_qc_python(cfg)
|
||||
if not py:
|
||||
print("[错误] 找不到带 librosa/torch/demucs 的 Python。")
|
||||
print(" 用 config/local.json 的 qc_python 或 SONGFORGE_QC_PYTHON 指定")
|
||||
return 2
|
||||
print("QC python %s" % py)
|
||||
|
||||
_cache = pmod.under_workflows(cfg["paths"]["qc_cache_dir"])
|
||||
pmod.ensure_dir(_cache)
|
||||
|
||||
job = dict(comfy_root=_comfy_root(cfg), music_dir=music, ref=ref,
|
||||
files=files, asr=not args.no_asr,
|
||||
cache_dir=_cache,
|
||||
style_ref=style_ref if os.path.isfile(style_ref) else None,
|
||||
# 坑位:--save-baseline 必须不让 worker 读旧基线。
|
||||
baseline_file=None if args.save_baseline else
|
||||
(base_json if os.path.isfile(base_json) else None))
|
||||
|
||||
env = dict(os.environ)
|
||||
env["SONGFORGE_COMFY_ROOT"] = _comfy_root(cfg)
|
||||
env["PYTHONIOENCODING"] = "utf-8"
|
||||
print("测量中(首次要加载 demucs + whisper + CAMPPlus,请耐心)...")
|
||||
if not job["style_ref"]:
|
||||
print("[提示] 没有 style_ref.json,风格判定不会运行"
|
||||
"(先跑 tools/make_style_ref.py)")
|
||||
cmd = [py, WORKER, json.dumps(job, ensure_ascii=False)]
|
||||
try:
|
||||
r = subprocess.run(cmd, capture_output=True, env=env, timeout=args.timeout)
|
||||
except subprocess.TimeoutExpired:
|
||||
print("[错误] QC 超时(%ds)" % args.timeout)
|
||||
return 2
|
||||
|
||||
out = r.stdout.decode("utf-8", "replace")
|
||||
if "###JSON###" not in out:
|
||||
print(out[-2000:])
|
||||
print(r.stderr.decode("utf-8", "replace")[-2000:])
|
||||
print("[错误] worker 没有产出结果")
|
||||
return 2
|
||||
rep = json.loads(out.split("###JSON###", 1)[1].strip())
|
||||
|
||||
if args.save_baseline:
|
||||
pmod.ensure_dir(os.path.dirname(base_json))
|
||||
with open(base_json, "w", encoding="utf-8") as f:
|
||||
json.dump(dict(base=rep["baseline"], soft=rep["soft"],
|
||||
note="由 songforge qc --baseline-only --save-baseline "
|
||||
"在 8 首已认可老歌上校准,不要手改"), f,
|
||||
ensure_ascii=False, indent=1)
|
||||
print("\n基线已固化: %s" % base_json)
|
||||
|
||||
return _render(rep, args, rundir=rundir, cfg=cfg)
|
||||
|
||||
|
||||
def _render(rep, args, rundir=None, cfg=None):
|
||||
base, soft, verd = rep["baseline"], rep["soft"], rep["verdicts"]
|
||||
rows = rep["rows"]
|
||||
cfg = cfg or _cfg()
|
||||
|
||||
print("\n== 基线(%d 首已认可老歌)==" % base.get("n_old", 0))
|
||||
if base.get("n_old"):
|
||||
print(" 歌词召回 中位 %.0f%% 声线最近邻 中位 %.3f 自相似 %.3f"
|
||||
% (base.get("coverage", 0) * 100, base.get("voice_floor", 0),
|
||||
base.get("voice_self", 0)))
|
||||
print(" 动态 %.1f dB 质心 %.0f Hz F0漂移 P90 %.1f 半音"
|
||||
% (base.get("dynamic_db", 0), base.get("centroid", 0),
|
||||
base.get("f0_drift_st", 0)))
|
||||
print(" 放行线:召回>=%.0f%% 声线NN>=%.3f 自相似>=%.3f 动态>=%.1fdB"
|
||||
% (soft.get("coverage", 0) * 100, soft.get("voice_nn", 0),
|
||||
soft.get("voice_self", 0), soft.get("dyn_lo", 0)))
|
||||
print(" 质心>=%.0fHz F0漂移<=%.1f半音 速度漂移<=%.1f%%"
|
||||
% (soft.get("centroid_lo", 0), soft.get("f0_drift_st_max", 0),
|
||||
soft.get("tempo_drift_max", 0) * 100))
|
||||
print(" 实测速度 vs 设定 BPM 容差 ±%.0f%%"
|
||||
% (18.0,))
|
||||
else:
|
||||
print(" [WARN] 没有老歌基线,本次只跑硬阈值,软阈值不可信")
|
||||
|
||||
print("\n== 风格分类 ==")
|
||||
sr = rows[0].get("style_trusted") if rows else None
|
||||
print(" 语料可信度:%s" % ("可信(分组留一通过)" if sr
|
||||
else ("不可信,只当参考" if sr is not None
|
||||
else "未运行")))
|
||||
print("%-22s %-7s %6s %-22s %-7s"
|
||||
% ("file", "判定", "置信", "最像历史作品", "其风格"))
|
||||
for r in rows:
|
||||
if "error" in r or not r.get("style_fam"):
|
||||
continue
|
||||
print("%-22s %-7s %5.0f%% %-22s %-7s"
|
||||
% (r["file"][:22], r["style_fam"],
|
||||
r.get("style_conf", 0) * 100, r.get("style_like", "?")[:22],
|
||||
r.get("style_like_family", "")))
|
||||
|
||||
print("\n== 明细 ==")
|
||||
print("%-22s %6s %6s %6s %6s %6s %7s %7s %7s %6s"
|
||||
% ("file", "dur", "peak", "lufs", "dyn", "cent", "bpm设定", "bpm实测",
|
||||
"人声进", "泄漏"))
|
||||
for r in rows:
|
||||
if "error" in r:
|
||||
print("%-22s ERROR %s" % (r["file"][:22], r["error"][:70]))
|
||||
continue
|
||||
want = (r.get("expect") or {}).get("bpm") or 0
|
||||
print("%-22s %6.1f %6.3f %6.1f %6.1f %6.0f %7s %7.1f %7s %6d"
|
||||
% (r["file"][:22], r["duration"], r["peak"], r["lufs"],
|
||||
r["dynamic_db"], r["centroid_hz"],
|
||||
want or "-", r.get("tempo_med", 0),
|
||||
"-" if r.get("vocal_onset") is None
|
||||
else "%.1f" % r["vocal_onset"],
|
||||
r.get("leak_segs", 0)))
|
||||
for r in rows:
|
||||
if r.get("leaks"):
|
||||
print("\n 指令泄漏明细 %s:" % r["file"][:22])
|
||||
for lk in r["leaks"]:
|
||||
# 命中证据优先打实证短语;只有短语为空(纯集合重合)
|
||||
# 才退回打指令词命中率。
|
||||
hit = lk.get("leak_phrase") or "指令词命中 %3.0f%%" % (
|
||||
(lk.get("instr_hit") or 0) * 100)
|
||||
print(" t0=%6.2fs %s %s"
|
||||
% (lk["t0"], hit, lk.get("text", "")))
|
||||
|
||||
print("\n== 声线 / 歌词 ==")
|
||||
print("%-22s %-14s %8s %7s %7s %6s %6s %7s"
|
||||
% ("file", "最近邻老歌", "NN相似", "自相似", "cov", "f0中位",
|
||||
"f0漂移", "t漂移"))
|
||||
for r in rows:
|
||||
if "error" in r:
|
||||
continue
|
||||
print("%-22s %-14s %8.3f %7.3f %6.1f%% %6.1f %5.1fst %6.1f%%"
|
||||
% (r["file"][:22], (r.get("voice_nn") or "-")[:14],
|
||||
r.get("voice_nn_sim", 0), r.get("voice_self", 0),
|
||||
r.get("coverage", 0) * 100, r.get("f0_med", 0),
|
||||
r.get("f0_drift_st", 0), r.get("tempo_drift", 0) * 100))
|
||||
|
||||
print("\n== 结论 ==")
|
||||
npass = 0
|
||||
for v in verd:
|
||||
print(" [%s] %s" % (v["verdict"], v["file"]))
|
||||
if v["verdict"] == "PASS":
|
||||
npass += 1
|
||||
for w in v.get("why", []):
|
||||
print(" - %s" % w)
|
||||
|
||||
outp = args.out
|
||||
if not outp:
|
||||
if rundir:
|
||||
base_dir = rundir
|
||||
elif args.run:
|
||||
runs = pmod.under_workflows(cfg["paths"]["runs_dir"])
|
||||
found = catmod.find_run_dir(runs, _normalize_run_id(args.run))
|
||||
base_dir = found or pmod.tmp_today(cfg)
|
||||
else:
|
||||
base_dir = pmod.tmp_today(cfg)
|
||||
outp = os.path.join(base_dir, "qc.json")
|
||||
os.makedirs(os.path.dirname(outp) or ".", exist_ok=True)
|
||||
with open(outp, "w", encoding="utf-8") as f:
|
||||
json.dump(rep, f, ensure_ascii=False, indent=1)
|
||||
print("\n报告: %s" % outp)
|
||||
|
||||
if args.run and verd:
|
||||
new_names = set()
|
||||
for row in rows:
|
||||
if row.get("group") == "new" or (
|
||||
row.get("group") is None and "error" not in row):
|
||||
new_names.add(os.path.basename(str(row.get("file") or "")))
|
||||
new_v = [v for v in verd
|
||||
if os.path.basename(str(v.get("file") or "")) in new_names] \
|
||||
or verd
|
||||
n_ok = sum(1 for v in new_v if v.get("verdict") == "PASS")
|
||||
if n_ok == len(new_v):
|
||||
verdict_str = "PASS"
|
||||
elif n_ok == 0:
|
||||
verdict_str = "FAIL"
|
||||
else:
|
||||
verdict_str = "PARTIAL"
|
||||
try:
|
||||
catmod.update_qc_verdict(cfg, _normalize_run_id(args.run),
|
||||
verdict_str)
|
||||
except Exception as e:
|
||||
print("[WARN] catalog 更新失败: %s" % e)
|
||||
|
||||
if not verd:
|
||||
print("本次只有基线校准,没有待验收新歌 -> 退出码 0")
|
||||
return 0
|
||||
return 0 if npass == len(verd) else 1
|
||||
|
||||
|
||||
def add_qc(sub):
|
||||
q = sub.add_parser("qc", help="生成后验收(时长/声线/歌词/开头器乐/音频质量)")
|
||||
q.add_argument("--run", help="验收某次 run 产出的 FLAC")
|
||||
q.add_argument("--file", action="append", help="直接验收文件,可多次")
|
||||
q.add_argument("--baseline", action="store_true",
|
||||
help="额外测 8 首已认可老歌,一并派生软阈值")
|
||||
q.add_argument("--baseline-only", dest="baseline_only",
|
||||
action="store_true", help="只跑老歌基线(校准用)")
|
||||
q.add_argument("--save-baseline", dest="save_baseline",
|
||||
action="store_true", help="把本次派生阈值固化到 qc_baseline.json")
|
||||
q.add_argument("--music-dir", dest="music_dir")
|
||||
q.add_argument("--ref", help="声线参考音频")
|
||||
q.add_argument("--dur", type=int, help="期望时长(秒),用于时长校验")
|
||||
q.add_argument("--bpm", type=int)
|
||||
q.add_argument("--intro-bars", dest="intro_bars", type=int, default=8)
|
||||
q.add_argument("--lang", dest="lang", default="zh",
|
||||
choices=["zh", "en"],
|
||||
help="歌词语种,决定 ASR 语种与召回口径。"
|
||||
"英文歌必须传 en,否则 ASR 按中文解码、召回恒为 0。")
|
||||
q.add_argument("--no-asr", dest="no_asr", action="store_true")
|
||||
q.add_argument("--timeout", type=int, default=7200)
|
||||
q.add_argument("--allow-missing", dest="allow_missing", action="store_true")
|
||||
q.add_argument("--out")
|
||||
q.set_defaults(func=cmd_qc)
|
||||
return q
|
||||
+788
@@ -0,0 +1,788 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""全链路自检(不连 ComfyUI 生成)。"""
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
|
||||
from ..config import loader
|
||||
from ..core import paths as pmod
|
||||
from ..core import workflow as wfmod
|
||||
from ..lyrics import meter
|
||||
from ..rules import ironlaw
|
||||
from .assemble import assemble, do_validate
|
||||
from .common import ROOT, now_id
|
||||
|
||||
# 第三张专辑的低音吟唱 demo 歌词,稀疏档与 chill 集成自测都读它。
|
||||
# 素材库迁到 _archive/lyrics_corpus 后,selftest 仍读包内 probe。
|
||||
# 旧文件名 02_InstLowChill.txt 已不在树里,以现有 03 为准。
|
||||
_CHILL_DEMO = os.path.join(
|
||||
ROOT, "songforge", "lyrics", "probe", "03_InstLowChill6.txt")
|
||||
|
||||
|
||||
def _t_runid():
|
||||
"""运行 ID 必须唯一。
|
||||
|
||||
旧实现只到秒,连跑三首会落进同一个 runs 目录,lyrics.txt / tags.txt /
|
||||
manifest.json 互相覆盖,dry-run 记录直接失效。这里既验唯一性,
|
||||
也验真实目录里连续三次分配不冲突。
|
||||
"""
|
||||
layout = pmod.path_layout(loader.load({}))
|
||||
scratch = os.path.join(layout["tmp"], "selftest_runid")
|
||||
if os.path.isdir(scratch):
|
||||
for f in os.listdir(scratch):
|
||||
p = os.path.join(scratch, f)
|
||||
if os.path.isdir(p):
|
||||
os.rmdir(p)
|
||||
os.rmdir(scratch)
|
||||
os.makedirs(scratch)
|
||||
try:
|
||||
ids = [now_id("t", scratch) for _ in range(3)]
|
||||
# 目录必须已被原子占位,防止后续写入互相覆盖
|
||||
assert all(os.path.isdir(os.path.join(scratch, i)) for i in ids), \
|
||||
"运行目录未占位: %s" % ids
|
||||
finally:
|
||||
for f in os.listdir(scratch):
|
||||
os.rmdir(os.path.join(scratch, f))
|
||||
os.rmdir(scratch)
|
||||
assert len(set(ids)) == 3, "运行 ID 冲突: %s" % ids
|
||||
|
||||
# 模拟目录已存在时必须自增,而不是覆盖旧记录
|
||||
base = now_id("t", None)
|
||||
assert len(base) > len("t-20261004-041513"), \
|
||||
"运行 ID 缺少微秒段: %s" % base
|
||||
root2 = os.path.join(layout["tmp"], "selftest_runid2")
|
||||
if os.path.isdir(root2):
|
||||
for f in os.listdir(root2):
|
||||
os.rmdir(os.path.join(root2, f))
|
||||
os.rmdir(root2)
|
||||
os.makedirs(root2)
|
||||
try:
|
||||
# now_id 已原子占位,这里再取一次必须自动自增,而不是复用旧目录
|
||||
first = now_id("t", root2)
|
||||
second = now_id("t", root2)
|
||||
finally:
|
||||
for d in os.listdir(root2):
|
||||
os.rmdir(os.path.join(root2, d))
|
||||
os.rmdir(root2)
|
||||
assert first != second, "目录已存在时未自增: %s / %s" % (first, second)
|
||||
return [("运行 ID 唯一", "连跑三次 %s ~ %s 互不冲突" % (ids[0], ids[-1]))]
|
||||
|
||||
|
||||
def _t_path_layout():
|
||||
"""配置里的目录键必须能解析,且与 DEFAULTS 对齐。"""
|
||||
cfg = loader.load({})
|
||||
p = cfg["paths"]
|
||||
for key in ("runs_dir", "tmp_dir", "lyrics_dir", "qc_cache_dir",
|
||||
"style_cache_dir", "output_prefix", "baseline_dir",
|
||||
"catalog_file"):
|
||||
assert key in p and p[key], "paths.%s 缺失" % key
|
||||
layout = pmod.path_layout(cfg)
|
||||
runs_n = layout["runs"].replace("\\", "/")
|
||||
tmp_n = layout["tmp"].replace("\\", "/")
|
||||
assert runs_n.endswith("songforge/data/runs"), "runs 路径异常: %s" % runs_n
|
||||
assert tmp_n.endswith("songforge/data/tmp"), "tmp 路径异常: %s" % tmp_n
|
||||
assert os.path.isdir(layout["baseline"]), "baseline 目录缺失: %s" % layout["baseline"]
|
||||
for name in ("qc_baseline.json", "qc_baseline_old.json", "style_ref.json"):
|
||||
assert os.path.isfile(os.path.join(layout["baseline"], name)), \
|
||||
"缺少基线文件 %s" % name
|
||||
return [("目录布局", "data/runs|tmp + baseline + catalog 已从 config.paths 解析")]
|
||||
|
||||
|
||||
def _t_paths():
|
||||
root = pmod.comfy_root()
|
||||
assert os.path.isdir(os.path.join(root, "models", "checkpoints")), \
|
||||
"ComfyUI 根目录推导错误: %s" % root
|
||||
return [("ComfyUI 根推导", root)]
|
||||
|
||||
|
||||
def _t_config_priority():
|
||||
os.environ["SONGFORGE_SHIFT"] = "4.4"
|
||||
try:
|
||||
cfg = loader.load({})
|
||||
assert abs(cfg["sampler"]["shift"] - 4.4) < 1e-6, "环境变量未生效"
|
||||
cfg = loader.load({"sampler.shift": 6.0})
|
||||
assert abs(cfg["sampler"]["shift"] - 6.0) < 1e-6, "命令行未覆盖环境变量"
|
||||
finally:
|
||||
os.environ.pop("SONGFORGE_SHIFT", None)
|
||||
base = loader.load({})["sampler"]["shift"]
|
||||
assert abs(base - 5.5) < 1e-6, "默认 shift 被污染: %s" % base
|
||||
return [("配置优先级 命令行>环境>配置", "shift 5.5 -> 4.4 -> 6.0")]
|
||||
|
||||
|
||||
def _t_model_locked():
|
||||
cfg = loader.load({})
|
||||
ck = cfg["model"]["checkpoint"]
|
||||
assert ck == "ace_step_1.5_turbo_swl_fused.safetensors", "checkpoint 被换: %s" % ck
|
||||
assert cfg["model"]["lora"] == "swl_voice_best_ep257.safetensors", "LoRA 被换"
|
||||
assert ck not in cfg["model"]["denied_checkpoints"], "当前模型在禁用名单"
|
||||
return [("模型锁定", ck + " + " + cfg["model"]["lora"])]
|
||||
|
||||
|
||||
def _t_ironlaw():
|
||||
tags = ironlaw.build_tags("Chinese pop")
|
||||
for kw in ("swlvoice", "8 bar instrumental intro hook", "no opening vocals",
|
||||
"no pure vocal intro", "no humming"):
|
||||
assert kw in tags, "tags 缺少 %s" % kw
|
||||
lyrics = ironlaw.build_lyrics(
|
||||
[("[verse]", "第一句歌词\n第二句歌词")], "kick and snare", bars=8)
|
||||
assert lyrics.lstrip().startswith("[intro]"), "未强制器乐前奏"
|
||||
assert "[Instrumental: 8 bar" in lyrics, "前奏未写明 8 小节"
|
||||
assert not ironlaw.check_rules(lyrics, tags), ironlaw.check_rules(lyrics, tags)
|
||||
# 缺前奏必须被拦
|
||||
no_intro = "[verse]\n没有前奏直接唱"
|
||||
assert ironlaw.check_rules(no_intro, tags), "缺器乐前奏未被拦截"
|
||||
# 中文结构标记必须被拦
|
||||
marked = lyrics.replace("[verse]", "[主歌]")
|
||||
assert ironlaw.check_rules(marked, tags), "中文结构标记未被拦截"
|
||||
return [("铁律注入", "8 小节器乐前奏 + 反人声条款齐备,缺前奏/中文标记均被拦")]
|
||||
|
||||
|
||||
def _t_sparse_profile():
|
||||
"""稀疏档必须真的放行"只有几句英文语气词"的写法,且仍然拦得住异常值。
|
||||
|
||||
背景:第三张专辑人声压过音乐,根因之一是 default 密度档按"人声填满全曲"
|
||||
反推(175s/124BPM 下限 254 音节),把 84 音节的低音吟唱判成"歌词过少",
|
||||
于是每次都被迫写满整首,人声自然盖过器乐。sparse 档就是为了给出
|
||||
"可唱小节只占 25%" 的合法区间。
|
||||
"""
|
||||
cfg = loader.load({})["lyrics"]
|
||||
sparse = meter.pick(cfg, "sparse")
|
||||
assert sparse["sing_bar_ratio"] < cfg["sing_bar_ratio"], \
|
||||
"sparse 档可唱小节占比没有低于 default,音乐主导无从体现"
|
||||
assert sparse["ideal_line_syllable"][0] < cfg["ideal_line_syllable"][0], \
|
||||
"sparse 档句子下限没有放宽,mmm / ooh 这类短句仍会被拦"
|
||||
|
||||
dur, bpm = 175, 124
|
||||
_, lo, hi = meter.target_words(dur, bpm, sparse)
|
||||
# 直接用真实 demo 歌词做样本,而不是抄一份内联的:
|
||||
# 抄的内联文本一漂移,测试就测不到真正要生成的那份文件了。
|
||||
probe = _CHILL_DEMO
|
||||
assert os.path.exists(probe), "找不到低音 demo 歌词 %s" % probe
|
||||
with open(probe, encoding="utf-8") as f:
|
||||
body = f.read()
|
||||
syl = meter.syllable(meter.only_lyrics(body))
|
||||
assert meter.check(body, dur, bpm, cfg)[0], \
|
||||
"default 档居然放行了稀疏歌词,档间没有区分度"
|
||||
errs, st = meter.check(body, dur, bpm, sparse)
|
||||
assert not errs, "sparse 档误杀了低音语气词写法: %s" % errs
|
||||
assert lo <= syl <= hi, \
|
||||
"sparse 放行区间 %d~%d 与实测 %d 音节不一致" % (lo, hi, syl)
|
||||
|
||||
# 过短仍要拦:sparse 不是"无限制放宽"
|
||||
thin = ("[intro]\n[Instrumental: 8 bar]\n[verse]\nmmm\nooh\n")
|
||||
assert any("过少" in e for e in meter.check(thin, dur, bpm, sparse)[0]), \
|
||||
"sparse 档放过短歌词"
|
||||
|
||||
# 过多仍要拦:写成满首人声时 sparse 也必须报错,否则会退回旧病
|
||||
fat = ("[intro]\n[Instrumental: 8 bar]\n[verse]\n"
|
||||
+ "\n".join(["mmm mmm low and slow"] * 90))
|
||||
assert any("过多" in e for e in meter.check(fat, dur, bpm, sparse)[0]), \
|
||||
"sparse 档放过多歌词,音乐主导会被稀释"
|
||||
return [("稀疏密度档",
|
||||
"sparse 放行 %d~%d 音节(default 拒绝同一份),过少过多仍被拦"
|
||||
% (lo, hi))]
|
||||
|
||||
|
||||
def _t_instrumental():
|
||||
"""纯器乐模式的标签必须自洽,且不与风格字典的唱法描述对撞。"""
|
||||
tags = ironlaw.build_tags(
|
||||
style="English melodic future bass", instruments="huge supersaw lead",
|
||||
mood="vast uplifting EDM",
|
||||
extra="male low whisper vocal, hushed and intimate",
|
||||
instrumental=True)
|
||||
# 反人声条款必须在
|
||||
for kw in ("no vocals anywhere", "instrumental only", "no voice at all",
|
||||
"no humming", "no choir", "no vocal sample", "swlvoice",
|
||||
"8 bar instrumental intro hook"):
|
||||
assert kw in tags, "纯器乐 tags 缺少 %s" % kw
|
||||
# 关键回归点:风格字典的唱法描述绝不能混进来跟 "no vocals" 对撞。
|
||||
for bad in ("whisper vocal", "sparse English words", "no belting"):
|
||||
assert bad not in tags, "纯器乐 tags 混入了唱法描述: %s" % bad
|
||||
# 器乐正向要求必须在(这里只校验铁律常量层,风格字典自带的配器词不算)
|
||||
for kw in ("long instrumental passages", "extended instrumental breakdown",
|
||||
"huge powerful drop", "instrumental sections are the main focus",
|
||||
"drums and bass carry the track"):
|
||||
assert kw in tags, "纯器乐 tags 缺少器乐主导描述 %s" % kw
|
||||
# 绝不能出现把器乐段填满的条款(这会让模型加人声)
|
||||
assert "no long empty instrumental" not in tags, \
|
||||
"纯器乐 tags 含 no long empty instrumental,会逼模型填人声"
|
||||
assert "no long empty instrumental" not in ironlaw.RULE_CHILL_MEMO, \
|
||||
"吟唱 tags 含 no long empty instrumental,会逼模型填人声"
|
||||
# 前奏不能是"先器乐后人声"那句:纯器乐里它等于命令模型把人声加回来
|
||||
assert "vocals enter only after" not in tags, \
|
||||
"纯器乐 tags 仍含 'vocals enter only after the instrumental hook'"
|
||||
assert "no breakdown" not in tags, \
|
||||
"纯器乐 tags 含 no breakdown,与 extended instrumental breakdown 冲突"
|
||||
# 歌词体必须是纯结构标记,没有可唱正文
|
||||
lyrics = ironlaw.build_inst_lyrics("huge supersaw", bars=8)
|
||||
assert lyrics.lstrip().startswith("[intro]"), "纯器乐歌词未以 [intro] 开头"
|
||||
body = meter.only_lyrics(lyrics)
|
||||
assert body.strip() == "", "纯器乐歌词体出现可唱正文: %r" % body[:80]
|
||||
assert meter.syllable(body) == 0, "纯器乐歌词体音节数非 0"
|
||||
# 结构段名不能是 [verse] / [chorus]:ACE-Step 见到这两个标记就会按
|
||||
# "有人唱的段落"来铺,即使下面只挂 [inst] 也容易把人声请回来。
|
||||
for bad_tag in ("[verse]", "[chorus]"):
|
||||
assert bad_tag not in lyrics.lower(), \
|
||||
"纯器乐歌词体含 %s,会重新诱导人声" % bad_tag
|
||||
for good_tag in ("[drop]", "[breakdown]", "[build]"):
|
||||
assert good_tag in lyrics.lower(), "纯器乐歌词体缺少 %s 段" % good_tag
|
||||
# 结构铁律照旧生效
|
||||
assert not ironlaw.check_rules(lyrics, tags), ironlaw.check_rules(lyrics, tags)
|
||||
# 密度闸门必须让行纯器乐
|
||||
cfg = loader.load({})
|
||||
errs, _ = do_validate(lyrics, tags, cfg,
|
||||
{"duration": 175, "bpm": 124}, {}, strict=False,
|
||||
instrumental=True)
|
||||
assert not errs, "纯器乐被密度闸门拦下: %s" % errs
|
||||
# chill 模式的稳定条款也不能再要求 no breakdown
|
||||
chill_tags = ironlaw.build_tags(style="future bass",
|
||||
extra="male low whisper vocal",
|
||||
chill_vocal=True)
|
||||
assert "no breakdown" not in chill_tags, \
|
||||
"chill tags 含 no breakdown,与 quiet breakdown 冲突"
|
||||
# 声线稳定性约束已被实测证明是反效果:标签里每多一句谈人声,
|
||||
# 人声越从 0 秒就进。chill 现在只保留乐器/调性的稳定性要求。
|
||||
assert "consistent vocal timbre" not in chill_tags, \
|
||||
"chill tags 又开始强调人声稳定性,实测会退回 0 秒开唱"
|
||||
assert "consistent instrumentation in every section" in chill_tags, \
|
||||
"chill tags 丢了配器稳定性约束"
|
||||
return [("纯器乐模式", "反人声标签自洽、无唱法对撞、无 no breakdown、"
|
||||
"结构体不诱发人声、密度闸门让行")]
|
||||
|
||||
|
||||
def _t_chill_delay():
|
||||
"""chill 模式:验证标签与结构两侧自洽(不退化成纯器乐,也不退回 0 秒开唱)。
|
||||
|
||||
五轮实测踩出来的结论:
|
||||
chill 1 8 小节前奏 + "vocals enter only after…"
|
||||
-> vocals stem 从 0.0 秒就有能量
|
||||
chill 2 标签加长到 16 小节 + "first 20 seconds are entirely instrumental"
|
||||
+ "the human voice does not enter until the second drop"
|
||||
-> 第一句仍然落 0.0 秒,标签侧时间约束全部无效
|
||||
chill 3 结构重排 + 强人声描述
|
||||
-> 稀疏低音语气词,能量 +45~50 dB,歌词重合 0.98
|
||||
(当前唯一确认真唱的一版)
|
||||
chill 4 标签彻底不讨论人声(复用纯器乐反人声条款)
|
||||
-> 人声归零,等同纯器乐
|
||||
chill 5 只保留音色质感,删掉密度/时机描述
|
||||
-> 人声同样归零。此前把 ASR 的 4 段当成"少量真唱"是误判:
|
||||
文本 "oh new healing / lömpe Minor" 歌词里根本没有,
|
||||
能量只 +11 dB、ASR 覆盖率 100%,是持续 pad 幻听。
|
||||
|
||||
关键结论:标签侧写时长/时机对人声起点零作用;唯一旋钮是
|
||||
"密度与时机类人声描述的多少",而它降下去不是"延后"而是"消失"。
|
||||
所以自测只能守住"结构占位存在 + 标签不退回纯器乐",
|
||||
人声起点必须靠音频 QC(ab_vocal_curve.py / ab_is_real_voice.py)判定。
|
||||
"""
|
||||
tags = ironlaw.build_tags(style="English melodic future bass",
|
||||
instruments="huge supersaw lead",
|
||||
mood="vast uplifting emotional EDM",
|
||||
extra="male low whisper vocal, "
|
||||
"hushed and intimate, sparse English words",
|
||||
chill_vocal=True)
|
||||
# 人声仍然允许存在,不能把 chill 误改成纯器乐
|
||||
for kw in ("male low whisper vocal", "hushed and intimate",
|
||||
"low register vocal texture", "soft breathy phrases",
|
||||
"instrumental sections are the main focus",
|
||||
"long instrumental passages",
|
||||
"instrumental breakdown and instrumental drop sections"):
|
||||
assert kw in tags, "chill tags 缺少器乐主导描述 %s" % kw
|
||||
# 人声禁落器乐段
|
||||
for kw in ("no singing in any breakdown", "no singing over the first drop",
|
||||
"no long vocal phrases", "no choir"):
|
||||
assert kw in tags, "chill tags 缺少段落归属约束 %s" % kw
|
||||
# chill 不能退回普通前奏规则:那句只说"稍后进人声",实测拦不住
|
||||
assert "vocals enter only after the instrumental hook has played" not in tags, \
|
||||
"chill tags 退回 RULE_INTRO,实测 0 秒开唱"
|
||||
|
||||
# ---- 五轮实测收敛出来的核心断言 ----
|
||||
# InstLowChill / 2 / 3 / 4 / 5 五版量出一条干净的梯度,证明 prompt 对
|
||||
# 人声的控制力是真实存在的,旋钮 = "标签里有多少句在描述人声":
|
||||
# chill 2 强人声描述 + 16 小节前奏
|
||||
# -> ASR 23 段,voc/acc 0.483,人声压过音乐
|
||||
# chill 3 结构重排 + 强人声描述
|
||||
# -> 稀疏低音语气词,voc/acc 0.376,唯一确认真唱的一版
|
||||
# chill 4 标签彻底不讨论人声(复用纯器乐反人声条款)
|
||||
# -> 人声归零,等同纯器乐
|
||||
# chill 5 只留音色质感,删掉密度/时机描述
|
||||
# -> 人声同样归零(ASR 全是持续 pad 幻听)
|
||||
#
|
||||
# 时机类措辞必须继续禁掉:chill 2/3 都写了
|
||||
# "second half / enters late / first 20 seconds",第一句全部落 0.0 秒。
|
||||
for bad in ("voice enters", "the human voice", "no voice in the first",
|
||||
"until the second drop", "in the second half of the track",
|
||||
"first 20 seconds"):
|
||||
assert bad not in tags.lower(), \
|
||||
"chill tags 又开始讨论人声进场时机(%s),实测会退回 0 秒开唱" % bad
|
||||
|
||||
# 密度类措辞必须保留:这是 chill 3 唯一产生真唱的东西,
|
||||
# 删掉(chill 5)人声直接归零,不是延后而是消失。
|
||||
for kw in ("voice is sparse and low volume", "used only as sparse texture",
|
||||
"sparse english words", "mostly instrumental between phrases"):
|
||||
assert kw in tags.lower(), \
|
||||
"chill tags 丢了密度触发词 %s,实测会退回纯器乐" % kw
|
||||
|
||||
# 风格字典的 extra 绝不能混进来(六句里五句在谈人声)。
|
||||
# 这里不再靠关键词黑名单(因为 chill 自己现在也用同几个密度词,
|
||||
# 黑名单会误伤),改用哨兵值:故意传一个只在 extra 里的短语,
|
||||
# 结果里出现就说明 extra 泄漏了。
|
||||
probe = "zzz-extra-leak-probe"
|
||||
tags_probe = ironlaw.build_tags(style="s",
|
||||
instruments="i", mood="m",
|
||||
extra=probe,
|
||||
chill_vocal=True)
|
||||
assert probe not in tags_probe.lower(), "chill 分支把风格字典 extra 混进来了"
|
||||
|
||||
# 但也不能把人声彻底禁掉,否则就是纯器乐、不是吟唱。
|
||||
# 注意只匹配纯器乐专属的反人声条款:
|
||||
# - 不能用裸 "no singing":"no singing in any breakdown" 含这个子串,
|
||||
# 那是吟唱要的人声约束。
|
||||
# - 不能用 "instrumental only":它来自 "intro is instrumental only",
|
||||
# 那是前奏必须保留的约束。
|
||||
for kw in ("no vocals anywhere", "no voice at all", "no lead vocal",
|
||||
"no backing vocal", "this is a backing track",
|
||||
"drums bass synths chords and lead melodies only"):
|
||||
assert kw not in tags.lower(), \
|
||||
"chill tags 含纯器乐条款 %s,吟唱被人声禁令吞掉" % kw
|
||||
|
||||
return [("吟唱标签梯度",
|
||||
"时机类措辞全部禁掉,密度触发词保留,"
|
||||
"风格字典 extra 未泄漏")]
|
||||
|
||||
|
||||
def _t_groove_first_beat():
|
||||
"""器乐前奏必须从第 0 秒就有底鼓和贝斯,不能是空场。
|
||||
|
||||
这里有一个被实测推翻过的旧结论,必须留在注释里免得又走回去:
|
||||
上一轮认定"前 15 秒是空场",依据是 ab_lowfreq.py 报的
|
||||
-68~-75 dBFS。那份数据是错的——它把滤波器的瞬态响应和单帧跳变
|
||||
当成了整段电平。ab_bands.py / ab_head_rhythm.py 复测(dBFS,
|
||||
head = 前 16 秒,mid = 16~32 秒):
|
||||
|
||||
GroovePure head sub -66.0 / mid sub -55.8
|
||||
head full -32.7 / mid full -25.0
|
||||
|
||||
也就是说开头有音乐(full band 只低 7.7 dB,hat/snare 瞬态齐备),
|
||||
缺的只是 sub/kick。所以真正要修的是"前奏要有节奏组",
|
||||
不是"开头别是空场",而"空场"这个说法本身就是误判。
|
||||
|
||||
修法(见 ironlaw.intro_rhythm_only):前奏括号只保留节奏组,
|
||||
剔除 pad / riser / reverb / lead。这一条比堆禁令有效——
|
||||
GroovePure 已经证明四条禁令(no ambient intro / no pad only intro /
|
||||
no beatless intro / no white noise riser in the opening)全部无效。
|
||||
"""
|
||||
for flag, kw in ((False, "instrumental"), (True, "chill")):
|
||||
tags = ironlaw.build_tags(style="English melodic future bass",
|
||||
instruments="huge supersaw lead",
|
||||
mood="vast uplifting emotional EDM",
|
||||
chill_vocal=flag,
|
||||
instrumental=not flag)
|
||||
for need in ("full four on the floor groove from the very first beat",
|
||||
"kick drum, sub bass and walking bass on bar one",
|
||||
"no pad only intro", "no beatless intro",
|
||||
"no riser in the opening"):
|
||||
assert need in tags.lower(), \
|
||||
"%s tags 缺第一拍 groove 约束: %s" % (kw, need)
|
||||
|
||||
# 普通有声歌曲也一并受益:RULE_INTRO 同样要保证鼓从第一拍进。
|
||||
assert "rhythmic drums from the very first beat" in \
|
||||
ironlaw.build_tags("test").lower(), "普通歌曲前奏丢了第一拍底鼓"
|
||||
|
||||
return [("第一拍 groove",
|
||||
"chill/纯器乐都要求底鼓+贝斯从第 0 拍进,"
|
||||
"并点名禁掉 pad only / beatless / riser 空场信号")]
|
||||
|
||||
|
||||
def _t_intro_instrument_filter():
|
||||
"""前奏括号必须过滤掉 pad/riser/reverb/lead,只留节奏组。
|
||||
|
||||
这是本轮真正起作用的改动。上一轮只在标签里堆禁令,GroovePure 实测
|
||||
无效(开头 sub 仍 -66.0 dBFS,中段 -55.8 dBFS),因为真正的病灶是
|
||||
歌词体那一行:
|
||||
[Instrumental: 8 bar instrumental intro hook, huge supersaw lead,
|
||||
sidechain compressed pad, punchy electro kick, crisp clap,
|
||||
trap snare, white noise riser, big reverb, melodic drop, ...]
|
||||
空场信号(lead/pad/riser/reverb)4 个,节奏组(kick/clap/snare)3 个,
|
||||
3:4 输了,模型就把前奏铺成 ambient。
|
||||
|
||||
所以这里守三件事:
|
||||
1) 过滤后必须还留着 kick(不能把节奏组也误删成空括号);
|
||||
2) pad / riser / reverb / lead 一个都不能留在前奏括号里;
|
||||
3) 标签侧的 instruments 段保持原样——drop 段本来就要 pad 和 lead,
|
||||
只过滤前奏括号,不能把整首歌的配器改掉。
|
||||
"""
|
||||
src = ("huge supersaw lead, sidechain compressed pad, "
|
||||
"punchy electro kick, crisp clap, trap snare, "
|
||||
"white noise riser, big reverb, melodic drop")
|
||||
got = ironlaw.intro_rhythm_only(src)
|
||||
low = got.lower()
|
||||
|
||||
assert "kick" in low, "前奏括号把底鼓也过滤掉了: %r" % got
|
||||
for bad in ("pad", "riser", "reverb", "supersaw", "lead", "noise"):
|
||||
assert bad not in low, "前奏括号残留空场信号 %s: %r" % (bad, got)
|
||||
|
||||
# 加法兜底:kick 和 bass 两条必须显式出现在前奏括号里。
|
||||
# 上一轮漏了这一条,风格字典里一条贝斯都没写,过滤后只剩三条中高频,
|
||||
# 结果两版新音频开头 sub 比全曲峰值低 53.5 / 31.2 dB。
|
||||
assert "bass" in low, "前奏括号没有 bass:%r" % got
|
||||
|
||||
# 空 / 中文 / 全被过滤掉 -> 必须回落到默认节奏组,不能留空括号
|
||||
for probe in ("", None, "ambient pad, huge supersaw lead, big reverb"):
|
||||
fb = ironlaw.intro_rhythm_only(probe)
|
||||
assert "kick" in fb.lower(), "前奏括号回落失败 %r -> %r" % (probe, fb)
|
||||
assert "bass" in fb.lower(), "回落值缺 bass %r -> %r" % (probe, fb)
|
||||
for bad in ("pad", "riser", "reverb", "supersaw", "lead"):
|
||||
assert bad not in fb.lower(), "回落值带空场信号 %s: %r" % (bad, fb)
|
||||
|
||||
# 回落值不能是 DEFAULT_INSTRUMENT:它含 "chord stabs",而 "chord stab"
|
||||
# 就在 _RHYTHM_DROP 里,回落会把刚剔除掉的空场信号塞回前奏。
|
||||
assert "stab" not in ironlaw.DEFAULT_INTRO_RHYTHM.lower(), \
|
||||
"前奏回落值含 chord stab,与过滤规则自相矛盾"
|
||||
|
||||
# 标签侧不受影响:instruments 段必须还是原样
|
||||
tags = ironlaw.build_tags(style="s", instruments=src, mood="m",
|
||||
instrumental=True)
|
||||
assert "white noise riser" in tags, \
|
||||
"过滤逻辑泄漏到了标签侧,drop 段会丢掉 riser"
|
||||
assert "sidechain compressed pad" in tags, \
|
||||
"过滤逻辑泄漏到了标签侧,drop 段会丢掉 pad"
|
||||
|
||||
# 歌词体里前奏那一行必须既没有 riser/pad,又必须有 bass
|
||||
lyr = ironlaw.build_inst_lyrics(src, bars=8)
|
||||
head = lyr.split("\n")[1].lower()
|
||||
assert "riser" not in head and "pad" not in head, \
|
||||
"纯器乐歌词体前奏行仍带空场信号: %s" % head
|
||||
assert "bass" in head, "纯器乐歌词体前奏行缺 bass: %s" % head
|
||||
lyr2 = ironlaw.build_chill_lyrics([("[verse]", "ooh")], src, bars=8,
|
||||
total_bars=90)
|
||||
head2 = lyr2.split("\n")[1].lower()
|
||||
assert "riser" not in head2 and "pad" not in head2, \
|
||||
"吟唱歌词体前奏行仍带空场信号: %s" % head2
|
||||
assert "bass" in head2, "吟唱歌词体前奏行缺 bass: %s" % head2
|
||||
|
||||
return [("前奏配器过滤",
|
||||
"前奏括号剔除 pad/riser/reverb/lead,并强制补齐 kick+bass,"
|
||||
"标签侧配器保持原样")]
|
||||
|
||||
|
||||
def _t_chill_no_conflict():
|
||||
"""chill 标签里绝不能出现和"低音英文语气词"直接冲突的禁令。
|
||||
|
||||
这三条(no humming / no mm mm syllables / no oh oh syllables)第一次改
|
||||
chill 铁律就是为了解开这个冲突,加回来等于自己把要求唱的内容禁掉。
|
||||
"""
|
||||
tags = ironlaw.build_tags(style="s", instruments="i", mood="m",
|
||||
chill_vocal=True)
|
||||
for bad in ("no humming", "no mm mm syllables", "no oh oh syllables",
|
||||
"no droning sustained vowels"):
|
||||
assert bad not in tags.lower(), \
|
||||
"chill tags 含 %s,与要求的低音语气词直接冲突" % bad
|
||||
# 反过来,"稀疏低音语气词"必须真的在标签里,否则就退化成纯器乐了。
|
||||
for need in ("male low whisper vocal", "low register vocal texture",
|
||||
"soft breathy phrases"):
|
||||
assert need in tags.lower(), "chill tags 丢了低音唱腔描述: %s" % need
|
||||
|
||||
return [("吟唱无冲突",
|
||||
"no humming / no mm mm / no oh oh 三条禁令未回归")]
|
||||
|
||||
|
||||
def _t_chill_integration():
|
||||
"""chill 集成路径:assemble + check_rules 必须同时通过。
|
||||
|
||||
上一轮 14/14 全绿却照样生成失败,原因是这个自测只调 build_tags(),
|
||||
没走 assemble() + check_rules(),于是标签与歌词头小节数不一致这种
|
||||
集成冲突根本测不出来。集成层的坑必须在这里测。
|
||||
"""
|
||||
cfg = loader.load({})
|
||||
st = {"bpm": 124, "duration": 175, "key": "A minor",
|
||||
"style": "English melodic future bass",
|
||||
"instruments": "huge supersaw lead",
|
||||
"mood": "vast uplifting emotional EDM",
|
||||
"extra": "male low whisper vocal, sparse English words"}
|
||||
|
||||
class _A(object):
|
||||
chill_vocal = True
|
||||
instrumental = False
|
||||
|
||||
raw = open(_CHILL_DEMO, encoding="utf-8").read()
|
||||
tags2, lyrics2 = assemble(_A, cfg, st, raw, "selftest")
|
||||
errs = ironlaw.check_rules(lyrics2, tags2)
|
||||
assert not errs, "chill 集成校验不通过: %s" % errs
|
||||
|
||||
# 结构核心断言:第一个有人唱的段之前,纯器乐段必须占满前半段。
|
||||
# 注意这只保证"器乐占位存在",不保证人声起点被推后——实测两者无关。
|
||||
# 音频层是否真的晚进,必须用 ab_vocal_curve.py / ab_is_real_voice.py 验。
|
||||
lines = [l.strip() for l in lyrics2.split("\n") if l.strip()]
|
||||
vocal_tags = ("[verse]", "[chorus]", "[hook]", "[bridge]", "[pre-chorus]")
|
||||
first_v = next((i for i, l in enumerate(lines)
|
||||
if l.lower() in vocal_tags), None)
|
||||
assert first_v is not None, "chill 结构体里找不到任何人声段"
|
||||
head = [l.lower() for l in lines[:first_v]]
|
||||
# 只数"纯器乐段",不能把 [inst] 占位和段名一起算进去。
|
||||
# 旧写法把两者相加,一个 breakdown 组就被算成 2,报出"8 个器乐段",
|
||||
# 实际上只有 4 组——测试描述在骗人,验收语义必须按组算。
|
||||
inst_secs = [h for h in head
|
||||
if h in ("[breakdown]", "[build]", "[drop]", "[post-drop]")]
|
||||
n_inst = len(inst_secs)
|
||||
assert n_inst >= 4, \
|
||||
"chill 第一个器乐段之前只有 %d 组纯器乐段,人声会紧跟 intro 进来" % n_inst
|
||||
# 每个纯器乐段名后面必须真的挂 [inst],否则模型会当成有人唱的段
|
||||
for i, h in enumerate(head):
|
||||
if h in ("[breakdown]", "[build]", "[drop]", "[post-drop]"):
|
||||
assert i + 1 < len(head) and head[i + 1] == "[inst]", \
|
||||
"chill 器乐段 %s 后面没挂 [inst],会被当成可唱段" % h
|
||||
|
||||
# 器乐占位不能重复:原始歌词里的 [inst] 段已经重排过,
|
||||
# 再留一遍会让人声提前回来
|
||||
assert head.count("[inst]") <= n_inst, "chill 结构体器乐占位异常重复"
|
||||
|
||||
# 前奏小节数:标签与歌词头必须一致
|
||||
mh = re.search(r"\[instrumental:[^\]]*?(\d+)\s*bar", lyrics2, re.I)
|
||||
mt = re.search(r"(\d+)\s*bar instrumental intro", tags2.lower())
|
||||
assert mh and mt and mh.group(1) == mt.group(1), \
|
||||
"chill 标签 %s 小节与歌词头 %s 小节不一致" % (
|
||||
mt.group(1) if mt else "?", mh.group(1) if mh else "?")
|
||||
|
||||
# 反向闸门:两者不一致必须被拦下
|
||||
short_tags = tags2.replace("%s bar instrumental intro" % mt.group(1),
|
||||
"16 bar instrumental intro", 1)
|
||||
errs = ironlaw.check_rules(lyrics2, short_tags)
|
||||
assert any("必须一致" in e for e in errs), \
|
||||
"tags 与歌词头前奏小节数不一致未被拦截"
|
||||
|
||||
# 前奏小节数缺失也必须被拦
|
||||
errs = ironlaw.check_rules(lyrics2, re.sub(r"\d+\s*bar instrumental",
|
||||
"instrumental", tags2))
|
||||
assert any("小节数" in e for e in errs), "tags 未写前奏小节数未被拦截"
|
||||
|
||||
return [("吟唱结构占位",
|
||||
"人声段之前留了 %d 组纯器乐段(只管密度,不管起点),"
|
||||
"标签侧完全不讨论人声进场时机" % n_inst)]
|
||||
|
||||
|
||||
def _t_lexicon():
|
||||
lex = loader.lexicon()
|
||||
assert "长板" in (lex["banned"].get("longboard") or []), "禁用词表缺 长板"
|
||||
good = "[intro]\n[Instrumental: 8 bar]\n[verse]\n我在街头双翘上起跳"
|
||||
assert not ironlaw.check_lexicon(good, lex), ironlaw.check_lexicon(good, lex)
|
||||
bad = "[intro]\n[verse]\n我戴着头盔冲下坡"
|
||||
errs = ironlaw.check_lexicon(bad, lex)
|
||||
assert errs, "禁用词未被拦截"
|
||||
return [("身份词典", "街头双翘通过 / 长板类禁用词 %d 条被拦" % len(errs))]
|
||||
|
||||
|
||||
def _t_density():
|
||||
cfg = loader.load({})["lyrics"]
|
||||
st = loader.styles()["pop"]
|
||||
cap = meter.capacity(st["duration"], st["bpm"], cfg)
|
||||
assert cap["hi"] > cap["lo"] > 0, "容量区间异常"
|
||||
|
||||
# 过短必须被拦
|
||||
too_thin = "[intro]\n[Instrumental: 8 bar]\n[verse]\n太短了"
|
||||
assert meter.check(too_thin, st["duration"], st["bpm"], cfg)[0], "过短未拦"
|
||||
|
||||
# 过多必须被拦
|
||||
too_fat = "[intro]\n[Instrumental: 8 bar]\n[verse]\n" + \
|
||||
"\n".join(["这一句有十二个字刚刚好"] * 300)
|
||||
assert meter.check(too_fat, st["duration"], st["bpm"], cfg)[0], "过多未拦"
|
||||
|
||||
# 按 target_words 反推合规歌词,必须放行(防止阈值过严误杀)
|
||||
mid, lo, hi = meter.target_words(st["duration"], st["bpm"], cfg)
|
||||
line = "这一句刚好十二个字的样子" # 12 音节
|
||||
nlines = max(1, int(mid / 12))
|
||||
body = "\n".join([line] * nlines)
|
||||
ok = "[intro]\n[Instrumental: 8 bar]\n[verse]\n" + body
|
||||
errs, stt = meter.check(ok, st["duration"], st["bpm"], cfg)
|
||||
assert not errs, "按 target 写的合规歌词被误杀: %s" % errs
|
||||
return [("时长对应硬校验",
|
||||
"%ds/%dBPM 容量 %d~%d,目标 %d 字,过短/过多均被拦,合规放行"
|
||||
% (st["duration"], st["bpm"], cap["lo"], cap["hi"], mid))]
|
||||
|
||||
|
||||
def _t_regression():
|
||||
"""实歌回归:8 首用户已生成并认可的歌必须全部通过密度校验。
|
||||
|
||||
这是阈值校准的锚点。原来按理论值(每拍2.6~4.4音节、每句下限7)设阈值,
|
||||
结果这 8 首全部被判"歌词过少"(填充率只有 26%~43%),说明阈值从未
|
||||
对齐过真实输出。任何阈值改动都必须先过这一关。
|
||||
"""
|
||||
old_dir = r"c:\Users\孙文龙\.trae-cn\work\6ab523de391489c168050edc"
|
||||
if not os.path.isdir(old_dir):
|
||||
return [("实歌回归", "FAIL 找不到 remake_v5.py 目录")]
|
||||
sys.path.insert(0, old_dir)
|
||||
try:
|
||||
import remake_v5 as old
|
||||
except Exception as e:
|
||||
return [("实歌回归", "FAIL 无法导入 remake_v5 (%s)" % e)]
|
||||
|
||||
cfg = loader.load({})["lyrics"]
|
||||
bad = []
|
||||
worst_pl = 99.0
|
||||
for title, seed, bpm, key, dur, inst in old.META:
|
||||
lyrics = old.build_song(title)[2]
|
||||
errs, st = meter.check(lyrics, dur, bpm, cfg)
|
||||
worst_pl = min(worst_pl, st["per_line"])
|
||||
if errs:
|
||||
bad.append("%s: %s" % (title, errs[0].split(":")[0]))
|
||||
if bad:
|
||||
return [("实歌回归", "FAIL %d/%d 首被误杀 -> %s"
|
||||
% (len(bad), len(old.META), " | ".join(bad)))]
|
||||
assert worst_pl >= cfg["ideal_line_syllable"][0], \
|
||||
"实测最短平均句长 %.1f 已低于配置下限 %.1f,阈值与实歌脱节" % (
|
||||
worst_pl, cfg["ideal_line_syllable"][0])
|
||||
return [("实歌回归", "8 首已验证实歌全部通过(最短平均句长 %.1f 音节)"
|
||||
% worst_pl)]
|
||||
|
||||
|
||||
def _t_parity():
|
||||
"""与旧脚本 remake_v5.py 的 API prompt 逐字段对比。"""
|
||||
old_dir = r"c:\Users\孙文龙\.trae-cn\work\6ab523de391489c168050edc"
|
||||
sys.path.insert(0, old_dir)
|
||||
try:
|
||||
import remake_v5 as old
|
||||
except Exception as e:
|
||||
return [("旧脚本 parity", "跳过:无法导入 remake_v5 (%s)" % e)]
|
||||
|
||||
title = "身体先听懂"
|
||||
owf, otags, olyrics = old.build_song(title)
|
||||
oapi = old.convert(owf)
|
||||
|
||||
cfg = loader.load({})
|
||||
template = wfmod.load_template(cfg["paths"]["workflow_template"])
|
||||
m = [x for x in old.META if x[0] == title][0]
|
||||
_, oseed, obpm, okey, odur, oinst = m
|
||||
nwf = wfmod.build_workflow(
|
||||
template, title=title, tags=otags, lyrics=olyrics, seed=oseed,
|
||||
bpm=obpm, duration=odur, keyscale=okey, sampler=cfg["sampler"],
|
||||
encode=cfg["encode"], model=cfg["model"],
|
||||
output_prefix="MyMusic/parity_%s" % title, output_format="flac",
|
||||
language="zh", timesignature="4")
|
||||
napi = wfmod.render(nwf)
|
||||
|
||||
if set(oapi) != set(napi):
|
||||
return [("旧脚本 parity", "FAIL 节点集不同 %s vs %s"
|
||||
% (sorted(oapi), sorted(napi)))]
|
||||
|
||||
diffs = []
|
||||
for nid in sorted(oapi, key=int):
|
||||
for k in sorted(set(oapi[nid]["inputs"]) | set(napi[nid]["inputs"])):
|
||||
a = oapi[nid]["inputs"].get(k)
|
||||
b = napi[nid]["inputs"].get(k)
|
||||
if k in ("tags", "lyrics"):
|
||||
# 新版 build_tags 措辞不同,只校验关键铁律都在
|
||||
for kw in ("swlvoice", "8 bar instrumental intro hook"):
|
||||
if (kw in str(a)) != (kw in str(b)):
|
||||
diffs.append("节点%s.%s 铁律不一致" % (nid, k))
|
||||
continue
|
||||
if k == "filename_prefix":
|
||||
continue # 输出前缀本就故意不同
|
||||
if a != b:
|
||||
diffs.append("节点%s.%s: 旧=%r 新=%r" % (nid, k, a, b))
|
||||
if diffs:
|
||||
return [("旧脚本 parity", "FAIL %d 处不同:%s"
|
||||
% (len(diffs), " | ".join(diffs[:6])))]
|
||||
return [("旧脚本 parity", "核心 API payload 与 remake_v5.py 等价(%d 节点;"
|
||||
"filename_prefix 与歌词措辞为有意差异)" % len(oapi))]
|
||||
|
||||
|
||||
def _t_fallback():
|
||||
"""fallback 资产必须仍在原位。"""
|
||||
root = pmod.comfy_root()
|
||||
need = [
|
||||
os.path.join(ROOT, "song_rules.py"),
|
||||
os.path.join(ROOT, "urbanhiphop-身体先听懂.json"),
|
||||
pmod.output_dir(root, "MyMusic"),
|
||||
]
|
||||
missing = [p for p in need if not os.path.exists(p)]
|
||||
bak_root = os.path.join(ROOT, "bak")
|
||||
stamps = sorted(os.listdir(bak_root)) if os.path.isdir(bak_root) else []
|
||||
if missing:
|
||||
return [("fallback 资产", "FAIL 缺失 %s" % missing)]
|
||||
if not stamps:
|
||||
return [("fallback 资产", "FAIL bak 目录为空")]
|
||||
return [("fallback 资产", "song_rules.py / 模板 / output/MyMusic 就位,bak %d 份"
|
||||
% len(stamps))]
|
||||
|
||||
|
||||
def _t_window():
|
||||
"""target_words 报的区间必须与 check 实际放行窗口一致。
|
||||
|
||||
这是最容易出"文档说能过、实际过不了"的地方:容量 [lo,hi] 是理论值,
|
||||
但 check 还叠加了填充率红线,真正生效的窗口必须两者取交。
|
||||
"""
|
||||
cfg = loader.load({})["lyrics"]
|
||||
for name in ("pop", "punk", "urbanhiphop"):
|
||||
st = loader.styles()[name]
|
||||
dur, bpm = st["duration"], st["bpm"]
|
||||
mid, lo, hi = meter.target_words(dur, bpm, cfg)
|
||||
assert lo <= mid <= hi, "%s 目标字数 %d 不在放行区间 %d~%d" % (
|
||||
name, mid, lo, hi)
|
||||
|
||||
cap = meter.capacity(dur, bpm, cfg)
|
||||
assert cap["lo"] <= hi and lo <= cap["hi"], \
|
||||
"%s 放行区间 %d~%d 超出理论容量 %d~%d" % (
|
||||
name, lo, hi, int(cap["lo"]), int(cap["hi"]))
|
||||
|
||||
# 窗口两端必须真的被 check 拦/放,边界不能只存在于文档里
|
||||
line = "这一句刚好十二个字的样子"
|
||||
|
||||
def build(n):
|
||||
return ("[intro]\n[Instrumental: 8 bar]\n[verse]\n"
|
||||
+ "\n".join([line] * n))
|
||||
|
||||
n_mid = max(1, int(mid / 12))
|
||||
ok_errs, _ = meter.check(build(n_mid), dur, bpm, cfg)
|
||||
assert not ok_errs, "%s 按目标字数写的歌词被误杀: %s" % (name, ok_errs)
|
||||
|
||||
# 少一个字就该报"过少",多一个字就该报"过多",容差必须小
|
||||
few = meter.check(build(max(1, int((lo - 30) / 12))), dur, bpm, cfg)[0]
|
||||
assert any("过少" in e for e in few), \
|
||||
"%s 低于放行下限却未报过少: %s" % (name, few)
|
||||
many = meter.check(build(int((hi + 30) / 12) + 1), dur, bpm, cfg)[0]
|
||||
assert any("过多" in e for e in many), \
|
||||
"%s 高于放行上限却未报过多: %s" % (name, many)
|
||||
return [("字数窗口自洽",
|
||||
"pop/punk/urbanhiphop 的 target 区间 == check 放行窗口,边界确实生效")]
|
||||
|
||||
|
||||
def cmd_selftest(args):
|
||||
from .common import session_logger
|
||||
cfg = loader.load({})
|
||||
run, log = session_logger(args, cfg, "selftest")
|
||||
cases = [_t_runid, _t_paths, _t_path_layout, _t_config_priority,
|
||||
_t_model_locked, _t_ironlaw,
|
||||
_t_instrumental, _t_chill_delay, _t_groove_first_beat,
|
||||
_t_intro_instrument_filter, _t_chill_no_conflict,
|
||||
_t_chill_integration,
|
||||
_t_sparse_profile, _t_lexicon,
|
||||
_t_density, _t_window, _t_regression, _t_parity, _t_fallback]
|
||||
npass = 0
|
||||
print("== songforge selftest ==")
|
||||
log.info("selftest 开始", cases=len(cases))
|
||||
for fn in cases:
|
||||
try:
|
||||
notes = fn()
|
||||
for name, detail in notes:
|
||||
# 测试函数用返回文本表达失败时,必须显式识别,不能算作通过
|
||||
bad = bool(re.search(r"\bFAIL\b|\bERR\b|失败", str(detail)))
|
||||
print(" [%s] %-22s %s"
|
||||
% ("FAIL" if bad else "ok", name, detail))
|
||||
if bad:
|
||||
raise AssertionError(detail)
|
||||
npass += 1
|
||||
except AssertionError as e:
|
||||
print(" [--] %-22s %s" % (fn.__name__[3:], e or "断言失败"))
|
||||
log.warn("case 失败", case=fn.__name__, err=str(e))
|
||||
except Exception as e:
|
||||
print(" [ERR] %-22s %s: %s"
|
||||
% (fn.__name__[3:], type(e).__name__, e))
|
||||
log.error("case 异常", case=fn.__name__, err=str(e))
|
||||
print("== 结果 ==")
|
||||
print(" %d/%d 通过" % (npass, len(cases)))
|
||||
log.info("selftest 结束", passed=npass, total=len(cases), log_dir=run.dir)
|
||||
log.close()
|
||||
return 0 if npass == len(cases) else 1
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- parser
|
||||
|
||||
Reference in New Issue
Block a user