From 6d79ab93a2b107842cf5300f924e055ce4212ce4 Mon Sep 17 00:00:00 2001 From: swl Date: Sat, 10 Oct 2026 13:03:44 +0800 Subject: [PATCH] Add align/recheck/compare, plugins, and refresh Agent docs. Wire deliver_report/listen_pack/retry hooks; gen --auto-retry and intro light check; make AGENTS_MIN the up-to-date agent entry with full command map. Co-authored-by: Cursor --- .cursor/rules/songforge.mdc | 10 +- AGENTS.md | 11 +- AGENTS_MIN.md | 117 +++++++------- README.md | 16 +- cli/commands.py | 61 +++++++- cli/extras.py | 196 ++++++++++++++++++++++++ cli/main.py | 9 +- cli/qc.py | 13 ++ cli/selftest.py | 24 ++- docs/使用手册.md | 12 +- lyrics/align.py | 92 +++++++++++ plugins/README.md | 40 +++-- plugins/deliver_report/__init__.py | 1 + plugins/deliver_report/plugin.py | 84 ++++++++++ plugins/hooks.py | 4 +- plugins/listen_pack/__init__.py | 1 + plugins/listen_pack/plugin.py | 60 ++++++++ plugins/retry_on_intro_fail/__init__.py | 1 + plugins/retry_on_intro_fail/plugin.py | 54 +++++++ tools/intro_check_worker.py | 65 ++++++++ 20 files changed, 780 insertions(+), 91 deletions(-) create mode 100644 cli/extras.py create mode 100644 lyrics/align.py create mode 100644 plugins/deliver_report/__init__.py create mode 100644 plugins/deliver_report/plugin.py create mode 100644 plugins/listen_pack/__init__.py create mode 100644 plugins/listen_pack/plugin.py create mode 100644 plugins/retry_on_intro_fail/__init__.py create mode 100644 plugins/retry_on_intro_fail/plugin.py create mode 100644 tools/intro_check_worker.py diff --git a/.cursor/rules/songforge.mdc b/.cursor/rules/songforge.mdc index e57d948..07e035c 100644 --- a/.cursor/rules/songforge.mdc +++ b/.cursor/rules/songforge.mdc @@ -7,10 +7,10 @@ alwaysApply: true # songforge -1. **先读** `AGENTS_MIN.md`;不要默认加载整份 `AGENTS.md` / `ironlaw.py` / `qc_worker.py`。 -2. 做歌流水线:`doctor_all` → `target` → 写词 → `check` → `gen` → `qc --baseline`。结束后按 `AGENTS_MIN.md`「生歌报告」模板向用户汇报。 -3. 音频探测 / 转码 / 截取:只用 `songforge media info|convert|clip`(经 qc_python)。 +1. **进仓先读** `AGENTS_MIN.md`(唯一默认入口)。不要默认加载整份 `AGENTS.md` / `ironlaw.py` / `qc_worker.py`。 +2. 流水线:`doctor_all` → `target` →(可选 `align`)→ `check` → `gen`(可 `--auto-retry`)→ `recheck` → `qc --baseline` →(可选 `compare`)。结束后按 MIN「生歌报告」汇报;优先看 run 目录 `report.md` / `listen/`。 +3. 音频只用 `media` / `recheck` / `qc`(经 qc_python)。扩展写 `plugins/`,见 `plugins/README.md`。 4. 缺 `local.toml`、Comfy 不通、风格/歌名不清、想改模型或 QC 阈值 → **先问用户**。 -5. **改代码铁律**:发现需改仓库代码时,先说明缺口与范围并**征求同意**;建议用户用 git 分支/commit/stash 备份,或新写插件/旁路少动核心;再问是否 commit、是否 push、推哪个 remote。未获许可不得改核心、不得提交/推送。用户同意收尾时再 commit+push。 -6. 禁止开曲衬词;默认 lean tags;**禁止**在 tags 点名 mm/ooh/嗯 等音节。扩展写 `plugins/`,少改 `core/`/`rules/`。 +5. **改代码铁律**:先说明缺口与范围并征求同意;建议 git 备份或新插件;再问是否 commit/push、推哪个 remote。未许可不得改核心、不得提交/推送。 +6. 禁止开曲衬词;默认 lean tags;禁止在 tags 点名 mm/ooh/嗯。 7. 勿提交 `.secrets/`、`config/local.toml`、`.venv/`;勿 `--force` push。 diff --git a/AGENTS.md b/AGENTS.md index 88f4a9f..9e56ab3 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -125,14 +125,19 @@ doctor → target → 写歌词 → check → gen → qc --baseline ```powershell .\.venv\Scripts\python.exe songforge.py doctor .\.venv\Scripts\python.exe songforge.py target --style pop --bpm 104 --dur 180 -# 按 target 区间写歌词到 input/lyrics/xxx.txt(勿写 [intro] / tags) +# 按 target 写词;密度不够用 align(勿写 [intro] / tags) +.\.venv\Scripts\python.exe songforge.py align --style pop --lyrics-file input\lyrics\xxx.txt --bpm 104 --dur 180 .\.venv\Scripts\python.exe songforge.py check --style pop --title 歌名 --lyrics-file input\lyrics\xxx.txt --bpm 104 --dur 180 -.\.venv\Scripts\python.exe songforge.py gen --style pop --title 歌名 --lyrics-file input\lyrics\xxx.txt --bpm 104 --dur 180 +.\.venv\Scripts\python.exe songforge.py gen --style pop --title 歌名 --lyrics-file input\lyrics\xxx.txt --bpm 104 --dur 180 --auto-retry 2 +.\.venv\Scripts\python.exe songforge.py recheck --run .\.venv\Scripts\python.exe songforge.py qc --run --baseline +.\.venv\Scripts\python.exe songforge.py compare --runs , .\.venv\Scripts\python.exe songforge.py catalog --limit 20 ``` -- `check` 不过 → **只改歌词** +插件产物:`report.md`、`listen/`、`intro_check.json`、`retry_hint.json`(见 `plugins/README.md`)。 + +- `check` 不过 → **只改歌词**(或 `align`) - `doctor` 服务不可达 → 请用户启动 ComfyUI;核对 `local.toml` - `qc` 必须带 `--baseline`;`--run` 可用 run_id 或 `data/runs/...` 路径 - 索引:`catalog` / `catalog --rebuild` diff --git a/AGENTS_MIN.md b/AGENTS_MIN.md index b9fc3ed..d5c18b4 100644 --- a/AGENTS_MIN.md +++ b/AGENTS_MIN.md @@ -1,42 +1,60 @@ # songforge — Agent 极简卡(默认只读这份) -> 省 token:**先读本文**。失败 / 改铁律 / 深挖时再打开 [`AGENTS.md`](AGENTS.md)。 -> **禁止**把 `ironlaw.py` / `qc_worker.py` 整文件塞进上下文;用 CLI 输出代替。 +> 省 token:**进仓先读本文**。失败 / 改铁律 / 深挖再打开 [`AGENTS.md`](AGENTS.md)。 +> **禁止**整文件粘贴 `ironlaw.py` / `qc_worker.py`;用 CLI 输出代替。 + +## 进仓 30 秒 + +```powershell +cd +.\scripts\doctor_all.ps1 +.\.venv\Scripts\python.exe songforge.py --help +``` + +本机路径:`config/local.toml`(缺则从 `.example` 复制)。算法:`config/default.toml`。 ## 交付 硬条件:`gen` 的 run_id + flac + `qc --baseline` → **PASS**。 -`style_fam` / `filler_ratio` 只是弱参考;**不要**对外说「风格已验证」。 +`style_fam` / 全曲 `filler_ratio` 弱参考;**开曲衬词**(`intro_filler` / `recheck`)是硬门。 +**不要**说「风格已验证」。 -生歌结束后,用下面模板向用户汇报(缺项写 `—`,FAIL 时停在「结论」并说明下一步): +生歌结束后用此模板汇报(缺项写 `—`): ```text ## 生歌报告 - 结论:PASS | FAIL - 歌名 / 风格 / BPM / 设定时长:… - run_id:… -- 音频路径:…(flac;可用 media info 补实测时长) -- 歌词:路径 + check 密度结果(音节/区间) -- QC:verdict;时长;实测 BPM(±18%);voice_nn;开曲衬词(intro_filler,硬门);filler_ratio(全曲弱参考) -- 弱参考(勿当闸门):style_fam / style_conf;全曲 filler_ratio -- 模型/采样:ckpt+LoRA(0.5/0.5);shift/steps/cfg/temperature(应与铁律一致) -- 种子 / 备注:…(重跑或异常时) -- 建议:听哪一段;是否重 gen / 只改词;是否 commit(若改过仓) +- 音频路径:…(flac;可用 media info) +- 歌词:路径 + check / align 密度 +- QC:verdict;BPM;voice_nn;开曲衬词;filler_ratio(弱) +- 产物:report.md;listen/(PASS 后);intro_check.json +- 建议:听 listen/;FAIL 换 seed(--auto-retry)或改词 ``` -## 流水线 +插件会在 run 目录自动写 `report.md`(`deliver_report`)。 + +## 流水线(照这个跑) ```text -doctor_all → target → 写歌词 → check → gen → qc --baseline +doctor_all → target → [align] → check → gen [--auto-retry N] → recheck → qc --baseline → compare? ``` -仓根: - ```powershell -.\scripts\doctor_all.ps1 -.\.venv\Scripts\python.exe songforge.py <命令> +.\.venv\Scripts\python.exe songforge.py target --style pop --bpm 104 --dur 180 +# 密度不够时一键对齐(复用已有行,不造新词): +.\.venv\Scripts\python.exe songforge.py align --style pop --lyrics-file input\lyrics\xxx.txt --bpm 104 --dur 180 +.\.venv\Scripts\python.exe songforge.py check --style pop --title 歌名 --lyrics-file input\lyrics\xxx.txt --bpm 104 --dur 180 +.\.venv\Scripts\python.exe songforge.py gen --style pop --title 歌名 --lyrics-file input\lyrics\xxx.txt --bpm 104 --dur 180 --auto-retry 2 +.\.venv\Scripts\python.exe songforge.py recheck --run +.\.venv\Scripts\python.exe songforge.py qc --run --baseline +.\.venv\Scripts\python.exe songforge.py compare --runs , ``` +仓根入口:`.\.venv\Scripts\python.exe songforge.py <命令>` +嵌套 ComfyUI 时:`cd \user\default\workflows` 后 `python songforge\songforge.py …` + ## 硬锁定 | 项 | 值 | @@ -44,57 +62,52 @@ doctor_all → target → 写歌词 → check → gen → qc --baseline | ckpt+LoRA | `ace_step_1.5_turbo_swl_fused` + `swl_voice_best_ep257`(0.5/0.5) | | 采样 | shift=5.5 / steps=28 / cfg=2.0 / temperature=0.68 | | 时长 | 150–200s;8 小节器乐前奏(CLI 注入) | -| 衬音 | **禁止开曲衬词**(嗯/呀/ooh…);默认 lean tags;QC 前奏窗衬词硬 FAIL | -| tags | `prefer_lean_tags=true`;勿 `--full-tags`(易诱发开曲衬词);禁点名音节 | +| 衬音 | 禁止开曲衬词;默认 lean tags;禁点名 mm/ooh/嗯 | +| tags | `prefer_lean_tags=true`;勿随意 `--full-tags` | -本机路径:`config/local.toml`。算法:`config/default.toml`。 +## 命令速查(Agent 常用) -## 微内核目录 +| 命令 | 用途 | +|------|------| +| `doctor` / `doctor_all.ps1` | 体检 | +| `target` | 查字数窗口 | +| `align` | 歌词音节对齐放行窗 | +| `check` | 本地校验 | +| `gen` | 生成;`--auto-retry N` 开曲 FAIL 换 seed;`--skip-recheck` 跳过轻检 | +| `recheck` | 开曲轻检(前奏窗 ASR) | +| `qc --baseline` | 全量验收(必带 baseline) | +| `compare --runs a,b` | 多 run 对比 | +| `media info\|convert\|clip` | 音频窄接口 | +| `catalog` / `prune` | 索引 / 清 tmp | + +## 目录(微内核) | 层 | 路径 | |----|------| -| 内核 | `core/`(引擎/路径/workflow)、`rules/`、`cli/` | -| 插件 | **`plugins/`**(钩子总线 + `builtin/` + 第三方) | -| 数据 | `input/lyrics/`、`data/runs/`、`config/` | +| 内核 | `core/` `rules/` `cli/` `lyrics/` | +| **插件** | **`plugins/`** ← 扩展写这里 | +| 数据 | `input/lyrics/` `data/runs/` `config/` | +| 文档 | **本文** → `AGENTS.md` → `docs/使用手册.md` → `plugins/README.md` | -扩展优先写 `plugins/.py` 或 `plugins//plugin.py`,少改内核。说明见 [`plugins/README.md`](plugins/README.md)。 +内置/包式插件:`intro_guard`、`deliver_report`、`listen_pack`、`retry_on_intro_fail`。 +钩子:`after_assemble` / `after_gen` / `after_qc`。详见 [`plugins/README.md`](plugins/README.md)。 -## 音频栈(librosa 等) +## 音频栈 -- **已用**(经 `qc_python`,勿塞进轻量 `.venv`):librosa、soundfile、torch、torchaudio、demucs、whisper -- CLI:`media` / `qc` 走该栈;不要在 Agent 里手写探测脚本 - -## 音频窄接口(勿自写脚本) - -```powershell -.\.venv\Scripts\python.exe songforge.py media info --file path.flac -.\.venv\Scripts\python.exe songforge.py media convert --file in.flac --to wav -.\.venv\Scripts\python.exe songforge.py media clip --file in.flac --start 12 --dur 8 --out out.wav -``` - -(走 `qc_python`,与 qc 同栈。) +经 `qc_python`(勿塞进轻量 `.venv`):**librosa、soundfile、torch、torchaudio、demucs、whisper**。 +`media` / `qc` / `recheck` 都走这栈。 ## 遇事先问 | 情况 | 动作 | |------|------| -| 无 local.toml / Comfy 不通 / 缺模型 | **问用户**路径与是否已启动 | +| 无 local.toml / Comfy 不通 / 缺模型 | **问**路径与是否已启动 | | 风格/歌名/歌词来源不清 | **问** | | 想改采样/换模型/放宽 QC | **拒绝**,除非用户书面坚持 | -| secrets / push 哪个 remote | **问**;勿提交 `.secrets`、`local.toml` | +| 需改仓库代码 | 先说明范围征求同意;建议 git 备份或新插件;问 commit/push/remote | +| secrets / 推哪个 remote | **问**;勿提交 `.secrets`、`local.toml` | -## 改代码铁律(防意外) - -发现现状不满足需求、**需要改仓库代码**时: - -1. **先停手**,说明缺口与拟改范围,**征求用户明确同意**后再动文件。 -2. **建议用户选一种安全路径**(可并列问): - - git 备份:新分支 / commit / stash / tag,再改; - - 或**新写插件/旁路脚本**,少动核心铁律与默认配置。 -3. **问是否 commit、是否 push、推哪个 remote**;未获许可不得提交或推送。 -4. 用户同意收尾时再 `commit` +(若许可)`push`;勿擅自 `--force`、勿提交 secrets / `local.toml`。 - -可自行处理(不必问改代码):`check` 密度失败只改歌词;漏 `--baseline` 补上;`prune --days 7` 清 tmp。 +可自行:`check` 密度失败 → `align` 或改词;漏 `--baseline` 补上;`prune --days 7`。 ## 样例 diff --git a/README.md b/README.md index a50cee4..72837ad 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ | [`docs/使用手册.md`](docs/使用手册.md) | 人类 | 命令与配置详解 | | [`config/local.toml.example`](config/local.toml.example) | 所有人 | 第一次连本机 ComfyUI | -> 给 Agent:默认只读 `AGENTS_MIN.md`。缺 `local.toml`、Comfy 没起、风格/歌名不清、想改模型或阈值时——**先问用户**。需改代码时先征求同意,建议 git 备份或新插件旁路,并问清是否 commit/push。音频探测/转码/截取用 `songforge media`,不要读大源码。 +> 给 Agent:**进仓只读 [`AGENTS_MIN.md`](AGENTS_MIN.md)**(含 align/recheck/compare、插件与报告模板)。缺 `local.toml`、Comfy 没起、风格/歌名不清——**先问用户**。扩展写 [`plugins/`](plugins/README.md)。 ## 一键体检 @@ -71,14 +71,16 @@ cd songforge ```powershell .\.venv\Scripts\python.exe songforge.py target --style pop --bpm 104 --dur 180 -.\.venv\Scripts\python.exe songforge.py check --style pop --title 灯还亮着 --lyrics-file input\lyrics\pop_灯还亮着.txt -.\.venv\Scripts\python.exe songforge.py gen --style pop --title 灯还亮着 --lyrics-file input\lyrics\pop_灯还亮着.txt -.\.venv\Scripts\python.exe songforge.py qc --run gen-<时间戳> --baseline -.\.venv\Scripts\python.exe songforge.py prune --days 7 # 清过期 tmp -.\.venv\Scripts\python.exe songforge.py prune --days 7 --dry-run +.\.venv\Scripts\python.exe songforge.py align --style pop --lyrics-file input\lyrics\pop_灯还亮着.txt --bpm 104 --dur 180 +.\.venv\Scripts\python.exe songforge.py check --style pop --title 灯还亮着 --lyrics-file input\lyrics\pop_灯还亮着.txt +.\.venv\Scripts\python.exe songforge.py gen --style pop --title 灯还亮着 --lyrics-file input\lyrics\pop_灯还亮着.txt --auto-retry 2 +.\.venv\Scripts\python.exe songforge.py recheck --run gen-<时间戳> +.\.venv\Scripts\python.exe songforge.py qc --run gen-<时间戳> --baseline +.\.venv\Scripts\python.exe songforge.py compare --runs gen-a,gen-b +.\.venv\Scripts\python.exe songforge.py prune --days 7 ``` -`qc` 必须带 `--baseline`。样例歌词见 `input/lyrics/`。Agent 规则见 [`AGENTS.md`](AGENTS.md)。 +`qc` 必须带 `--baseline`。PASS 后看 `report.md` 与 `listen/`。Agent 入口:[`AGENTS_MIN.md`](AGENTS_MIN.md)。 ## 硬约束(摘要) diff --git a/cli/commands.py b/cli/commands.py index 698e2b2..cd29342 100644 --- a/cli/commands.py +++ b/cli/commands.py @@ -340,6 +340,9 @@ def cmd_gen(args): dry_run=True) run.save_json("manifest.json", man_dry) catmod.append_entry(cfg, catmod.entry_from_gen(run.dir, man_dry)) + from ..plugins.hooks import bus + bus.emit("after_gen", run_dir=run.dir, manifest=man_dry, + cfg=cfg, args=args) log.info("dry-run 结束", seeds=seeds) log.close() return 0 @@ -351,8 +354,21 @@ def cmd_gen(args): if not ok: raise ServerError("ComfyUI 不可达: %s" % info) + from ..plugins.hooks import bus + from .extras import run_intro_check + from ..plugins.retry_on_intro_fail.plugin import should_retry_intro_blob + + cro = pmod.comfy_root(cfg=cfg) + music = pmod.music_dir(cfg, cro) + auto_retry = int(getattr(args, "auto_retry", 0) or 0) + do_recheck = not bool(getattr(args, "skip_recheck", False)) + jobs = list(built) results = [] - for sd, wf, wf_path, sub in built: + retries_left = auto_retry + ji = 0 + while ji < len(jobs): + sd, wf, wf_path, sub = jobs[ji] + ji += 1 pid = eng.submit_retry(wfmod.render(wf)) log.info("已提交", seed=sd, prompt_id=pid) @@ -364,7 +380,40 @@ def cmd_gen(args): results.append(dict(seed=sd, prompt_id=pid, files=outs, workflow=os.path.basename(wf_path))) - cro = pmod.comfy_root(cfg=cfg) + if not do_recheck or not outs: + continue + audio = None + for fn in outs: + p = fn if os.path.isabs(fn) else os.path.join( + music, os.path.basename(fn)) + if os.path.isfile(p): + audio = p + break + if not audio: + continue + _code, blob = run_intro_check( + cfg, audio, bpm=st["bpm"], intro_bars=ibars, lang=lang) + run.save_json("intro_check.json", blob or {}) + log.info("开曲轻检", ok=(blob or {}).get("ok")) + if should_retry_intro_blob(blob) and retries_left > 0: + retries_left -= 1 + sd2 = seed_base + len(jobs) + sub2 = "%s/%s-s%d-%s-%s" % ( + prefix, safe_title, len(jobs) + 1, style_tag, date_stamp) + wf2 = wfmod.build_workflow( + template, title=title, tags=tags, lyrics=lyrics, seed=sd2, + bpm=st["bpm"], duration=st["duration"], keyscale=st["key"], + sampler=cfg["sampler"], encode=cfg["encode"], + model=cfg["model"], output_prefix=sub2, output_format=fmt, + language=lang, + timesignature=cfg["song"]["time_signature"]) + wf_path2 = run.path("wf_%s_s%d.json" % (safe_title, len(jobs) + 1)) + with open(wf_path2, "w", encoding="utf-8") as f: + json.dump(wf2, f, ensure_ascii=False, indent=1) + jobs.append((sd2, wf2, wf_path2, sub2)) + log.warn("开曲轻检 FAIL,追加 seed 重试", seed=sd2, + left=retries_left) + manifest = dict( run_id=run.run_id, title=title, style=args.style, bpm=st["bpm"], key=st["key"], duration=st["duration"], @@ -375,16 +424,18 @@ def cmd_gen(args): intro_bars=ibars, sampler=cfg["sampler"], encode=cfg["encode"], model=cfg["model"], density=stats, results=results, - output_dir=pmod.music_dir(cfg, cro), + output_dir=music, 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)) + bus.emit("after_gen", run_dir=run.dir, manifest=manifest, cfg=cfg, args=args) 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)) + % (st["duration"], st["bpm"], st["key"], + [r["seed"] for r in results])) if inst: print(" [纯器乐] 无可唱歌词,密度闸门不适用") else: @@ -395,6 +446,8 @@ def cmd_gen(args): print(" seed=%s -> %s" % (r["seed"], ", ".join(r["files"] or ["无输出"]))) print(" 产物目录: %s" % run.dir) print(" 音频输出: %s" % manifest["output_dir"]) + if os.path.isfile(run.path("report.md")): + print(" 报告: %s" % run.path("report.md")) log.close() return 0 diff --git a/cli/extras.py b/cli/extras.py new file mode 100644 index 0000000..d971a44 --- /dev/null +++ b/cli/extras.py @@ -0,0 +1,196 @@ +# -*- coding: utf-8 -*- +"""扩展命令:compare / align / recheck。""" +import json +import os +import subprocess + +from ..config import loader +from ..core import catalog as catmod +from ..core import paths as pmod +from ..lyrics import align as alignmod +from ..lyrics import meter +from .common import build_cfg, resolve_profile, runs_root, style_of +from .qc import find_qc_python + + +def _load_run(cfg, run_id): + rundir = catmod.find_run_dir(runs_root(cfg), run_id) + if not rundir: + return None, None, None + man = {} + mp = os.path.join(rundir, "manifest.json") + if os.path.isfile(mp): + with open(mp, encoding="utf-8") as f: + man = json.load(f) + qc = {} + qp = os.path.join(rundir, "qc.json") + if os.path.isfile(qp): + with open(qp, encoding="utf-8") as f: + qc = json.load(f) + return rundir, man, qc + + +def _qc_rows(qc): + if not qc: + return [] + return qc.get("rows") or qc.get("results") or [] + + +def _first_audio(cfg, man, rundir): + music = man.get("output_dir") or pmod.music_dir(cfg, pmod.comfy_root(cfg=cfg)) + for r in man.get("results") or []: + for fn in r.get("files") or []: + p = fn if os.path.isabs(fn) else os.path.join(music, os.path.basename(fn)) + if os.path.isfile(p): + return p + # 也搜 rundir + p2 = os.path.join(rundir, os.path.basename(fn)) + if os.path.isfile(p2): + return p2 + return None + + +def cmd_compare(args): + cfg = loader.load({}) + ids = [] + for part in (args.runs or []): + ids.extend([x.strip() for x in part.split(",") if x.strip()]) + if len(ids) < 2: + print("[错误] 至少两个 run:songforge compare --runs a,b") + return 2 + print("%-26s %-8s %-5s %-5s %-6s %-8s %-7s %-8s %s" + % ("run_id", "style", "BPM", "dur", "QC", "onset", "衬词比", + "voice_nn", "title")) + for rid in ids: + rundir, man, qc = _load_run(cfg, rid) + if not rundir: + print("%-26s (找不到)" % rid) + continue + rows = [r for r in _qc_rows(qc) if r.get("group") != "baseline"] + r0 = rows[0] if rows else {} + verd = None + for v in (qc.get("verdicts") or []): + verd = v.get("verdict") + break + if not verd: + verd = man.get("qc_verdict") or "-" + print("%-26s %-8s %-5s %-5s %-6s %-8s %-7s %-8s %s" + % ((man.get("run_id") or rid)[:26], + (man.get("style") or "-")[:8], + man.get("bpm") or "-", + man.get("duration") or "-", + (verd or "-")[:6], + r0.get("vocal_onset") if r0.get("vocal_onset") is not None else "-", + ("%.0f%%" % (float(r0.get("intro_filler_ratio") or 0) * 100)) + if r0 else "-", + (str(r0.get("voice_nn") or "-"))[:8], + (man.get("title") or "-")[:16])) + return 0 + + +def cmd_align(args): + cfg = build_cfg(args) + st = style_of(cfg, args.style, args) + if args.lyrics_file: + with open(args.lyrics_file, encoding="utf-8") as f: + raw = f.read() + elif args.lyrics: + raw = args.lyrics + else: + print("[错误] 需要 --lyrics-file 或 --lyrics") + return 2 + prof = resolve_profile(args) + cfg_ly = meter.pick(cfg["lyrics"], prof) + out, diag = alignmod.align_lyrics( + raw, st["duration"], st["bpm"], cfg_ly, target=args.target) + dest = args.out or (args.lyrics_file and ( + os.path.splitext(args.lyrics_file)[0] + "_aligned.txt")) + if not dest: + dest = os.path.join(pmod.tmp_today(cfg), "aligned.txt") + pmod.ensure_dir(os.path.dirname(dest)) + with open(dest, "w", encoding="utf-8") as f: + f.write(out) + print("== align ==") + print(" 音节 %d -> %d 目标 %d 窗口 %d~%d ok=%s" + % (diag["before"], diag["after"], diag["want"], + diag["win_lo"], diag["win_hi"], diag.get("ok"))) + print(" 写出: %s" % dest) + if not diag.get("ok"): + print(" [WARN] %s" % diag.get("note")) + return 1 + return 0 + + +def run_intro_check(cfg, audio_path, bpm=104, intro_bars=8, lang="zh"): + """子进程开曲轻检。返回 (code, blob)。""" + py = find_qc_python(cfg) + if not py: + return 2, dict(error="no qc_python") + worker = os.path.join(pmod.PKG_ROOT, "tools", "intro_check_worker.py") + job = dict(file=os.path.abspath(audio_path), bpm=bpm, + intro_bars=intro_bars, lang=lang) + env = dict(os.environ) + env["PYTHONIOENCODING"] = "utf-8" + env["SONGFORGE_COMFY_ROOT"] = pmod.comfy_root(cfg=cfg) + try: + r = subprocess.run( + [py, worker, json.dumps(job, ensure_ascii=False)], + capture_output=True, env=env, timeout=600) + except subprocess.TimeoutExpired: + return 2, dict(error="timeout") + out = r.stdout.decode("utf-8", "replace") + if "###JSON###" not in out: + return 2, dict(error="no json", log=out[-800:]) + blob = json.loads(out.split("###JSON###", 1)[1].strip()) + return (0 if not blob.get("error") else 1), blob + + +def cmd_recheck(args): + cfg = loader.load({}) + rundir, man, _qc = _load_run(cfg, args.run) + if not rundir: + print("[错误] 找不到 run: %s" % args.run) + return 2 + audio = args.file or _first_audio(cfg, man, rundir) + if not audio or not os.path.isfile(audio): + print("[错误] 找不到音频。可传 --file") + return 2 + bpm = args.bpm or man.get("bpm") or 104 + bars = args.intro_bars or man.get("intro_bars") or 8 + lang = args.lang or man.get("language") or "zh" + code, blob = run_intro_check(cfg, audio, bpm=bpm, intro_bars=bars, lang=lang) + outp = os.path.join(rundir, "intro_check.json") + with open(outp, "w", encoding="utf-8") as f: + json.dump(blob, f, ensure_ascii=False, indent=1) + print(json.dumps(blob, ensure_ascii=False, indent=1)) + print("报告: %s" % outp) + if blob.get("error"): + return 2 + return 0 if blob.get("ok") else 1 + + +def add_extras(sub): + c = sub.add_parser("compare", help="对比多次 run 的 QC/开曲指标") + c.add_argument("--runs", action="append", required=True, + help="run_id 列表,逗号分隔,可多次") + c.set_defaults(func=cmd_compare) + + a = sub.add_parser("align", help="歌词音节一键对齐到 target 放行窗") + a.add_argument("--style", required=True) + a.add_argument("--lyrics-file", dest="lyrics_file") + a.add_argument("--lyrics") + a.add_argument("--out", help="输出路径(默认 *_aligned.txt)") + a.add_argument("--bpm", type=int) + a.add_argument("--dur", type=int) + a.add_argument("--target", type=int, help="目标音节(默认取建议值)") + a.add_argument("--chill-vocal", dest="chill_vocal", action="store_true") + a.add_argument("--density-profile", dest="density_profile") + a.set_defaults(func=cmd_align) + + r = sub.add_parser("recheck", help="开曲轻检(前奏窗 ASR 衬词)") + r.add_argument("--run", required=True) + r.add_argument("--file", help="直接指定音频") + r.add_argument("--bpm", type=int) + r.add_argument("--intro-bars", dest="intro_bars", type=int) + r.add_argument("--lang", default=None) + r.set_defaults(func=cmd_recheck) diff --git a/cli/main.py b/cli/main.py index 7c38c3c..0eb90ed 100644 --- a/cli/main.py +++ b/cli/main.py @@ -3,7 +3,7 @@ 命令: doctor / styles / target / check / gen / runs / show / catalog / - prune / media / qc / selftest + prune / media / qc / compare / align / recheck / selftest 设计约束:所有业务参数来自配置/命令行,本文件不写死任何模型或采样常量。 实现见 common / assemble / commands / selftest / qc。 @@ -120,6 +120,10 @@ def build_parser(): help="注入完整 RULE_NEG(默认关闭;易诱发开曲衬词)") g.add_argument("--dry-run", dest="dry_run", action="store_true", help="只写 workflow 与 API 预览,不提交 ComfyUI") + g.add_argument("--auto-retry", dest="auto_retry", type=int, default=0, + help="开曲轻检 FAIL 时额外换 seed 重提次数(默认 0)") + g.add_argument("--skip-recheck", dest="skip_recheck", action="store_true", + help="生成后不做开曲轻检(默认会做并写 intro_check.json)") g.set_defaults(func=cmds.cmd_gen) r = sub.add_parser("runs", help="列出历史运行") @@ -153,6 +157,9 @@ def build_parser(): from .qc import add_qc add_qc(sub) + + from .extras import add_extras + add_extras(sub) return p diff --git a/cli/qc.py b/cli/qc.py index bfc23f6..6b80851 100644 --- a/cli/qc.py +++ b/cli/qc.py @@ -404,6 +404,19 @@ def _render(rep, args, rundir=None, cfg=None): except Exception as e: print("[WARN] catalog 更新失败: %s" % e) + if rundir and cfg: + try: + from ..plugins.hooks import bus + bus.emit("after_qc", run_dir=rundir, report=rep, verd=verd, + rows=rows, cfg=cfg, args=args) + except Exception as e: + print("[WARN] after_qc 插件: %s" % e) + if os.path.isfile(os.path.join(rundir, "report.md")): + print("交付报告: %s" % os.path.join(rundir, "report.md")) + listen = os.path.join(rundir, "listen") + if os.path.isdir(listen): + print("听感包: %s" % listen) + if not verd: print("本次只有基线校准,没有待验收新歌 -> 退出码 0") return 0 diff --git a/cli/selftest.py b/cli/selftest.py index 0da92c2..3873847 100644 --- a/cli/selftest.py +++ b/cli/selftest.py @@ -778,6 +778,8 @@ def _t_plugins_and_lean_default(): """微内核钩子已加载;默认 prefer_lean_tags;assemble 不注入 RULE_NEG 堆。""" from ..plugins.hooks import bus assert bus._handlers.get("after_assemble"), "intro_guard 未挂 after_assemble" + assert bus._handlers.get("after_gen"), "deliver_report 未挂 after_gen" + assert bus._handlers.get("after_qc"), "qc 插件未挂 after_qc" cfg = loader.load({}) assert cfg["rules"].get("prefer_lean_tags") is True @@ -791,7 +793,6 @@ def _t_plugins_and_lean_default(): st = {"bpm": 104, "duration": 180, "key": "F major", "style": "Chinese pop", "instruments": "kick snare bass", "mood": "bright", "extra": ""} - # 密度够过闸的短样 raw = ("[verse]\n" + "\n".join( ["街口的灯还亮着我往前走一步"] * 20)) tags, _ly = assemble(_A(), cfg, st, raw, "selftest") @@ -799,8 +800,25 @@ def _t_plugins_and_lean_default(): "默认 lean 仍注入了 RULE_NEG opening 堆" assert "no wordless vocals" in tags.lower() assert "mm mm" not in tags.lower() - return [("微内核与 lean 默认", - "plugins.after_assemble 已挂;默认 tags 为 lean 且无音节点名")] + + # align:过少歌词应能拉进窗口 + from ..lyrics import align as alignmod + from ..lyrics import meter as mmod + thin = "[verse]\n太短了\n再一句\n" + out, diag = alignmod.align_lyrics(thin, 180, 104, cfg["lyrics"]) + assert diag.get("ok"), "align 未能把过少歌词对齐: %s" % diag + assert diag["after"] >= diag["win_lo"] + + # deliver_report 钩子可写报告 + import tempfile + td = tempfile.mkdtemp(prefix="sf-rep-") + man = dict(run_id="t", title="测", style="pop", bpm=104, duration=180, + results=[dict(seed=1, files=["x.flac"])]) + bus.emit("after_gen", run_dir=td, manifest=man, cfg=cfg, args=None) + assert os.path.isfile(os.path.join(td, "report.md")), "未写出 report.md" + + return [("微内核/lean/align/报告", + "钩子齐;lean 默认;align 进窗;report.md 可写")] def cmd_selftest(args): diff --git a/docs/使用手册.md b/docs/使用手册.md index 9936595..85270bc 100644 --- a/docs/使用手册.md +++ b/docs/使用手册.md @@ -28,12 +28,12 @@ python songforge\songforge.py <命令> [选项] ### 给 Agent / 自动化 -1. 先读 [`AGENTS_MIN.md`](../AGENTS_MIN.md);不够再读 [`AGENTS.md`](../AGENTS.md)。 -2. 没有 `local.toml`、Comfy 没起、风格/歌名/歌词来源不清 → **先问用户**,不要猜路径或改铁律。 -3. 需改仓库代码时:先征求同意;建议 git 备份或新插件旁路(目录 `plugins/`);问清是否 commit/push、推哪个 remote。 -4. 交付以 `qc --baseline` PASS 为准;开曲衬词(`intro_filler`)是硬门;`style_fam` / 全曲 `filler_ratio` 仍是弱参考。 -5. 探测/转码/截取用 `songforge media`(librosa/soundfile 在 `qc_python`),不要整读 `qc_worker.py`。 -6. 默认 `prefer_lean_tags`;不要随意 `--full-tags`(否定句膨胀会诱发开曲衬词)。 +1. 进仓先读 [`AGENTS_MIN.md`](../AGENTS_MIN.md)(含 align/recheck/compare/插件);不够再读 [`AGENTS.md`](../AGENTS.md)。 +2. 没有 `local.toml`、Comfy 没起、风格/歌名/歌词来源不清 → **先问用户**。 +3. 需改代码:先征求同意;优先 [`plugins/`](../plugins/README.md);问清 commit/push/remote。 +4. 交付:`qc --baseline` PASS;看 `report.md` / `listen/`;开曲衬词硬门;`style_fam` 弱参考。 +5. 音频用 `media` / `recheck` / `qc`(`qc_python`:librosa/demucs/whisper…)。 +6. 默认 `prefer_lean_tags`;密度不够用 `align`;开曲 FAIL 用 `gen --auto-retry`。 --- diff --git a/lyrics/align.py b/lyrics/align.py new file mode 100644 index 0000000..a17b00f --- /dev/null +++ b/lyrics/align.py @@ -0,0 +1,92 @@ +# -*- coding: utf-8 -*- +"""把歌词正文音节对齐到 meter 放行窗口(一键凑密度)。""" +import re + +from . import meter + +_TAG = re.compile(r"^\[.+\]\s*$") + + +def _body_lines(text): + """保留结构:返回 [(is_tag, line)]。""" + rows = [] + for ln in text.split("\n"): + s = ln.rstrip("\r") + if _TAG.match(s.strip()): + rows.append((True, s)) + else: + rows.append((False, s)) + return rows + + +def _syllables_of_rows(rows): + body = "\n".join(ln for is_tag, ln in rows if not is_tag and ln.strip()) + return meter.syllable(body) + + +def align_lyrics(lyrics, duration, bpm, cfg_lyrics, target=None): + """调整正文行数使音节落在放行窗;返回 (新歌词, 诊断 dict)。 + + 过少:循环复用已有可唱行(不造新词)。 + 过多:从末尾删可唱行(保留至少每段一行若可能)。 + 结构标记与空行尽量保留。 + """ + cfg_lyrics = meter.pick(cfg_lyrics, None) + mid, wlo, whi = meter.target_words(duration, bpm, cfg_lyrics) + want = int(target) if target is not None else mid + want = max(wlo, min(want, whi)) + + rows = _body_lines(lyrics) + before = _syllables_of_rows(rows) + singable = [ln for is_tag, ln in rows if (not is_tag) and ln.strip()] + if not singable: + return lyrics, dict(before=before, after=before, want=want, + win_lo=wlo, win_hi=whi, changed=False, + note="无可唱行") + + # 过少:在最后一个非空可唱行后插入副本 + guard = 0 + while _syllables_of_rows(rows) < wlo and guard < 500: + guard += 1 + # 找最后一个可唱行位置 + idx = None + for i in range(len(rows) - 1, -1, -1): + if (not rows[i][0]) and rows[i][1].strip(): + idx = i + break + if idx is None: + break + donor = singable[guard % len(singable)] + rows.insert(idx + 1, (False, donor)) + + # 过多:从末尾删可唱行,但至少留 4 行 + guard = 0 + while _syllables_of_rows(rows) > whi and guard < 500: + guard += 1 + n_sing = sum(1 for is_tag, ln in rows if (not is_tag) and ln.strip()) + if n_sing <= 4: + break + for i in range(len(rows) - 1, -1, -1): + if (not rows[i][0]) and rows[i][1].strip(): + rows.pop(i) + break + + # 微调到 want 附近(仍夹在窗口内) + guard = 0 + while _syllables_of_rows(rows) < want and _syllables_of_rows(rows) < whi \ + and guard < 200: + guard += 1 + idx = next((i for i in range(len(rows) - 1, -1, -1) + if (not rows[i][0]) and rows[i][1].strip()), None) + if idx is None: + break + rows.insert(idx + 1, (False, singable[guard % len(singable)])) + + after = _syllables_of_rows(rows) + out = "\n".join(ln for _, ln in rows) + if not out.endswith("\n"): + out += "\n" + ok = wlo <= after <= whi + return out, dict(before=before, after=after, want=want, + win_lo=wlo, win_hi=whi, changed=(out != lyrics), + ok=ok, note=("ok" if ok else "仍未落入窗口")) diff --git a/plugins/README.md b/plugins/README.md index 48e04f6..f9d737b 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -1,17 +1,39 @@ # plugins — 扩展目录(微内核外挂) +Agent / 开发者:**新能力优先写插件**,少改 `core/` / `rules/` / `cli/`。 + | 路径 | 用途 | |------|------| -| `plugins/hooks.py` | 钩子总线(内核只 `emit`,不 import 业务插件) | -| `plugins/builtin/` | 内置插件(随仓发布) | -| `plugins/.py` | 单文件插件 | -| `plugins//plugin.py` | 包式插件入口 | +| `hooks.py` | 钩子总线(内核只 `emit`) | +| `builtin/` | 随仓内置(如 `intro_guard`) | +| `/plugin.py` | 包式插件(推荐) | +| `.py` | 单文件插件 | ## 钩子 -- `before_build_tags` -- `after_assemble`(可返回 `{"tags","lyrics"}`) -- `before_qc_judge` / `after_qc_judge`(QC 子进程侧另见 qc_worker) +| 钩子 | 何时 | 典型用途 | +|------|------|----------| +| `before_build_tags` | assemble 前 | 改风格参数 | +| `after_assemble` | tags/歌词就绪 | 守卫、改写 | +| `after_gen` | gen/dry-run 写完 manifest | `deliver_report` | +| `after_qc` | qc 渲染结束 | `deliver_report` / `listen_pack` / `retry_on_intro_fail` | -内核层:`core/`(引擎/路径/workflow)、`rules/`(铁律)、`cli/`(命令)。 -业务扩展优先写插件,少改内核。 +## 当前插件 + +| 插件 | 作用 | +|------|------| +| `builtin/intro_guard` | tags 禁止点名衬词音节 | +| `deliver_report` | 写 `data/runs/.../report.md` | +| `listen_pack` | QC PASS 后截 `listen/intro.wav` + `chorus_approx.wav` | +| `retry_on_intro_fail` | 写 `retry_hint.json`;配合 `gen --auto-retry` | + +## 最小插件示例 + +```python +# plugins/hello/plugin.py +from ..hooks import bus + +@bus.on("after_gen") +def _hi(run_dir=None, **kw): + print("[hello]", run_dir) +``` diff --git a/plugins/deliver_report/__init__.py b/plugins/deliver_report/__init__.py new file mode 100644 index 0000000..1adfb6f --- /dev/null +++ b/plugins/deliver_report/__init__.py @@ -0,0 +1 @@ +# package plugin diff --git a/plugins/deliver_report/plugin.py b/plugins/deliver_report/plugin.py new file mode 100644 index 0000000..a19d912 --- /dev/null +++ b/plugins/deliver_report/plugin.py @@ -0,0 +1,84 @@ +# -*- coding: utf-8 -*- +"""生歌结束后按 AGENTS_MIN 模板写 report.md。""" +import json +import os + +from ..hooks import bus + + +def _write_report(rundir, man, qc=None, intro=None): + if not rundir: + return + rows = [] + verd = "-" + if qc: + rows = [r for r in (qc.get("rows") or []) if r.get("group") != "baseline"] + vs = qc.get("verdicts") or [] + if vs: + verd = vs[0].get("verdict") or "-" + r0 = rows[0] if rows else {} + audio = "-" + for r in man.get("results") or []: + files = r.get("files") or [] + if files: + audio = files[0] + break + intro_line = "-" + if intro: + intro_line = "ok=%s n=%s ratio=%s" % ( + intro.get("ok"), intro.get("intro_filler_n"), + intro.get("intro_filler_ratio")) + elif r0: + intro_line = "n=%s ratio=%s" % ( + r0.get("intro_filler_n"), r0.get("intro_filler_ratio")) + body = "\n".join([ + "## 生歌报告", + "- 结论:%s" % (verd if verd != "-" else (man.get("qc_verdict") or "—")), + "- 歌名 / 风格 / BPM / 设定时长:%s / %s / %s / %s" % ( + man.get("title") or "—", man.get("style") or "—", + man.get("bpm") or "—", man.get("duration") or "—"), + "- run_id:%s" % (man.get("run_id") or os.path.basename(rundir)), + "- 音频路径:%s" % audio, + "- 歌词:见本目录 lyrics.txt;密度见 manifest.density", + "- QC:verdict=%s;voice_nn=%s sim=%s;开曲衬词 %s;filler_ratio=%s" % ( + verd, r0.get("voice_nn") or "—", r0.get("voice_nn_sim") or "—", + intro_line, r0.get("filler_ratio") if r0 else "—"), + "- 弱参考:style_fam=%s style_conf=%s" % ( + r0.get("style_fam") or "—", r0.get("style_conf") or "—"), + "- 模型/采样:见 manifest.model / sampler / encode", + "- 种子:%s" % ( + ", ".join(str(x.get("seed")) for x in (man.get("results") or []) + if x.get("seed") is not None) or "—"), + "- 建议:先听 listen/ 前奏与副歌片段;FAIL 则改词或换 seed 重 gen", + "", + ]) + path = os.path.join(rundir, "report.md") + with open(path, "w", encoding="utf-8") as f: + f.write(body) + return path + + +@bus.on("after_gen") +def _on_gen(run_dir=None, manifest=None, **_kw): + if not run_dir or not manifest: + return None + path = _write_report(run_dir, manifest) + return dict(report=path) if path else None + + +@bus.on("after_qc") +def _on_qc(run_dir=None, report=None, **_kw): + if not run_dir: + return None + man = {} + mp = os.path.join(run_dir, "manifest.json") + if os.path.isfile(mp): + with open(mp, encoding="utf-8") as f: + man = json.load(f) + intro = None + ip = os.path.join(run_dir, "intro_check.json") + if os.path.isfile(ip): + with open(ip, encoding="utf-8") as f: + intro = json.load(f) + path = _write_report(run_dir, man, qc=report, intro=intro) + return dict(report=path) if path else None diff --git a/plugins/hooks.py b/plugins/hooks.py index 038dd0c..62f34d6 100644 --- a/plugins/hooks.py +++ b/plugins/hooks.py @@ -5,7 +5,9 @@ before_build_tags kwargs: args, cfg, style, chill, instrumental, lean after_assemble kwargs: tags, lyrics, args, cfg, style, lean -> 可返回 {"tags":..., "lyrics":...} 覆盖 - before_qc_judge kwargs: result, expect, soft -> 可追加 why 列表 + after_gen kwargs: run_dir, manifest, cfg, args + after_qc kwargs: run_dir, report, verd, rows, cfg, args + before_qc_judge kwargs: result, expect, soft (qc_worker 子进程内,可选) after_qc_judge kwargs: verdict, why, result 插件目录: diff --git a/plugins/listen_pack/__init__.py b/plugins/listen_pack/__init__.py new file mode 100644 index 0000000..1adfb6f --- /dev/null +++ b/plugins/listen_pack/__init__.py @@ -0,0 +1 @@ +# package plugin diff --git a/plugins/listen_pack/plugin.py b/plugins/listen_pack/plugin.py new file mode 100644 index 0000000..0a0ff89 --- /dev/null +++ b/plugins/listen_pack/plugin.py @@ -0,0 +1,60 @@ +# -*- coding: utf-8 -*- +"""PASS 后截取前奏 + 副歌附近听感片段到 run/listen/。""" +import json +import os + +from ..hooks import bus + + +def _clip(cfg, src, start, dur, out): + from ...cli.media import _run + code, blob = _run(dict( + op="clip", file=os.path.abspath(src), + start=float(start), dur=float(dur), out=os.path.abspath(out))) + return code == 0 and blob and not blob.get("error") + + +def _audio_from(cfg, man, rundir): + from ...cli.extras import _first_audio + return _first_audio(cfg, man or {}, rundir) + + +@bus.on("after_qc") +def _pack(run_dir=None, report=None, cfg=None, **_kw): + if not run_dir or not report or not cfg: + return None + verd = report.get("verdicts") or [] + rows = report.get("rows") or [] + new_names = {r.get("file") for r in rows if r.get("group") != "old"} + new_v = [v for v in verd if v.get("file") in new_names] or [ + v for v in verd if v.get("verdict")] + if not new_v or any(v.get("verdict") != "PASS" for v in new_v): + return None + man = {} + mp = os.path.join(run_dir, "manifest.json") + if os.path.isfile(mp): + with open(mp, encoding="utf-8") as f: + man = json.load(f) + src = _audio_from(cfg, man, run_dir) + if not src or not os.path.isfile(src): + return None + bpm = float(man.get("bpm") or 104) + bars = int(man.get("intro_bars") or 8) + intro_sec = bars * 60.0 / bpm * 4.0 + listen = os.path.join(run_dir, "listen") + os.makedirs(listen, exist_ok=True) + outs = [] + a = os.path.join(listen, "intro.wav") + if _clip(cfg, src, 0.0, intro_sec, a): + outs.append(a) + # 副歌附近:约在 40% 时长处截 15s + dur = float(man.get("duration") or 180) + chorus_t = max(intro_sec + 1.0, dur * 0.40) + b = os.path.join(listen, "chorus_approx.wav") + if _clip(cfg, src, chorus_t, 15.0, b): + outs.append(b) + meta = dict(source=src, clips=outs, intro_sec=intro_sec, + chorus_start=chorus_t) + with open(os.path.join(listen, "pack.json"), "w", encoding="utf-8") as f: + json.dump(meta, f, ensure_ascii=False, indent=1) + return dict(listen=listen, clips=outs) diff --git a/plugins/retry_on_intro_fail/__init__.py b/plugins/retry_on_intro_fail/__init__.py new file mode 100644 index 0000000..1adfb6f --- /dev/null +++ b/plugins/retry_on_intro_fail/__init__.py @@ -0,0 +1 @@ +# package plugin diff --git a/plugins/retry_on_intro_fail/plugin.py b/plugins/retry_on_intro_fail/plugin.py new file mode 100644 index 0000000..83198c4 --- /dev/null +++ b/plugins/retry_on_intro_fail/plugin.py @@ -0,0 +1,54 @@ +# -*- coding: utf-8 -*- +"""开曲衬词 FAIL 时给出重试建议;供 gen --auto-retry 读取。""" +import json +import os + +from ..hooks import bus + +# 判定文案关键字(与 qc_worker judge 一致) +_INTRO_KEYS = ("开曲衬词", "前 2s 就已唱出", "人声在") + + +def is_intro_fail(why_list): + text = " ".join(why_list or []) + return any(k in text for k in _INTRO_KEYS) + + +@bus.on("after_qc") +def _hint(run_dir=None, report=None, **_kw): + if not run_dir or not report: + return None + verd = report.get("verdicts") or [] + reasons = [] + for v in verd: + if v.get("verdict") == "FAIL": + reasons.extend(v.get("why") or []) + if not is_intro_fail(reasons): + # 也看 intro_check.json + ip = os.path.join(run_dir, "intro_check.json") + if os.path.isfile(ip): + with open(ip, encoding="utf-8") as f: + blob = json.load(f) + if blob.get("fail") or blob.get("ok") is False: + reasons.append("intro_check fail") + else: + return None + else: + return None + hint = dict( + retry=True, + reason="intro_filler_or_early_vocal", + why=reasons[:8], + suggest="换 seed 重 gen,或 songforge recheck --run 后再决定", + ) + path = os.path.join(run_dir, "retry_hint.json") + with open(path, "w", encoding="utf-8") as f: + json.dump(hint, f, ensure_ascii=False, indent=1) + return hint + + +def should_retry_intro_blob(blob): + """给 gen --auto-retry 用:intro_check blob 是否应换 seed。""" + if not blob or blob.get("error"): + return False + return bool(blob.get("fail") or blob.get("ok") is False) diff --git a/tools/intro_check_worker.py b/tools/intro_check_worker.py new file mode 100644 index 0000000..fe94a00 --- /dev/null +++ b/tools/intro_check_worker.py @@ -0,0 +1,65 @@ +# -*- coding: utf-8 -*- +"""开曲轻检:只切前奏窗做 ASR 衬词统计(比全曲 QC 快)。 + +用法(qc_python): + python intro_check_worker.py '{"file":"...","bpm":104,"intro_bars":8,"lang":"zh"}' +""" +import json +import os +import sys +import traceback + +HERE = os.path.dirname(os.path.abspath(__file__)) +if HERE not in sys.path: + sys.path.insert(0, HERE) + + +def main(job): + import numpy as np + import qc_worker as q + + path = job["file"] + bpm = float(job.get("bpm") or 104) + bars = int(job.get("intro_bars") or 8) + lang = job.get("lang") or "zh" + intro_sec = bars * 60.0 / bpm * 4.0 + # 多取 0.5s 余量 + take = min(intro_sec + 0.5, 60.0) + + y, sr, ch = q._load_audio(path) + n = int(take * sr) + head = y[:max(1, n)] + a = q.asr(head, sr, lang) + segs = a.get("segments") or [] + # 时间戳相对 clip 起点,整段都在前奏窗内 + stats = q.intro_filler_stats(segs, intro_sec + 1.0, lang) + hard = q.HARD + fail = (stats["intro_filler_n"] >= hard["intro_filler_min_chars"] + and stats["intro_filler_ratio"] >= hard["intro_filler_ratio"]) + # 前 2s 真唱字(content words)也算开曲违规 + early = 0 + for s in segs: + if float(s.get("t0", 0)) >= 2.0: + continue + early += len(q.content_words(s.get("text") or "", lang)) + if early > 0: + fail = True + return dict( + ok=not fail, + fail=fail, + intro_sec=round(intro_sec, 2), + early_content=early, + asr_head=(a.get("text") or "")[:160], + **stats, + ) + + +if __name__ == "__main__": + try: + job = json.loads(sys.argv[1]) + out = main(job) + except Exception as e: + out = dict(error=str(e), type=type(e).__name__, + trace=traceback.format_exc()[-800:]) + print("###JSON###") + print(json.dumps(out, ensure_ascii=False))