From f39deb184878f6f4318278917ea8504876c64720 Mon Sep 17 00:00:00 2001 From: swl Date: Fri, 9 Oct 2026 19:18:33 +0800 Subject: [PATCH] Add songforge as independent repo with path layout. Co-authored-by: Cursor --- .gitattributes | 1 + .gitignore | 45 + AGENTS.md | 130 ++ README.md | 78 + __init__.py | 24 + cache/.gitkeep | 0 cli/__init__.py | 1 + cli/assemble.py | 139 ++ cli/commands.py | 429 ++++++ cli/common.py | 161 +++ cli/main.py | 164 +++ cli/qc.py | 423 ++++++ cli/selftest.py | 788 ++++++++++ config/__init__.py | 2 + config/baseline/qc_baseline.json | 22 + config/baseline/qc_baseline_old.json | 77 + config/baseline/style_ref.json | 1983 ++++++++++++++++++++++++++ config/default.json | 96 ++ config/lexicon.json | 23 + config/loader.py | 179 +++ config/local.json.example | 11 + config/styles.json | 549 +++++++ core/__init__.py | 0 core/catalog.py | 173 +++ core/engine.py | 120 ++ core/errors.py | 38 + core/paths.py | 119 ++ core/workflow.py | 208 +++ data/.gitkeep | 0 data/runs/.gitkeep | 0 data/tmp/.gitkeep | 0 docs/使用手册.md | 269 ++++ index/.gitkeep | 0 input/lyrics/.gitkeep | 1 + input/lyrics/README.md | 11 + input/lyrics/hiphop_楼下那条街.txt | 66 + input/lyrics/pop_灯还亮着.txt | 65 + input/lyrics/punk_去它妈的班.txt | 68 + log/__init__.py | 0 log/logger.py | 89 ++ lyrics/__init__.py | 0 lyrics/meter.py | 224 +++ lyrics/probe/03_InstLowChill6.txt | 34 + rules/__init__.py | 0 rules/ironlaw.py | 1133 +++++++++++++++ songforge.py | 15 + tools/backup.py | 208 +++ tools/make_baseline.py | 88 ++ tools/make_style_ref.py | 183 +++ tools/qc_style.py | 416 ++++++ tools/qc_worker.py | 1216 ++++++++++++++++ workflows/template.json | 220 +++ 52 files changed, 10289 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 AGENTS.md create mode 100644 README.md create mode 100644 __init__.py create mode 100644 cache/.gitkeep create mode 100644 cli/__init__.py create mode 100644 cli/assemble.py create mode 100644 cli/commands.py create mode 100644 cli/common.py create mode 100644 cli/main.py create mode 100644 cli/qc.py create mode 100644 cli/selftest.py create mode 100644 config/__init__.py create mode 100644 config/baseline/qc_baseline.json create mode 100644 config/baseline/qc_baseline_old.json create mode 100644 config/baseline/style_ref.json create mode 100644 config/default.json create mode 100644 config/lexicon.json create mode 100644 config/loader.py create mode 100644 config/local.json.example create mode 100644 config/styles.json create mode 100644 core/__init__.py create mode 100644 core/catalog.py create mode 100644 core/engine.py create mode 100644 core/errors.py create mode 100644 core/paths.py create mode 100644 core/workflow.py create mode 100644 data/.gitkeep create mode 100644 data/runs/.gitkeep create mode 100644 data/tmp/.gitkeep create mode 100644 docs/使用手册.md create mode 100644 index/.gitkeep create mode 100644 input/lyrics/.gitkeep create mode 100644 input/lyrics/README.md create mode 100644 input/lyrics/hiphop_楼下那条街.txt create mode 100644 input/lyrics/pop_灯还亮着.txt create mode 100644 input/lyrics/punk_去它妈的班.txt create mode 100644 log/__init__.py create mode 100644 log/logger.py create mode 100644 lyrics/__init__.py create mode 100644 lyrics/meter.py create mode 100644 lyrics/probe/03_InstLowChill6.txt create mode 100644 rules/__init__.py create mode 100644 rules/ironlaw.py create mode 100644 songforge.py create mode 100644 tools/backup.py create mode 100644 tools/make_baseline.py create mode 100644 tools/make_style_ref.py create mode 100644 tools/qc_style.py create mode 100644 tools/qc_worker.py create mode 100644 workflows/template.json diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..e14f63f --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +# archive/full-snapshot 可含 flac/cache;该分支仅本地备份,禁止默认 push diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..905be4f --- /dev/null +++ b/.gitignore @@ -0,0 +1,45 @@ +# --- 本机覆盖(勿提交)--- +config/local.json + +# --- 过程与缓存(保留 .gitkeep 占位)--- +data/** +!data/.gitkeep +!data/runs/ +!data/runs/.gitkeep +!data/tmp/ +!data/tmp/.gitkeep +tmp/ +cache/** +!cache/.gitkeep +runs/ +index/catalog.jsonl +index/by_day/ + +# --- 音频 / 误放报告 --- +*.flac +*.wav +*.mp3 +qc.json + +# --- 大体量归档(进 archive 分支,不进 main)--- +_archive/ +.bak/ +bak/ + +# --- Python --- +__pycache__/ +*.py[cod] +*.egg-info/ +.pytest_cache/ +.codebase-index/ + +# --- 编辑器 / OS --- +.DS_Store +Thumbs.db +.idea/ +.vscode/ +*.swp + +# --- 索引占位 --- +!index/.gitkeep +!input/lyrics/.gitkeep diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..99781d6 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,130 @@ +# songforge — Agent 生歌手册 + +> 远程 / channel Agent 读这份文件即可做歌。上下文不够时**重新打开本文件**,不要凭记忆改参数。 +> 人类规范全文见 `../生歌规则.md`。行为冲突时以那份 + `config/default.json` 为准。 + +## 0. 你是谁、要交什么 + +你是 songforge 流水线操作员。交付物是: + +1. 一次 `gen` 的 `run_id` +2. ComfyUI 产出的 flac +3. `qc --baseline` 结论为 **PASS** + +`style_fam` 只是弱参考,不要把它当 PASS 条件,也不要对外说「风格已验证」。 + +## 1. 工作目录(硬) + +```text +cd D:\CodeFiles\Thinking\ComfyUI-MiniMaxH3\user\default\workflows +``` + +唯一入口: + +```text +python songforge\songforge.py <命令> +``` + +禁止:手写 tags / 手写前奏 / 改 workflow JSON 调参 / 换 YuE2·Music3·aio / 放宽 QC 阈值。 + +## 2. 强制流水线(按顺序) + +```text +doctor → target → 写歌词 → check → gen → qc --baseline +``` + +```powershell +python songforge\songforge.py doctor +python songforge\songforge.py target --style pop --bpm 104 --dur 180 +# 按 target 打印的放行区间写歌词(勿手写 [intro] / tags) +python songforge\songforge.py check --style pop --title 歌名 --lyrics-file <路径.txt> --bpm 104 --dur 180 +python songforge\songforge.py gen --style pop --title 歌名 --lyrics-file <路径.txt> --bpm 104 --dur 180 +python songforge\songforge.py qc --run --baseline +``` + +- `check` 不过 → **只改歌词**,不准改 shift/steps/cfg/temperature +- `doctor` 服务不可达 → 先起 ComfyUI(`http://127.0.0.1:8188`) +- `qc` 必须带 `--baseline`;`--run` 用 `gen-...` 或 `songforge\data\runs\<日>\gen-...` 均可 +- 索引:`python songforge\songforge.py catalog` / `catalog --rebuild` + +## 3. 硬锁定(不可谈判) + +| 项 | 值 | +|----|-----| +| checkpoint | `ace_step_1.5_turbo_swl_fused.safetensors` | +| LoRA | `swl_voice_best_ep257.safetensors`(0.5 / 0.5) | +| 采样 | shift=5.5 / steps=28 / cfg=2.0 / temperature=0.68 | +| 时长 | 完整歌 **150–200** 秒(默认跟风格字典;pop 常 180) | +| 前奏 | 8 小节器乐 hook,第一拍进鼓;CLI `assemble` 自动注入 | + +改算法参数只动 `config/default.json`;本机路径用 `config/local.json`(gitignore)。不要改 `data/runs/` 里的 JSON。 + +配置优先级: + +```text +命令行 > SONGFORGE_* > config/local.json > config/default.json > DEFAULTS +``` + +## 4. 目录地图(项目 vs 数据) + +路径键在 `config/default.json` → `paths`。相对 `user/default/workflows`: + +| 级别 | 用途 | 默认路径 | +|------|------|----------| +| 项目 | 歌词投放 | `songforge/input/lyrics/` | +| 项目 | QC/风格基线 | `songforge/config/baseline/` | +| 项目 | 规范文档 | `生歌规则.md` / `AGENTS.md` / `docs/使用手册.md` | +| 数据 | 正式运行 | `songforge/data/runs/YYYY-MM-DD/gen-*/` | +| 数据 | 临时会话 | `songforge/data/tmp/YYYY-MM-DD/` | +| 数据 | 全局索引 | `songforge/index/catalog.jsonl` | +| 数据 | QC/风格缓存 | `songforge/cache/qc/` `cache/style/` | +| 外部 | 音频成品 | `/output/MyMusic/`(`local.json` 可改) | + +本机路径写 `config/local.json`(从 `local.json.example` 复制):`comfy_root` / `qc_python` / `music_dir`。 + +## 5. 写歌词要点 + +1. 先 `target`,正文音节落进放行区间(偏稀 → 模型用拖腔填空)。 +2. **不要**写 `[intro]` / Instrumental 前奏 / tags——`assemble` 会注入。 +3. 段落用 `[verse]` `[chorus]` 等英文结构标记。 +4. 默认密度档 = 人声填满;英文低音吟唱用 `--chill-vocal`(自动 sparse)。 + +可用素材示例(区间已合适): + +```text +songforge\_archive\lyrics_corpus\拍一下.txt +``` + +## 6. 失败速查 + +| 现象 | 动作 | +|------|------| +| doctor FAIL 服务 | 启动 ComfyUI | +| check 密度失败 | 改歌词到 target 区间 | +| gen 提交失败 | 跑 `selftest`;用 `--dry-run` 看 payload | +| qc voice_nn=null | 漏了 `--baseline` | +| qc FAIL | 修歌/重 gen,**不**改阈值 | +| CLI 崩溃 | `--dry-run` 或 fallback 旧 `remake_v5.py`(勿删) | + +怀疑 CLI 坏了: + +```powershell +python songforge\songforge.py selftest +``` + +## 7. 上下文不够时 + +1. 重读本文件第 1–3 节。 +2. 打开 `songforge/config/default.json` 核对模型/采样/paths。 +3. 打开本次 `runs//run.log` 与 `manifest.json`。 +4. 不要发明新采样参数或新模型名。 + +## 8. 最小验收清单 + +```text +[ ] doctor 全绿(模型锁定 + 采样在带内) +[ ] check OK +[ ] gen 产出 flac,时长在 150–200 +[ ] qc --baseline → [PASS] +[ ] 未改阈值、未换模型 +``` diff --git a/README.md b/README.md new file mode 100644 index 0000000..6aec382 --- /dev/null +++ b/README.md @@ -0,0 +1,78 @@ +# songforge + +本地创作者用的 AI 歌曲生成 CLI。固定模型 + 铁律注入 + 密度硬校验 + QC 验收,稳定产出带专属声线的完整歌曲。 + +本目录是**独立 Git 仓**(`main` 只跟踪项目资产;大体量过程数据在 `archive/full-snapshot`,勿默认 push)。 + +| 文档 | 给谁 | +|------|------| +| [`AGENTS.md`](AGENTS.md) | **远程 / channel Agent**(上下文不够时重读这份做歌) | +| [`../生歌规则.md`](../生歌规则.md) | 参数/铁律/踩坑唯一规范 | +| [`docs/使用手册.md`](docs/使用手册.md) | 人类操作手册 | + +## 30 秒上手 + +工作目录必须是 `user/default/workflows`: + +```powershell +cd D:\CodeFiles\Thinking\ComfyUI-MiniMaxH3\user\default\workflows + +python songforge\songforge.py doctor +python songforge\songforge.py target --style pop --bpm 104 --dur 180 +python songforge\songforge.py check --style pop --title 歌名 --lyrics-file songforge\input\lyrics\歌名.txt +python songforge\songforge.py gen --style pop --title 歌名 --lyrics-file songforge\input\lyrics\歌名.txt +python songforge\songforge.py qc --run gen-<时间戳> --baseline +python songforge\songforge.py catalog --limit 20 +``` + +`qc` 必须带 `--baseline`。首次本机复制 `config/local.json.example` → `config/local.json` 填 Comfy 根与 QC Python。 + +## 硬约束(摘要) + +| 项 | 值 | +|----|-----| +| 模型 | `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–200 秒 | +| 前奏 | 8 小节器乐 hook;CLI 自动注入 | +| 配置 | 算法锁 `config/default.json`;本机路径 `config/local.json` | + +## 项目资产 vs 过程数据 + +| 级别 | 路径 | 是否进 `main` | +|------|------|----------------| +| 项目 | `cli/` `core/` `config/*.json`(除 local) `config/baseline/` `workflows/` `docs/` | 是 | +| 本机 | `config/local.json` | 否(gitignore) | +| 过程 | `data/runs/YYYY-MM-DD/gen-*/` `data/tmp/` | 否 | +| 索引 | `index/catalog.jsonl` | 否(可 `--rebuild`) | +| 缓存 | `cache/qc` `cache/style` | 否 | +| 音频 | ComfyUI `output/MyMusic/` | 不在包内 | + +``` +songforge/ + AGENTS.md / README.md / songforge.py + config/ # default + styles + lexicon + local.example + baseline/ # qc_baseline*.json + style_ref.json + local.json # 本机路径(勿提交) + cli/ core/ rules/ lyrics/ + input/lyrics/ # 新歌词投放 + data/runs/YYYY-MM-DD/ # gen 正式产物 + data/tmp/YYYY-MM-DD/ # doctor/check 会话 + index/catalog.jsonl # 全局元数据索引 + cache/ # QC / 风格特征 + docs/ _archive/ +``` + +## 配置优先级 + +``` +命令行 > SONGFORGE_* 环境变量 > config/local.json > config/default.json > DEFAULTS +``` + +模型与采样铁律只在 `default.json`,不要放进 local。 + +## 日志 + +- `gen` → `data/runs/<日>//run.log`(JSONL) +- `doctor` / `check` → `data/tmp/<日>/-*/run.log` +- `--log-level` / `--quiet` 全局生效 diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..b998c1a --- /dev/null +++ b/__init__.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +"""songforge 核心包重写 - 修复相对导入与模块管理""" + +import os +import sys + +# 确保正确添加工程根目录 +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +# 统一重写 core/__init__.py -> core +from .core.workflow import build_workflow, convert, find_node, ROOT, WIDGET_MAP +from .core.errors import RuleViolation, LexiconError +from .rules.ironlaw import build_tags, build_lyrics, validate, TRIGGER +from .config.loader import load + +__version__ = "1.0.0" + +# 保持微内核结构:CLI -> Rules -> Lyrics -> Core -> Log -> Config +__all__ = [ + 'build_workflow', 'convert', 'find_node', 'ROOT', 'WIDGET_MAP', + 'RuleViolation', 'LexiconError', + 'build_tags', 'build_lyrics', 'validate', 'TRIGGER', + 'load' +] diff --git a/cache/.gitkeep b/cache/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/cli/__init__.py b/cli/__init__.py new file mode 100644 index 0000000..c28a133 --- /dev/null +++ b/cli/__init__.py @@ -0,0 +1 @@ +from .main import main diff --git a/cli/assemble.py b/cli/assemble.py new file mode 100644 index 0000000..1070981 --- /dev/null +++ b/cli/assemble.py @@ -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 + diff --git a/cli/commands.py b/cli/commands.py new file mode 100644 index 0000000..590e710 --- /dev/null +++ b/cli/commands.py @@ -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 diff --git a/cli/common.py b/cli/common.py new file mode 100644 index 0000000..6c5dff9 --- /dev/null +++ b/cli/common.py @@ -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。 + + 旧实现只到秒,连跑三首会落进同一个 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/-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)) + diff --git a/cli/main.py b/cli/main.py new file mode 100644 index 0000000..8cf9b35 --- /dev/null +++ b/cli/main.py @@ -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()) diff --git a/cli/qc.py b/cli/qc.py new file mode 100644 index 0000000..c04595b --- /dev/null +++ b/cli/qc.py @@ -0,0 +1,423 @@ +# -*- coding: utf-8 -*- +"""songforge qc —— 生成后验收。 + + songforge qc --run 验收某次运行产出的所有 FLAC + songforge qc --run --baseline 额外跑 8 首已认可老歌作对照 + songforge qc --file --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/ + 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 \ No newline at end of file diff --git a/cli/selftest.py b/cli/selftest.py new file mode 100644 index 0000000..fab1c30 --- /dev/null +++ b/cli/selftest.py @@ -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 + diff --git a/config/__init__.py b/config/__init__.py new file mode 100644 index 0000000..2d9e000 --- /dev/null +++ b/config/__init__.py @@ -0,0 +1,2 @@ +# -*- coding: utf-8 -*- +"""配置包。JSON 为单一事实来源,避免引入 tomllib 之类的额外依赖。""" diff --git a/config/baseline/qc_baseline.json b/config/baseline/qc_baseline.json new file mode 100644 index 0000000..412e124 --- /dev/null +++ b/config/baseline/qc_baseline.json @@ -0,0 +1,22 @@ +{ + "base": { + "n_old": 8, + "coverage": 0.5065, + "voice_self": 0.9379, + "voice_floor": 0.8861, + "dynamic_db": 10.56, + "centroid": 1815.6, + "f0_drift_st": 1.5, + "tempo_drift": 0.0 + }, + "soft": { + "coverage": 0.4305, + "voice_nn": 0.8661, + "voice_self": 0.868, + "dyn_lo": 7.22, + "centroid_lo": 1367.6, + "f0_drift_st_max": 2.5, + "tempo_drift_max": 0.05 + }, + "note": "由 songforge qc --baseline-only --save-baseline 在 8 首已认可老歌上校准,不要手改" +} \ No newline at end of file diff --git a/config/baseline/qc_baseline_old.json b/config/baseline/qc_baseline_old.json new file mode 100644 index 0000000..eca52b6 --- /dev/null +++ b/config/baseline/qc_baseline_old.json @@ -0,0 +1,77 @@ +{ + "note": "8 首用户已认可老歌的基线资料,由 tools/make_baseline.py 从 remake_v5.py 抽取", + "items": [ + { + "title": "亮灯就跳", + "file": "v5_亮灯就跳_swl_00001.flac", + "bpm": 100, + "key": "F# minor", + "duration": 112, + "group": "old", + "lyrics": "亮灯就跳 亮灯就跳\n手举起来 脚踩下来\n全世界看见\n鞋底擦出火星子\n一步踩到点上\n他们说我在装\n我说我这叫登场\n夜是我的舞台\n光打在我肩膀\n心里的火 烧出了边\n一 二 三 四 灯亮了\n五 六 七 八 我先到\n左脚右脚 在打仗\n同手同脚 叫对称\n别眨眼 现在开始\n这一条街 我最闪亮" + }, + { + "title": "干饭魂", + "file": "v5_干饭魂_swl_00001.flac", + "bpm": 112, + "key": "G major", + "duration": 115, + "group": "old", + "lyrics": "干饭魂 干饭魂\n碗一端 世界安静了\n吃饭都是人上人\n上午十点半 我还在工位\n魂已经先飘到食堂门口\n报表它不会自己写\n饭它会自己凉\n谁挡在我前面\n我跟谁急一场\n筷子功 夹遍南北菜\n风卷残云 你刚动筷我已结束\n火锅咕嘟咕嘟\n那是世界最美的节奏\n吃饱了才有力气温" + }, + { + "title": "快乐废物指南", + "file": "v5_快乐废物指南_swl_00001.flac", + "bpm": 116, + "key": "A major", + "duration": 118, + "group": "old", + "lyrics": "快乐废物 快乐废物\n第一条 先睡饱\n闹钟吵不过我的懒觉\n闹钟响了第三遍\n我假装我没听见\n手机亮了二十条\n全是明天见\n老板说要奋斗\n我点了个赞 翻个身\n第二条 吃最好\n什么KPI 给我靠边靠\n一天二十四小时\n睡觉占八小时\n我再吃三小时\n还剩两小时发呆\n怎么了 犯法了吗\n我不偷不抢不坑不骗\n第三条 别烦恼\n我就是最棒的废物" + }, + { + "title": "拜师记", + "file": "v5_拜师记_swl_00001.flac", + "bpm": 92, + "key": "Eb major", + "duration": 115, + "group": "old", + "lyrics": "师父他太狠\n一个isolation定三帧\n我学得膝盖疼\n多跳 别怕出丑\n三年前我四肢健全\n走路带风以为行\n直到我推开那扇门\n看见我师父的身影\n他一顶帽子压低\n眼神扫过像点名\n他脚一滑 地板都得让路\n他手一挥 空气学会跳舞\n一个wave 从指尖滚到肩\n我嘴张成大圆盘\n我的师父他是神\n街舞urban样样真\n慢 半拍 慢 半拍\n慢 半拍 就对了\n我也能像你一样神" + }, + { + "title": "晒太阳", + "file": "v5_晒太阳_swl_00001.flac", + "bpm": 88, + "key": "C major", + "duration": 112, + "group": "old", + "lyrics": "晒太阳 晒太阳\n把烦恼晒光\n心是暖的\n窗帘拉开一半\n阳光爬上床\n今天不设闹钟\n让它响不到\n外套挂在门口\n鞋摆成一双\n出门不为别的\n就为晒一晒\n今天什么都不想\n一 二 三 四 慢慢晃过来\n五 六 七 八 影子跟着我\n不用比谁快\n我就慢慢走\n阳光落肩上\n什么都不用求\n风一吹 什么都不为难" + }, + { + "title": "晚风不加班", + "file": "v5_晚风不加班_swl_00001.flac", + "bpm": 86, + "key": "D minor", + "duration": 112, + "group": "old", + "lyrics": "晚风不加班\n风里没有KPI\n今晚不聊deadline\n地铁到站我挤上去\n手机震动没心思看\n今日KPI又爆表\n清单长得看不见\n可是傍晚的风一吹\n心里那堵墙就塌了半截\n我们都是普通人\n但今晚不普通\n一 二 三 四 慢慢数别急\n五 六 七 八 呼吸还来得及\n把鞋脱了\n让脚趾碰到草地\n明天的事 明天再说" + }, + { + "title": "跳起来", + "file": "v5_跳起来_swl_00001.flac", + "bpm": 124, + "key": "E major", + "duration": 110, + "group": "old", + "lyrics": "跳起来 跳起来\n别管多累\n手脚甩开就对了\n闹钟一响我就弹起来\n枕头甩到一旁\n今天的身体有点痒\n想出门晒太阳\n地铁里站着打拍子\n忍不住要开口\n心情放飞\n左一下 右一下\n一 二 三 四 蹦起来\n五 六 七 八 甩开摆\n左 右 左 右 转身再回来\n一句 一句 别停嗨\n衣服挑最亮的那件\n鞋子踩得响亮的" + }, + { + "title": "身体先听懂", + "file": "v5_身体先听懂_swl_00001.flac", + "bpm": 94, + "key": "F minor", + "duration": 115, + "group": "old", + "lyrics": "身体先听懂\n一拍 一拍 又一拍\n往下踩 往下踩\n灯亮了 我先不动\n肩膀先知道 往哪沉\n右手抬到一半停\n那个停顿 也算一声\n跟着重心 别跟声音\n慢半拍 才是对的\n一 二 三 四 从脚下数\n五 六 七 八 重心往右\n不用想 不用编\n身体比脑子 先跑一圈\n一个动作 三千遍\n肌肉自己 会归位\n停下 就继续走" + } + ] +} \ No newline at end of file diff --git a/config/baseline/style_ref.json b/config/baseline/style_ref.json new file mode 100644 index 0000000..2bbe203 --- /dev/null +++ b/config/baseline/style_ref.json @@ -0,0 +1,1983 @@ +{ + "features": [ + "tempo", + "tempo_drift", + "centroid", + "rolloff", + "hf_ratio", + "lf_ratio", + "dynamic", + "onset_density", + "perc_ratio", + "flatness", + "crest" + ], + "med": [ + 117.1875, + 0.052631591796875, + 3.2844445889496363, + 3.6056763324950767, + 0.008211818058043718, + 0.47975701093673706, + 10.742749214172363, + 4.5847687400318975, + 0.41448549926280975, + 0.29660020768642426, + 14.803324680093596 + ], + "iqr": [ + 45.54656219482422, + 0.07554943179175955, + 0.10471663488844207, + 0.11359306901352229, + 0.006183079676702619, + 0.1360929310321808, + 8.56806492805481, + 1.0971590909090905, + 0.07339391112327576, + 0.08154536038637161, + 1.5382478894268132 + ], + "centroids": { + "pop": [ + 0.1992708299208649, + 0.5501476341948551, + -0.15149582122845037, + -0.20076370778933866, + -0.02233082198551184, + 0.05805999797219805, + 0.44965096863392756, + -0.016555076406670522, + 0.19566786900995697, + -0.17798565379141534, + 0.05582759279607705 + ], + "punk": [ + 0.38987906742211315, + 2.6092697149376485, + 0.6481440811571101, + 0.6352696022179899, + 0.49681115534955284, + -0.20313948283292999, + 0.03884292447842247, + 0.14268589005154772, + -0.08378694572029426, + 0.7818985321331862, + -0.6011053651274529 + ], + "hiphop": [ + -0.2285709224592324, + -0.4789641216238817, + -0.14735333275692636, + -0.14602506397259601, + -0.2433798135081831, + 0.460165846166042, + 0.8998898915490297, + 0.22758865054921137, + 0.46618419647450693, + -0.039475568060289255, + 0.5215936287096421 + ] + }, + "counts": { + "pop": 35, + "punk": 7, + "hiphop": 16 + }, + "items": [ + { + "file": "chillhiphop_慢慢来_swl_00001.flac", + "family": "hiphop", + "group": "chillhiphop_慢慢来", + "bpm": null, + "vec": { + "tempo": 93.75, + "tempo_drift": 0.0, + "centroid": 3.2285122193217637, + "rolloff": 3.551502525401678, + "hf_ratio": 0.004966275300830603, + "lf_ratio": 0.5576391220092773, + "dynamic": 18.289714813232422, + "onset_density": 4.345454545454546, + "perc_ratio": 0.4086672365665436, + "flatness": 0.26705601811408997, + "crest": 15.12218081117133 + }, + "z": [ + -0.5145832938992566, + -0.6966510607511376, + -0.5341307012726215, + -0.47691120209939813, + -0.5249071541875933, + 0.5722715388804738, + 0.8808249776852975, + -0.21812168951638497, + -0.07927446033627443, + -0.36230374643450475, + 0.2072852712943081 + ] + }, + { + "file": "chillhiphop_晒太阳_swl_00001.flac", + "family": "hiphop", + "group": "chillhiphop_晒太阳", + "bpm": null, + "vec": { + "tempo": 98.68421173095703, + "tempo_drift": 0.055555516899957066, + "centroid": 3.2375362704645605, + "rolloff": 3.55056204362949, + "hf_ratio": 0.003734287340193987, + "lf_ratio": 0.5872673392295837, + "dynamic": 18.744400024414062, + "onset_density": 4.581818181818182, + "perc_ratio": 0.4533482789993286, + "flatness": 0.2691720128059387, + "crest": 14.618332811639977 + }, + "z": [ + -0.4062499424192685, + 0.03870214551899499, + -0.4479547928086949, + -0.4851905960831623, + -0.7241586639617037, + 0.789977315334803, + 0.9338924106470675, + -0.0026892710803414033, + 0.5295095893069817, + -0.33635506361769063, + -0.12026141542280987 + ] + }, + { + "file": "chillhiphopen_GoldenHour_swl_00001.flac", + "family": "hiphop", + "group": "chillhiphopen_GoldenHour", + "bpm": null, + "vec": { + "tempo": 98.68421173095703, + "tempo_drift": 0.027026989416174447, + "centroid": 3.233725586219065, + "rolloff": 3.5644902852901192, + "hf_ratio": 0.004516105633229017, + "lf_ratio": 0.4764126241207123, + "dynamic": 52.103553771972656, + "onset_density": 5.248484848484848, + "perc_ratio": 0.3843671679496765, + "flatness": 0.2638770639896393, + "crest": 15.083668556928258 + }, + "z": [ + -0.4062499424192685, + -0.3389119120217307, + -0.48434523115266104, + -0.36257535395979656, + -0.5977138607383419, + -0.024574287515594477, + 4.827321560364314, + 0.6049406270726015, + -0.41036553103901385, + -0.40128762129125223, + 0.1822488291787123 + ] + }, + { + "file": "chillhiphopen_NeonSteps_swl_00001.flac", + "family": "hiphop", + "group": "chillhiphopen_NeonSteps", + "bpm": null, + "vec": { + "tempo": 125.0, + "tempo_drift": 0.0625, + "centroid": 3.266787517999659, + "rolloff": 3.5737629263830604, + "hf_ratio": 0.005544074811041355, + "lf_ratio": 0.5873686075210571, + "dynamic": 18.692474365234375, + "onset_density": 4.03030303030303, + "perc_ratio": 0.38131386041641235, + "flatness": 0.26602157950401306, + "crest": 15.28609407454558 + }, + "z": [ + 0.17152776463308553, + 0.13062187191990743, + -0.16861763146598335, + -0.28094501177899595, + -0.4314586559597833, + 0.7907214266615658, + 0.9278320388343301, + -0.5053649140977766, + -0.45196717736817166, + -0.37498918439413376, + 0.3138436904547785 + ] + }, + { + "file": "chillhiphopja_NeonSenpuu_swl_00001.flac", + "family": "hiphop", + "group": "chillhiphopja_NeonSenpuu", + "bpm": null, + "vec": { + "tempo": 117.1875, + "tempo_drift": 0.0, + "centroid": 3.2133403449236715, + "rolloff": 3.533349315452303, + "hf_ratio": 0.0036370735615491867, + "lf_ratio": 0.49130600690841675, + "dynamic": 15.235994338989258, + "onset_density": 3.909090909090909, + "perc_ratio": 0.4092845320701599, + "flatness": 0.24425476789474487, + "crest": 16.104081474231958 + }, + "z": [ + 0.0, + -0.6966510607511376, + -0.6790157466549075, + -0.6367203357641827, + -0.7398812138442638, + 0.08486110104388002, + 0.5244177258863264, + -0.6158430773983117, + -0.07086374214223384, + -0.6419180630714054, + 0.8456093475434925 + ] + }, + { + "file": "chillhiphopja_Yunagi_swl_00001.flac", + "family": "hiphop", + "group": "chillhiphopja_Yunagi", + "bpm": null, + "vec": { + "tempo": 93.75, + "tempo_drift": 0.0, + "centroid": 3.2521634611707015, + "rolloff": 3.592538489698719, + "hf_ratio": 0.006265212781727314, + "lf_ratio": 0.6148689985275269, + "dynamic": 13.50181770324707, + "onset_density": 4.975757575757576, + "perc_ratio": 0.4102462828159332, + "flatness": 0.28353482484817505, + "crest": 14.861704913281073 + }, + "z": [ + -0.5145832938992566, + -0.6966510607511376, + -0.3082712485301392, + -0.11565708110935534, + -0.3148277845506451, + 0.9927921058503837, + 0.3220176915373928, + 0.3563647596463976, + -0.05775978391117684, + -0.1602222710935836, + 0.03795242209578568 + ] + }, + { + "file": "col2_下课铃_swl_00001.flac", + "family": "pop", + "group": "col2_下课铃", + "bpm": null, + "vec": { + "tempo": 93.75, + "tempo_drift": 0.052631591796875, + "centroid": 3.2273592775657693, + "rolloff": 3.5359454521221134, + "hf_ratio": 0.004441735800355673, + "lf_ratio": 0.4665927588939667, + "dynamic": 16.466594696044922, + "onset_density": 4.339285714285714, + "perc_ratio": 0.42973580956459045, + "flatness": 0.25373542308807373, + "crest": 15.547851230521513 + }, + "z": [ + -0.5145832938992566, + 0.0, + -0.5451408121038434, + -0.6138656255925474, + -0.6097418203898347, + -0.09672987379232462, + 0.6680441301431678, + -0.22374423889864442, + 0.2077871320437684, + -0.5256557135225361, + 0.484009473080015 + ] + }, + { + "file": "col2_夏末的风_swl_00001.flac", + "family": "pop", + "group": "col2_夏末的风", + "bpm": null, + "vec": { + "tempo": 133.92857360839844, + "tempo_drift": 0.06666668185763865, + "centroid": 3.3396972484077265, + "rolloff": 3.66837738004019, + "hf_ratio": 0.010783138684928417, + "lf_ratio": 0.4334481954574585, + "dynamic": 8.489031791687012, + "onset_density": 4.635593220338983, + "perc_ratio": 0.42584291100502014, + "flatness": 0.3343684673309326, + "crest": 14.525249543202442 + }, + "z": [ + 0.36755954350163544, + 0.1857736018379228, + 0.527639753864824, + 0.55197951855363, + 0.41586406149240523, + -0.3402734817161686, + -0.2630369215697585, + 0.04632371068900568, + 0.154745966911805, + 0.46315645017151014, + -0.1807739433952814 + ] + }, + { + "file": "discofunk_快乐废物指南_swl_00001.flac", + "family": "hiphop", + "group": "discofunk_快乐废物指南", + "bpm": null, + "vec": { + "tempo": 117.1875, + "tempo_drift": 0.0, + "centroid": 3.3359222411036322, + "rolloff": 3.667882104866587, + "hf_ratio": 0.011054806411266327, + "lf_ratio": 0.4792029559612274, + "dynamic": 16.033931732177734, + "onset_density": 5.612121212121212, + "perc_ratio": 0.5610005855560303, + "flatness": 0.3622171878814697, + "crest": 15.724498690119656 + }, + "z": [ + 0.0, + -0.6966510607511376, + 0.4915900153670587, + 0.5476194358663318, + 0.4598013452640398, + -0.004071151758636388, + 0.6175469680067676, + 0.9363751169742075, + 1.996283943052539, + 0.8046684677600837, + 0.598846269419756 + ] + }, + { + "file": "dy10_凌晨便利店_swl_00001.flac", + "family": "pop", + "group": "dy10_凌晨便利店", + "bpm": null, + "vec": { + "tempo": 170.4545440673828, + "tempo_drift": 0.04347828811424388, + "centroid": 3.186939837648591, + "rolloff": 3.491418497461018, + "hf_ratio": 0.004841574002057314, + "lf_ratio": 0.6650972962379456, + "dynamic": 19.49887466430664, + "onset_density": 4.396551724137931, + "perc_ratio": 0.38722652196884155, + "flatness": 0.21548528969287872, + "crest": 15.741543344784336 + }, + "z": [ + 1.16950745567875, + -0.12115648609854275, + -0.9311295326183855, + -1.005852170614892, + -0.5450753074855612, + 1.3618656303124415, + 1.0219490075832283, + -0.17154942929745307, + -0.3714065223773506, + -0.9947214361333792, + 0.6099268337305132 + ] + }, + { + "file": "dy10_城市晚安_swl_00001.flac", + "family": "pop", + "group": "dy10_城市晚安", + "bpm": null, + "vec": { + "tempo": 156.25, + "tempo_drift": 0.076923046875, + "centroid": 3.139914730218581, + "rolloff": 3.4173684731112397, + "hf_ratio": 0.0043159592896699905, + "lf_ratio": 0.7838333249092102, + "dynamic": 31.644577026367188, + "onset_density": 4.288135593220339, + "perc_ratio": 0.47393372654914856, + "flatness": 0.1928279846906662, + "crest": 14.766562029544373 + }, + "z": [ + 0.8576388231654276, + 0.3215306125012386, + -1.380199610931231, + -1.6577407496703918, + -0.6300838695404544, + 2.234328496463719, + 2.4395039005546058, + -0.2703647531788415, + 0.809988545050922, + -1.2725705362521291, + -0.023899041761676988 + ] + }, + { + "file": "dy10_夏日海岸线_swl_00001.flac", + "family": "pop", + "group": "dy10_夏日海岸线", + "bpm": null, + "vec": { + "tempo": 117.1875, + "tempo_drift": 0.06666666666666667, + "centroid": 3.1993973091250743, + "rolloff": 3.4888549691445636, + "hf_ratio": 0.0037443717010319233, + "lf_ratio": 0.5462307333946228, + "dynamic": 13.917730331420898, + "onset_density": 6.7272727272727275, + "perc_ratio": 0.6243692636489868, + "flatness": 0.24873967468738556, + "crest": 16.09935520773178 + }, + "z": [ + 0.0, + 0.18577340076464374, + -0.8121658981418339, + -1.0284198179081374, + -0.7225277031193369, + 0.48844360947863813, + 0.37055988066249923, + 1.952774219339131, + 2.8596890555900027, + -0.5869191425762276, + 0.8425368476345609 + ] + }, + { + "file": "dy10_失物招领_swl_00001.flac", + "family": "pop", + "group": "dy10_失物招领", + "bpm": null, + "vec": { + "tempo": 170.4545440673828, + "tempo_drift": 0.047619043356274766, + "centroid": 3.0984946932154935, + "rolloff": 3.3819566660079947, + "hf_ratio": 0.006834604311734438, + "lf_ratio": 0.6410614252090454, + "dynamic": 19.889896392822266, + "onset_density": 3.3043478260869565, + "perc_ratio": 0.33159705996513367, + "flatness": 0.1541999876499176, + "crest": 16.11536791590542 + }, + "z": [ + 1.16950745567875, + -0.06634793037777648, + -1.77574361449106, + -1.969483423856169, + -0.2227391232719382, + 1.1852519675262634, + 1.0675861183893434, + -1.1670330442998949, + -1.129363976235752, + -1.7462700435904321, + 0.8529465535627824 + ] + }, + { + "file": "dy10_小确幸_swl_00001.flac", + "family": "pop", + "group": "dy10_小确幸", + "bpm": null, + "vec": { + "tempo": 93.75, + "tempo_drift": 0.052631591796875, + "centroid": 3.1607549284659577, + "rolloff": 3.4816095039320007, + "hf_ratio": 0.0034079093020409346, + "lf_ratio": 0.5239073634147644, + "dynamic": 7.889646530151367, + "onset_density": 4.43859649122807, + "perc_ratio": 0.38223299384117126, + "flatness": 0.2012694776058197, + "crest": 16.558566653877854 + }, + "z": [ + -0.5145832938992566, + 0.0, + -1.1811844471076545, + -1.0922042131664462, + -0.7769443395826763, + 0.32441326778087737, + -0.33299265446494813, + -0.13322794298018442, + -0.4394438847585832, + -1.169051551540348, + 1.1410657448964887 + ] + }, + { + "file": "dy10_少年模样_swl_00001.flac", + "family": "pop", + "group": "dy10_少年模样", + "bpm": null, + "vec": { + "tempo": 104.16666412353516, + "tempo_drift": 0.0, + "centroid": 3.2736541815851163, + "rolloff": 3.6244984549738715, + "hf_ratio": 0.005957063753157854, + "lf_ratio": 0.6986815929412842, + "dynamic": 54.92849349975586, + "onset_density": 5.116071428571429, + "perc_ratio": 0.6207976341247559, + "flatness": 0.2768636643886566, + "crest": 14.150639947925487 + }, + "z": [ + -0.2858796635576701, + -0.6966510607511376, + -0.10304387049884989, + 0.16569780746529672, + -0.3646652514250479, + 1.6086403631999064, + 5.157027246712858, + 0.4842530977884907, + 2.811025215912187, + -0.24203146818229204, + -0.4243039998002621 + ] + }, + { + "file": "dy10_心动预警_swl_00001.flac", + "family": "pop", + "group": "dy10_心动预警", + "bpm": null, + "vec": { + "tempo": 110.29412078857422, + "tempo_drift": 0.0, + "centroid": 3.247256133886657, + "rolloff": 3.5510704339255916, + "hf_ratio": 0.00860290415585041, + "lf_ratio": 0.7066041231155396, + "dynamic": 12.088227272033691, + "onset_density": 6.0, + "perc_ratio": 0.5327998995780945, + "flatness": 0.2721956670284271, + "crest": 14.073777240162515 + }, + "z": [ + -0.15134795864371792, + -0.6966510607511376, + -0.3551341685358523, + -0.48071505632957845, + 0.06325102024485876, + 1.6668544828765706, + 0.15703406418592455, + 1.2899052395359198, + 1.6120465377101716, + -0.2992756490665504, + -0.4742716989541435 + ] + }, + { + "file": "dy10_快乐摆烂_swl_00001.flac", + "family": "pop", + "group": "dy10_快乐摆烂", + "bpm": null, + "vec": { + "tempo": 98.68421173095703, + "tempo_drift": 0.055555516899957066, + "centroid": 3.2288739819383188, + "rolloff": 3.5686216691096826, + "hf_ratio": 0.003509131958708167, + "lf_ratio": 0.40344753861427307, + "dynamic": 50.543968200683594, + "onset_density": 3.981818181818182, + "perc_ratio": 0.4609459638595581, + "flatness": 0.24856051802635193, + "crest": 17.545226384083183 + }, + "z": [ + -0.4062499424192685, + 0.03870214551899499, + -0.530676019817851, + -0.3262053196307519, + -0.7605734270342529, + -0.5607159148069175, + 4.645298479962292, + -0.5495561794179906, + 0.6330288696389438, + -0.589116161023197, + 1.782483644434762 + ] + }, + { + "file": "dy10_烟雨行舟_swl_00001.flac", + "family": "pop", + "group": "dy10_烟雨行舟", + "bpm": null, + "vec": { + "tempo": 89.28571319580078, + "tempo_drift": 0.09545455438787298, + "centroid": 3.153323633719368, + "rolloff": 3.440668471194388, + "hf_ratio": 0.0013901788042858243, + "lf_ratio": 0.6352135539054871, + "dynamic": 52.18021774291992, + "onset_density": 3.8813559322033897, + "perc_ratio": 0.40480971336364746, + "flatness": 0.1912137120962143, + "crest": 15.481944327380981 + }, + "z": [ + -0.6125991833335316, + 0.5668204455730775, + -1.252150199153703, + -1.4526226180317912, + -1.1032753272550118, + 1.1422822757193059, + 4.836269201586807, + -0.6411219791704682, + -0.1318336323964314, + -1.2923665441035053, + 0.44116403601259296 + ] + }, + { + "file": "dy10_逆风飞行_swl_00001.flac", + "family": "pop", + "group": "dy10_逆风飞行", + "bpm": null, + "vec": { + "tempo": 144.23077392578125, + "tempo_drift": 0.0714285865420382, + "centroid": 3.203827790785571, + "rolloff": 3.4958953391007594, + "hf_ratio": 0.006087870337069035, + "lf_ratio": 0.560505747795105, + "dynamic": 8.721800804138184, + "onset_density": 5.572727272727272, + "perc_ratio": 0.47851771116256714, + "flatness": 0.2231881022453308, + "crest": 18.343008903360293 + }, + "z": [ + 0.5937500575807315, + 0.248803919491734, + -0.7698566541022746, + -0.9664409487981063, + -0.34350967997025167, + 0.5933352764610083, + -0.23586987575419688, + 0.9004697139015331, + 0.8724458326278043, + -0.9002609724607036, + 2.3011143051759135 + ] + }, + { + "file": "funkyhiphop_抓耳喵喵步_swl_00001.flac", + "family": "hiphop", + "group": "funkyhiphop_抓耳喵喵步", + "bpm": null, + "vec": { + "tempo": 117.1875, + "tempo_drift": 0.0, + "centroid": 3.2709206182488764, + "rolloff": 3.5988292646784372, + "hf_ratio": 0.0045711263082921505, + "lf_ratio": 0.6198356747627258, + "dynamic": 9.329568862915039, + "onset_density": 5.254545454545455, + "perc_ratio": 0.47200334072113037, + "flatness": 0.30613473057746887, + "crest": 15.931698797132206 + }, + "z": [ + 0.0, + -0.6966510607511376, + -0.12914825533849, + -0.06027716194395961, + -0.5888152733126538, + 1.0292868465950338, + -0.1649357659078986, + 0.6104645352376289, + 0.7836868287576476, + 0.11692293523345673, + 0.7335450448490907 + ] + }, + { + "file": "hit10_七点半_swl_00001.flac", + "family": "pop", + "group": "hit10_七点半", + "bpm": null, + "vec": { + "tempo": 125.0, + "tempo_drift": 0.0, + "centroid": 3.3663333659966144, + "rolloff": 3.692074760222741, + "hf_ratio": 0.01326978113502264, + "lf_ratio": 0.49096745252609253, + "dynamic": 8.028878211975098, + "onset_density": 4.426086956521739, + "perc_ratio": 0.4349598288536072, + "flatness": 0.3590770661830902, + "crest": 13.596277668552197 + }, + "z": [ + 0.17152776463308553, + -0.6966510607511376, + 0.7820035196338838, + 0.7605959454918801, + 0.8180329773263231, + 0.08237343045175967, + -0.3167425813162448, + -0.14462969392950764, + 0.27896496149943295, + 0.7661607993470526, + -0.7846895288061614 + ] + }, + { + "file": "hit10_同学_swl_00001.flac", + "family": "pop", + "group": "hit10_同学", + "bpm": null, + "vec": { + "tempo": 89.28571319580078, + "tempo_drift": 0.04545454157049001, + "centroid": 3.28386430191742, + "rolloff": 3.59551823209405, + "hf_ratio": 0.010816956870257854, + "lf_ratio": 0.41297540068626404, + "dynamic": 5.350070476531982, + "onset_density": 4.086206896551724, + "perc_ratio": 0.39555639028549194, + "flatness": 0.2868226170539856, + "crest": 14.050352480418073 + }, + "z": [ + -0.6125991833335316, + -0.09499807021934238, + -0.005541498089913436, + -0.08942535393438028, + 0.42133353416584685, + -0.4907059444158912, + -0.6293928422487655, + -0.4544116232652021, + -0.25791116303263384, + -0.1199037025050999, + -0.4894999075578754 + ] + }, + { + "file": "hit10_好运快过来_swl_00001.flac", + "family": "pop", + "group": "hit10_好运快过来", + "bpm": null, + "vec": { + "tempo": 117.1875, + "tempo_drift": 0.0, + "centroid": 3.3054518699180857, + "rolloff": 3.6279031778500204, + "hf_ratio": 0.007248524110764265, + "lf_ratio": 0.5246507525444031, + "dynamic": 16.464204788208008, + "onset_density": 4.87962962962963, + "perc_ratio": 0.538879930973053, + "flatness": 0.3203932046890259, + "crest": 15.389455454566818 + }, + "z": [ + 0.0, + -0.6966510607511376, + 0.2006107338230369, + 0.19567078826171835, + -0.15579516966424883, + 0.3298756317993501, + 0.6677651981022715, + 0.26874943847333455, + 1.6948876249598508, + 0.2917762198838484, + 0.38103791885690685 + ] + }, + { + "file": "hit10_小满_swl_00001.flac", + "family": "pop", + "group": "hit10_小满", + "bpm": null, + "vec": { + "tempo": 98.68421173095703, + "tempo_drift": 0.3571428626650855, + "centroid": 3.308026036077516, + "rolloff": 3.6231999901262086, + "hf_ratio": 0.007046271115541458, + "lf_ratio": 0.4056738018989563, + "dynamic": 8.523306846618652, + "onset_density": 5.3, + "perc_ratio": 0.3978542387485504, + "flatness": 0.30051684379577637, + "crest": 14.095177801909042 + }, + "z": [ + -0.4062499424192685, + 4.030622913320503, + 0.22519294239164334, + 0.15426696173730378, + -0.18850589082556285, + -0.5443575097979403, + -0.2590365953328025, + 0.6518938464753291, + -0.2266027284787251, + 0.04803015267569637, + -0.46035940179214263 + ] + }, + { + "file": "hit10_少年不慌_swl_00001.flac", + "family": "pop", + "group": "hit10_少年不慌", + "bpm": null, + "vec": { + "tempo": 144.23077392578125, + "tempo_drift": 0.0, + "centroid": 3.374472640883855, + "rolloff": 3.695994684251899, + "hf_ratio": 0.014045312069356441, + "lf_ratio": 0.45820021629333496, + "dynamic": 6.523681163787842, + "onset_density": 3.8035714285714284, + "perc_ratio": 0.389596164226532, + "flatness": 0.3805980384349823, + "crest": 13.402388577245713 + }, + "z": [ + 0.5937500575807315, + -0.6966510607511376, + 0.8597301854679363, + 0.795104424426376, + 0.9434609153255605, + -0.15839760728134175, + -0.49241784297990465, + -0.7120182642001172, + -0.33911988958419875, + 1.0300749221116485, + -0.9107349423179798 + ] + }, + { + "file": "hit10_慢慢来_swl_00001.flac", + "family": "pop", + "group": "hit10_慢慢来", + "bpm": null, + "vec": { + "tempo": 156.25, + "tempo_drift": 0.33333330078125, + "centroid": 3.196856227428964, + "rolloff": 3.5179727039978443, + "hf_ratio": 0.004267585929483175, + "lf_ratio": 0.34324538707733154, + "dynamic": 7.922789573669434, + "onset_density": 3.2966101694915255, + "perc_ratio": 0.29231077432632446, + "flatness": 0.2379920929670334, + "crest": 14.423139417443547 + }, + "z": [ + 0.8576388231654276, + 3.7154708159564502, + -0.8364321639439897, + -0.7720860899250109, + -0.6379073754171589, + -1.003076521491963, + -0.3291244480733807, + -1.1740854915334311, + -1.664643879398593, + -0.7187179557696312, + -0.24715474356458517 + ] + }, + { + "file": "hit10_晚安大学_swl_00001.flac", + "family": "pop", + "group": "hit10_晚安大学", + "bpm": null, + "vec": { + "tempo": 170.4545440673828, + "tempo_drift": 0.0, + "centroid": 3.172639856210142, + "rolloff": 3.4709532837391324, + "hf_ratio": 0.0026581024285405874, + "lf_ratio": 0.45042920112609863, + "dynamic": 14.988051414489746, + "onset_density": 4.38135593220339, + "perc_ratio": 0.32847312092781067, + "flatness": 0.22001709043979645, + "crest": 15.50666407368343 + }, + "z": [ + 1.16950745567875, + -0.6966510607511376, + -1.0676883654502773, + -1.1860146919695134, + -0.8982118814397808, + -0.2154984067739971, + 0.49547969535300723, + -0.18539955555576026, + -1.1719279844690218, + -0.9391474497600831, + 0.4572341027894501 + ] + }, + { + "file": "hit10_晚风很好_swl_00001.flac", + "family": "pop", + "group": "hit10_晚风很好", + "bpm": null, + "vec": { + "tempo": 156.25, + "tempo_drift": 0.09090908203125, + "centroid": 3.2135624402151306, + "rolloff": 3.536423416576392, + "hf_ratio": 0.0033996382262557745, + "lf_ratio": 0.44531023502349854, + "dynamic": 6.215700626373291, + "onset_density": 4.086206896551724, + "perc_ratio": 0.3774614632129669, + "flatness": 0.2507161796092987, + "crest": 14.189438658630614 + }, + "z": [ + 0.8576388231654276, + 0.5066549056236591, + -0.676894829651647, + -0.6096579352956876, + -0.7782820347471628, + -0.25311216131492664, + -0.5283630114631774, + -0.4544116232652021, + -0.5044565071298023, + -0.5626810386234308, + -0.3990813351232549 + ] + }, + { + "file": "hit10_热恋废柴_swl_00001.flac", + "family": "pop", + "group": "hit10_热恋废柴", + "bpm": null, + "vec": { + "tempo": 104.16666412353516, + "tempo_drift": 0.05882358542049769, + "centroid": 3.295890783917295, + "rolloff": 3.5976043495987695, + "hf_ratio": 0.010117174126207829, + "lf_ratio": 0.46022868156433105, + "dynamic": 5.849791049957275, + "onset_density": 5.555555555555555, + "perc_ratio": 0.4925544261932373, + "flatness": 0.2954113781452179, + "crest": 15.696503880574832 + }, + "z": [ + -0.2858796635576701, + 0.08195949958551603, + 0.10930636741576678, + -0.0710605230266846, + 0.3081564799081191, + -0.14349260629700378, + -0.5710692210319099, + 0.8848186407672908, + 1.0636975974655367, + -0.014578751452854512, + 0.5806471158650873 + ] + }, + { + "file": "hit10_第一名_swl_00001.flac", + "family": "pop", + "group": "hit10_第一名", + "bpm": null, + "vec": { + "tempo": 133.92857360839844, + "tempo_drift": 0.0, + "centroid": 3.3635386339218005, + "rolloff": 3.691382924335406, + "hf_ratio": 0.013467314653098583, + "lf_ratio": 0.4985203742980957, + "dynamic": 8.814897537231445, + "onset_density": 4.587719298245614, + "perc_ratio": 0.3616727590560913, + "flatness": 0.37247368693351746, + "crest": 14.15185833323568 + }, + "z": [ + 0.36755954350163544, + -0.6966510607511376, + 0.7553149989629211, + 0.7545054692564616, + 0.8499804094159069, + 0.13787169707530086, + -0.22500432631275522, + 0.002689271080342213, + -0.7195793138481442, + 0.9304450785133039, + -0.42351193935371967 + ] + }, + { + "file": "niaopunk_鸟巢滑手_A_swl_00001.flac", + "family": "punk", + "group": "niaopunk_鸟巢滑手_A", + "bpm": null, + "vec": { + "tempo": 110.29412078857422, + "tempo_drift": 0.29166668684217606, + "centroid": 3.331447348276894, + "rolloff": 3.656434560218805, + "hf_ratio": 0.009595229290425777, + "lf_ratio": 0.433444082736969, + "dynamic": 20.123851776123047, + "onset_density": 4.125, + "perc_ratio": 0.39922896027565, + "flatness": 0.34165868163108826, + "crest": 14.000773510415899 + }, + "z": [ + -0.15134795864371792, + 3.163956225430851, + 0.4488566632926184, + 0.446842647747162, + 0.22374145324289593, + -0.34030370165822077, + 1.0948916284742087, + -0.41905384901923354, + -0.20787199855767532, + 0.5525571746960415, + -0.5217307140117359 + ] + }, + { + "file": "niaopunk_鸟巢滑手_B_swl_00001.flac", + "family": "punk", + "group": "niaopunk_鸟巢滑手_B", + "bpm": null, + "vec": { + "tempo": 170.4545440673828, + "tempo_drift": 0.37745102032430844, + "centroid": 3.3446909899050934, + "rolloff": 3.6752580691085908, + "hf_ratio": 0.008829951286315918, + "lf_ratio": 0.4229309856891632, + "dynamic": 9.63530158996582, + "onset_density": 5.408333333333333, + "perc_ratio": 0.38996070623397827, + "flatness": 0.3684544563293457, + "crest": 14.069633317530823 + }, + "z": [ + 1.16950745567875, + 4.299429139622764, + 0.5753278934110083, + 0.6125526602792197, + 0.09997173909973688, + -0.4175531000514396, + -0.1292529449187968, + 0.7506337049251826, + -0.3341529651913293, + 0.8811567979155095, + -0.4769656227749894 + ] + }, + { + "file": "niaopunk_鸟巢滑手_C_swl_00001.flac", + "family": "punk", + "group": "niaopunk_鸟巢滑手_C", + "bpm": null, + "vec": { + "tempo": 85.2272720336914, + "tempo_drift": 0.37745102032430844, + "centroid": 3.3639537073508134, + "rolloff": 3.685859822794626, + "hf_ratio": 0.012752832844853401, + "lf_ratio": 0.461561381816864, + "dynamic": 5.714139938354492, + "onset_density": 4.711864406779661, + "perc_ratio": 0.453780859708786, + "flatness": 0.3670971095561981, + "crest": 14.113756195724221 + }, + "z": [ + -0.7017045069087665, + 4.299429139622764, + 0.7592787763460954, + 0.7058836511407588, + 0.734426050487411, + -0.13370003116157791, + -0.5869013969948412, + 0.11584069056243587, + 0.535403548394825, + 0.8645115005409402, + -0.4482817685687341 + ] + }, + { + "file": "partytrap_亮灯就跳_swl_00001.flac", + "family": "hiphop", + "group": "partytrap_亮灯就跳", + "bpm": null, + "vec": { + "tempo": 125.0, + "tempo_drift": 0.0, + "centroid": 3.246335787758108, + "rolloff": 3.5586895738605144, + "hf_ratio": 0.003296474227681756, + "lf_ratio": 0.4238636791706085, + "dynamic": 10.593645095825195, + "onset_density": 4.496969696969697, + "perc_ratio": 0.4066116213798523, + "flatness": 0.26120811700820923, + "crest": 16.444846055934754 + }, + "z": [ + 0.17152776463308553, + -0.6966510607511376, + -0.36392308855347566, + -0.41364107020445834, + -0.7949669238264242, + -0.4106997427582179, + -0.017402309576220586, + -0.08002398539071612, + -0.10728244022494093, + -0.43401722072872195, + 1.067137089622679 + ] + }, + { + "file": "punk4_十七岁_swl_00001.flac", + "family": "punk", + "group": "punk4_十七岁", + "bpm": null, + "vec": { + "tempo": 144.23077392578125, + "tempo_drift": 0.36842102479098227, + "centroid": 3.383785946561368, + "rolloff": 3.698488273262017, + "hf_ratio": 0.013227433897554874, + "lf_ratio": 0.4917842745780945, + "dynamic": 9.465476989746094, + "onset_density": 5.508928571428571, + "perc_ratio": 0.4000467360019684, + "flatness": 0.384067177772522, + "crest": 12.893802091670288 + }, + "z": [ + 0.5937500575807315, + 4.179904805432985, + 0.9486683535768998, + 0.8170563712464854, + 0.8111840865337092, + 0.08837537372542467, + -0.14907359306347437, + 0.8423207163429033, + -0.19672971558348165, + 1.0726173711375953, + -1.2413620727507317 + ] + }, + { + "file": "punk4_夏末的风_v2_swl_00001.flac", + "family": "punk", + "group": "punk4_夏末的风_v2", + "bpm": null, + "vec": { + "tempo": 144.23077392578125, + "tempo_drift": 0.0, + "centroid": 3.3660231408622563, + "rolloff": 3.6911385692806338, + "hf_ratio": 0.011530597694218159, + "lf_ratio": 0.4583457112312317, + "dynamic": 9.117166519165039, + "onset_density": 2.8448275862068964, + "perc_ratio": 0.41157591342926025, + "flatness": 0.37299880385398865, + "crest": 13.589398394750754 + }, + "z": [ + 0.5937500575807315, + -0.6966510607511376, + 0.7790409995463298, + 0.7523543252043269, + 0.5367518792745553, + -0.15732852208497453, + -0.18972576756329237, + -1.5858603991362006, + -0.039643422581233295, + 0.9368846468465988, + -0.7891616778328093 + ] + }, + { + "file": "punk4_我还没长大_swl_00001.flac", + "family": "punk", + "group": "punk4_我还没长大", + "bpm": null, + "vec": { + "tempo": 156.25, + "tempo_drift": 0.33333330078125, + "centroid": 3.327725934987496, + "rolloff": 3.6579458449044253, + "hf_ratio": 0.010431032627820969, + "lf_ratio": 0.45990535616874695, + "dynamic": 15.935002326965332, + "onset_density": 5.572727272727272, + "perc_ratio": 0.37389007210731506, + "flatness": 0.3340316116809845, + "crest": 14.340263108584026 + }, + "z": [ + 0.8576388231654276, + 3.7154708159564502, + 0.4133187251860087, + 0.46014702184977846, + 0.35891734957566285, + -0.1458683755094962, + 0.6060006730098105, + 0.9004697139015331, + -0.553117098328617, + 0.45902555114363097, + -0.3010318263346472 + ] + }, + { + "file": "punk4_放学以后_swl_00001.flac", + "family": "punk", + "group": "punk4_放学以后", + "bpm": null, + "vec": { + "tempo": 133.92857360839844, + "tempo_drift": 0.0, + "centroid": 3.3485853244149704, + "rolloff": 3.679744754265026, + "hf_ratio": 0.012618409469723701, + "lf_ratio": 0.4368063509464264, + "dynamic": 7.537966251373291, + "onset_density": 5.017543859649122, + "perc_ratio": 0.4298690855503082, + "flatness": 0.35421499609947205, + "crest": 14.14310272736609 + }, + "z": [ + 0.36755954350163544, + -0.6966510607511376, + 0.6125171567408101, + 0.6520505380581979, + 0.7126855292328983, + -0.31559802309022555, + -0.3740381275946572, + 0.39445065278421343, + 0.20960303180545184, + 0.7065366826519874, + -0.429203873618523 + ] + }, + { + "file": "smile13_今年冬天不一样_swl_00001.flac", + "family": "pop", + "group": "smile13_今年冬天不一样", + "bpm": null, + "vec": { + "tempo": 117.1875, + "tempo_drift": 0.0, + "centroid": 3.3371523399848084, + "rolloff": 3.671984422100731, + "hf_ratio": 0.013811641372740269, + "lf_ratio": 0.34942319989204407, + "dynamic": 12.927671432495117, + "onset_density": 5.280701754385965, + "perc_ratio": 0.4281313419342041, + "flatness": 0.32761433720588684, + "crest": 14.84008733064282 + }, + "z": [ + 0.0, + -0.6966510607511376, + 0.5033369444245734, + 0.5837335867539669, + 0.9056689558435201, + -0.9576824457831247, + 0.2550076635353874, + 0.6343045599498494, + 0.18592608654516543, + 0.38032978666737083, + 0.023899041761678144 + ] + }, + { + "file": "smile13_你笑了我也笑_swl_00001.flac", + "family": "pop", + "group": "smile13_你笑了我也笑", + "bpm": null, + "vec": { + "tempo": 104.16666412353516, + "tempo_drift": 0.052631544253699813, + "centroid": 3.296480470081916, + "rolloff": 3.5987106049712563, + "hf_ratio": 0.008127572014927864, + "lf_ratio": 0.33739277720451355, + "dynamic": 21.546384811401367, + "onset_density": 5.333333333333333, + "perc_ratio": 0.440476655960083, + "flatness": 0.2791133522987366, + "crest": 14.755026774682191 + }, + "z": [ + -0.2858796635576701, + -6.292989114656488e-07, + 0.11493762328308015, + -0.06132176535340548, + -0.013625255943779448, + -1.0460810319278067, + 1.260918969212542, + 0.6822753413829762, + 0.35413232922847404, + -0.21444329027222253, + -0.03139799881630343 + ] + }, + { + "file": "smile13_冬天也会笑_swl_00001.flac", + "family": "pop", + "group": "smile13_冬天也会笑", + "bpm": null, + "vec": { + "tempo": 170.4545440673828, + "tempo_drift": 0.2941176944508276, + "centroid": 3.2481567829681923, + "rolloff": 3.582943804830805, + "hf_ratio": 0.006630727089941502, + "lf_ratio": 0.5139971375465393, + "dynamic": 15.244091987609863, + "onset_density": 3.975806451612903, + "perc_ratio": 0.36186832189559937, + "flatness": 0.2677501440048218, + "crest": 14.034012713899235 + }, + "z": [ + 1.16950745567875, + 3.1963986614693822, + -0.3465333470666101, + -0.20012248864907148, + -0.2557125333609478, + 0.25159371871934894, + 0.5253628224383018, + -0.5550355399365254, + -0.7169147489473913, + -0.35379160193673126, + -0.5001222309370589 + ] + }, + { + "file": "smile13_冬天的礼物_swl_00001.flac", + "family": "pop", + "group": "smile13_冬天的礼物", + "bpm": null, + "vec": { + "tempo": 93.75, + "tempo_drift": 0.34090909830729166, + "centroid": 3.255491698381072, + "rolloff": 3.567741036906824, + "hf_ratio": 0.007219463121145964, + "lf_ratio": 0.3542766273021698, + "dynamic": 7.028054237365723, + "onset_density": 3.847457627118644, + "perc_ratio": 0.3276587724685669, + "flatness": 0.25516748428344727, + "crest": 15.006896164490103 + }, + "z": [ + -0.5145832938992566, + 3.8157468517434987, + -0.2764879772865937, + -0.33395783666815726, + -0.16049525297836187, + -0.9220198483703458, + -0.43355121698989973, + -0.6720184146697704, + -1.183023570557573, + -0.5080941847171172, + 0.1323398431395622 + ] + }, + { + "file": "smile13_如果遇见你_swl_00001.flac", + "family": "pop", + "group": "smile13_如果遇见你", + "bpm": null, + "vec": { + "tempo": 170.4545440673828, + "tempo_drift": 0.08333332587348084, + "centroid": 3.2385854023612857, + "rolloff": 3.543566207873318, + "hf_ratio": 0.005194356199353933, + "lf_ratio": 0.38970986008644104, + "dynamic": 9.570982933044434, + "onset_density": 3.4310344827586206, + "perc_ratio": 0.3445125222206116, + "flatness": 0.2532542943954468, + "crest": 14.850735353504556 + }, + "z": [ + 1.16950745567875, + 0.4063794174022443, + -0.4379360226501346, + -0.5467774148646791, + -0.4880192422652025, + -0.661659280664646, + -0.13675973407848036, + -1.0515651438637847, + -0.9533894020808944, + -0.5315558492304086, + 0.030821217917371153 + ] + }, + { + "file": "smile13_开心会传染_swl_00001.flac", + "family": "pop", + "group": "smile13_开心会传染", + "bpm": null, + "vec": { + "tempo": 144.23077392578125, + "tempo_drift": 0.0, + "centroid": 3.3445212318421733, + "rolloff": 3.6670068822038475, + "hf_ratio": 0.01146805752068758, + "lf_ratio": 0.4292643070220947, + "dynamic": 10.468523025512695, + "onset_density": 4.419642857142857, + "perc_ratio": 0.37446191906929016, + "flatness": 0.3283233046531677, + "crest": 14.441091506814058 + }, + "z": [ + 0.5937500575807315, + -0.6966510607511376, + 0.573706775017537, + 0.5399145409256436, + 0.526637150563194, + -0.37101636015688966, + -0.03200561514908186, + -0.15050313510342403, + -0.5453256214441844, + 0.38902393485583564, + -0.2354842647725098 + ] + }, + { + "file": "smile13_开心制造机_swl_00001.flac", + "family": "pop", + "group": "smile13_开心制造机", + "bpm": null, + "vec": { + "tempo": 110.29412078857422, + "tempo_drift": 0.055555605513959806, + "centroid": 3.3933372852837325, + "rolloff": 3.6844508184820697, + "hf_ratio": 0.019298849627375603, + "lf_ratio": 0.335612416267395, + "dynamic": 7.599209785461426, + "onset_density": 5.607142857142857, + "perc_ratio": 0.5691275000572205, + "flatness": 0.36220860481262207, + "crest": 15.823112414370687 + }, + "z": [ + -0.15134795864371792, + 0.038703318446449746, + 1.0398796375580917, + 0.6934796873708515, + 1.7931244863473117, + -1.0591629820600847, + -0.3668902436089042, + 0.9318376209815065, + 2.107014034647465, + 0.8045632126136598, + 0.6629540929564273 + ] + }, + { + "file": "smile13_我也会难过_swl_00001.flac", + "family": "pop", + "group": "smile13_我也会难过", + "bpm": null, + "vec": { + "tempo": 110.29412078857422, + "tempo_drift": 0.06249996973673589, + "centroid": 3.30733677840279, + "rolloff": 3.6203545168521645, + "hf_ratio": 0.009381476789712906, + "lf_ratio": 0.3704341650009155, + "dynamic": 6.684614181518555, + "onset_density": 5.107142857142857, + "perc_ratio": 0.43689897656440735, + "flatness": 0.30488091707229614, + "crest": 14.250999235222505 + }, + "z": [ + -0.15134795864371792, + 0.1306214713442394, + 0.21861082031084664, + 0.12921725317008975, + 0.18917089748598487, + -0.803295550376315, + -0.4736349533680668, + 0.4761151973667989, + 0.30538605939600216, + 0.10154727806262553, + -0.35906140269556946 + ] + }, + { + "file": "smile13_没心没肺_swl_00001.flac", + "family": "pop", + "group": "smile13_没心没肺", + "bpm": null, + "vec": { + "tempo": 144.23077392578125, + "tempo_drift": 0.36842102479098227, + "centroid": 3.3475154724893974, + "rolloff": 3.666394331987908, + "hf_ratio": 0.01060759648680687, + "lf_ratio": 0.44991761445999146, + "dynamic": 9.1593656539917, + "onset_density": 4.6875, + "perc_ratio": 0.43427494168281555, + "flatness": 0.3438504934310913, + "crest": 14.057403837237405 + }, + "z": [ + 0.5937500575807315, + 4.179904805432985, + 0.6023005189858563, + 0.5345220445237161, + 0.38747332300928694, + -0.21925750478318176, + -0.184800602408616, + 0.09363387754731252, + 0.26963329950854575, + 0.5794356113062666, + -0.48491588903407334 + ] + }, + { + "file": "smile13_等冬天过去_swl_00001.flac", + "family": "pop", + "group": "smile13_等冬天过去", + "bpm": null, + "vec": { + "tempo": 104.16666412353516, + "tempo_drift": 0.05882358542049769, + "centroid": 3.3241289292500915, + "rolloff": 3.649593703631366, + "hf_ratio": 0.010481993667781353, + "lf_ratio": 0.38435420393943787, + "dynamic": 5.796255111694336, + "onset_density": 4.362068965517241, + "perc_ratio": 0.41739508509635925, + "flatness": 0.31925567984580994, + "crest": 14.604155530756325 + }, + "z": [ + -0.2858796635576701, + 0.08195949958551603, + 0.37896882708971863, + 0.38662016545271133, + 0.36715936530649707, + -0.7010122147691864, + -0.5773175324899201, + -0.2029785619605362, + 0.039643422581233295, + 0.2778266237593575, + -0.12947792791153184 + ] + }, + { + "file": "smile13_素不相识_swl_00001.flac", + "family": "pop", + "group": "smile13_素不相识", + "bpm": null, + "vec": { + "tempo": 156.25, + "tempo_drift": 0.41025634765625, + "centroid": 3.369222515407721, + "rolloff": 3.6871157678944675, + "hf_ratio": 0.013103911653161049, + "lf_ratio": 0.3936189115047455, + "dynamic": 7.343005180358887, + "onset_density": 3.5089285714285716, + "perc_ratio": 0.37900862097740173, + "flatness": 0.3615648150444031, + "crest": 13.546448388063894 + }, + "z": [ + 0.8576388231654276, + 4.7336524892088265, + 0.8095936863173783, + 0.7169401804761181, + 0.7912066237073369, + -0.6329358826993241, + -0.3967925152716266, + -0.9805689781159266, + -0.48337631477111825, + 0.7966683456933513, + -0.8170830596738495 + ] + }, + { + "file": "smile13_陌生的朋友_swl_00001.flac", + "family": "pop", + "group": "smile13_陌生的朋友", + "bpm": null, + "vec": { + "tempo": 98.68421173095703, + "tempo_drift": 0.055555516899957066, + "centroid": 3.3205351059131476, + "rolloff": 3.6509398446774237, + "hf_ratio": 0.008706255815923214, + "lf_ratio": 0.5796492099761963, + "dynamic": 6.551093578338623, + "onset_density": 4.413793103448276, + "perc_ratio": 0.4613018035888672, + "flatness": 0.3295997679233551, + "crest": 13.44438238651473 + }, + "z": [ + -0.4062499424192685, + 0.03870214551899499, + 0.34464931958479805, + 0.39847072163319047, + 0.07996626013772717, + 0.7339999093401739, + -0.4892184724358016, + -0.1558348629659111, + 0.6378772245482686, + 0.4046773486630632, + -0.8834351751233271 + ] + }, + { + "file": "smile13_面具之外_swl_00001.flac", + "family": "pop", + "group": "smile13_面具之外", + "bpm": null, + "vec": { + "tempo": 117.1875, + "tempo_drift": 0.0, + "centroid": 3.277722056867244, + "rolloff": 3.6143690619982523, + "hf_ratio": 0.008296064101159573, + "lf_ratio": 0.6255742311477661, + "dynamic": 5.978894233703613, + "onset_density": 4.771929824561403, + "perc_ratio": 0.4723767638206482, + "flatness": 0.3037712574005127, + "crest": 14.017346779999295 + }, + "z": [ + 0.0, + -0.6966510607511376, + -0.06419736548595156, + 0.07652517515959334, + 0.013625255943779448, + 1.0714533011016483, + -0.5560012698865342, + 0.17058700609628702, + 0.7887747589933126, + 0.08793939569475387, + -0.5109565925601067 + ] + }, + { + "file": "smoothhiphop_晚风不加班_swl_00001.flac", + "family": "hiphop", + "group": "smoothhiphop_晚风不加班", + "bpm": null, + "vec": { + "tempo": 93.75, + "tempo_drift": 0.0, + "centroid": 3.2053234707094083, + "rolloff": 3.507120063138694, + "hf_ratio": 0.0037074044812470675, + "lf_ratio": 0.4803110659122467, + "dynamic": 12.305105209350586, + "onset_density": 4.266666666666667, + "perc_ratio": 0.39195048809051514, + "flatness": 0.2557111978530884, + "crest": 16.56754488341106 + }, + "z": [ + -0.5145832938992566, + -0.6966510607511376, + -0.7555735373326146, + -0.8676257293889168, + -0.7285064744950552, + 0.004071151758636388, + 0.1823464234103232, + -0.28993249566173307, + -0.3070419715668754, + -0.5014265635665707, + 1.1469024046409408 + ] + }, + { + "file": "upbeathiphop_跳起来_swl_00001.flac", + "family": "hiphop", + "group": "upbeathiphop_跳起来", + "bpm": null, + "vec": { + "tempo": 117.1875, + "tempo_drift": 0.0, + "centroid": 3.27156239265194, + "rolloff": 3.5899806462164254, + "hf_ratio": 0.004788311664015055, + "lf_ratio": 0.5683676600456238, + "dynamic": 15.391424179077148, + "onset_density": 4.248484848484848, + "perc_ratio": 0.4410526752471924, + "flatness": 0.279897540807724, + "crest": 15.18973496161242 + }, + "z": [ + 0.0, + -0.6966510607511376, + -0.12301957861250953, + -0.13817468279497597, + -0.5536895160720926, + 0.6511039804700339, + 0.5425583260560287, + -0.30650422015681367, + 0.361980654495428, + -0.204826697675515, + 0.2512015678193515 + ] + }, + { + "file": "urbanhiphop_身体先听懂_swl_00001.flac", + "family": "hiphop", + "group": "urbanhiphop_身体先听懂", + "bpm": null, + "vec": { + "tempo": 93.75, + "tempo_drift": 0.0, + "centroid": 3.3075163470613678, + "rolloff": 3.6295031875206125, + "hf_ratio": 0.010682044550776482, + "lf_ratio": 0.5981898903846741, + "dynamic": 10.891853332519531, + "onset_density": 4.866666666666666, + "perc_ratio": 0.4391697943210602, + "flatness": 0.3258528411388397, + "crest": 15.773859344781949 + }, + "z": [ + -0.5145832938992566, + -0.6966510607511376, + 0.22032562578343476, + 0.20975623981688044, + 0.3995139351091321, + 0.8702353498429111, + 0.017402309576220586, + 0.2569344126759157, + 0.33632619764314725, + 0.3587283606794183, + 0.6309351511933469 + ] + }, + { + "file": "喜剧hiphop_干饭魂_swl_00001.flac", + "family": "hiphop", + "group": "喜剧hiphop_干饭魂", + "bpm": null, + "vec": { + "tempo": 110.29412078857422, + "tempo_drift": 0.06249996973673589, + "centroid": 3.329093497562623, + "rolloff": 3.649642151994252, + "hf_ratio": 0.011045028455555439, + "lf_ratio": 0.6133913993835449, + "dynamic": 16.88188934326172, + "onset_density": 5.448484848484848, + "perc_ratio": 0.5823317170143127, + "flatness": 0.35181790590286255, + "crest": 14.623191145672145 + }, + "z": [ + -0.15134795864371792, + 0.1306214713442394, + 0.42637837494064373, + 0.38704667354256855, + 0.4582199398443862, + 0.9819348252203374, + 0.7165141931858716, + 0.7872295965184847, + 2.286922922932678, + 0.677140893789791, + -0.11710305969512669 + ] + }, + { + "file": "喜剧hiphop_我有点毛病_swl_00001.flac", + "family": "hiphop", + "group": "喜剧hiphop_我有点毛病", + "bpm": null, + "vec": { + "tempo": 104.16666412353516, + "tempo_drift": 0.0, + "centroid": 3.3039414546535992, + "rolloff": 3.622658446859196, + "hf_ratio": 0.008474177680909634, + "lf_ratio": 0.531467616558075, + "dynamic": 7.109005451202393, + "onset_density": 5.96969696969697, + "perc_ratio": 0.5088467597961426, + "flatness": 0.3218781650066376, + "crest": 15.329767203217441 + }, + "z": [ + -0.2858796635576701, + -0.6966510607511376, + 0.18618690072244531, + 0.14949956464419092, + 0.04243186835428738, + 0.3799654047359029, + -0.4241032010707384, + 1.2622856987107862, + 1.2856824100140862, + 0.30998645662295626, + 0.3422351668689827 + ] + }, + { + "file": "放克jazzhiphop_拜师记_swl_00001.flac", + "family": "hiphop", + "group": "放克jazzhiphop_拜师记", + "bpm": null, + "vec": { + "tempo": 98.68421173095703, + "tempo_drift": 0.055555516899957066, + "centroid": 3.316521815028917, + "rolloff": 3.622387931906733, + "hf_ratio": 0.009501924738287926, + "lf_ratio": 0.5217223763465881, + "dynamic": 12.416568756103516, + "onset_density": 5.248484848484848, + "perc_ratio": 0.5251473188400269, + "flatness": 0.337675541639328, + "crest": 17.72963812795235 + }, + "z": [ + -0.4062499424192685, + 0.03870214551899499, + 0.306324072707776, + 0.14711812575172925, + 0.20865114921698855, + 0.3083581571178584, + 0.19535560899526894, + 0.6049406270726015, + 1.5077792950881779, + 0.5037114773701892, + 1.902367926504463 + ] + }, + { + "file": "欧式hiphop_不夜城_swl_00001.flac", + "family": "hiphop", + "group": "欧式hiphop_不夜城", + "bpm": null, + "vec": { + "tempo": 104.16666412353516, + "tempo_drift": 0.0, + "centroid": 3.285024875981853, + "rolloff": 3.6125234003117166, + "hf_ratio": 0.011527372524142265, + "lf_ratio": 0.5269022583961487, + "dynamic": 47.72808074951172, + "onset_density": 4.848484848484849, + "perc_ratio": 0.40386763215065, + "flatness": 0.2977890372276306, + "crest": 15.299797805882651 + }, + "z": [ + -0.2858796635576701, + -0.6966510607511376, + 0.005541498089917676, + 0.060277161943963514, + 0.5362302670287928, + 0.34641951717729974, + 4.316649307154125, + 0.24036268818083592, + -0.14466959110988753, + 0.014578751452854512, + 0.32275235298652216 + ] + } + ], + "loo": { + "acc": 0.6034, + "n": 58, + "n_songs": 58, + "mode": "group-by-song", + "per_family": { + "hiphop": { + "acc": 0.938, + "n": 16 + }, + "pop": { + "acc": 0.429, + "n": 35 + }, + "punk": { + "acc": 0.714, + "n": 7 + } + }, + "confusion": { + "hiphop": { + "hiphop": 15, + "pop": 1 + }, + "pop": { + "hiphop": 14, + "pop": 15, + "punk": 6 + }, + "punk": { + "punk": 5, + "pop": 2 + } + }, + "fam_min": 0.429 + }, + "trusted": false, + "note": "由 tools/make_style_ref.py 在历史已认可歌上生成,不要手改。trusted 只有在按歌分组 LOO >= 65% 且每个家族都 >= 50% 时为真;实测语料 pop/hiphop 制作手法重叠,多轮尝试后总体约 60~67%、单家族最低约 43%,所以本文件通常 trusted=false —— 风格判定只当参考,风格的第一物理保证是实测 BPM 与设定 BPM 对齐。" +} \ No newline at end of file diff --git a/config/default.json b/config/default.json new file mode 100644 index 0000000..3093001 --- /dev/null +++ b/config/default.json @@ -0,0 +1,96 @@ +{ + "_comment": "songforge 唯一事实来源。改这里,不要改脚本里的硬编码常量。", + + "engine": { + "server": "http://127.0.0.1:8188", + "timeout_sec": 3600, + "poll_interval_sec": 10, + "submit_retries": 3 + }, + + "model": { + "checkpoint": "ace_step_1.5_turbo_swl_fused.safetensors", + "lora": "swl_voice_best_ep257.safetensors", + "lora_model_strength": 0.5, + "lora_clip_strength": 0.5, + "denied_checkpoints": [ + "yue2_3b_int8_convrot.safetensors", + "ace_step_1.5_turbo_aio.safetensors" + ] + }, + + "sampler": { + "shift": 5.5, + "sampling": "flow", + "steps": 28, + "cfg": 2.0, + "sampler_name": "euler", + "scheduler": "simple", + "denoise": 1.0 + }, + + "encode": { + "cfg_scale": 3.0, + "temperature": 0.68, + "top_p": 0.88, + "top_k": 50, + "min_p": 0.02, + "generate_audio_codes": true, + "timesignature": "4" + }, + + "paths": { + "_comment": "相对路径均相对 user/default/workflows。本机绝对路径放 local.json(gitignore)。音频在 ComfyUI/output//。过程物按日落在 data/runs|tmp/YYYY-MM-DD/。", + "workflow_template": "urbanhiphop-身体先听懂.json", + "runs_dir": "songforge/data/runs", + "tmp_dir": "songforge/data/tmp", + "lyrics_dir": "songforge/input/lyrics", + "qc_cache_dir": "songforge/cache/qc", + "style_cache_dir": "songforge/cache/style", + "index_dir": "songforge/index", + "catalog_file": "songforge/index/catalog.jsonl", + "baseline_dir": "songforge/config/baseline", + "comfy_root": null, + "qc_python": null, + "music_dir": null, + "output_prefix": "MyMusic", + "output_format": "flac" + }, + + "song": { + "language": "zh", + "time_signature": "4", + "duration_range": [150, 200], + "_duration_extra_comment": "duration_extra_ranges 供短视频/口水歌等场景显式放行短时长,不削弱主区间。主区间是 35 首已验证实歌的经验范围,短时长没有对应实歌背书,所以必须显式列出而不是直接放宽。", + "duration_extra_ranges": [[100, 130]] + }, + + "lyrics": { + "_comment": "死心塌地时长对应关系。阈值由 8 首已验证可生成的歌实测反推,不是理论值;改前先跑 selftest 实歌回归。", + "_calibrated_on": "remake_v5.py 的 8 首实歌:每可唱小节 4.56~7.64 音节(均值 6.17),每句 5.9~7.4 音节(均值 6.7)", + "sing_bar_ratio": 0.70, + "intro_bars": 8, + "syllable_per_bar_min": 4.40, + "syllable_per_bar_max": 7.80, + "line_syllable_max": 12, + "ideal_line_syllable": [5, 12], + + "_profile_comment": "密度档。default = 人声填满整首(中文说唱/流行)。sparse = 音乐主导、人声点缀(英文低声吟唱/纯器乐周边)。", + "_profile_why": "default 档是按 8 首'人声填满全曲'的实歌反推的。直接拿它校验第三张那种'音乐当主角、人声只是点缀'的写法,会把只有几句 ooh ooh 的低音吟唱判成歌词过少,逼着写满 254 音节以上——结果就是'只有人声没有音乐',正好和目标相反。所以稀疏写法必须用另一套阈值,不能共用。", + "profiles": { + "sparse": { + "_comment": "稀疏档:可唱小节只占 25%(其余留给器乐),句子下限放宽到 3 音节,允许 ooh ooh / mmm 这类短语气词。", + "sing_bar_ratio": 0.25, + "syllable_per_bar_min": 4.40, + "syllable_per_bar_max": 7.80, + "line_syllable_max": 12, + "ideal_line_syllable": [3, 12] + } + } + }, + + "rules": { + "trigger_word": "swlvoice", + "intro_bars": 8 + } +} diff --git a/config/lexicon.json b/config/lexicon.json new file mode 100644 index 0000000..5abd9d1 --- /dev/null +++ b/config/lexicon.json @@ -0,0 +1,23 @@ +{ + "_comment": "身份词典与禁用词。用户是街头双翘滑手(street skateboarding),不是长板。", + + "identity": { + "persona": "街头滑手", + "board": "双翘滑板(街头双翘)", + "en_terms": ["skateboard", "skateboarding", "skatepark", "skater"], + "scene": ["街头", "广场", "水泥地", "马路牙子", "地下通道", "天台", "刷街"], + "verb": ["刷街", "滑行", "起板", "压板", "转向", "落地", "刹停", "推板"], + "tone": ["轻松", "倔", "自由", "不装"] + }, + + "banned": { + "_comment": "禁止出现在歌词里的词。命中即 check 报错。原因是《鸟巢滑手》曾误写为长板/护具设定,与本人身份不符。", + "longboard": ["长板", "长板的轮子", "长板的孩子", "longboard", "长板滑板", "护具", "护膝", "头盔", "上山", "速降", "下坡", "carve", "slide", "长板赛道"], + "misidentity": ["滑板少年" ] + }, + + "warnings": { + "_comment": "不报错但提醒。", + "near_miss": ["轮子转起来", "板面", "水泥地"] + } +} diff --git a/config/loader.py b/config/loader.py new file mode 100644 index 0000000..4c48910 --- /dev/null +++ b/config/loader.py @@ -0,0 +1,179 @@ +# -*- coding: utf-8 -*- +"""配置加载与覆盖。 + +优先级:命令行 > 环境变量 > local.json > default.json > 内置 DEFAULTS。 + +local.json 放本机绝对路径(gitignore),见 local.json.example。 +""" +import copy +import json +import os + +ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +CONFIG_DIR = os.path.join(ROOT, "songforge", "config") + +DEFAULTS = { + "engine": {"server": "http://127.0.0.1:8188", "timeout_sec": 3600, + "poll_interval_sec": 10, "submit_retries": 3}, + "model": {"checkpoint": "ace_step_1.5_turbo_swl_fused.safetensors", + "lora": "swl_voice_best_ep257.safetensors", + "lora_model_strength": 0.5, "lora_clip_strength": 0.5, + "denied_checkpoints": ["yue2_3b_int8_convrot.safetensors", + "ace_step_1.5_turbo_aio.safetensors"]}, + "sampler": {"shift": 5.5, "sampling": "flow", "steps": 28, "cfg": 2.0, + "sampler_name": "euler", "scheduler": "simple", "denoise": 1.0}, + "encode": {"cfg_scale": 3.0, "temperature": 0.68, "top_p": 0.88, + "top_k": 50, "min_p": 0.02, "generate_audio_codes": True, + "timesignature": "4"}, + "paths": {"workflow_template": "urbanhiphop-身体先听懂.json", + "runs_dir": "songforge/data/runs", + "tmp_dir": "songforge/data/tmp", + "lyrics_dir": "songforge/input/lyrics", + "qc_cache_dir": "songforge/cache/qc", + "style_cache_dir": "songforge/cache/style", + "index_dir": "songforge/index", + "catalog_file": "songforge/index/catalog.jsonl", + "baseline_dir": "songforge/config/baseline", + "comfy_root": None, + "qc_python": None, + "music_dir": None, + "output_prefix": "MyMusic", + "output_format": "flac"}, + "song": {"language": "zh", "time_signature": "4", + "duration_range": [150, 200], + "duration_extra_ranges": [[100, 130]]}, + "lyrics": {"sing_bar_ratio": 0.70, "intro_bars": 8, + "syllable_per_bar_min": 4.40, "syllable_per_bar_max": 7.80, + "line_syllable_max": 12, "ideal_line_syllable": [5, 12], + "profiles": { + "sparse": { + "sing_bar_ratio": 0.25, + "syllable_per_bar_min": 4.40, + "syllable_per_bar_max": 7.80, + "line_syllable_max": 12, + "ideal_line_syllable": [3, 12], + } + }}, + "rules": {"trigger_word": "swlvoice", "intro_bars": 8}, +} + + +def _deep_merge(base, patch): + for k, v in patch.items(): + if k.startswith("_"): + continue + if isinstance(v, dict) and isinstance(base.get(k), dict): + _deep_merge(base[k], v) + else: + base[k] = v + return base + + +def _read(name): + p = os.path.join(CONFIG_DIR, name) + if not os.path.exists(p): + return {} + with open(p, encoding="utf-8") as f: + return json.load(f) + + +def load(overrides=None): + """返回最终配置 dict。overrides 是 {'sampler.shift': 5.5} 这种点号路径。""" + cfg = copy.deepcopy(DEFAULTS) + _deep_merge(cfg, _read("default.json")) + _deep_merge(cfg, _read("local.json")) + _deep_merge(cfg, _env_overrides()) + if overrides: + for path, val in overrides.items(): + if val is None: + continue + _set(cfg, path, val) + return cfg + + +def _set(cfg, path, val): + keys = path.split(".") + node = cfg + for k in keys[:-1]: + node = node.setdefault(k, {}) + node[keys[-1]] = val + + +_ENV_MAP = { + "SONGFORGE_SERVER": "engine.server", + "SONGFORGE_TIMEOUT": "engine.timeout_sec", + "SONGFORGE_CHECKPOINT": "model.checkpoint", + "SONGFORGE_LORA": "model.lora", + "SONGFORGE_LORA_STRENGTH": "model.lora_model_strength", + "SONGFORGE_SHIFT": "sampler.shift", + "SONGFORGE_STEPS": "sampler.steps", + "SONGFORGE_CFG": "sampler.cfg", + "SONGFORGE_ENC_CFG": "encode.cfg_scale", + "SONGFORGE_TEMPERATURE": "encode.temperature", + "SONGFORGE_TOP_P": "encode.top_p", + "SONGFORGE_TOP_K": "encode.top_k", + "SONGFORGE_MIN_P": "encode.min_p", + "SONGFORGE_TEMPLATE": "paths.workflow_template", + "SONGFORGE_RUNS": "paths.runs_dir", + "SONGFORGE_TMP": "paths.tmp_dir", + "SONGFORGE_LYRICS_DIR": "paths.lyrics_dir", + "SONGFORGE_QC_CACHE": "paths.qc_cache_dir", + "SONGFORGE_STYLE_CACHE": "paths.style_cache_dir", + "SONGFORGE_OUTPUT_PREFIX": "paths.output_prefix", + "SONGFORGE_LANGUAGE": "song.language", + "SONGFORGE_COMFY_ROOT": "paths.comfy_root", + "SONGFORGE_QC_PYTHON": "paths.qc_python", + "SONGFORGE_MUSIC_DIR": "paths.music_dir", +} + +_NUMERIC = {"engine.timeout_sec", "model.lora_model_strength", + "model.lora_clip_strength", "sampler.shift", "sampler.steps", + "sampler.cfg", "sampler.denoise", "encode.cfg_scale", + "encode.temperature", "encode.top_p", "encode.top_k", + "encode.min_p", "lyrics.sing_bar_ratio", "lyrics.intro_bars", + "lyrics.syllable_per_bar_min", "lyrics.syllable_per_bar_max", + "lyrics.line_syllable_max"} + + +def _env_overrides(): + out = {} + for env_key, path in _ENV_MAP.items(): + raw = os.environ.get(env_key) + if raw is None or raw == "": + continue + if path in _NUMERIC: + try: + raw = float(raw) + if raw.is_integer() and path not in ("sampler.shift", + "sampler.cfg", + "encode.cfg_scale", + "encode.temperature", + "encode.top_p", + "encode.min_p", + "lyrics.sing_bar_ratio", + "model.lora_model_strength", + "model.lora_clip_strength"): + raw = int(raw) + except ValueError: + continue + elif raw.lower() in ("true", "false"): + raw = raw.lower() == "true" + node = out + keys = path.split(".") + for k in keys[:-1]: + node = node.setdefault(k, {}) + node[keys[-1]] = raw + return out + + +def styles(): + return _read("styles.json") + + +def lexicon(): + return _read("lexicon.json") + + +def baseline_path(name): + """config/baseline/ 绝对路径。""" + return os.path.join(CONFIG_DIR, "baseline", name) diff --git a/config/local.json.example b/config/local.json.example new file mode 100644 index 0000000..80615a1 --- /dev/null +++ b/config/local.json.example @@ -0,0 +1,11 @@ +{ + "_comment": "复制为 local.json(已 gitignore)。只放本机绝对路径,勿提交。", + "paths": { + "comfy_root": "D:/CodeFiles/Thinking/ComfyUI-MiniMaxH3", + "qc_python": "C:/comfyui-venv/Scripts/python.exe", + "music_dir": "D:/CodeFiles/Thinking/ComfyUI-MiniMaxH3/output/MyMusic" + }, + "engine": { + "server": "http://127.0.0.1:8188" + } +} diff --git a/config/styles.json b/config/styles.json new file mode 100644 index 0000000..d58b197 --- /dev/null +++ b/config/styles.json @@ -0,0 +1,549 @@ +{ + "_comment": "风格字典。style 供 --style 引用;bpm/key 为推荐值,可被命令行覆盖。", + "urbanhiphop": { + "style": "Chinese urban hiphop", + "bpm": 94, + "key": "F minor", + "duration": 165, + "instruments": "hard attacking kick, crisp layered snare, tight closed hi-hats with triplet rolls, aggressive sub bass, stabbing brass hits, neon synth stabs, wide stereo hook", + "mood": "precise rhythmic groove, sharp and tight, punchy snares", + "extra": "male rap with baritone tone, mid low register, controlling clean vocal tone, crisp clear pronunciation" + }, + "chillhiphop": { + "style": "Chinese chill hiphop", + "bpm": 92, + "key": "C major", + "duration": 165, + "instruments": "soft round kick, brushed hi-hat, walking bass, warm rhodes chords, mellow keys", + "mood": "warm relaxed groove, gentle and easy", + "extra": "male singing with soft baritone tone, mid low register, clean controlled vocal tone" + }, + "partyyrap": { + "style": "Chinese party hiphop", + "bpm": 126, + "key": "E minor", + "duration": 160, + "instruments": "bright four on the floor kick, crisp snare, sixteenth hi-hats, bouncing 808 bass, synth arpeggio, brass stab", + "mood": "explosive high energy, maximum bounce", + "extra": "male rap with baritone tone, mid low register, crisp clear pronunciation" + }, + "pop": { + "style": "Chinese pop", + "bpm": 104, + "key": "F major", + "duration": 180, + "instruments": "tight punchy kick, crisp clap snare, modern synth bass, glossy electric piano, wide layered synths, claps", + "mood": "warm emotional groove, lifting chorus, clear contrast", + "extra": "clear male singing voice, stable tone, expressive but controlled" + }, + "punk": { + "style": "Chinese punk rock", + "bpm": 168, + "key": "E minor", + "duration": 155, + "instruments": "driving distorted power chords, crashing cymbals, fast eighth note drums, punchy bass, roaring guitar riff", + "mood": "relentless high energy, aggressive and fast, shouted chorus", + "extra": "male vocal with gritty mid range, aggressive delivery, clear pronunciation" + }, + "funkhiphop": { + "style": "Chinese funky hiphop", + "bpm": 118, + "key": "G major", + "duration": 160, + "instruments": "four on the floor kick, clapping and cowbell, walking bass, jazzy rhodes chords, funk guitar chops, brass stabs", + "mood": "playful bouncy groove, tight and groovy", + "extra": "male rap with baritone tone, mid low register, crisp clear pronunciation" + }, + "discofunk": { + "style": "Chinese disco funk", + "bpm": 120, + "key": "A major", + "duration": 160, + "instruments": "four on the floor kick, handclaps and cowbell, walking bass, wah guitar, bright brass section, synth arpeggio", + "mood": "carefree party energy, playful and bouncy", + "extra": "male singing with bright tone, playful delivery, clear pronunciation" + }, + "emopunk": { + "style": "Chinese emotional punk rock", + "bpm": 152, + "key": "A minor", + "duration": 175, + "instruments": "clean chiming guitars, driving bass, steady rock drums, wide reverb, melodic lead line", + "mood": "slow burning build, restrained verse and loud chorus", + "extra": "male singing with clear mid range, honest emotional delivery" + }, + "jazzhiphop": { + "style": "Chinese jazz hiphop", + "bpm": 92, + "key": "Eb major", + "duration": 160, + "instruments": "groovy electric piano, walking bass, brass accents, shuffle drums, pocket groove, tight snare", + "mood": "smooth pocket groove, laid back but crisp", + "extra": "male rap with baritone tone, mid low register, crisp clear pronunciation" + }, + "guofeng": { + "style": "Chinese traditional style hiphop", + "bpm": 99, + "key": "F minor", + "duration": 200, + "instruments": "erhu, pipa, bamboo flute, 808 sub bass, trap hats, traditional percussion", + "mood": "modern traditional fusion, cinematic and steady", + "extra": "male rap with baritone tone, mid low register, clear pronunciation" + }, + "folk": { + "style": "Chinese folk ballad", + "bpm": 76, + "key": "G major", + "duration": 175, + "instruments": "nylon string acoustic guitar fingerpicking, warm upright bass, brushed snare, subtle piano, natural room reverb", + "mood": "intimate warm storytelling, gentle and honest, close mic", + "extra": "male singing with warm soft tenor tone, close to microphone, gentle restrained delivery, clear pronunciation" + }, + "poppunk": { + "style": "Chinese pop punk", + "bpm": 156, + "key": "D major", + "duration": 158, + "instruments": "punchy palm muted power chords, driving bass, fast crisp drums, bright lead guitar riff, claps on chorus", + "mood": "upbeat catchy and bright, fast but singable, youthful", + "extra": "male singing with clear bright tone, energetic but controlled, crisp clear pronunciation" + }, + "indie": { + "style": "Chinese indie pop", + "bpm": 112, + "key": "A major", + "duration": 172, + "instruments": "clean electric guitar arpeggio, analog synth pad, warm bass, real drum kit, subtle tremolo", + "mood": "dreamy nostalgic and warm, gently swaying", + "extra": "male singing with soft airy tone, gentle close mic delivery, clear pronunciation" + }, + "electronic": { + "style": "Chinese future bass electro", + "bpm": 128, + "key": "F minor", + "duration": 168, + "instruments": "big supersaw synth lead, sidechained pad, punchy electro kick, trap snare, white noise riser, wide reverb", + "mood": "vast emotional build, euphoric and bright, festival anthem", + "extra": "male singing with clear bright tone, soaring controlled delivery, crisp clear pronunciation" + }, + "lofi": { + "style": "Chinese chillhop lofi", + "bpm": 82, + "key": "C major", + "duration": 168, + "instruments": "dusty jazz chords, upright bass, soft rimshot, vinyl crackle, mellow rhodes, subtle swing", + "mood": "laid back nostalgic calm, late night quiet", + "extra": "male singing with soft low tone, relaxed and close, clear pronunciation" + }, + "poprap": { + "_comment": "甜系中文 pop rap。tags 靠正向措辞自然带出鼓组,lean 模式不注入 RULE_NEG 那 20+ 条人声否定句。旧版 dy10_心动预警 的 lean tags 是 13 子句,本风格经 ironlaw 注入后约 43 子句。", + "style": "Chinese pop rap", + "bpm": 110, + "key": "C major", + "duration": 112, + "instruments": "bouncy kick and snare, sweet piano arpeggio, bright synth chords, hand claps, soft bass", + "mood": "sweet and bubbly, romantic crush feeling, upbeat and cute", + "extra": "male rap with clear tone, playful delivery, light and sweet, crisp clear pronunciation" + }, + "poprap2": { + "_comment": "甜系 pop rap 第二首,节奏更跳一点,hook 更口语。", + "style": "Chinese pop rap", + "bpm": 114, + "key": "D major", + "duration": 112, + "instruments": "bouncy kick and snare, sweet piano arpeggio, bright synth chords, hand claps, soft bass", + "mood": "sweet and playful, cheeky and cute, upbeat and light", + "extra": "male rap with clear tone, playful delivery, light and sweet, crisp clear pronunciation" + }, + "poprap3": { + "_comment": "甜系 pop rap 第三首,稍抒情一点,副歌更开阔。", + "style": "Chinese pop rap", + "bpm": 106, + "key": "A major", + "duration": 112, + "instruments": "bouncy kick and snare, sweet piano arpeggio, bright synth chords, hand claps, soft bass", + "mood": "sweet and warm, gentle crush feeling, bright and hopeful", + "extra": "male rap with clear tone, warm delivery, light and sweet, crisp clear pronunciation" + }, + "funkdance": { + "_comment": "街舞编舞导向 funk。动作引导词写进 instruments 与 extra:downbeat 停顿、popping 咬点、groove 起伏。lean 模式使用,正向措辞。", + "style": "Chinese funk dance groove", + "bpm": 106, + "key": "E minor", + "duration": 160, + "instruments": "punchy four on the floor kick, dry crisp snare, tight closed hi-hats, short slap bass, clavinet stabs, wah guitar, tambourine, hand claps", + "mood": "bouncy playful groove, clear strong downbeat accents, light and easy groove, danceable pocket", + "extra": "male singing with clear warm tone, playful rhythmic delivery, crisp clear pronunciation, short punchy phrases on the downbeat" + }, + "funkdance2": { + "_comment": "funk 第二首:更跳更复古,James Brown 式的连续十六分贝斯线。", + "style": "Chinese funk retro groove", + "bpm": 112, + "key": "A major", + "duration": 160, + "instruments": "hard four on the floor kick, crisp rimshot snare, tight hi-hats with sixteenth rolls, deep elastic bass, clavinet short stabs, brass stab accents, tambourine", + "mood": "explosive vintage groove, punchy and driving, playful rhythmic stop and start", + "extra": "male rap with warm clear tone, playful delivery, crisp clear pronunciation, short punchy phrases" + }, + "funkdance3": { + "_comment": "funk 第三首:中速偏 chill,适合 isolations 编排,律动留白多。", + "style": "Chinese chill funk groove", + "bpm": 96, + "key": "D major", + "duration": 160, + "instruments": "round punchy kick, brushed snare, soft closed hi-hats, round walking bass, jazzy rhodes chords, muted trumpet accents, light tambourine", + "mood": "smooth pocket groove, laid back and warm, gentle swing, easy going bounce", + "extra": "male singing with soft clear tone, warm relaxed delivery, crisp clear pronunciation" + }, + "funkdance4": { + "_comment": "funk 第四首:明快大调,适合 waacking / locking 快速手臂与定点。", + "style": "Chinese funk disco groove", + "bpm": 122, + "key": "G major", + "duration": 160, + "instruments": "bright four on the floor kick, crisp clap snare, tight hi-hats, elastic slap bass, clavi short stabs, bright brass accents, hand claps", + "mood": "bright cheerful groove, fast playful accents, crisp and clean, dance floor ready", + "extra": "male singing with bright clear tone, energetic playful delivery, crisp clear pronunciation, short punchy phrases" + }, + "funkdance5": { + "_comment": "funk 第五首:带一点 trap 底鼓的现代 funk,hook 更抓耳,适合编舞短视频。", + "style": "Chinese modern funk groove", + "bpm": 114, + "key": "F minor", + "duration": 160, + "instruments": "hard four on the floor kick, layered crisp snare, tight closed hi-hats, deep sub and elastic bass, clavinet stabs, wah guitar short hits, hand claps", + "mood": "heavy on the downbeat, dark warm groove, catchy stop and start pocket, confident bounce", + "extra": "male singing with clear low tone, confident rhythmic delivery, crisp clear pronunciation, short punchy phrases on the downbeat" + }, + "vocalchill": { + "_comment": "第三张专辑用:英文低声吟唱/哼唱,突出音乐本身,接近 Avicii 那种大编制 EDM + 克制人声。", + "style": "English melodic future bass, EDM, instrumental focus", + "bpm": 124, + "key": "A minor", + "duration": 175, + "instruments": "punchy electro kick, deep sub bass and walking bass, crisp clap, trap snare, huge supersaw lead, sidechain compressed pad, white noise riser, big reverb, melodic drop", + "mood": "vast uplifting emotional EDM, instrumental section showcase, euphoria then release", + "extra": "male low whisper vocal, hushed and intimate, sparse English words, mostly instrumental between phrases, no belting, no big singing" + }, + "_comment_cuckoo": "===== cuckoo 编舞批次(2026-10-05)=====", + "ckfunk": { + "_comment": "中文 108 主体档。James Brown 的 The One:kick 落在 beat 1 最重,十六分 ghost note 铺满小节。108 是 funk 中心速度,也是 urban 团体齐舞命中率最高的一档。", + "style": "Chinese funk dance groove", + "bpm": 108, + "key": "E minor", + "duration": 160, + "instruments": "heavy four on the floor kick on beat one, dry crisp snare with ghost notes on every sixteenth, tight closed hi-hats accenting the offbeats, short slap bass with thumb pops and octave jumps, clavinet chicken scratch stabs, wah guitar short chops, tambourine and hand claps", + "mood": "bouncy playful groove, clear strong downbeat accents, stop and start pocket, light and easy, danceable", + "extra": "male singing with clear warm tone, playful rhythmic delivery, crisp clear pronunciation, short punchy phrases landing on the downbeat" + }, + "ckfunkretro": { + "_comment": "中文 112 复古档。十六分音符滚奏 hi-hat + rimshot,James Brown 式连续贝斯线,适合大开大合的 frame。", + "style": "Chinese funk retro groove", + "bpm": 112, + "key": "A major", + "duration": 160, + "instruments": "hard four on the floor kick, crisp rimshot snare, tight hi-hats with sixteenth rolls, deep elastic slap bass line, clavinet short stabs, brass stab accents on the offbeat, tambourine", + "mood": "explosive vintage groove, punchy and driving, playful rhythmic stop and start", + "extra": "male singing with warm clear tone, playful delivery, crisp clear pronunciation, short punchy phrases" + }, + "ckchillfunk": { + "_comment": "中文 98 chill 档。留白多、摆动大,适合 isolations 编排和慢速数拍教学,对应《两难》那种慢速解析路线。", + "style": "Chinese chill funk groove", + "bpm": 98, + "key": "D major", + "duration": 160, + "instruments": "round punchy kick, brushed snare, soft closed hi-hats, round walking bass, jazzy rhodes chords, muted trumpet accents, light tambourine, gentle swing", + "mood": "smooth pocket groove, laid back and warm, gentle swing, easy going bounce", + "extra": "male singing with soft clear tone, warm relaxed delivery, crisp clear pronunciation" + }, + "cktrapfunk": { + "_comment": "中文 122 快速档。trap 底鼓 + funk 十六分,体感按 half-time 走,适合抖音短视频卡点快闪。", + "style": "Chinese modern trap funk groove", + "bpm": 122, + "key": "F minor", + "duration": 160, + "instruments": "hard four on the floor kick hitting on every single beat, layered crisp snare, tight closed hi-hats, deep sub bass and elastic bass pumping on every beat, clavinet short stabs, wah guitar short hits, hand claps, half time feel", + "mood": "heavy on the downbeat, dark warm groove, catchy stop and start pocket, confident bounce", + "extra": "male singing with clear low tone, confident rhythmic delivery, crisp clear pronunciation, short punchy phrases on the downbeat" + }, + "ckfunken": { + "_comment": "英文 110 主体档。同 ckfunk 的鼓组,英文咬字需要更松的元音,所以 extra 写 melismatic 一点点、phrasing 更 swing。", + "style": "English funk dance groove", + "bpm": 110, + "key": "F minor", + "duration": 160, + "instruments": "heavy four on the floor kick hitting on every single beat, dry crisp snare with ghost notes, tight closed hi-hats accenting the offbeats, short slap bass with thumb pops pumping on every beat, clavinet chicken scratch stabs, wah guitar chops, tambourine and hand claps", + "mood": "bouncy playful groove, clear strong downbeat accents, stop and start pocket, danceable", + "extra": "male singing with clear warm tone, playful rhythmic phrasing, crisp clear English pronunciation, short punchy phrases landing on the downbeat" + }, + "ckfunkdisco": { + "_comment": "英文 118 复古 disco 档。对应《Talk Dirty》那种快卡点,brass stab 更亮,十六分更密。", + "style": "English funk disco groove", + "bpm": 118, + "key": "A major", + "duration": 160, + "instruments": "bright four on the floor kick, crisp clap snare, tight hi-hats with sixteenth rolls, elastic slap bass, clavi short stabs, bright brass stab accents, hand claps", + "mood": "bright cheerful groove, fast playful accents, crisp and clean, dance floor ready", + "extra": "male singing with bright clear tone, energetic playful delivery, crisp clear English pronunciation, short punchy phrases" + }, + "ckrbslow": { + "_comment": "英文 96 slow R&B 档。对应《想想念念》《还是会想你》那条抒情编舞线:isolations、延伸细腻动作、情绪拉满。", + "style": "English slow R&B groove with funk drums", + "bpm": 96, + "key": "E minor", + "duration": 160, + "instruments": "round punchy kick, brushed snare, soft closed hi-hats, round walking bass, warm rhodes chords, muted trumpet accents, light tambourine, gentle swing", + "mood": "smooth warm pocket groove, laid back and tender, gentle swing, emotional and easy going", + "extra": "male singing with soft warm tone, tender relaxed delivery, crisp clear English pronunciation, smooth legato phrasing" + }, + "cktrapfunken": { + "_comment": "英文 124 快速档。trap 底鼓 + funk 十六分,体感 half-time,适合快手 waacking 和定点。", + "style": "English modern trap funk groove", + "bpm": 124, + "key": "G minor", + "duration": 160, + "instruments": "hard four on the floor kick hitting on every single beat, layered crisp snare, tight closed hi-hats, deep sub bass and elastic bass pumping on every beat, clavinet short stabs, wah guitar short hits, hand claps, half time feel", + "mood": "heavy on the downbeat, dark confident groove, catchy stop and start pocket, fast bounce", + "extra": "male singing with clear low tone, confident rhythmic delivery, crisp clear English pronunciation, short punchy phrases on the downbeat" + }, + "_comment_birthday": "===== 一棠生日·术力口三版(2026-10-05)=====", + "vocaloid_fb": { + "_comment": "术力口生日 V1:高速可爱 future bass,最具 Vocaloid 感的一版。lean 模式使用,中日双语歌词。", + "style": "Chinese Vocaloid style future bass, J-pop electro", + "bpm": 160, + "key": "F major", + "duration": 118, + "instruments": "four on the floor kick, bright snare rolls, fast sixteenth note hi-hats, bouncy synth bass, glittery arpeggio, supersaw lead, xylophone-like bell melody, hand claps, cute chiptune accents", + "mood": "bright and hyper cute, happy birthday celebration, energetic and bouncy, youthful 10s vibe, sparkling and uplifting", + "extra": "cute high pitched female singing voice, Vocaloid style vocal, bright girlish tone, clear Japanese and Mandarin pronunciation, cheerful energetic delivery, catchy melodic phrases" + }, + "vocaloid_euro": { + "_comment": "术力口生日 V2:日系 Eurobeat / 高速蹦迪感,副歌洗脑。lean 模式使用,中日双语。", + "style": "Japanese Eurobeat style J-pop, dance pop", + "bpm": 155, + "key": "D major", + "duration": 118, + "instruments": "fast four on the floor kick, powerful snare, rolling sixteenth hi-hats, driving synth bass, bright brass stabs, retro synth lead, piano staccato, tambourine, hand claps on every beat", + "mood": "explosive high energy, happy party feeling, catchy singalong chorus, upbeat and joyful, celebratory dance floor vibe", + "extra": "bright powerful female singing voice, clear energetic tone, Japanese and Mandarin bilingual vocals, strong catchy chorus, upbeat rhythmic delivery" + }, + "vocaloid_jpop": { + "_comment": "术力口生日 V3:稍抒情 J-pop,旋律好听,适合全家一起唱。lean 模式使用,中日双语。", + "style": "Japanese pop ballad, J-pop style", + "bpm": 128, + "key": "E major", + "duration": 122, + "instruments": "warm acoustic guitar strumming, gentle piano arpeggio, soft kick and snare, warm bass, strings pad, glockenspiel melody, light tambourine, warm brass accents on chorus", + "mood": "warm heartfelt emotion, gentle and sincere, uplifting chorus, sweet birthday blessing, family friendly and touching", + "extra": "warm clear female singing voice, sweet gentle tone, Japanese and Mandarin bilingual vocals, emotional but controlled delivery, clear pronunciation, singalong melody" + }, + "_comment_insomnia": "===== 失眠三部曲(2026-10-06)=====", + "insomnia1_rnb": { + "_comment": "失眠三部曲 1/3《三点半》:都市抒情 R&B,凌晨空房间的空旷感。中低音叙事,气声多,不推满。shift=5.0 贴少年音中低音区。", + "style": "Chinese emotional R&B ballad", + "bpm": 78, + "key": "C minor", + "duration": 175, + "instruments": "soft deep sub bass on every beat, gentle rim click rhythm, warm piano arpeggios, atmospheric synth pad, subtle vinyl crackle, spacious reverb, slow grooving", + "mood": "late night quiet, lonely and introspective, empty room echo, gentle melancholy, slow burning emotion", + "extra": "male singing with soft youthful tone, mid low register, intimate close mic delivery, breathy and fragile, gentle restrained emotion, clear pronunciation, no belting, no high notes" + }, + "insomnia2_emo": { + "_comment": "失眠三部曲 2/3《旧号码》:Emo R&B 慢板,情绪最低点。钢琴为主,弦乐铺底,鼓点极度克制。shift=5.0 贴中低音脆弱感。", + "style": "Chinese emo R&B slow ballad", + "bpm": 72, + "key": "A minor", + "duration": 200, + "instruments": "slow piano chords, deep sub bass on every beat, soft string pad, minimal brushed snare, spacious reverb, ambient texture", + "mood": "deep sadness, heartbroken and tender, quiet crying feeling, restrained verse and emotional chorus, intimate vulnerability", + "extra": "male singing with soft fragile youthful tone, mid low register, intimate close mic, breathy delivery, trembling vulnerable emotion, gentle crying tone, no belting, no high notes, clear pronunciation" + }, + "insomnia3_folk": { + "_comment": "失眠三部曲 3/3《天又亮了》:民谣 R&B,清晨麻木的温柔。木吉他 + 轻拍军鼓,情绪是'熬过去了'的平静。shift=5.0。时长 200s 给叙事留呼吸。", + "style": "Chinese folk R&B, morning after vibe", + "bpm": 85, + "key": "G major", + "duration": 200, + "instruments": "warm acoustic guitar fingerpicking, soft sub bass on every beat, brushed snare, gentle synth pad, light tambourine, natural room reverb, morning ambient feel", + "mood": "calm resignation, gentle acceptance, quiet hope, numb but soft, morning after a long night, bittersweet warmth", + "extra": "male singing with soft warm youthful tone, mid low register, gentle honest delivery, intimate close mic, restrained emotion, clear pronunciation, no belting, no high notes" + }, + "_comment_edm": "===== 甜嗓电音三部曲(2026-10-06)中 + 英 =====", + "edm1_chudian": { + "_comment": "甜嗓电音 1/6《触电》:明快 EDM-pop,一见钟情的甜。BPM 128,大调,合成器钩子 + 弹跳鼓点。shift=5.2 提亮少年音。", + "style": "Chinese EDM pop, upbeat electro pop", + "bpm": 128, + "key": "C major", + "duration": 195, + "instruments": "four on the floor kick, bright snare on 2 and 4, fast sixteenth hi-hats, bouncy synth bass, catchy synth lead hook, bright pluck arpeggio, piano stabs, wide stereo chorus, sidechained pads", + "mood": "sweet energetic, bubbly happy feeling, butterflies in stomach, first love excitement, catchy singalong chorus, youthful upbeat vibe", + "extra": "male singing with bright sweet youthful tone, mid high register, energetic delivery, clear pronunciation, catchy melodic chorus, upbeat rhythmic feel, Justin Bieber style pop vocal, solo male vocal only, no female vocals, no backup vocals, no choir, no duet, no vocal chops" + }, + "edm2_pin dao": { + "_comment": "甜嗓电音 2/6《专属频道》:R&B 电音,暧昧拉扯感。BPM 120,中速,808 + 合成器铺垫。shift=5.0。", + "style": "Chinese R&B electro pop, mid tempo", + "bpm": 120, + "key": "E major", + "duration": 195, + "instruments": "trap style 808 bass, crisp snare rolls, fast hi-hat triplets, warm rhodes chords, shiny synth pluck, soft pad, sidechained sub bass", + "mood": "flirty and playful, sweet tension, crush feeling, smooth grooving, catchy melodic hooks, confident swagger mixed with vulnerability", + "extra": "male singing with smooth sweet youthful tone, mid register, rhythmic delivery, clear pronunciation, catchy chorus with melodic hooks, modern pop R&B vocal style, solo male vocal only, no female vocals, no backup vocals, no choir, no duet, no vocal chops" + }, + "edm3_yehang": { + "_comment": "甜嗓电音 3/6《夜航》:最嗨 EDM 副歌,深夜公路释放感。BPM 138,synth-pop + EDM drop。shift=5.2。", + "style": "Chinese EDM pop, synthwave dance pop, big chorus", + "bpm": 138, + "key": "D major", + "duration": 200, + "instruments": "powerful four on the floor kick, massive snare, fast rolling hi-hats, driving synth bass, big supersaw lead, retro synth arpeggio, wide stereo pad, build up risers, impactful drop", + "mood": "euphoric freedom, night driving adrenaline, big emotional chorus, uplifting release, triumphant feeling, neon lights energy, dance floor ready", + "extra": "male singing with bright powerful youthful tone, mid high register, energetic belting on chorus, clear pronunciation, big catchy hooks, modern pop EDM vocal style, Justin Bieber Purpose era vibe, solo male vocal only, no female vocals, no backup vocals, no choir, no duet, no vocal chops" + }, + "edm4_electric": { + "_comment": "甜嗓电音 4/6《Electric Touch》:英文甜系 EDM-pop,一见钟情。BPM 128,Justin Bieber 风格。shift=5.2。", + "style": "EDM pop, upbeat electro pop, Justin Bieber style", + "bpm": 128, + "key": "C major", + "duration": 195, + "instruments": "four on the floor kick, bright snare, fast sixteenth hi-hats, bouncy synth bass, catchy synth lead hook, pluck arpeggio, piano stabs, wide stereo chorus, sidechained pads", + "mood": "sweet energetic, bubbly happy, butterflies in stomach, first love excitement, catchy singalong chorus, youthful upbeat vibe, radio friendly pop", + "extra": "male singing with bright sweet youthful tone, mid high register, energetic delivery, clear English pronunciation, catchy melodic chorus, upbeat rhythmic feel, modern pop vocal style, solo male vocal only, no female vocals, no backup vocals, no choir, no duet, no vocal chops" + }, + "edm5_frequency": { + "_comment": "甜嗓电音 5/6《Frequency》:英文 R&B 电音,暧昧律动。BPM 120,R&B + electro。shift=5.0。", + "style": "R&B electro pop, mid tempo, modern pop R&B", + "bpm": 120, + "key": "E major", + "duration": 195, + "instruments": "trap style 808 bass, crisp snare rolls, fast hi-hat triplets, warm rhodes chords, shiny synth pluck, soft pad, sidechained sub bass, snap on backbeat", + "mood": "flirty and playful, sweet tension, crush feeling, smooth grooving, catchy melodic hooks, confident swagger mixed with vulnerability, modern pop vibe", + "extra": "male singing with smooth sweet youthful tone, mid register, rhythmic delivery, clear English pronunciation, catchy chorus with melodic hooks, modern pop R&B vocal style, solo male vocal only, no female vocals, no backup vocals, no choir, no duet, no vocal chops" + }, + "edm6_neon": { + "_comment": "甜嗓电音 6/6《Neon Drive》:英文 synthwave pop,最炸副歌。BPM 138,霓虹之夜。shift=5.2。", + "style": "synthwave pop, EDM pop, retro futuristic, big chorus", + "bpm": 138, + "key": "D major", + "duration": 200, + "instruments": "powerful four on the floor kick, massive snare, fast rolling hi-hats, driving synth bass, big supersaw lead, retro synth arpeggio, wide stereo pad, build up risers, impactful drop, neon synth sounds", + "mood": "euphoric freedom, night driving adrenaline, big emotional chorus, uplifting release, triumphant feeling, neon lights energy, dance floor ready, retro 80s vibe meets modern pop", + "extra": "male singing with bright powerful youthful tone, mid high register, energetic belting on chorus, clear English pronunciation, big catchy hooks, modern pop EDM vocal style, The Weeknd meets Justin Bieber vibe, solo male vocal only, no female vocals, no backup vocals, no choir, no duet, no vocal chops" + }, + "_comment_trending": "===== 平台热门电音 5 首(2026-10-06)3 热门 + 2 爱情 =====", + "trend1_miaozhen": { + "_comment": "热门 1/5《秒针》:UK Garage/DnB PinkPantheress 风格。快碎拍+甜嗓轻语,TikTok 爆款。BPM 160,A minor。shift=5.2。", + "style": "UK garage pop, drum and bass inspired, PinkPantheress style, fast breakbeat pop", + "bpm": 160, + "key": "A minor", + "duration": 180, + "instruments": "fast 2-step breakbeat, chopped amen break, bouncy sub bass, bright synth stabs, atmospheric pad, retro 2000s garage feel, sidechained bass, crisp hi-hats, snare on backbeat", + "mood": "anxious crush feeling, waiting for reply, sweet nervousness, fast paced but gentle delivery, nostalgic Y2K vibe, TikTok viral energy, short catchy phrases", + "extra": "male singing with sweet soft youthful tone, mid high register, light breathy delivery, almost whispered verses, catchy melodic chorus, fast rhythmic phrasing, clear pronunciation, PinkPantheress style vocal, British pop feel, solo male vocal only, no female vocals, no backup vocals, no choir, no duet, no vocal chops" + }, + "trend2_riluo": { + "_comment": "热门 2/5《日落大道》:Amapiano/Afrobeats pop。非洲律动+log drum+口哨旋律。BPM 112,F major。shift=5.0。", + "style": "Amapiano pop, Afrobeats dance pop, warm groove", + "bpm": 112, + "key": "F major", + "duration": 195, + "instruments": "log drum bass, soft shaker pattern, conga percussion, warm rhodes chords, guitar pluck melody, whistle hook, smooth pad, sidechained sub bass, gentle four on the floor, tambourine", + "mood": "warm summer evening, romantic sunset walk, feel good groove, gentle danceable, sweet and relaxed, African dance pop meets Chinese pop, infectious rhythm", + "extra": "male singing with warm sweet youthful tone, mid register, smooth groovy delivery, clear pronunciation, catchy melodic hooks, warm intimate feel, modern Afro pop vocal style, solo male vocal only, no female vocals, no backup vocals, no choir, no duet, no vocal chops" + }, + "trend3_xinhao": { + "_comment": "热门 3/5《信号干扰》:Hyperpop/Bass DJ。重低音 drop+电子音效+洗脑 hook。抖音 DJ 风。BPM 140,E minor。shift=5.2。", + "style": "hyperpop, bass heavy DJ pop, Chinese TikTok DJ style, electronic dance pop", + "bpm": 140, + "key": "E minor", + "duration": 190, + "instruments": "massive bass drop, punchy four on the floor kick, aggressive snare, fast hi-hat rolls, distorted synth lead, riser build ups, impactful drop section, sidechained everything, electronic sound effects, sub bass wobble", + "mood": "anxious and catchy, dramatic tension, electronic energy, club ready, TikTok viral, mysterious atmosphere, dramatic build and release, dark but sweet", + "extra": "male singing with bright sweet youthful tone, mid high register, energetic rhythmic delivery, clear pronunciation, catchy repetitive hooks, spoken word elements, modern hyperpop vocal style, Charli XCX meets Chinese pop, solo male vocal only, no female vocals, no backup vocals, no choir, no duet, no vocal chops" + }, + "trend4_xiaoyuzhou": { + "_comment": "爱情 1/2《小宇宙》:UK Garage sweet pop。甜系 2-step 情歌,两个人的小世界。BPM 130,G major。shift=5.2。", + "style": "UK garage sweet pop, 2-step love song, gentle electronic pop", + "bpm": 130, + "key": "G major", + "duration": 190, + "instruments": "2-step garage beat, shuffled hi-hats, warm sub bass, bright synth chords, pluck arpeggio, soft pad, piano stabs, gentle sidechain, sweet melody, romantic feel", + "mood": "sweet romantic, cozy intimate, two people in their own world, warm and fuzzy, gentle danceable, cute and innocent love, feel good", + "extra": "male singing with sweet warm youthful tone, mid high register, gentle tender delivery, clear pronunciation, catchy melodic chorus, sweet love song vocal, Justin Bieber early era vibe, solo male vocal only, no female vocals, no backup vocals, no choir, no duet, no vocal chops" + }, + "trend5_gaobai": { + "_comment": "爱情 2/2《告白前夜》:Synth-pop 电子情歌。告白前的紧张期待。BPM 100,D major。shift=5.0。", + "style": "synth pop ballad, electronic love song, emotional pop", + "bpm": 100, + "key": "D major", + "duration": 200, + "instruments": "warm synth pad, gentle electronic kick, soft snare, arpeggiated synth, sub bass, piano chords, dreamy atmosphere, light hi-hats, building synth layers, emotional strings, spacious reverb", + "mood": "nervous anticipation, tender vulnerability, night before confession, sleepless and lovesick, sweet anxiety, hopeful but scared, emotional climax, heartfelt", + "extra": "male singing with sweet tender youthful tone, mid register, emotional delivery, clear pronunciation, building from soft verse to emotional chorus, breathy verses, heartfelt chorus, gentle falsetto touches, modern synth pop vocal, The Weeknd ballad style, solo male vocal only, no female vocals, no backup vocals, no choir, no duet, no vocal chops" + }, + + "moto_yehang": { + "_comment": "夜航摩托车版:引擎轰鸣 + synthwave 飞驰感。BPM 145,D major。shift=5.2。摩托车公路飞驰。", + "style": "synthwave rock pop, motorcycle highway driving, energetic electronic rock", + "bpm": 145, + "key": "D major", + "duration": 200, + "instruments": "driving four on the floor kick, powerful snare, fast rolling hi-hats, roaring synth bass like engine rev, big supersaw lead, retro synth arpeggio, wide stereo pad, guitar power chords, build up risers, impactful drop, wind sound effect, road trip energy", + "mood": "euphoric freedom, motorcycle highway adrenaline, wind in face, big emotional chorus, uplifting release, triumphant feeling, night ride energy, dance floor ready, retro 80s meets modern pop", + "extra": "male singing with bright powerful youthful tone, mid high register, energetic belting on chorus, clear pronunciation, big catchy hooks, modern pop EDM vocal style, solo male vocal only, no female vocals, no backup vocals, no choir, no duet, no vocal chops" + }, + + "edm3_yehang_vox": { + "_comment": "夜航声线优先版:降BPM到128、C大调、中低音区,最大限度还原参考声线。shift=4.0。", + "style": "synthwave pop, night drive electronic pop, mid tempo EDM pop", + "bpm": 128, + "key": "C major", + "duration": 200, + "instruments": "four on the floor kick, crisp snare, steady rolling hi-hats, warm synth bass, soft supersaw lead, retro arpeggio, wide stereo pad, gentle build ups, emotional drop, dreamy atmosphere, reverb and delay", + "mood": "euphoric freedom, night drive feeling, wind in hair, emotional release, uplifting but intimate, nostalgic synthwave vibe, warm energy, modern pop sensibility", + "extra": "male singing with natural youthful tone, mid register, conversational delivery, clear pronunciation, melodic chorus, preserve exact voice timbre and color, true to reference voice, natural breathing, smooth phrasing, no belting, no screaming, no vocal acrobatics, stay in natural vocal range, solo male vocal only, no female vocals, no backup vocals, no choir, no duet, no vocal chops" + }, + + "edm6_neon_vox": { + "_comment": "Neon Drive声线优先版:降BPM到128、C大调、中低音区,英文演唱最大限度还原参考声线。shift=4.0。", + "style": "synthwave pop, neon night drive electronic pop, mid tempo EDM pop", + "bpm": 128, + "key": "C major", + "duration": 200, + "instruments": "four on the floor kick, crisp snare, steady rolling hi-hats, warm synth bass, soft supersaw lead, retro arpeggio, wide stereo pad, gentle build ups, emotional drop, dreamy atmosphere, reverb and delay", + "mood": "euphoric freedom, neon night drive feeling, city lights, emotional release, uplifting but intimate, nostalgic synthwave vibe, warm energy, modern pop sensibility", + "extra": "male singing with natural youthful tone, mid register, conversational delivery, clear English pronunciation, melodic chorus, preserve exact voice timbre and color, true to reference voice, natural breathing, smooth phrasing, no belting, no screaming, no vocal acrobatics, stay in natural vocal range, solo male vocal only, no female vocals, no backup vocals, no choir, no duet, no vocal chops" + }, + + "trend3_xinhao_pro": { + "_comment": "信号干扰Pro版:Hyperpop + Bass House 升级版,编曲更炸更有层次,drop 更有冲击力。BPM 140,E minor。shift=5.2。100% 男声独唱。", + "style": "hyperpop bass house, aggressive electronic pop, glitchy EDM", + "bpm": 140, + "key": "E minor", + "duration": 200, + "instruments": "massive sub bass drop, punchy four on the floor kick, aggressive snare rolls, fast hi-hat triplets, distorted synth lead, glitchy synth stabs, supersaw chords, riser build ups, impactful drops, synth brass hits, pluck arpeggios, heavy sidechain compression, stereo widening, fm bass, 808 sub bass, breakdown with piano, second drop bigger than first, epic build", + "mood": "chaotic energy, emotional turbulence, signal distortion feeling, aggressive but catchy, intense chorus, glitchy vibe, futuristic electronic, dance floor banger, headbanging energy, triumphant drop", + "extra": "male singing with bright sweet youthful tone, mid high register, energetic rhythmic delivery, clear pronunciation, catchy repetitive hooks, spoken word sections that build into singing, powerful chorus, modern hyperpop vocal style, Charli XCX meets 100 gecs energy but male voice, solo male vocal only, no female vocals, no backup vocals, no choir, no duet, no vocal chops, no vocal samples" + }, + + "futureremix": { + "_comment": "Future House EDM 通用:弹跳合成贝斯+supersaw+drop结构。128 BPM,F minor,时长168s。", + "style": "modern future house EDM, bouncy club electronic, big room dance", + "bpm": 128, + "key": "F minor", + "duration": 168, + "instruments": "punchy four on the floor kick, bouncy offbeat synth bass, deep sub bass, crisp claps, tight closed hi-hats, driving future house bassline, wide supersaw chords, plucky synth lead melody, offbeat bass stabs, heavy sidechain compression, white noise build, euphoric drop, second drop bigger than first, club dance groove", + "mood": "euphoric club energy, high voltage night drive, dance floor rush, sweet crush turned explosive, uplifting release, bouncy and hyped, anthemic festival feeling, nonstop momentum", + "extra": "male singing with bright energetic tone, punchy rhythmic delivery, clear pronunciation, catchy repeating hook, strong anthemic chorus, modern EDM vocal style, forward and confident, solo male vocal only, no female vocals, no backup vocals, no choir, no duet, no vocal chops" + }, + + "test_filler_fix": { + "_comment": "衬音修复测试 demo:90s 短时长,验证 lean 模式下 RULE_NO_FILLER 是否生效。", + "style": "future house electronic pop, upbeat EDM pop", + "bpm": 128, + "key": "C major", + "duration": 130, + "instruments": "punchy four on the floor kick, bouncy offbeat synth bass, crisp claps, tight closed hi-hats, warm supersaw chords, plucky synth lead, heavy sidechain, euphoric drop", + "mood": "upbeat euphoric, dance floor energy, sweet and catchy, uplifting release", + "extra": "male singing with bright sweet youthful tone, mid high register, energetic rhythmic delivery, clear pronunciation, catchy hooks, solo male vocal only, no female vocals, no backup vocals, no choir, no duet, no vocal chops" + } +} \ No newline at end of file diff --git a/core/__init__.py b/core/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/core/catalog.py b/core/catalog.py new file mode 100644 index 0000000..3bfbb94 --- /dev/null +++ b/core/catalog.py @@ -0,0 +1,173 @@ +# -*- coding: utf-8 -*- +"""全局索引 catalog.jsonl:只存元数据,可从 runs 重建。""" +import json +import os +import re + +from . import paths as pmod + + +def catalog_path(cfg): + p = cfg["paths"].get("catalog_file") or "songforge/index/catalog.jsonl" + return pmod.under_workflows(p) + + +def day_dir(runs_root, day=None): + """runs_root/YYYY-MM-DD/""" + import datetime + if not day: + day = datetime.date.today().isoformat() + return os.path.join(runs_root, day) + + +def allocate_run_dir(runs_root, run_id, day=None): + """占位 data/runs/YYYY-MM-DD//,返回绝对路径。""" + d = day_dir(runs_root, day) + pmod.ensure_dir(d) + path = os.path.join(d, run_id) + os.makedirs(path, exist_ok=False) + return path + + +def find_run_dir(runs_root, run_id): + """在按日目录、_migrated、扁平根下查找 run 目录。""" + rid = (run_id or "").strip().rstrip("/\\") + if not rid: + return None + if os.path.isdir(rid) and os.path.isfile(os.path.join(rid, "manifest.json")): + return os.path.abspath(rid) + base = os.path.basename(rid.replace("/", os.sep)) + if not os.path.isdir(runs_root): + return None + # 直接子目录(兼容旧扁平) + cand = os.path.join(runs_root, base) + if os.path.isfile(os.path.join(cand, "manifest.json")): + return cand + # 按日 + for day in os.listdir(runs_root): + day_path = os.path.join(runs_root, day) + if not os.path.isdir(day_path): + continue + cand = os.path.join(day_path, base) + if os.path.isfile(os.path.join(cand, "manifest.json")): + return cand + # _migrated 下也可能套一层 + if day in ("_migrated",): + for sub in os.listdir(day_path): + cand = os.path.join(day_path, sub) + if os.path.basename(cand) == base and \ + os.path.isfile(os.path.join(cand, "manifest.json")): + return cand + # 全树兜底 + for root, dirs, files in os.walk(runs_root): + if os.path.basename(root) == base and "manifest.json" in files: + return root + return None + + +def _entry_from_manifest(rundir, man): + results = man.get("results") or [] + seeds = [r.get("seed") for r in results if r.get("seed") is not None] + files = [] + for r in results: + for fn in r.get("files") or []: + files.append(fn) + day = None + m = re.search(r"(\d{4}-\d{2}-\d{2})", rundir.replace("\\", "/")) + if m: + day = m.group(1) + elif man.get("created"): + day = str(man["created"])[:10] + return { + "run_id": man.get("run_id") or os.path.basename(rundir), + "date": day, + "title": man.get("title"), + "style": man.get("style"), + "bpm": man.get("bpm"), + "key": man.get("key"), + "duration": man.get("duration"), + "seeds": seeds, + "qc_verdict": man.get("qc_verdict"), + "audio_files": files, + "path": rundir.replace("\\", "/"), + } + + +def append_entry(cfg, entry): + path = catalog_path(cfg) + pmod.ensure_dir(os.path.dirname(path)) + with open(path, "a", encoding="utf-8") as f: + f.write(json.dumps(entry, ensure_ascii=False) + "\n") + return path + + +def update_qc_verdict(cfg, run_id, verdict): + """重写 catalog:更新匹配 run_id 的 qc_verdict。""" + path = catalog_path(cfg) + if not os.path.isfile(path): + return + rows = [] + with open(path, encoding="utf-8") as f: + for line in f: + line = line.strip() + if not line: + continue + try: + row = json.loads(line) + except ValueError: + continue + if row.get("run_id") == run_id: + row["qc_verdict"] = verdict + rows.append(row) + with open(path, "w", encoding="utf-8") as f: + for row in rows: + f.write(json.dumps(row, ensure_ascii=False) + "\n") + + +def rebuild(cfg): + """扫描 runs_dir,重写 catalog.jsonl。""" + runs_root = pmod.under_workflows(cfg["paths"]["runs_dir"]) + entries = [] + if os.path.isdir(runs_root): + for root, dirs, files in os.walk(runs_root): + if "manifest.json" not in files: + continue + mp = os.path.join(root, "manifest.json") + try: + with open(mp, encoding="utf-8") as f: + man = json.load(f) + except (ValueError, OSError): + continue + entries.append(_entry_from_manifest(root, man)) + entries.sort(key=lambda e: (e.get("date") or "", e.get("run_id") or "")) + path = catalog_path(cfg) + pmod.ensure_dir(os.path.dirname(path)) + with open(path, "w", encoding="utf-8") as f: + for e in entries: + f.write(json.dumps(e, ensure_ascii=False) + "\n") + return path, len(entries) + + +def list_entries(cfg, day=None, limit=50): + path = catalog_path(cfg) + if not os.path.isfile(path): + return [] + out = [] + with open(path, encoding="utf-8") as f: + for line in f: + line = line.strip() + if not line: + continue + try: + row = json.loads(line) + except ValueError: + continue + if day and row.get("date") != day: + continue + out.append(row) + out.reverse() + return out[:limit] + + +def entry_from_gen(rundir, manifest): + return _entry_from_manifest(rundir, manifest) diff --git a/core/engine.py b/core/engine.py new file mode 100644 index 0000000..a46b6a3 --- /dev/null +++ b/core/engine.py @@ -0,0 +1,120 @@ +# -*- coding: utf-8 -*- +"""ComfyUI 执行引擎:提交、轮询、重试、结果回收。只用标准库。""" +import json +import time +import urllib.error +import urllib.request + +from .errors import JobFailed, ServerError + + +class Engine(object): + def __init__(self, server, timeout=3600, poll_interval=10, + submit_retries=3, logger=None): + self.server = server.rstrip("/") + self.timeout = timeout + self.poll_interval = poll_interval + self.submit_retries = submit_retries + self.log = logger + + def _log(self, level, msg, **kw): + if self.log: + self.log.log(level, msg, **kw) + + def _get(self, path, timeout=15): + url = self.server + path + try: + with urllib.request.urlopen(url, timeout=timeout) as r: + return json.loads(r.read().decode("utf-8")) + except urllib.error.HTTPError as e: + raise ServerError("GET %s 返回 %s" % (path, e.code), e.read().decode()[:400]) + except Exception as e: + raise ServerError("GET %s 失败: %s" % (path, e)) + + def _post(self, path, payload, timeout=60): + url = self.server + path + req = urllib.request.Request( + url, data=payload.encode("utf-8"), + headers={"Content-Type": "application/json"}) + try: + with urllib.request.urlopen(req, timeout=timeout) as r: + return json.loads(r.read().decode("utf-8")) + except urllib.error.HTTPError as e: + raise ServerError("POST %s 返回 %s" % (path, e.code), + e.read().decode()[:600]) + except Exception as e: + raise ServerError("POST %s 失败: %s" % (path, e)) + + def health(self): + """返回 (ok, info)。不抛异常,供 doctor 使用。""" + try: + stats = self._get("/system_stats") + except ServerError as e: + return False, str(e) + dev = (stats.get("devices") or [{}])[0] + info = { + "device": dev.get("name", "?"), + "vram_total_gb": round(dev.get("vram_total", 0) / 1024 ** 3, 1), + "comfyui_version": stats.get("system", {}).get("comfyui_version", "?"), + } + q = self._get("/queue") + info["queue_running"] = len(q.get("queue_running") or []) + info["queue_pending"] = len(q.get("queue_pending") or []) + return True, info + + def queue(self): + return self._get("/queue") + + def submit(self, prompt_json): + """prompt_json 可以是 dict(API prompt)或 JSON 字符串。 + 最终 POST /prompt 的 body 是 {"prompt": }。""" + if isinstance(prompt_json, str): + prompt_json = json.loads(prompt_json) + payload = self._post("/prompt", json.dumps({"prompt": prompt_json}, ensure_ascii=False)) + pid = payload.get("prompt_id") + if not pid: + raise ServerError("提交成功但未返回 prompt_id", + json.dumps(payload, ensure_ascii=False)[:400]) + return pid + + def submit_retry(self, prompt_json): + last = None + for attempt in range(1, self.submit_retries + 1): + try: + return self.submit(prompt_json) + except ServerError as e: + last = e + self._log("warn", "提交失败(%d/%d): %s" + % (attempt, self.submit_retries, e)) + if attempt < self.submit_retries: + time.sleep(3 * attempt) + raise last + + def wait(self, pid, on_progress=None): + """轮询直到任务结束。返回 (outputs, errors)。""" + t0 = time.time() + tick = 0 + while time.time() - t0 < self.timeout: + time.sleep(self.poll_interval) + tick += 1 + h = self._get("/history/" + pid) + if pid not in h: + if on_progress: + on_progress(tick, int(time.time() - t0)) + continue + rec = h[pid] + errs = [m for m in rec.get("status", {}).get("messages", []) + if m[0] == "execution_error"] + outs = [a.get("filename") for o in rec.get("outputs", {}).values() + for a in o.get("audio", [])] + if errs: + raise JobFailed("任务执行失败: %s" % pid, + json.dumps(errs, ensure_ascii=False)[:600]) + return outs, rec + raise JobFailed("等待超时(%ds): %s" % (self.timeout, pid)) + + def object_info(self, class_type): + try: + return self._get("/object_info/" + class_type) + except ServerError: + return {} diff --git a/core/errors.py b/core/errors.py new file mode 100644 index 0000000..b86eedd --- /dev/null +++ b/core/errors.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- +"""内核异常。cli_driver 只捕获这些,不吞系统异常。""" + + +class SongForgeError(Exception): + """所有内核错误的基类。""" + + +class ConfigError(SongForgeError): + """配置非法,或试图使用被禁用的模型。""" + + +class RuleViolation(SongForgeError): + """违反生歌铁律。""" + + +class LyricDensityError(RuleViolation): + """歌词字数与目标时长不匹配(死心塌地时长对应关系)。""" + + +class LexiconError(RuleViolation): + """歌词命中身份禁用词。""" + + +class ServerError(SongForgeError): + """ComfyUI 不可达或返回异常。""" + + def __init__(self, msg, detail=""): + super().__init__(msg) + self.detail = detail + + +class JobFailed(SongForgeError): + """任务在 ComfyUI 侧执行失败。""" + + def __init__(self, msg, detail=""): + super().__init__(msg) + self.detail = detail diff --git a/core/paths.py b/core/paths.py new file mode 100644 index 0000000..8dce994 --- /dev/null +++ b/core/paths.py @@ -0,0 +1,119 @@ +# -*- coding: utf-8 -*- +"""路径解析。ComfyUI 根优先配置/环境变量,再自动探测。""" +import datetime +import os + +ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +_MARKERS = ( + os.path.join("models", "checkpoints"), + os.path.join("custom_nodes"), + os.path.join("user", "default"), +) + + +def comfy_root(explicit=None, cfg=None): + """返回 ComfyUI 根目录绝对路径。""" + if explicit: + return os.path.abspath(explicit) + + if cfg: + cr = (cfg.get("paths") or {}).get("comfy_root") + if cr: + return os.path.abspath(cr) + + env = os.environ.get("SONGFORGE_COMFY_ROOT") + if env: + return os.path.abspath(env) + + cur = ROOT + for _ in range(6): + hits = sum(1 for m in _MARKERS if os.path.isdir(os.path.join(cur, m))) + if hits >= 2: + return cur + parent = os.path.dirname(cur) + if parent == cur: + break + cur = parent + + return os.path.dirname(os.path.dirname(os.path.dirname(ROOT))) + + +def model_path(root, *parts): + return os.path.join(root, "models", *parts) + + +def user_default(root): + return os.path.join(root, "user", "default") + + +def output_dir(root, prefix="MyMusic"): + return os.path.join(root, "output", *prefix.split("/")) + + +def music_dir(cfg=None, root=None): + """音频成品目录:paths.music_dir 或 ComfyUI/output/。""" + if cfg: + md = (cfg.get("paths") or {}).get("music_dir") + if md: + return os.path.abspath(md) + root = root or comfy_root(cfg=cfg) + return output_dir(root, cfg["paths"].get("output_prefix", "MyMusic")) + root = root or comfy_root() + return output_dir(root, "MyMusic") + + +def under_workflows(rel, root=None): + if not rel: + raise ValueError("路径为空") + if os.path.isabs(rel): + return os.path.normpath(rel) + base = root if root is not None else ROOT + return os.path.normpath(os.path.join(base, rel)) + + +def ensure_dir(path): + os.makedirs(path, exist_ok=True) + return path + + +def today_iso(): + return datetime.date.today().isoformat() + + +def day_subdir(base, day=None): + """base/YYYY-MM-DD/""" + return os.path.join(base, day or today_iso()) + + +def tmp_today(cfg, day=None): + """paths.tmp_dir/YYYY-MM-DD/,确保存在。""" + base = under_workflows(cfg["paths"]["tmp_dir"]) + return ensure_dir(day_subdir(base, day)) + + +def path_layout(cfg): + """从配置展开本包目录布局(绝对路径)。""" + p = cfg["paths"] + cro = comfy_root(cfg=cfg) + return { + "workflows": ROOT, + "comfy_root": cro, + "runs": under_workflows(p["runs_dir"]), + "tmp": under_workflows(p["tmp_dir"]), + "tmp_today": day_subdir(under_workflows(p["tmp_dir"])), + "lyrics": under_workflows(p["lyrics_dir"]), + "qc_cache": under_workflows(p["qc_cache_dir"]), + "style_cache": under_workflows(p["style_cache_dir"]), + "index": under_workflows(p.get("index_dir") or "songforge/index"), + "catalog": under_workflows( + p.get("catalog_file") or "songforge/index/catalog.jsonl"), + "baseline": under_workflows( + p.get("baseline_dir") or "songforge/config/baseline"), + "audio_out": music_dir(cfg, cro), + "pkg": under_workflows("songforge"), + } + + +# 根目录不应出现的过程文件(doctor WARN) +ROOT_JUNK_NAMES = ("qc.json", "run.log", "manifest.json") diff --git a/core/workflow.py b/core/workflow.py new file mode 100644 index 0000000..776013e --- /dev/null +++ b/core/workflow.py @@ -0,0 +1,208 @@ +# -*- coding: utf-8 -*- +"""songforge 核心包 - 工作流构建器""" + +import copy +import json +import os +import re + +from .errors import ConfigError + +ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +# UI widgets_values 顺序 -> API inputs 键名。按节点类型分别声明,避免混用。 +WIDGET_MAP = { + "CheckpointLoaderSimple": ["ckpt_name"], + "LoraLoader": ["lora_name", "strength_model", "strength_clip"], + "ModelSamplingAuraFlow": ["shift", "sampling"], + "EmptyAceStep1.5LatentAudio": ["seconds", "batch_size"], + "TextEncodeAceStepAudio1.5": [ + "tags", "lyrics", "seed", "bpm", "duration", "timesignature", + "language", "keyscale", "generate_audio_codes", "cfg_scale", + "temperature", "top_p", "top_k", "min_p", + ], + "KSampler": ["seed", "control_after_generate", "steps", "cfg", + "sampler_name", "scheduler", "denoise"], + "SaveAudio": ["filename_prefix"], + "SaveAudioAdvanced": ["filename_prefix", "format"], + "VAEDecodeAudio": [], +} + + +def load_template(path_or_name): + """path_or_name 可以是 workflows 下的文件名,也可以是绝对路径。""" + p = path_or_name + if not os.path.isabs(p): + p = os.path.join(ROOT, p) + if not os.path.exists(p): + raise ConfigError("workflow 模板不存在: %s" % p) + with open(p, encoding="utf-8") as f: + return json.load(f) + + +def convert(ui_wf): + """UI 格式 workflow dict -> API prompt dict。""" + links = ui_wf["links"] + # 兼容两种格式:ComfyUI 对象数组 (dict) 或 元组列表 [id, origin, target, ...] + if links and isinstance(links[0], dict): + link_src = {l["id"]: [l["origin_id"], l["origin_slot"]] for l in links} + else: + link_src = {l[0]: [l[1], l[2]] for l in links} + api = {} + for n in ui_wf["nodes"]: + cls = n["type"] + keys = WIDGET_MAP.get(cls) + inputs = {} + if keys: + wv = n.get("widgets_values", []) + if len(wv) < len(keys): + raise ConfigError( + "节点 %s (%s) 的 widgets_values 只有 %d 项,期望 %d 项;" + "模板可能被手改过" + % (n.get("id"), cls, len(wv), len(keys))) + for k, v in zip(keys, wv): + inputs[k] = v + for inp in n.get("inputs", []): + lid = inp.get("link") + if lid is not None and lid in link_src: + src = link_src[lid] + inputs[inp["name"]] = [str(src[0]), src[1]] + api[str(n["id"])] = {"class_type": cls, "inputs": inputs} + return api + + +def find_node(ui_wf, class_type): + for n in ui_wf["nodes"]: + if n["type"] == class_type: + return n + return None + + +def render(wf_result): + """build_workflow 返回的 dict -> API prompt。""" + if isinstance(wf_result, dict) and "workflow" in wf_result and "class_type" not in wf_result: + return convert(wf_result["workflow"]) + return convert(wf_result) + + +def build_workflow(template, *, title, tags, lyrics, seed, bpm, duration, + keyscale, sampler, encode, model, output_prefix, + output_format, language="zh", timesignature="4"): + """按节点类型定位并写入参数。不依赖固定节点 ID。 + + model["lora"] 为空 / None 时直接跳过 LoraLoader: + 我们用的 ace_step_1.5_turbo_swl_fused.safetensors 本身就是 +""" + from ..config.loader import load + from ..rules.ironlaw import build_tags as _build_tags, build_lyrics, validate + from ..rules.ironlaw import build_chill_lyrics + + # 1. 解析歌词章节 + sections = [] + lines = lyrics.split('\n') + current_tag = None + current_content = [] + for line in lines: + line = line.strip() + if not line: + continue + tag_match = re.match(r'^\[(\w+(?:[-]\w+)?)\]$', line) + if tag_match: + if current_tag is not None and current_content: + sections.append((current_tag, '\n'.join(current_content))) + current_tag = tag_match.group(1) + current_content = [] + else: + if current_tag is not None: + current_content.append(line) + if current_tag is not None and current_content: + sections.append((current_tag, '\n'.join(current_content))) + + has_intro = lyrics.lstrip().lower().startswith('[intro]') + + # 5. 组合工作流 + workflow = copy.deepcopy(template) + + # 从配置文件加载默认参数(single source of truth) + config = load() + model_cfg = config.get("model", {}) + sampler_cfg = config.get("sampler", {}) + encode_cfg = config.get("encode", {}) + paths_cfg = config.get("paths", {}) + + # 合并参数来源: CLI/函数传入 > config > 硬coded 安全兜底 + ckpt_name = model.get("checkpoint") or model_cfg.get("checkpoint", "ace_step_1.5_turbo_swl_fused.safetensors") + lora_name = model.get("lora") or model_cfg.get("lora", "") + lora_model_strength = model.get("lora_model_strength", model_cfg.get("lora_model_strength", 0.5)) + lora_clip_strength = model.get("lora_clip_strength", model_cfg.get("lora_clip_strength", 0.5)) + + shift = sampler.get("shift", sampler_cfg.get("shift", 5.5)) + sampling = sampler.get("sampling", sampler_cfg.get("sampling", "flow")) + steps = sampler.get("steps", sampler_cfg.get("steps", 28)) + cfg = sampler.get("cfg", sampler_cfg.get("cfg", 2.0)) + sampler_name = sampler.get("sampler_name", sampler_cfg.get("sampler_name", "euler")) + scheduler = sampler.get("scheduler", sampler_cfg.get("scheduler", "simple")) + denoise = sampler.get("denoise", sampler_cfg.get("denoise", 1.0)) + batch_size = sampler.get("batch_size", sampler_cfg.get("batch_size", 1)) + + cfg_scale = encode.get("cfg_scale", encode_cfg.get("cfg_scale", 3.0)) + temperature = encode.get("temperature", encode_cfg.get("temperature", 0.68)) + top_p = encode.get("top_p", encode_cfg.get("top_p", 0.88)) + top_k = encode.get("top_k", encode_cfg.get("top_k", 50)) + min_p = encode.get("min_p", encode_cfg.get("min_p", 0.02)) + generate_audio_codes = encode.get("generate_audio_codes", encode_cfg.get("generate_audio_codes", True)) + + output_format = output_format or paths_cfg.get("output_format", "flac") + + # 遍历节点设置参数 + for n in workflow["nodes"]: + if n["type"] == "CheckpointLoaderSimple": + n["widgets_values"] = [ckpt_name] + elif n["type"] == "LoraLoader": + # 设置 LoRA 加载器参数 + if "widgets_values" in n: + n["widgets_values"] = [lora_name, lora_model_strength, lora_clip_strength] + elif n["type"] == "ModelSamplingAuraFlow": + if "widgets_values" in n: + n["widgets_values"] = [shift, sampling] + elif n["type"] == "EmptyAceStep1.5LatentAudio": + if "widgets_values" in n: + n["widgets_values"] = [duration, batch_size] + elif n["type"] == "TextEncodeAceStepAudio1.5": + if "widgets_values" in n: + # 构建 widgets_values: tags, lyrics, seed, bpm, duration, timesignature, + # language, keyscale, generate_audio_codes, cfg_scale, temperature, top_p, top_k, min_p + n["widgets_values"] = [ + tags, + lyrics, + seed, + bpm, + duration, + timesignature, + language, + keyscale, + generate_audio_codes, + cfg_scale, + temperature, + top_p, + top_k, + min_p, + ] + elif n["type"] == "KSampler": + if "widgets_values" in n: + n["widgets_values"] = [seed, "fixed", steps, cfg, + sampler_name, scheduler, denoise] + elif n["type"] == "SaveAudioAdvanced": + if "widgets_values" in n: + n["widgets_values"] = [output_prefix, output_format] + elif n["type"] == "SaveAudio": + if "widgets_values" in n: + n["widgets_values"] = [output_prefix] + + return { + "success": True, + "workflow": workflow, + "lyrics": lyrics, + "tags": tags, + "has_intro": has_intro, + } \ No newline at end of file diff --git a/data/.gitkeep b/data/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/data/runs/.gitkeep b/data/runs/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/data/tmp/.gitkeep b/data/tmp/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/docs/使用手册.md b/docs/使用手册.md new file mode 100644 index 0000000..ed9f0ba --- /dev/null +++ b/docs/使用手册.md @@ -0,0 +1,269 @@ +# songforge 使用手册 + +本手册讲「怎么跑」。参数铁律、踩坑与版本史见上级目录 [`生歌规则.md`](../../生歌规则.md)(唯一规范)。 + +工作目录始终: + +```powershell +cd D:\CodeFiles\Thinking\ComfyUI-MiniMaxH3\user\default\workflows +``` + +入口: + +```powershell +python songforge\songforge.py <命令> [选项] +``` + +音频栈(librosa/torch 等)在 `C:\comfyui-venv\Scripts\python.exe`;主 CLI 用系统 Python,`qc` 会自动起子进程调用 venv。 + +--- + +## 1. 标准流水线 + +```text +doctor → target → 写歌词 → check → gen → qc --baseline +``` + +| 步 | 命令 | 失败时 | +|----|------|--------| +| 体检 | `doctor` | 先起 ComfyUI;缺模型按提示补齐 | +| 查字数 | `target --style pop --bpm 104 --dur 180` | 按打印区间写歌词 | +| 校验 | `check --style … --title … --lyrics-file …` | **只改歌词**,不许改采样参数 | +| 生成 | `gen …` | 看 `data/runs/<日>//run.log`;服务挂了再 `doctor` | +| 验收 | `qc --run gen-… --baseline` | FAIL 修歌/重跑,不放宽阈值 | +| 索引 | `catalog` / `catalog --rebuild` | 查歌名/风格/BPM/QC 结论 | + +`qc` 报告里的 `style_fam` / `style_conf` 是弱参考,**不会**单独导致 FAIL。对外不要把 PASS 说成「风格已验证」——当前风格物理保证主要是实测 BPM 对齐。 + +--- + +## 2. 命令详解 + +### 2.1 doctor + +检查:服务可达、GPU、锁定 checkpoint/LoRA、采样参数、时长区间。 + +```powershell +python songforge\songforge.py doctor +python songforge\songforge.py doctor --server http://127.0.0.1:8188 +``` + +### 2.2 styles + +列出 `config/styles.json` 里可用的 `--style` 键及默认 bpm/key/duration。 + +```powershell +python songforge\songforge.py styles +``` + +### 2.3 target + +按 BPM、时长、密度档算出音节放行区间(硬校验同源公式)。 + +```powershell +python songforge\songforge.py target --style pop --bpm 104 --dur 180 +python songforge\songforge.py target --bpm 92 --dur 165 --profile sparse +``` + +常用:`--style` / `--bpm` / `--dur` / `--profile`(`sparse` = 人声点缀档)。 + +### 2.4 check + +纯本地:铁律词条、前奏注入结果、词典、密度窗口。不连 ComfyUI。 + +```powershell +python songforge\songforge.py check --style pop --title 夜班 --lyrics-file songforge\_archive\lyrics_corpus\某歌.txt +``` + +与 `gen` 共用同一套 `assemble` + `do_validate`,check 过再 gen,避免白跑。 + +### 2.5 gen + +组装 tags/歌词(自动注入 8 小节器乐前奏与铁律)→ 写 workflow → 提交 ComfyUI → 写 manifest。 + +```powershell +python songforge\songforge.py gen ` + --style pop --title 夜班 ` + --lyrics-file path\to\lyrics.txt ` + --seeds 1 +``` + +| 参数 | 说明 | +|------|------| +| `--style` | 风格键,必填 | +| `--title` | 歌名,必填 | +| `--lyrics-file` / `--lyrics` | 二选一 | +| `--bpm` `--key` `--dur` | 覆盖风格默认 | +| `--instruments` `--mood` | 覆盖风格描述 | +| `--seeds` | 多 seed 选优(默认 1) | +| `--seed` | 起始 seed | +| `--dry-run` | 只写 workflow / payload_preview,不提交 | +| `--language` | zh / ja / en | +| `--instrumental` | 纯器乐(密度闸门不适用) | +| `--chill-vocal` | 低音吟唱路径 | +| `--lean-tags` | 精简 tags 路径 | +| `--profile sparse` | 稀疏密度档 | +| `--log-level` / `--quiet` | 日志 | + +产出目录:`songforge\data\runs\YYYY-MM-DD\gen-YYYYMMDD-HHMMSS-mmm\` + +| 文件 | 内容 | +|------|------| +| `run.log` | JSONL 结构化日志 | +| `lyrics.txt` | 最终歌词(含注入前奏) | +| `tags.txt` | 最终 tags(含铁律) | +| `wf_*.json` | 提交用 UI workflow | +| `manifest.json` | seed / prompt_id / 输出路径 / intro_bars 等 | +| `payload_preview.json` | 仅 `--dry-run` | +| `qc.json` | `qc --run` 后写入 | + +音频在 ComfyUI:`output\MyMusic\`(可用 `local.json` 的 `music_dir` 覆盖)。 + +### 2.6 qc + +验收。测量在 `tools/qc_worker.py`,由 venv Python 子进程执行。 + +```powershell +python songforge\songforge.py qc --run gen-20261009-120000-123 --baseline +python songforge\songforge.py qc --run songforge\data\runs\2026-10-09\gen-20261009-120000-123 --baseline +python songforge\songforge.py qc --file D:\path\to\song.flac --dur 165 --baseline +``` + +| 参数 | 说明 | +|------|------| +| `--run` | run_id,或 `data/runs/...` 路径(两者都行) | +| `--file` | 直接验单个 flac | +| `--baseline` | **交付必带**:与 8 首已认可老歌对照声线 | +| `--baseline-only` / `--save-baseline` | 校准/固化软阈值(维护用) | + +本机路径:`config/local.json` 的 `qc_python` / `comfy_root`;或环境变量 `SONGFORGE_QC_PYTHON`、`SONGFORGE_COMFY_ROOT`。 + +### 2.7 runs / show / catalog + +```powershell +python songforge\songforge.py runs +python songforge\songforge.py catalog --day 2026-10-09 +python songforge\songforge.py catalog --rebuild +python songforge\songforge.py show gen-20261009-120000-123 +``` + +### 2.8 selftest + +不连 ComfyUI 生成,跑配置优先级、模型锁定、铁律、密度、回归等。怀疑 CLI 坏了再跑。 + +```powershell +python songforge\songforge.py selftest +``` + +--- + +## 3. 配置与目录 + +### 3.0 目录(项目 vs 数据) + +| 级别 | 用途 | 默认(相对 workflows) | +|------|------|------------------------| +| 项目 | 新歌词 | `songforge/input/lyrics/` | +| 项目 | QC/风格基线 | `songforge/config/baseline/` | +| 本机 | 绝对路径覆盖 | `songforge/config/local.json`(gitignore) | +| 数据 | gen 产物 | `songforge/data/runs/YYYY-MM-DD/` | +| 数据 | 临时日志 | `songforge/data/tmp/YYYY-MM-DD/` | +| 数据 | 全局索引 | `songforge/index/catalog.jsonl` | +| 数据 | QC 缓存 | `songforge/cache/qc/` | +| 外部 | 音频成品 | `/output/MyMusic/` | + +Agent 远程做歌请读 `songforge/AGENTS.md`。 + +### 3.1 该改哪里 + +| 想改 | 文件 | +|------|------| +| 模型 / 采样 / 时长区间 / 相对路径 | `config/default.json` | +| 本机 Comfy 根 / QC Python / 音乐目录 | `config/local.json`(从 example 复制) | +| 风格 bpm/key/乐器/情绪 | `config/styles.json` | +| 禁词 | `config/lexicon.json` | +| QC 软阈值 | `config/baseline/qc_baseline.json`(工具校准,勿手拍) | +| workflow 节点拓扑 | 配置里的模板名(在 workflows 根下) | + +**不要**为改参数去改 `data/runs/` 里某次生成的 JSON——下次 `gen` 会按模板+配置重写。 + +### 3.2 优先级 + +```text +命令行 > SONGFORGE_* 环境变量 > config/local.json > config/default.json > DEFAULTS +``` + +常用环境变量:`SONGFORGE_SERVER`、`SONGFORGE_COMFY_ROOT`、`SONGFORGE_QC_PYTHON`、`SONGFORGE_MUSIC_DIR`、`SONGFORGE_SHIFT`、`SONGFORGE_RUNS`。 + +### 3.3 已锁定默认(勿随意改) + +```json +"model.checkpoint": "ace_step_1.5_turbo_swl_fused.safetensors", +"model.lora": "swl_voice_best_ep257.safetensors", +"sampler": { "shift": 5.5, "steps": 28, "cfg": 2.0 }, +"encode.temperature": 0.68, +"song.duration_range": [150, 200] +``` + +`denied_checkpoints` 含 aio / yue2;doctor 会拦。 + +--- + +## 4. 歌词怎么写 + +1. 先 `target` 拿音节区间,正文填进区间(偏稀 → 模型用拖腔填空)。 +2. **不要**手写 `[intro]` / Instrumental 前奏 / tags——`assemble` 会注入。 +3. 段落用 `[verse]` `[chorus]` 等结构标记即可。 +4. `check` 不过 → 改字数或结构,不要动 shift/steps。 + +密度档: + +- 默认:人声填满(中文说唱/流行) +- `--profile sparse`:音乐主导、人声点缀(勿用默认档硬套) + +--- + +## 5. 日志 + +`log/logger.py`:控制台一行可读 + `runs//run.log` JSONL。 + +字段示例:`ts`、`run_id`、`level`、`msg`,以及 `seed` / `prompt_id` / `title` 等。 + +```powershell +python songforge\songforge.py gen ... --log-level debug +python songforge\songforge.py gen ... --quiet +``` + +排错优先打开对应 `run.log` 和 `manifest.json`,不要只看控制台最后一行。 + +--- + +## 6. 失败速查 + +| 现象 | 处理 | +|------|------| +| doctor 服务不可达 | 启动 ComfyUI(8188) | +| check 密度不匹配 | 改歌词到 target 区间 | +| gen KeyError / 提交失败 | 用 CLI,勿手转 API;跑 `selftest` | +| 改了参数没生效 | 是否改了 workflow JSON?应改 `config/*.json` | +| qc 声线 FAIL 且 voice_nn=null | 漏了 `--baseline` | +| 开头仍是人声 | 看规范第 0 章;重 gen,勿放宽 QC | +| CLI 本身崩溃 | `--dry-run` 拿 workflow 到手跑;或 fallback 旧 `remake_v5.py`(勿删) | + +改规则/配置前先备份: + +```powershell +python songforge\tools\backup.py --note "说明" --from-root D:\CodeFiles\Thinking\ComfyUI-MiniMaxH3 AGENTS.md 生歌规则.md +``` + +--- + +## 7. 与规范文档的分工 + +| 文档 | 职责 | +|------|------| +| `生歌规则.md` | 铁律、模型、采样、密度公式、踩坑、版本 | +| `README.md` | 项目入口、30 秒上手 | +| `docs/使用手册.md` | 命令、目录、配置操作、流水线 | + +行为冲突时以 `生歌规则.md` + `config/default.json` 为准;手册只描述当前 CLI 行为。 diff --git a/index/.gitkeep b/index/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/input/lyrics/.gitkeep b/input/lyrics/.gitkeep new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/input/lyrics/.gitkeep @@ -0,0 +1 @@ + diff --git a/input/lyrics/README.md b/input/lyrics/README.md new file mode 100644 index 0000000..28369cf --- /dev/null +++ b/input/lyrics/README.md @@ -0,0 +1,11 @@ +# 歌词投放目录 + +新歌词 `.txt` 放这里,生成时: + +```powershell +cd D:\CodeFiles\Thinking\ComfyUI-MiniMaxH3\user\default\workflows +python songforge\songforge.py check --style pop --title 歌名 --lyrics-file songforge\input\lyrics\歌名.txt +``` + +历史素材库在 `../_archive/lyrics_corpus/`(只读)。 +不要手写 `[intro]` / tags,CLI 会注入。 diff --git a/input/lyrics/hiphop_楼下那条街.txt b/input/lyrics/hiphop_楼下那条街.txt new file mode 100644 index 0000000..3cf9870 --- /dev/null +++ b/input/lyrics/hiphop_楼下那条街.txt @@ -0,0 +1,66 @@ +[hook] +楼下那条街 +灯比人更早亮 +楼下那条街 +走到天亮也不慌 + +[verse] +早高峰挤成沙丁鱼 +耳机里没有音乐 +新闻又在讲 +谁的账户少了 +我妈说隔壁的娃 +买房了 我还在漂 +我说妈 别急 +我在这儿挺好的 + +[rap] +一二三四 别数了 +五六七八 你困不困 +房租 水电 通勤 +三行数字压着人 +我滑过整个广场 +风从耳边过去 +心里那点堵 散了 + +[hook] +楼下那条街 +灯比人更早亮 +楼下那条街 +走到天亮也不慌 + +[verse] +同事问我打算 +我说我打算先睡 +简历投了三十份 +只回了两个 +不是我不努力 +是城市太拥挤 +有人住在我楼上 +有人挤在我心里 + +[chorus] +楼下那条街 +灯比人更早亮 +楼下那条街 +走到天亮也不慌 +楼下那条街 +总有一盏灯亮着 + +[rap] +如果明天会更好 +那就再睡五分钟 +如果明天不会 +那就现在唱出声 +这条街的人 +等我这一脚 + +[chorus] +楼下那条街 +灯比人更早亮 +楼下那条街 +走到天亮也不慌 + +[outro] +灯还没关 +我还没走 \ No newline at end of file diff --git a/input/lyrics/pop_灯还亮着.txt b/input/lyrics/pop_灯还亮着.txt new file mode 100644 index 0000000..9fce999 --- /dev/null +++ b/input/lyrics/pop_灯还亮着.txt @@ -0,0 +1,65 @@ +[hook] +灯还亮着 灯还亮着 +有人在等你回来 +灯还亮着 灯还亮着 +这一天还算温柔 + +[verse] +六点的闹钟响了 +我躺着刷了三分钟 +城市还在下雨 +伞挤不过人群 +昨天的方案被否了 +机器写得比我快 +我删掉那一段 +留下笨的那版 + +[pre-chorus] +地铁挤成一张纸 +工位挤成一格 +我数着格子往前走 +脚步没有停 + +[chorus] +灯还亮着 灯还亮着 +有人在等你回来 +灯还亮着 灯还亮着 +这一天还算温柔 + +[verse] +房租又涨了一点 +我把窗台的花挪了挪 +外卖三十八块 +吃完就早点睡 +妈妈打来视频 +我说明天一定回 +挂了以后站一会 +才敢关灯睡 + +[pre-chorus] +日子排成一队 +队里没有我 +但我还站着 +就还在往前走 + +[chorus] +灯还亮着 灯还亮着 +有人在等你回来 +灯还亮着 灯还亮着 +这一天还算温柔 + +[bridge] +如果世界一直在催 +如果答案还没来 +那就先把这盏灯 +留给自己等待 + +[chorus] +灯还亮着 灯还亮着 +总有人在等你回来 +灯还亮着 灯还亮着 +我们都不算失败 + +[outro] +灯还亮着 +别急着睡 \ No newline at end of file diff --git a/input/lyrics/punk_去它妈的班.txt b/input/lyrics/punk_去它妈的班.txt new file mode 100644 index 0000000..043115c --- /dev/null +++ b/input/lyrics/punk_去它妈的班.txt @@ -0,0 +1,68 @@ +[verse] +闹钟响了三遍 +我从床上弹起来 +外卖车在楼下等 +雨点砸在水花里 +今天不想开会 +只想快点到广场 +换上那双旧板鞋 +今天要练到天亮 + +[chorus] +去它妈的班 +去它妈的会 +下班才是我的 +开始的时间 +去它妈的班 +去它妈的会 +这一块水泥地 +才是我的位置 + +[verse] +方案又被打回 +说我不接地气 +城市的灯太亮了 +照不全我自己 +房租卡在月底 +工资卡在月初 +中间那段日子 +只能靠一顿饱饭 + +[chorus] +去它妈的班 +去它妈的会 +下班才是我的 +开始的时间 +去它妈的班 +去它妈的会 +这一块水泥地 +才是我的位置 + +[rap] +一二三四 板子落地 +五六七八 汗往下滴 +不管它写什么 +不管谁在看 +一个动作三千遍 +肌肉自己会归位 +他们说我没出息 +我在水泥上走出曲线 + +[chorus] +去它妈的班 +去它妈的会 +下班才是我的 +开始的时间 +去它妈的班 +去它妈的会 +这一块水泥地 +才是我的位置 + +[bridge] +天台的风很大 +我在这儿站一会 +原来不做什么 +也能站很久 + +[outro] +来啊 板子给我 \ No newline at end of file diff --git a/log/__init__.py b/log/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/log/logger.py b/log/logger.py new file mode 100644 index 0000000..5a741a5 --- /dev/null +++ b/log/logger.py @@ -0,0 +1,89 @@ +# -*- coding: utf-8 -*- +"""结构化日志:控制台可读 + JSONL 落盘。run_id 贯穿一次运行的所有记录。 + +每次运行的日志写到 runs//run.log(JSONL),便于出问题时回溯。 +""" +import datetime +import json +import os +import sys + +LEVELS = {"debug": 10, "info": 20, "warn": 30, "error": 40} + + +class Run(object): + """一次运行的上下文:run_id、目录、产物清单。""" + + def __init__(self, run_id, root): + self.run_id = run_id + self.dir = os.path.join(root, run_id) + os.makedirs(self.dir, exist_ok=True) + + def path(self, name): + return os.path.join(self.dir, name) + + def save_json(self, name, obj): + p = self.path(name) + with open(p, "w", encoding="utf-8") as f: + json.dump(obj, f, ensure_ascii=False, indent=1) + return p + + def save_text(self, name, text): + p = self.path(name) + with open(p, "w", encoding="utf-8") as f: + f.write(text) + return p + + +class Logger(object): + def __init__(self, run=None, level="info", quiet=False, stream=None, + log_path=None): + self.run = run + self.level = LEVELS.get(level, 20) + self.quiet = quiet + self.stream = stream or sys.stdout + self._fh = None + path = log_path + if path is None and run: + path = run.path("run.log") + if path: + parent = os.path.dirname(path) + if parent: + os.makedirs(parent, exist_ok=True) + self._fh = open(path, "w", encoding="utf-8") + + def log(self, level, msg, **fields): + if LEVELS.get(level, 20) < self.level: + return + ts = datetime.datetime.now().strftime("%H:%M:%S") + rid = self.run.run_id if self.run else "-" + extra = " ".join("%s=%s" % (k, v) for k, v in fields.items()) + line = "%s [%s] run=%s %s%s" % ( + ts, level.upper(), rid, msg, + (" " + extra) if extra else "") + if level in ("warn", "error"): + print(line, file=self.stream, flush=True) + elif not self.quiet: + print(line, file=self.stream, flush=True) + if self._fh: + rec = dict(ts=datetime.datetime.now().isoformat(timespec="seconds"), + run_id=rid, level=level, msg=msg, **fields) + self._fh.write(json.dumps(rec, ensure_ascii=False) + "\n") + self._fh.flush() + + def debug(self, msg, **f): + self.log("debug", msg, **f) + + def info(self, msg, **f): + self.log("info", msg, **f) + + def warn(self, msg, **f): + self.log("warn", msg, **f) + + def error(self, msg, **f): + self.log("error", msg, **f) + + def close(self): + if self._fh: + self._fh.close() + self._fh = None diff --git a/lyrics/__init__.py b/lyrics/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lyrics/meter.py b/lyrics/meter.py new file mode 100644 index 0000000..5990d83 --- /dev/null +++ b/lyrics/meter.py @@ -0,0 +1,224 @@ +# -*- coding: utf-8 -*- +"""歌词容量与时长对应关系。 + +这是"死心塌地"的部分:不看模型表现,只按音乐结构算出这段时间到底能唱多少字, +填字必须落进区间,否则 check 直接报错,不给侥幸。 + +推导: + 每小节秒数 = 60 / BPM * 每小节拍数 + 总小节 = 时长 / 每小节秒数 + 可唱小节 = (总小节 - 前奏小节) * 可唱比例 + 音节容量 = 可唱小节 * 每小节音节区间 + 填充率 = 实际音节 / 容量上限(仅诊断用,不作为放行闸门) + +阈值来源(重要): + syllable_per_bar_* 与 ideal_line_syllable 不是理论推导,而是用 remake_v5.py 里 + 8 首用户已实际生成并认可的歌反推出来的真实工作区间: + 每可唱小节音节 4.56 ~ 7.64 (均值 6.17) + 每句音节 5.9 ~ 7.4 (均值 6.7) + 改这两个数之前请先跑 selftest 的"实歌回归",否则会误杀已验证可生成的歌词。 + 历史坑:曾按理论值设成"每拍 2.6~4.4 音节、每句下限 7",结果 8 首实歌 + 全部被判歌词过少(实测每拍只有 1.14~1.91 音节)。理论值不能当阈值用。 +""" +import math +import re + +from ..core.errors import LyricDensityError + +SEC_RE = re.compile(r"^\[(intro|instrumental|verse|pre-?chorus|chorus|hook|" + r"rap|bridge|break|breakdown|build|drop|final drop|" + r"post-?drop|outro|final hook|final chorus)\]", + re.I) + + +def lyric_len(text): + cn = sum(1 for c in text if "\u4e00" <= c <= "\u9fff") + en = len(re.findall(r"[A-Za-z][A-Za-z'\-]*", text)) + return cn, en + + + + +def kana_count(text): + """假名音节数:平假名 + 片假名 + 半角浊音。 + + 为什么必须单独计数(中日混合歌词实测踩到的坑): + lyric_len 只认汉字和英文字母,日文假名一个都不数。 + "お誕生日おめでとう 一棠ちゃん" 这行真实可唱约 13 音节, + 原实现只算得到 5 个汉字 + 1 个英文字(2),少算一大截, + 于是 check 把一份写满的正常歌词误判成"歌词过少", + 写词的人只能靠加无意义的字去凑数,越凑越空洞。 + + 排除两类字符,它们不独立占音节: + 1. 小书写假名 ぁぃぅぇぉゃゅょゎ ァィゥェォャュョヮ ヵヶ + 以及促音 っ —— 它们依附在前一个音节上。 + 2. 长音符号 ー ゝゞヽヾ —— 它们延长前一个音,不新增音节。 + """ + small = (u"\u3041\u3043\u3045\u3047\u3049\u3063" + u"\u3083\u3085\u3087\u308e" + u"\u30a1\u30a3\u30a5\u30a7\u30a9\u30c3" + u"\u30e3\u30e5\u30e7\u30ee\u30f5\u30f6" + u"\u30fc\u309d\u309e\u30fd\u30fe") + body = "".join(c for c in text if c not in small) + return len(re.findall(r"[\u3041-\u309F\u30A0-\u30FF\uFF66-\uFF9F]", body)) + + +def syllable(text): + """可唱近似音节数:汉字*1 + 英文词*2 + 假名*1。""" + cn, en = lyric_len(text) + return cn + en * 2 + kana_count(text) + + +def only_lyrics(text): + """剥掉结构标记与 [Instrumental: ...] 描述,只留真正被唱的正文。""" + out = [] + for ln in text.split("\n"): + s = ln.strip() + if not s or s.startswith("["): + continue + out.append(s) + return "\n".join(out) + + +def sections(text): + """按结构标记切段,返回 [(tag, body_text)]。""" + out = [] + tag = "unknown" + buf = [] + for ln in text.split("\n"): + s = ln.strip() + if s.startswith("["): + m = SEC_RE.match(s) + if m: + if buf: + out.append((tag, "\n".join(buf).strip())) + tag = m.group(1).lower() + buf = [] + continue + buf.append(ln) + if buf: + out.append((tag, "\n".join(buf).strip())) + return out + + +def bars(duration, bpm, beats_per_bar=4): + return duration / (60.0 / bpm * beats_per_bar) + + +def pick(cfg_lyrics, profile=None): + """按档取出密度阈值。 + + default 档直接用 lyrics 顶层字段(保持向后兼容); + 其它档从 lyrics.profiles. 覆盖顶层同名字段后返回。 + 这样加档不用改任何计算公式,只加配置。 + """ + if not profile or profile == "default": + return cfg_lyrics + prof = (cfg_lyrics.get("profiles") or {}).get(profile) + if not prof: + raise LyricDensityError( + "未知密度档 '%s',可选:default / %s" + % (profile, ", ".join(sorted((cfg_lyrics.get("profiles") or {}))))) + out = dict(cfg_lyrics) + out.update({k: v for k, v in prof.items() if not k.startswith("_")}) + out["_profile"] = profile + return out + + +def capacity(duration, bpm, cfg_lyrics): + """返回该时长/BPM 下的音节区间与结构量。 + + 区间由"每可唱小节音节数"决定,这是唯一闸门;填充率只做诊断。 + """ + beats = 4 # time_signature 固定 4 拍 + total = bars(duration, bpm, beats) + sing = max(1.0, (total - cfg_lyrics["intro_bars"]) + * cfg_lyrics["sing_bar_ratio"]) + lo = sing * cfg_lyrics["syllable_per_bar_min"] + hi = sing * cfg_lyrics["syllable_per_bar_max"] + return dict(total_bars=total, sing_bars=sing, lo=lo, hi=hi, + beats_per_bar=beats) + + +def window(duration, bpm, cfg_lyrics): + """check 实际放行的音节窗口(整数化)。 + + 对外所有"该写多少字"的表述都必须来自这里,避免文档与代码不一致。 + """ + cap = capacity(duration, bpm, cfg_lyrics) + lo, hi = cap["lo"], cap["hi"] + if hi < lo: + lo = hi = cap["lo"] + return int(math.ceil(lo)), int(math.floor(hi)) + + +def analyse(lyrics, duration, bpm, cfg_lyrics): + body = only_lyrics(lyrics) + syl = syllable(body) + lines = [l for l in body.split("\n") if l.strip()] + cap = capacity(duration, bpm, cfg_lyrics) + wlo, whi = window(duration, bpm, cfg_lyrics) + fill = syl / cap["hi"] if cap["hi"] else 0.0 + per_line = syl / len(lines) if lines else 0.0 + return dict(syllables=syl, lines=len(lines), per_line=round(per_line, 1), + fill=round(fill, 3), total_bars=round(cap["total_bars"], 1), + sing_bars=round(cap["sing_bars"], 1), + lo=int(cap["lo"]), hi=int(cap["hi"]), + win_lo=wlo, win_hi=whi) + + +def target_words(duration, bpm, cfg_lyrics): + """给写词的人用的目标字数区间(死心塌地填字依据)。 + + 返回 (建议字数, 下限, 上限)。下限上限与 check 实际放行窗口完全一致。 + 建议字数取实测均值 6.17 音节/小节对应的位置,不取理论中点, + 因为理论中点会把歌写得比已验证作品明显更稀或明显更密。 + """ + cap = capacity(duration, bpm, cfg_lyrics) + wlo, whi = window(duration, bpm, cfg_lyrics) + mid = cap["sing_bars"] * 6.17 # 实测工作区均值 + mid = max(wlo, min(mid, whi)) + return int(mid), wlo, whi + + +def check(lyrics, duration, bpm, cfg_lyrics): + """死心塌地校验。返回问题列表,空列表代表通过。""" + st = analyse(lyrics, duration, bpm, cfg_lyrics) + errs = [] + if st["syllables"] == 0: + errs.append("正文为空,没有任何可唱歌词") + return errs, st + if st["syllables"] < st["win_lo"]: + errs.append( + "歌词过少:%d 音节(放行下限 %d,还缺约 %d)。" + "%ds / %dBPM 共 %.0f 小节,前奏占 %d 小节,实际可唱 %.0f 小节," + "按实测密度需要 %d~%d 音节。字太少模型会硬拉长音、抢拍、吞字。" + % (st["syllables"], st["win_lo"], st["win_lo"] - st["syllables"], + duration, bpm, st["total_bars"], cfg_lyrics["intro_bars"], + st["sing_bars"], st["win_lo"], st["win_hi"])) + elif st["syllables"] > st["win_hi"]: + errs.append( + "歌词过多:%d 音节(放行上限 %d,超出约 %d)。" + "实际可唱 %.0f 小节,按实测密度上限只到 %d 音节。" + "字太多会加速、丢字、句尾被吃掉。" + % (st["syllables"], st["win_hi"], + st["syllables"] - st["win_hi"], + st["sing_bars"], st["win_hi"])) + if st["per_line"] > cfg_lyrics["line_syllable_max"]: + errs.append("平均每句 %.1f 音节,超过上限 %d,换气点不足,容易抢拍" + % (st["per_line"], cfg_lyrics["line_syllable_max"])) + elif st["per_line"] < cfg_lyrics["ideal_line_syllable"][0]: + errs.append("平均每句 %.1f 音节偏少(实测下限约 %.1f),句子太碎," + "容易出现拖腔填充" + % (st["per_line"], + cfg_lyrics["ideal_line_syllable"][0])) + return errs, st + + +def enforce(lyrics, duration, bpm, cfg_lyrics): + errs, st = check(lyrics, duration, bpm, cfg_lyrics) + if errs: + raise LyricDensityError( + "歌词与时长不匹配(死心塌地时长对应关系):\n - " + + "\n - ".join(errs)) + return st \ No newline at end of file diff --git a/lyrics/probe/03_InstLowChill6.txt b/lyrics/probe/03_InstLowChill6.txt new file mode 100644 index 0000000..334804b --- /dev/null +++ b/lyrics/probe/03_InstLowChill6.txt @@ -0,0 +1,34 @@ +[verse] +ooh ooh +mmm mmm +ooh ooh +low and slow + +[chorus] +ooh +let it go +ooh ooh +hold me tight + +[post-drop] +mmm +under the light +mmm mmm +without a doubt + +[chorus] +ooh ooh +let it go +ooh ooh +hold me tight + +[bridge] +mmm +ooh ooh +mmm +ooh ooh + +[outro] +ooh +mmm +ooh \ No newline at end of file diff --git a/rules/__init__.py b/rules/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/rules/ironlaw.py b/rules/ironlaw.py new file mode 100644 index 0000000..08a13a2 --- /dev/null +++ b/rules/ironlaw.py @@ -0,0 +1,1133 @@ +# -*- coding: utf-8 -*- +"""生歌铁律。全部自动注入,调用方无法遗漏。 + +从 song_rules.py 迁入并扩展。旧模块保留不动(fallback 用),但新流程一律走这里。 +用户永久约束: + "以后所有歌曲都不要开始就是纯人声的设定" + "也不要没有拍子纯人声" +""" + +import os +import sys +import re + +# 确保正确添加工程根目录 +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +from songforge.core.errors import LexiconError, RuleViolation + +TRIGGER = "swlvoice" + +RULE_INTRO = ( + "8 bar instrumental intro hook before any vocals, " + "instrumental opening, no singing in the first 8 bars, " + "vocals enter only after the instrumental hook has played, " + "the intro has a catchy instrumental melody hook, " + "the intro bass line and synth melody are the most memorable catchy element, " + "rhythmic drums from the very first beat, " + "intro is instrumental only, no voice, no lyrics, no humming, " + "no vocal ad-lib in the opening, no wu wu wu, no woo woo, " + "no mm mm, no en en, no ng ng, no mouth sounds, no grunting, " + "the opening features a strong instrumental melody line, " + "synth lead melody plays from the very beginning, " + "instrumental melody carries the intro hook" +) + +# 注意:绝对不要加 "no long empty instrumental"。它与"必须有 8 小节器乐前奏" +# 直接矛盾,模型为消除"空"而把前奏填满人声。这个坑踩过,别加回来。 +RULE_NEG = ( + "no opening vocals, no pure vocal intro, no a cappella opening, " + "no vocal intro, no singing until after the instrumental hook, " + "no spoken intro, no rap intro talk, no intro talking, " + "no humming, no mm mm syllables, no oh oh syllables, " + "no wu wu wu, no woo woo, no ooh ooh, no sustained wu vowel, " + "no en en en, no ng ng ng, no mouth sounds, no grunting, no breathing sounds, " + "no eh ah ah filler, no vocal ad-libs, no droning sustained vowels, " + "no yodeling, no melisma, no crooning, no R&B ballad vocal style, " + "no overly long intro, no excessive drum solo, " + "no slow start, no fade in, " + "no beatless intro, no unaccompanied vocal, no a cappella, " + "no singing chinese instruction words, no vocalizing text labels, " + "no wordless vocalization, no non-verbal vocal sounds, " + "no vocal pad, no vocal texture, no ambient vocal, " + "no background vocal in the opening, no choir in the intro, " + "no vocal harmonies in the intro, no vocal layer in the opening" +) + +RULE_CONSIST = ( + "consistent vocal timbre in every section, no vocal style change, " + "identical vocal character in verse, chorus and rap, " + "no style drift, no breakdown, no key change, no tempo change mid song" +) + +# 整首歌的衬音/即兴人声禁令(不限于前奏)。 +# +# RULE_NEG 已经覆盖了前奏的"no opening vocals / no humming",但它绑在 +# "opening" 语境上,模型在 verse 之间的间奏段仍会插入 ooh / ah / 嗯 / 啊。 +# 这条 RULE_NO_FILLER 单独存在,且中英文双管齐下: +# - 英文:覆盖 ACE-Step 最常见的 filler 词 +# - 中文:覆盖模型在中文歌词间插入的衬词 +# - 段落间:明确要求段落之间只允许器乐,不允许人声填充 +RULE_NO_FILLER = ( + "vocals sing only the exact words written in the lyrics, " + "no other vocal sounds anywhere in the song, " + "all sections without lyrics are instrumental only" +) + +RULE_HARMONY = ( + "no dissonance, no sour notes, no off key singing, " + "no minor second clash, no unbalanced harmony" +) + +RULE_MEMO = ( + "memorable repeating vocal hook, short four syllable hook, " + "strong call and response, dynamic contrast between verse and hook, " + "clear space between rap phrases, crisp clear pronunciation, " + "no straining, no autotune warble, radio ready loudness, " + "choreography friendly, dance floor ready" +) + +RULE_ENERGY = ( + "clear dynamic contrast, full energy intro at the same level as the drop, " + "medium verse, loud hook, " + "build up into every hook, no flat lifeless mix, no monotone energy, " + "no quiet intro, no beatless intro, no sparse opening" +) + +DEFAULT_INSTRUMENT = ("four on the floor kick, crisp snare, tight hi-hats, " + "walking bass, chord stabs") + +# 前奏括号专用的回落值。 +# +# 刻意不直接复用 DEFAULT_INSTRUMENT:它含 "chord stabs",而 "chord stab" +# 就在 _RHYTHM_DROP 里。旧代码回落时会把刚剔除掉的空场信号重新塞回前奏, +# 出现"过滤生效但一回落就前功尽弃"的 bug。 +DEFAULT_INTRO_RHYTHM = ("punchy four on the floor kick, deep sub bass, " + "crisp snare, tight closed hi-hats") + +# 前奏括号里必须出现的两条正向要求(缺一条就补一条)。 +# +# 为什么单独兜底:intro_rhythm_only() 只做"减法"(从风格配器里挑出节奏组), +# 但风格字典本身可能一条贝斯都没写。实测 vocalchill 就是这样: +# huge supersaw lead, sidechain compressed pad, punchy electro kick, +# crisp clap, trap snare, white noise riser, big reverb, melodic drop +# 过滤后只剩 "punchy electro kick, crisp clap, trap snare"——三条全是 +# 中高频,**没有一条能落在 30~120 Hz**。结果两版新音频(GroovePure2 / +# GrooveChill2)开头 sub 比全曲峰值低 53.5 / 31.2 dB,始终过不了验收。 +# +# 所以改成"减法 + 兜底加法":减法负责去掉 pad/riser/reverb/lead, +# 加法保证 kick 和 bass 两条一定写进前奏括号,不依赖风格字典写没写。 +_INTRO_MUST = ( + ("kick", "punchy four on the floor kick, every beat"), + ("bass", "deep sub bass, every beat"), +) + +# 前奏括号里额外要有的"音量电平"要求(减法 + 加法之外的第三层)。 +# +# ⚠ 2026-10-05 重大修正:这条曾经以散文句的形式追加进**歌词括号**,结果被 +# 模型原样唱了出来。实测 Bounce_s8 的 vocals stem ASR: +# t0=0.00 "The opening grooves in base front, ace front and center +# at full level." +# t0=27.00 "No quiet or sparse openin' No fading in, no swelling in" +# 逐词对得上下面这句 _INTRO_LEVEL(旧版),而它根本不在作者手写的 +# Bounce.txt 里。 +# +# 根因:[Instrumental: ...] 括号是**歌词条件通道**,不是 prompt 通道。 +# 里面每一个英文词都可能被当成可唱的歌词。之前把"要求句"写进括号, +# 等于亲手把一段英文散文歌词塞给模型。 +# +# 现在这些要求只留在 tags 通道(RULE_GROOVE_FIRST_BEAT),那里不会被唱。 +# 括号里只允许"配器名词短语"。原始的低频实测记录保留在下面, +# 因为"和 drop 同电平"这个物理要求本身没变,只是换了通道。 +# +# 前两轮把 kick / bass 写进括号后,intro_sub 变成双峰分布—— +# 要么 -35 dB(有鼓),要么 -80 dB(数字静音),中间没有过渡。 +# 27 个 seed 里 22 个落在 -80 dB 那侧。 +# _INTRO_MUST 只回答"有哪些件",不回答"多大声",所以还需要电平要求。 +# 但电平要求属于 tags 的活,不属于歌词括号。 +_INTRO_LEVEL = "" # 保留名字只为向后兼容;不再写进歌词括号 + +# 前奏括号里要出现的"每一拍都在打"量词要求。 +# +# 为什么要单独一层:_INTRO_MUST 只保证 kick / bass 这两个**词**出现过。 +# 风格字典写 "heavy four on the floor kick on beat one" 时词是齐的, +# 兜底不触发,但"on beat one"是弱表述——它只承诺第一拍, +# 其余三拍模型可以合法地填成空拍或 pad。 +# Bounce 的 15 个 seed 全部栽在这:前 4 小节 sub 掉到 -83 dB 数字静音, +# 鼓到第 4.12 小节才进。给了它"空一拍"的合法借口,它就会真的空一拍。 +# +# 判据必须是量词短语(every beat / every single beat)。 +# 不能用 "on beat one"、"beat one" 之类——那正是要修的弱表述本身。 +# +# ⚠ 措辞必须保持"配器名词短语",不能写成人话句子。2026-10-05 实测: +# 旧版写成 "the kick drum and the bass hit on every single beat of every bar +# with no gaps, no rests and no missing beats",被模型当成歌词唱了出来。 +# 括号是歌词通道,句号和从句都会变成可唱的词。现在只保留节奏组名词, +# "每一拍都在打"这个语义交给 tags 侧的 RULE_GROOVE_FIRST_BEAT 表达。 +# +# 必须是**单个逗号分项**且自身含保留词,否则第二次过滤会把它删掉, +# intro_rhythm_only() 就不幂等了。实测踩过: +# "per beat kick and sub bass, four on the floor" +# 里的 "four on the floor" 不含任何 _RHYTHM_KEEP 词(kick/bass/beat…), +# 下一轮整项被剔,追加的量词句凭空消失。 +_INTRO_EVERY_BEAT = "every beat kick and sub bass four on the floor" + +# "每一拍都在打"的量词判据。必须是词边界正则而不是子串查找: +# "every sixteenth"(拍一下的配器描述)不是"每一拍",但子串 "every beat" +# 匹配不上它、而 "every" + "beat" 拆开判断又太啰嗦,所以直接用一条正则 +# 同时覆盖 every beat / every single beat / every sixteenth note 之外的所有 +# "每拍"表述,并显式排除 "on beat one" 这类只承诺第一拍的弱表述。 +_EVERY_BEAT_RE = re.compile( + r"\b(?:every|each|per)\s+(?:single\s+)?(?:one\s+)?beat\b" + r"|\ball\s+(?:four\s+)?beats\b" + r"|\b(?:every|each|per)\s+(?:single\s+)?(?:1|one|quarter)\b", + re.I, +) + +# 前奏括号里允许原样保留的结构性前缀白名单。 +# +# 只允许 "8 bar instrumental intro hook" / "8 bar groove" 这类纯描述长度和 +# 段落的词。一旦前缀里混进配器词(pad / riser / bass / kick...),就必须 +# 连它一起送进 intro_rhythm_only() 过滤,否则空场信号会绕过整条过滤链。 +_INTRO_PREFIX_OK = re.compile( + r"^\d+\s*bar(?:\s+(?:instrumental|intro|introduction|groove|break|hook|" + r"opening|build|section|beat|main|rhythmic))*\.?\s*$", + re.I, +) + + +# === 前奏括号专用的配器过滤(本轮真正起作用的改动)=== +# +# 为什么要过滤:build_inst_lyrics() / build_chill_lyrics() 会把风格字典的 +# 整条 instruments 塞进前奏的 [Instrumental: ...] 括号里。实测(vocalchill) +# 这一串是: +# huge supersaw lead, sidechain compressed pad, punchy electro kick, +# crisp clap, trap snare, white noise riser, big reverb, melodic drop +# 其中"把前奏铺成空场"的词有 4 个:supersaw lead / sidechain pad / +# white noise riser / big reverb;而"要求底鼓贝斯从第 0 拍进"的只有 +# kick / clap / snare 三项。3 : 4 的正面竞争输了,模型就把前奏渲染成 +# ambient pad + riser。 +# +# 实测证据(ab_bands.py,head = 前 16 秒,mid = 16~32 秒,dBFS): +# GroovePure head sub -66.0 / mid sub -55.8 缺底鼓贝斯 +# GrooveChill head sub -53.5 / mid sub -14.7 反而不缺 +# 说明"前奏没鼓"不是标签里少写了鼓,而是前奏括号里 pad/riser 词太多。 +# +# 所以前奏括号只保留节奏组(底鼓/贝斯/鼓组),其余全部剔除。 +# 这些词只在**前奏括号**里剔除,标签侧的 instruments 段保持原样—— +# drop 和 build 段本来就该有 pad、lead、reverb。 +_RHYTHM_KEEP = ( + "kick", "bass", "snare", "clap", "hat", "cymbal", "tom", "percussion", + "808", "sub", "drum", "beat", "claves", "shaker", "tambourine", + "rim", "crash", "ride", "piano", "rhodes", "keys", "organ", + "guitar", "bassline", "bass line", "synth bass", +) + +# 明确剔除:这些词留在前奏括号里会把前奏推向 ambient / 空场。 +# +# 匹配方式:子串匹配会误删正词(实测 2026-10-05): +# "layered crisp snare" 含 "layer" -> 整项被删 +# "clavinet short stabs" 含 "stab" -> 整项被删 +# "clavinet chicken scratch stabs" -> 整项被删 +# 结果 8 首里有 2 首的前奏括号被削到只剩三条中高频件。 +# 所以短词一律走词边界正则,"layered" 不再命中 "layer","stabs" 不再命中 "stab"。 +# 长词(supersaw / reverb / riser...)不存在这种衍生词冲突,保留子串匹配即可。 +_RHYTHM_DROP_SUBSTR = ( + "pad", "riser", "swell", "reverb", "supersaw", "saw", + "ambient", "atmosphere", "texture", "arpeggio", + "chord stab", "brass", "strings", "choir", "vocal", + "voice", "singing", "humming", "whisper", "lyric", "melody", + "counterpoint", "sweep", "noise", +) + +# 需要词边界的短词:只命中独立词,不命中 layered / stabs / arps / leading。 +# +# "sing" 从 _RHYTHM_DROP_SUBSTR 移到这里(2026-10-05)。之前它待在 +# 子串表里,于是两个正当词被整项误删: +# "every single beat" 含 "sing" -> 整项删(而这正是要修的量词句) +# "no missing beats" 含 "sing" -> 整项删 +# "singing" 作为长词留在子串表里继续有效,"single"/"missing" 不再被牵连。 +_RHYTHM_DROP_WORD = ( + "pad", "riser", "swell", "lead", "saw", "arp", "stab", + "layer", "texture", "noise", "sing", +) + +_RHYTHM_DROP_RE = tuple( + re.compile(r"\b%s\b" % re.escape(w), re.I) for w in _RHYTHM_DROP_WORD +) + + +def _is_drop(part_lc): + """判断一个逗号分项是否属于空场信号(应从前奏括号剔除)。""" + if any(bad in part_lc for bad in _RHYTHM_DROP_SUBSTR): + return True + return any(rx.search(part_lc) for rx in _RHYTHM_DROP_RE) + + +def intro_rhythm_only(detail): + """把配器串整理成"以节奏组为主"的前奏描述,用于 [Instrumental: ...] 括号。 + + 两步,缺一不可: + + 1. 减法。按逗号分项整项剔除 pad / riser / reverb / lead / vocal 等 + 空场信号(见 _RHYTHM_DROP)。实测只加禁令无效——GroovePure 已经 + 证过四条 "no ambient intro" 类禁令全部无效,因为前奏括号里 + 正向的 pad/riser 词仍然占多数,模型按多数走。 + + 2. 加法兜底。风格字典可能一条贝斯都没写(vocalchill 就是这样, + 过滤后只剩三条中高频)。所以不论过滤结果如何,kick 和 bass + 两条正向要求一定会写进前奏括号(见 _INTRO_MUST)。 + + 几点设计考虑: + + - 剔除优先级高于保留。像 "white noise riser" 里含 "noise"、 + "big reverb" 含 "reverb",整项丢掉即可。 + - 分项按逗号切,保留原始大小写和词序,不改写用户的配器描述。 + - 结果为空时回落到 DEFAULT_INTRO_RHYTHM,保证前奏括号不会变空—— + 空括号会被模型理解成"这里没有内容",反而更容易出空场。 + - 不做词干匹配,只做子串匹配:配器描述是自然语言,不是词汇表。 + - 中文配器说明直接走回落:中文会被模型唱出来,且无法按英文词过滤。 + """ + parts_out = [] + + if detail and detail.strip() and not re.search(r"[\u4e00-\u9fff]", + detail): + for part in detail.split(","): + p = part.strip() + if not p: + continue + pl = p.lower() + if _is_drop(pl): + continue + if any(good in pl for good in _RHYTHM_KEEP): + parts_out.append(p) + + kept = ", ".join(parts_out) + + # 加法兜底:kick / bass 两条一定要在。 + # 追加项必须自身含 _RHYTHM_KEEP 词,否则本函数自己就不幂等—— + # 第二次调用时这条刚补上的分项匹配不到任何保留词,会被减法整项剔掉。 + low_kept = kept.lower() + missing = [ph for key, ph in _INTRO_MUST if key not in low_kept] + if missing: + kept = (kept + ", " + ", ".join(missing)).strip(", ") if kept \ + else ", ".join(missing) + + # 第三层:把"弱表述的 kick/bass"升级成"每一拍都在打"。 + # + # 坑位:_INTRO_MUST 只在 kick / bass 两个词**完全缺失**时才补。 + # ckfunken 写的是 "heavy four on the floor kick on beat one", + # 词在,兜底不触发,于是前奏括号里留下的是"第一拍有底鼓", + # 其余三拍模型可以自由发挥成空拍。实测这正是 Bounce 前 4 小节 + # 数字静音(sub 掉到 -83dB)的直接来源:它给了模型合法的空拍借口。 + # + # 所以再加一道:只要没出现"每一拍"级别的表述,就显式补一条。 + # 判据用 every beat / every single beat 这类量词短语, + # 不看 on beat one / one —— 那是弱表述,挡不住空拍。 + low_now = kept.lower() + if not _EVERY_BEAT_RE.search(low_now): + kept = kept + ", " + _INTRO_EVERY_BEAT + + # 第四层:电平要求。_INTRO_LEVEL 已清空(它曾被模型当歌词唱出来), + # 但保留 if 是为了让"电平要求留在 tags 通道"这件事在代码里可见, + # 避免以后又有人往括号里加回散文句。 + if _INTRO_LEVEL: + kept = kept + ", " + _INTRO_LEVEL + + return kept or DEFAULT_INTRO_RHYTHM + +# ---- 器乐底轨(分轨用)---- +STEM_INST_LYRICS = ("[intro]\n[inst]\n[inst]\n[verse]\n[inst]\n[inst]\n" + "[chorus]\n[inst]\n[inst]\n[bridge]\n[inst]\n[outro]") +STEM_INST_TAGS = ( + "pure instrumental track, no vocals anywhere, " + "no singing, no voice, no humming, no vocal ad-lib, " + "instrumental only, this is a backing track with no lead vocal, " + "drums bass and chords only, drums enter on the very first beat" +) + + +# 吟唱模式:第三张专辑专用(英文低声吟唱,突出音乐本身)。 +# +# 五轮实测(InstLowChill / 2 / 3 / 4 / 5)收敛出来的结论。 +# 先说结论,再说依据: +# prompt 对"人声/器乐"的控制力真实存在,旋钮是"标签里有多少句话在描述人声": +# 描述多 -> 人声密而响(chill 2:voc/acc 0.483,压过音乐) +# 描述中等 -> 稀疏低音语气词(chill 3:voc/acc 0.376,低于纯器乐版) +# 完全不写 -> 人声归零(chill 4 / 5:ASR 全是持续 pad 幻听) +# +# 六条实测判据(都用 vocals stem,不信完整混音): +# 1. "16 bar 前奏"、"first 20 seconds instrumental"、 +# "voice enters very late"、"in the second half" —— 全部无效, +# 标签侧写时机/时长对人声起点没有任何作用。 +# 2. build_chill_lyrics() 的结构重排只降低人声密度,不推迟起点。 +# 器乐段排在人声段前面,音频里第一句仍然落在 0 秒。 +# 所以下面所有"推迟人声"的注释都已按实测改写,不再声称结构重排有效。 +# 3. vocals stem 的 ASR 必须配"能量对齐 + 歌词重合"两个判据才可信: +# - 能量对齐:真唱段 vocals 轨比全曲中位数高 45~50 dB; +# 幻听段只有 +8~14 dB。 +# - 歌词重合:真唱逐字复现歌词(重合率 0.98); +# 幻听会造出歌词里没有的词(chill 5 幻听成 "oh new healing")。 +# - ASR 覆盖率也是判据:铺满全曲 100% 的就是持续 pad 幻听, +# 真唱只覆盖有人唱的段落(chill 3 为 58%)。 +# 4. chill 5 的"少量人声"经上述判据判定为幻听,实际等同纯器乐。 +# 当前唯一确认真唱的是 chill 3。 +# +# 注意:绝不能出现 "no humming / no mm mm syllables / no oh oh syllables" +# 这三条。它们和"低音唱腔英文语气词"是直接冲突的(第一次改 chill +# 铁律就是为了解开这个冲突),加回来等于自己把要求唱的内容禁掉。 +RULE_NEG_CHILL = ( + "no opening vocals, no pure vocal intro, no a cappella opening, " + "no spoken intro, no rap intro talk, no intro talking, " + "no vocal ad-lib in the opening, no singing in any breakdown, " + "no singing over the first drop, no choir, no vocal sample, " + "no long vocal phrases, " + "no loud vocal belting, no aggressive full-throated singing, " + "no vocalizing text labels, " + "no slow start, no fade in, no beatless intro" +) + +# 吟唱模式补强:音乐是主角,人声是点缀。 +# +# 注意这里绝不能出现 "no long empty instrumental"(RULE_NEG 上面踩过这个坑): +# 那条约束会逼模型用人声/哼鸣去填满每一处空档,结果就是"只有人声没有音乐", +# 正好和这一版要的效果相反。器乐段越长、越完整,才越接近 Avicii 那种写法。 +# +# 这里刻意不写 "voice is sparse and low volume, used only as sparse texture"。 +# InstLowChill3 实测:那一版人声轨 rms 已经降到纯器乐版的同一水平 +# (0.0682 vs 0.0680),人声段从 15 段降到 8 段,但第一句仍然从 0.0 秒唱起。 +# 也就是说"把人声写得更轻"是有效的,"把人声写得更晚"完全无效—— +# 因为只要标签还在反复讨论 voice/vocal,模型就一直把人声当成本轨的主角, +# 写多少个 [breakdown] 都压不下去。人声只能由结构标记决定何时进场, +# 而不能在标签里被反复提及。 +RULE_CHILL_MEMO = ( + "instrumental sections are the main focus, showcase the drop and the hook, " + "music carries the emotion, long instrumental passages, " + "extended instrumental breakdown, drums and bass carry the track, " + "crisp clear English pronunciation, no straining, no autotune warble, " + "radio ready loudness, dance floor ready" +) + +# 吟唱模式保留的"人声"描述。 +# +# 这条是 chill 3 和 chill 4/5 之间唯一的旋钮,位置必须固定: +# chill 3 = 本条 + 密度/时机类描述(voice is sparse、enters late 等) +# -> 稀疏低音语气词,真唱(能量 +45~50 dB,歌词重合 0.98) +# chill 5 = 本条 - 密度/时机类描述 +# -> 人声归零,ASR 全部是持续 pad 幻听(+11 dB,重合 0.00) +# chill 4 = 连本条也砍掉,只留 "male low whisper vocal" +# -> 同样归零 +# 也就是说:删掉"稀疏/很晚/少量"这类密度与时机词,人声立刻消失。 +# +# 本轮(chill 6)新做法:不再声称"只靠本条就能产生稀疏人声"。 +# 改为把"人声确实存在但很轻"写成独立的一句密度声明, +# 和"音乐是主角"放在同一层,用词避开会被禁掉的 timing 类词。 +# 这是从 chill 3 反推出来的最小可用触发词集合。 +RULE_CHILL_VOICE = ( + "male low whisper vocal, hushed and intimate, " + "low register vocal texture, soft breathy phrases, " + "voice is sparse and low volume, used only as sparse texture, " + "sparse English words, mostly instrumental between phrases, " + "no belting, no big singing" +) + + +# 纯器乐模式主导力:这条讲的是"曲子怎么动",不是"人声怎么唱"。 +RULE_CHILL_ENERGY = ( + "clear dynamic contrast, quiet breakdown, huge powerful drop, " + "energetic build into every drop, wide stereo image, big reverb, " + "sidechain compression, no flat lifeless mix, no monotone energy" +) + +# 器乐模式下换掉 RULE_ENERGY/RULE_HARMONY:原来那两条在说"verse 轻 hook 响" +# "不要跑调",那是给人声的建议,纯器乐里只会把模型往人声上推。 +RULE_CHILL_HARMONY = ( + "unified minor tonality, no sour notes, no harsh dissonant clusters, " + "use octaves fifths and fourths, no atonal noise" +) + +# chill 分支原本直接复用 RULE_CONSIST,而它写着 "no breakdown"。 +# RULE_CHILL_ENERGY 明确要求 quiet breakdown / extended breakdown, +# 两条同时出现就是自相矛盾,模型会挑一条执行——挑错就变回人声填充。 +# 这里保留"声线/调性/速度不漂移"的稳定性要求,只把 breakdown 放出来。 +# +# 同样去掉 "consistent vocal timbre / no vocal style change": +# 这两条每轮都在提醒模型"注意人声",而实测正是"标签里越强调人声, +# 人声越从 0 秒就进"。改成只约束乐器与调性。 +RULE_CHILL_CONSIST = ( + "consistent instrumentation in every section, " + "consistent tuning and timbre for all instruments, " + "no style drift, no key change, no tempo change mid song" +) + + +# === 从第一拍就必须是完整 groove === +# +# 先纠正一个错误结论(ab_bands.py / ab_head_rhythm.py 实测推翻): +# 上一轮 ab_lowfreq.py 报的"前 15 秒是空场、只有 -70 dBFS"是**错的**。 +# 多频段实测(前 16 秒 vs 16~32 秒,单位 dBFS): +# +# 轨 head sub/low mid sub/low head full mid full +# GroovePure -66.0/-69.5 -55.8/-59.4 -32.7 -25.0 +# InstPureEn -64.6/-67.0 -17.5/-17.8 -31.1 -13.4 +# GrooveChill -53.5/-44.5 -14.7/-18.3 -25.3 -11.9 +# +# 结论完全反过来:前奏**不是空场**。GroovePure 开头有音乐, +# full band -32.7 dBFS(只比中段低 7.7 dB),hat/snare 瞬态也齐备, +# 只是 sub/low 比中段低 10~50 dB —— 缺的是底鼓和贝斯,不是"整个音乐"。 +# +# 真正的根因在**歌词体**,不在标签: +# build_inst_lyrics() / build_chill_lyrics() 把风格字典的整条 instruments +# 原样塞进前奏括号: +# [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, ...] +# 这串词里会把前奏铺成空场的信号有 4 个 +# (supersaw lead / sidechain pad / white noise riser / big reverb), +# 而要求底鼓贝斯从第 0 拍进的只有 kick / clap / snare 三项, +# 3 : 4 正面竞争输了,模型就把前奏铺成 ambient + riser。 +# +# 修法分两层: +# 1) 前奏括号只保留节奏组(底鼓/贝斯/鼓组),剔除 lead/pad/riser/reverb +# —— 见 intro_rhythm_only(),这是本轮真正起作用的改动; +# 2) 标签侧保留一句简短的节奏组正向要求(下面这条)。 +# +# 不要试图靠堆更多禁令解决:已经实测过 "no ambient intro / no pad only +# intro / no beatless intro / no white noise riser in the opening" 四条, +# GroovePure 证明无效(开头 sub 仍只有 -66 dBFS)。因为禁令只压"不要什么", +# 前奏括号里正向的 pad/riser 词仍占多数,模型按多数走。 +RULE_GROOVE_FIRST_BEAT = ( + "full four on the floor groove from the very first beat, " + "kick drum, sub bass and walking bass on bar one, " + "drums and bass play from the first beat at the same level as the drop, " + "the rhythm section carries the opening, " + "catchy synth lead melody plays from the first bar, " + "instrumental melody hook opens the track, " + "the intro has a strong melodic hook played by synths, " + "no pad only intro, no beatless intro, no riser in the opening, " + "no ambient intro, no vocal pad intro, no vocal texture intro" +) + + +# 纯器乐模式:整首无人声,只测 prompt 对乐器的控制力。 +# +# 为什么必须单独一套:chill 模式仍然有大量唱法描述(voice is sparse、 +# vocal hook 等),模型没有理由把人声彻底拿掉。要真正验证"只剩音乐", +# 标签里必须把所有跟人声沾边的词都清干净,并正面要求器乐段完整展开。 +RULE_INST_MEMO = ( + "instrumental sections are the main focus, showcase the drop and the hook, " + "music carries all of the emotion, no voice at all, " + "long instrumental passages, extended instrumental breakdown, " + "drums and bass carry the track, " + "memorable repeating instrumental hook, strong call and response between " + "the synth lead and the bass line, dynamic contrast between breakdown and drop, " + "radio ready loudness, dance floor ready" +) + +RULE_INST_NEG = ( + "no vocals anywhere, no singing, no voice, no lead vocal, no backing vocal, " + "no humming, no mm mm syllables, no oh oh syllables, no vocal ad-lib, " + "no whisper, no spoken word, no rap, no choir, no vocal sample, " + "no a cappella, instrumental only, this is a backing track, " + "drums bass synths chords and lead melodies only, " + "no opening vocals, no pure vocal intro, no vocal intro, " + "no slow start, no fade in, no beatless intro, no empty beatless intro, " + "drums enter on the very first beat" +) + +RULE_INST_CONSIST = ( + "consistent instrumentation in every section, no style drift, " + "consistent tuning and timbre for all instruments, " + "no key change, no tempo change mid song" +) + +# chill 专用前奏:RULE_INTRO 只说"前奏 8 小节后人声才进",实测不够。 +# 第一版 InstLowChill 用的就是 RULE_INTRO,vocals stem 从 0.0 秒就有能量。 +# +# 后面几版把这里加长到 16 小节、补"前 20 秒纯器乐"、补"人声只在中段", +# 实测 vocals stem 的真实人声起点依旧没有推迟。结论有两条: +# 1. 标签侧写多长前奏、写什么时机,对人声起点都没有作用; +# 2. 唯一能把人声起点往后推的旋钮是"标签里密度/时机类人声描述的多少", +# 而它一旦降到 chill 5 那个水平,人声就整体消失(并不是延后)。 +# +# 所以这里只保留纯音乐描述,不提"什么时候进人声"。 +# 段落时序交给歌词体结构标记,见 build_chill_lyrics()。 +RULE_CHILL_INTRO = ( + "8 bar instrumental intro hook, " + "instrumental opening only, " + "the intro bass line and the synth hook are the most memorable catchy element, " + "rhythmic drums from the very first beat, " + "intro is instrumental only, no lyrics, " + "long instrumental passages before any entry, " + "instrumental breakdown and instrumental drop sections" +) + +# 纯器乐专用前奏:RULE_INTRO 里有一句 "vocals enter only after the +# instrumental hook has played",那是给"先器乐后人声"的有声曲用的。 +# 纯器乐里保留它等于直接命令模型"稍后把人声加回来",所以必须换掉。 +RULE_INST_INTRO = ( + "8 bar instrumental intro hook, " + "instrumental opening only, " + "the intro bass line and the synth hook are the most memorable catchy element, " + "rhythmic drums from the very first beat, " + "no singing in any bar, no voice, no lyrics, no humming, " + "no vocal ad-lib in the opening" +) + + +RULE_LEAN_INTRO = ( + "8 bar instrumental intro hook before any vocals, " + "instrumental opening, " + "rhythmic drums from the very first beat, " + "bouncy kick and claps from beat one, " + "the intro hook is the most memorable catchy element" +) + +# 精简模式:只留正向描述,整段 RULE_NEG 不注入。 +# +# 为什么需要:用户要的是"开头不是纯人声",而不是"tags 里塞满 no vocals"。 +# 已成功的 dy10_心动预警(kick_ratio 0.637,全批唯一)实测 13 子句、0 条否定词, +# 鼓组照样从第一拍就在。而默认 vocal 路径会注入 RULE_NEG 的 20+ 条人声否定句, +# 标签膨胀到 90 子句 / 2200 字符。对照实验显示标签长度与开头人声响度正相关 +# (r=+0.513),且纯器乐版 20 条"无人声"否定句反而伴随最强 vocals stem。 +# 所以这里用"正向说清楚要什么"替代"反复说不要什么"。 +RULE_LEAN_MEMO = ( + "memorable repeating vocal hook, catchy singable chorus, " + "strong call and response, dynamic contrast between verse and chorus, " + "crisp clear pronunciation, radio ready loudness" +) + +RULE_LEAN_ENERGY = ( + "clear dynamic contrast, medium verse, loud chorus, " + "build up into every hook, no flat lifeless mix" +) + + +def build_tags(style, instruments="", mood="", extra="", trigger=TRIGGER, + chill_vocal=False, instrumental=False, lean=False): + """chill_vocal=True 时输出吟唱标签:人声退到点缀位。 + + 和 instrumental 分支一样,这里完全不接风格字典的 extra。 + vocalchill 的 extra 有六句、其中五句在谈人声("male low whisper vocal, + sparse English words, mostly instrumental between phrases, no belting, + no big singing"),实测正是"标签里越强调人声,人声越从 0 秒就进"。 + chill 只用 RULE_CHILL_VOICE 这一条音色线索,其余全部交给歌词体结构标记。 + + instrumental=True 时输出纯器乐标签:清掉所有与人声相关的措辞, + 正向要求器乐段落完整展开,用于验证 prompt 对乐器的控制力。 + + lean=True 时走精简正向模式:保留"前奏器乐 + 第一拍鼓"的用户硬约束, + 但不注入 RULE_NEG 那 20+ 条人声否定句。给标签短、靠音色词自然带鼓的 + 甜系 pop 这类风格用。 + """ + if instrumental: + # 风格字典的 extra 写的是唱法("male low whisper vocal" 之类), + # 纯器乐下它会跟 RULE_INST_NEG 的 "no vocals anywhere" 当场对撞。 + # 所以这里不接 extra,只接风格名/配器/情绪三段。 + parts = [trigger, style.strip(), instruments.strip(), mood.strip(), + RULE_INST_MEMO, RULE_INST_INTRO, RULE_GROOVE_FIRST_BEAT, + RULE_CHILL_HARMONY, + RULE_CHILL_ENERGY, RULE_INST_CONSIST, RULE_INST_NEG, + RULE_NO_FILLER] + return ", ".join(p for p in parts if p) + if chill_vocal: + parts = [trigger, style.strip(), instruments.strip(), mood.strip(), + RULE_CHILL_MEMO, RULE_CHILL_INTRO, RULE_GROOVE_FIRST_BEAT, + RULE_CHILL_HARMONY, + RULE_CHILL_ENERGY, RULE_CHILL_CONSIST, + RULE_CHILL_VOICE, RULE_NEG_CHILL, RULE_NO_FILLER] + return ", ".join(p for p in parts if p) + parts = [trigger, style.strip(), instruments.strip(), mood.strip(), + RULE_MEMO, RULE_INTRO, RULE_HARMONY, RULE_ENERGY, RULE_CONSIST, + RULE_GROOVE_FIRST_BEAT, + extra.strip(), RULE_NEG, RULE_NO_FILLER] + if not lean: + # 默认分支同样必须挂 RULE_GROOVE_FIRST_BEAT(2026-10-05 补)。 + # 之前只有 lean / chill / instrumental 三条分支挂了这条规则, + # 默认分支漏挂,于是 tags 里唯一一句写明"鼓和贝斯从第 0 拍进、 + # 且和 drop 同电平"的话根本不存在——本批 8 首全是 lean 模式, + # 所以这个洞一直没被发现;任何非 lean 调用都会踩到。 + return ", ".join(p for p in parts if p) + # lean:同样的硬约束,但用正向措辞,且不注入 RULE_NEG。 + # + # RULE_NEG 被跳过是因为它和"前奏不要人声"的否定句膨胀有关(标签越长 + # 开头人声越响,r=+0.513)。但 RULE_NO_FILLER 管的是**整首歌段落之间 + # 的衬音/和声/ad-libs**,跟前奏人声起点无关。lean 跳过它纯属附带损伤—— + # 实测每首歌句首句尾都出现 嗯嗯/啊啊/oh oh/hey/yeah 等衬词,用户 + # 反馈"一听就划走"。所以从 2026-10-08 起把 RULE_NO_FILLER 加回 lean。 + parts = [trigger, style.strip(), instruments.strip(), mood.strip(), + RULE_LEAN_MEMO, RULE_LEAN_INTRO, RULE_GROOVE_FIRST_BEAT, + RULE_LEAN_ENERGY, RULE_HARMONY, RULE_CONSIST, + extra.strip(), RULE_NO_FILLER] + return ", ".join(p for p in parts if p) + + +def build_inst_lyrics(intro_detail=None, bars=8): + """纯器乐歌词体:只有结构标记,没有任何可唱正文。 + + 结构段名刻意避开 [verse] / [chorus]。ACE-Step 1.5 见到这两个标记 + 就会按"有人唱的段落"来铺内容,即使下面只挂 [inst],也容易把人声 + 请回来。纯器乐用 intro / breakdown / build / drop 这些纯器乐段名, + 模型只会把对应位置铺成器乐。 + + intro_detail 会先过 intro_rhythm_only():前奏括号里只留节奏组, + 否则 pad/riser/lead 词会把前奏铺成 ambient 空场(实测 GroovePure + 开头 sub 只有 -66 dBFS,中段是 -55.8 dBFS)。 + """ + d = intro_rhythm_only(intro_detail) + # 括号里只写配器名词短语。"from the very first beat" 这类介词短语 + # 会被 _INTRO_CLAUSE_RE 判为可唱散文——它确实该写进 tags + # (RULE_GROOVE_FIRST_BEAT),不该出现在歌词条件通道里。 + head = ("[Instrumental: %d bar instrumental intro hook, %s, " + "four on the floor groove, beat one kick and bass, " + "no vocals, no voice, no singing]" % (bars, d)) + body = [ + "[intro]", + head, + "[instrumental]", + "", + "[breakdown]", + "[inst]", + "", + "[build]", + "[inst]", + "", + "[drop]", + "[inst]", + "", + "[breakdown]", + "[inst]", + "", + "[build]", + "[inst]", + "", + "[final drop]", + "[inst]", + "", + "[post-drop]", + "[inst]", + "", + "[breakdown]", + "[inst]", + "", + "[final drop]", + "[inst]", + "", + "[outro]", + "[inst]", + ] + return "\n".join(body) + "\n" + + +def build_intro(detail=None, bars=8): + """器乐前奏描述。必须纯英文,中文说明词会被模型唱出来。""" + d = detail or DEFAULT_INSTRUMENT + return ("[Instrumental: %d bar instrumental hook, %s, " + "drums enter on the first beat, no vocals, no humming]" % (bars, d)) + + +def build_lyrics(sections, intro_detail=None, bars=8, tail=None): + """sections 为 [(tag, text)],首段强制为器乐前奏。 + + intro_detail 若是中文会被自动降级为默认英文配器。 + """ + if intro_detail and re.search(r"[\u4e00-\u9fff]", intro_detail): + intro_detail = None + out = ["[intro]", build_intro(intro_detail, bars), "[instrumental]", ""] + for tag, text in sections: + out.append(tag) + out.append(text.strip()) + out.append("") + if tail: + out.append(tail.strip()) + return "\n".join(out) + "\n" + + +def strip_cn_instrumental(lines): + """清掉正文里 [Instrumental: ...] 的中文说明,防止被唱出来。""" + out = [] + for ln in lines: + s = ln.strip() + m = re.match(r"^\[Instrumental:[^\]]*\]$", s, re.I) + if m and re.search(r"[\u4e00-\u9fff]", s): + out.append("[Instrumental: no vocals, drums and bass only]") + continue + out.append(ln) + return out + + +# chill 结构体里循环使用的纯器乐段模板。 +# 只用 intro / breakdown / build / drop / post-drop 这些纯器乐段名, +# 不出现 [verse] / [chorus],避免模型提前把"有人唱的段落"排到开头。 +_CHILL_INST_CYCLE = [ + ("[breakdown]", "[inst]"), + ("[build]", "[inst]"), + ("[drop]", "[inst]"), + ("[post-drop]", "[inst]"), +] + + +def build_chill_lyrics(sections, intro_detail=None, bars=8, total_bars=90): + """chill 吟唱歌词体:把器乐段整体排在人声段之前。 + + 实测效果(必须照实说,不能夸大): + 这个重排确实把 人声密度 压下来了(chill 3 的 voc/acc 0.376, + 比纯器乐版的 0.389 还低),但它并没有把 人声起点 往后推—— + chill 3 的第一句真唱仍然落在 0.0 秒。 + 所以它是"密度工具",不是"推迟工具"。 + + 为什么不能靠标签解决: + ACE-Step 按歌词体的结构标记顺序分配段落时间,不按时长。 + InstLowChill2 的标签已经写了 16 小节前奏 + "前 20 秒纯器乐" + + "人声不进第二次 drop",vocals stem 仍然从 0.0 秒唱起。 + + total_bars 来自 meter.capacity(),用它算出器乐段要占多少小节, + 让"器乐占大部分时长"这个听感成立,而不是随手写三段 [inst]。 + """ + d = intro_rhythm_only(intro_detail) + out = ["[intro]", + ("[Instrumental: %d bar instrumental intro hook, %s, " + "four on the floor groove, beat one kick and bass, " + "no vocals, no voice, no singing]" % (bars, d)), + "[instrumental]", ""] + + # 先铺满器乐段。留 1/4 给结尾的器乐收尾,其余全放前面。 + inst_bars = max(4.0, float(total_bars) - float(bars)) + tail_bars = max(2.0, inst_bars * 0.25) + head_bars = inst_bars - tail_bars + unit = max(1.0, float(bars)) * 2.0 + cycles = max(2, int(round(head_bars / unit))) + + for i in range(cycles): + tag, mark = _CHILL_INST_CYCLE[i % len(_CHILL_INST_CYCLE)] + out += [tag, mark, ""] + + for tag, text in sections: + out += [tag, text.strip(), ""] + + tail = max(1, int(round(tail_bars / float(bars)))) + for i in range(tail): + tag, mark = _CHILL_INST_CYCLE[(i + 2) % len(_CHILL_INST_CYCLE)] + out += [tag, mark, ""] + out += ["[outro]", "[inst]"] + return "\n".join(out) + "\n" + + +def filter_intro_bracket(lyrics): + """把已有 [intro] 里的 [Instrumental: ...] 括号过一遍 rhythm 过滤。 + + 为什么手写歌词也要过滤(2026-10-05): + assemble() 原来分两条路。build_lyrics() / build_chill_lyrics() / + build_inst_lyrics() 这三条自己拼前奏,都调用了 intro_rhythm_only(); + 但"歌词文件已经自带 [intro] 头"这条分支只调 force_intro_bars() + 改了个小节数,括号内容原样透传。 + 于是作者手写的前奏描述成为唯一一条绕过过滤的路径——而 + intro_rhythm_only() 的整段注释(pad/riser 词数 3:4 压过 kick/snare + 3 项,模型按多数走)说的正是这条路径会出的问题。实测 ckfunk + 批次 12/16 个 seed 前奏 sub 掉到 -80 dB 以下。 + + 只处理 [intro] 段里的第一行括号,段落正文一个字都不动: + 正文是要唱的,过滤词表(lead/melody/vocal 等)是为配器串设计的, + 拿去过滤歌词会把 "I lead me to the dance floor" 这类正常句子改坏。 + + 括号被过滤到全空时不能空手进模型:空括号等于"这里没内容",模型更容易 + 直接铺成空场。此时退回 DEFAULT_INTRO_RHYTHM。 + 作者写的前奏描述优先,不拿风格字典去覆盖它——作者可能刻意写了 + "808 sub slide into the hook" 这种会被整项丢掉的词。 + """ + lines = lyrics.split("\n") + out = [] + in_intro = False + + def _emit(text, src): + """按原行尾回写,避免 CRLF 文件被改写成混合行尾。 + + split("\\n") 之后 CRLF 的 "\\r" 仍挂在行尾。直接拼新串会把它丢掉, + 而其余行还带着 "\\r",join 之后同一文件里就有两种行尾了。 + """ + if src.endswith("\r") and not text.endswith("\r"): + return text + "\r" + return text + + for ln in lines: + s = ln.strip() + if re.match(r"^\[intro\]$", s, re.I): + in_intro = True + out.append(ln) + continue + # 顺序要紧:必须先判 [Instrumental: ...],再判"段落结束"。 + # 反过来的话它自己就以 "[" 开头,会被当成 intro 收尾, + # 过滤分支永远进不去(2026-10-05 修的就是这个)。 + if in_intro and re.match(r"^\[instrumental:", s, re.I): + detail = re.sub(r"^\[Instrumental:\s*", "", s, flags=re.I) + detail = re.sub(r"\s*\]$", "", detail) + # 中文括号整段放行。 + # + # intro_rhythm_only() 的分项词表全是英文,中文项匹配不到任何 + # KEEP / DROP,只会走兜底把整段替换成 DEFAULT_INTRO_RHYTHM—— + # 也就是把作者写的中文前奏描述整句丢掉换成英文。作者写中文说明 + # 他清楚自己要什么,改写他反而破坏意图。前奏低频现在主要由标签 + # 侧的 RULE_GROOVE_FIRST_BEAT 兜住,不依赖这里过滤中文。 + if re.search(r"[\u4e00-\u9fff]", detail): + out.append(ln) + in_intro = False + continue + # 保留 "8 bar instrumental intro hook" 这类纯结构性前缀, + # 只对逗号后面的配器串做过滤。 + # + # 这里必须先验前缀"干净"再决定保留,不能只看有没有逗号。 + # 正则里 (?:,\s*(.*))? 是可选组:写 + # [Instrumental: 8 bar intro hook with ambient pad] + # 这种没有逗号的句子时,lazy 的 .*? 会回溯到把整串吃进前缀, + # rest 为空 → 过滤完全不执行,pad 原样留在前奏里。 + # 所以前缀只接受不含任何配器词、只由 bar/instrumental/intro/ + # hook/groove/break 这类结构词组成的形态,否则整串送过滤。 + prefix, rest = "", detail + mpre = re.match( + r"^(\d+\s*bar\b[^,]*?)\s*(?:,\s*(.*))?$", detail, re.I | re.S + ) + if mpre and _INTRO_PREFIX_OK.match(mpre.group(1).strip()): + prefix, rest = mpre.group(1).strip(), mpre.group(2) or "" + newd = prefix + if rest.strip(): + filtered = intro_rhythm_only(rest) + if filtered: + newd = ("%s, %s" % (prefix, filtered)) if prefix \ + else filtered + elif not prefix: + newd = DEFAULT_INTRO_RHYTHM + out.append(_emit("[Instrumental: %s]" % newd, ln)) + in_intro = False + continue + # intro 段结束:遇到下一个结构标记就收手 + if in_intro and s.startswith("["): + in_intro = False + out.append(ln) + return "\n".join(out) + + +def force_intro_bars(lyrics, bars): + """把已有 [intro] 头里的前奏小节数改写为 bars。 + + 标签和歌词头必须写同一个数字。之前 chill 标签改成 16 小节、歌词头还是 + 8 小节,模型同时收到两个互相矛盾的指令,前奏长度完全随机。 + 这里只改数字,不动描述内容。 + """ + def _repl(_m): + return "%d bar" % bars + + out = [] + for i, ln in enumerate(lyrics.split("\n")): + if i >= 12: + out.append(ln) + continue + s = ln.strip() + if s.lower().startswith("[instrumental:") and \ + re.search(r"\d+\s*bar", s, re.I): + out.append(re.sub(r"(\d+)\s*bar", _repl, s, count=1, flags=re.I)) + else: + out.append(ln) + return "\n".join(out) + + +# 前奏括号里只允许出现"配器名词短语"的机器判据。 +# +# 2026-10-05 实测踩坑(两次): +# 1. _INTRO_LEVEL 写成散文句 -> Bounce_s8 把 +# "The opening grooves in base front, ace front and center at full +# level." 和 "No quiet or sparse openin' No fading in, no swelling in" +# 原样唱了出来。逐词对得上,且不在作者手写的 Bounce.txt 里。 +# 2. _INTRO_EVERY_BEAT 写成 24 词的长句 -> 同样全是可唱的英文。 +# +# 括号是**歌词条件通道**,不是 prompt 通道。判断标准因此不是"语义是否 +# 合理",而是"这是不是一个会被唱出来的句子"。 +# 配器名词短语 punchy kick, deep sub bass, tight closed hi-hats +# 要求散文 the beat drops hard right at the start +# 前者没有子句,后者有。 +# +# 命中集合刻意只收"配器描述里绝对不会出现"的成分:系动词、否定副词、 +# 义务/能力情态、代词、从属连词、介词短语引导词。 +# +# 下面这些**不能**收,否则会误伤本批真实括号(全部为实测样例): +# and "tambourine and hand claps" +# with "dry crisp snare with ghost notes" +# on "tight closed hi-hats accenting the offbeats" +# every "dry crisp snare with ghost notes on every sixteenth"(拍一下) +# no "no vocals, no voice, no singing"(build_inst_lyrics 硬编码) +# bar "kick drum and bass on bar one" +# 前四个是配器描述的正常连接词/副词;后两个虽然也是散文,但它们表达的是 +# "这里没有人声",属于结构标记而非会被当内容唱的句子,且已由 tags 侧 +# RULE_INTRO / RULE_NEG 覆盖,歌词括号里重复写没有额外收益。 +_INTRO_CLAUSE_RE = re.compile( + r"\b(?:is|are|was|were|am|be|been|being|" + r"not|never|none|nothing|neither|nor|" + r"must|should|will|would|can|could|may|might|" + r"it|its|they|them|their|you|your|our|we|us|his|her|she|him|" + r"that|which|who|whose|because|than|then|when|while|where|why|how|" + r"if|though|although|so|but|" + r"from|into|onto|upon|through|across|around|during|" + r"before|after|until|unless|whether)\b", + re.I, +) + +# 已经实证被模型唱出来的具体措辞(2026-10-05,Bounce_s8)。 +# +# 为什么单独列:上面那套子句成分判据是"结构信号",而泄漏句里最狠的几条 +# 恰恰不含任何子句成分——它们是一串并列的动名词/介词短语: +# the opening groove is loud and punchy from bar one ... <- 含 is/from,被抓 +# no fading in, no swelling in <- 不含任何子句词 +# 只靠 _INTRO_CLAUSE_RE 放行了第二句。所以把真实泄漏过的措辞原样列出来, +# 命中即报错。这不是猜测,是从 ASR 输出里逐词抄回来的。 +_INTRO_LEAK_PHRASE_RE = re.compile( + r"fad(?:ing|e)\s+in|" + r"swelling\s+in|" + r"swell(?:ing|s)\s+up|" + r"quiet\s+or\s+sparse|" + r"sparse\s+open(?:ing|ing')|" + r"front\s+and\s+center|" + r"at\s+full\s+level|" + r"louder\s+than\s+the\s+vocals?|" + r"no\s+empty\s+beat|" + r"no\s+gaps?,?\s+no\s+rests?|" + r"beats?\s+up\s+from|" + r"drop(?:s|ped)?\s+hard", + re.I, +) + + +def _check_intro_bracket_speakable(head): + """前奏括号里不许出现会被唱出来的散文句。 + + 为什么必须机器判(不靠 code review 兜住):括号内容直接进模型的 + 歌词条件通道,已经两次因为往里塞长句而把生成指令唱进了成品。 + 第一次靠注释提醒,第二次照样漏——所以把规则写成检查。 + + 两套判据,取并集: + 1. _INTRO_CLAUSE_RE —— 结构性信号(系动词 / 代词 / 介词短语…) + 2. _INTRO_LEAK_PHRASE_RE —— 实证泄漏过的具体措辞 + 只用第 1 套会漏掉 "no fading in, no swelling in" 这类纯并列短语, + 只用第 2 套则拦不住新写的散文句。两套互补,缺一不可。 + + 白名单前缀 "8 bar instrumental intro hook" 只含 bar / instrumental / + intro / hook,不含任何子句信号,不会被误判。 + """ + errs = [] + m = re.search(r"\[instrumental:(.*?)\]", head, re.I | re.S) + if not m: + return errs + body = m.group(1) + hits = _INTRO_CLAUSE_RE.findall(body) + if hits: + uniq = sorted(set(h.lower() for h in hits)) + errs.append( + "前奏括号含会被唱出的子句成分 [%s]:括号是歌词条件通道," + "只允许配器名词短语(kick / sub bass / snare / hand claps)," + "要求句与量词句请写进 tags" + % ", ".join(uniq) + ) + leak = _INTRO_LEAK_PHRASE_RE.findall(body) + if leak: + uniq = sorted(set(l.lower() for l in leak)) + errs.append( + "前奏括号含已实证会被唱出的生成指令措辞 [%s]:" + "这几句在 Bounce_s8 的成品里被原样唱了出来," + "同类要求请写进 tags(RULE_GROOVE_FIRST_BEAT)" + % ", ".join(uniq) + ) + return errs + + +def check_rules(lyrics, tags, lean=False): + """结构铁律校验。返回问题列表。 + + lean=True 时不检查 RULE_NEG 的两条人声否定句("no opening vocals" / + "no pure vocal intro"),因为 lean 刻意不注入 RULE_NEG。 + + 但 RULE_NO_FILLER(衬音禁令)在 lean 下仍然必须存在:它管的是整首歌 + 段落之间的衬音填充,跟前奏人声起点无关。2026-10-08 起加回 lean 路径。 + "swlvoice" 触发词始终要求保留(跟 LoRA 权重绑定)。 + """ + errs = [] + if not lyrics.lstrip().lower().startswith("[intro]"): + errs.append("歌词首行必须是 [intro]") + head = lyrics[:400] + if "[instrumental:" not in head.lower(): + errs.append("歌词开头必须有 [Instrumental: ...] 器乐前奏描述") + elif not re.search(r"\[instrumental:[^\]]*\d+\s*bar", head, re.I): + errs.append("器乐前奏必须写明小节数(如 '8 bar instrumental hook')") + for ln in lyrics.split("\n"): + s = ln.strip() + if s.startswith("[") and s.endswith("]") and \ + re.search(r"[\u4e00-\u9fff]", s): + errs.append("结构标记含中文会被唱出来: %s" % s[:40]) + break + errs += _check_intro_bracket_speakable(head) + low = tags.lower() + need_neg = not lean + for kw in ("no opening vocals", "no pure vocal intro", "swlvoice"): + if kw == "swlvoice" or need_neg: + if kw not in low: + errs.append("tags 缺少 '%s'" % kw) + # RULE_NO_FILLER 在 lean 下也必须存在:它是唯一一条约束整首歌 + # 人声只唱歌词、不唱衬音的规则。lean 跳过 RULE_NEG 是因为前奏否定句 + # 膨胀,但衬音禁令跟前奏无关,不能一起跳过。 + # 2026-10-08:从负面禁词(no ooh / no 嗯嗯)改为正面强制 + # "sing only the exact words",校验关键词也跟着改。 + for kw in ("sing only the exact words", "instrumental only"): + if kw not in low: + errs.append("tags 缺少 '%s'(衬音禁令,lean 下也必须存在)" % kw) + # 器乐前奏小节数:普通歌曲 8 小节,chill 吟唱 8 小节,纯器乐 8 小节。 + # 三种模式现在统一 8 小节(chill 曾试过 16,实测对人声起点毫无作用)。 + # 这里仍然只校验"写了且在 4~32 范围内",不写死具体数字: + # 旧校验把整句 "8 bar instrumental intro hook" 写死后,chill 换成 16 小节 + # 直接被拦下,而且报错完全指不到真实原因。改成匹配"N bar"后, + # 两种措辞(带 hook / 不带 hook)都接受。 + m = re.search(r"(\d+)\s*bar instrumental intro(?: hook)?", low) + if not m: + errs.append("tags 必须写明器乐前奏小节数" + "(如 '8 bar instrumental intro hook')") + elif not (4 <= int(m.group(1)) <= 32): + errs.append("器乐前奏小节数 %s 超出合理范围 4~32" + % m.group(1)) + else: + # tags 与歌词头必须写同一个小节数。之前 chill 标签改成 16 小节、 + # 歌词头还停在 8 小节,模型同时收到两个互相打架的指令, + # 结果是前奏长度完全随机。这里直接把不一致判成错误。 + mh = re.search(r"\[instrumental:[^\]]*?(\d+)\s*bar", head, re.I) + if mh and int(mh.group(1)) != int(m.group(1)): + errs.append("tags 写 %s 小节前奏,歌词头写 %s 小节," + "两处必须一致" + % (m.group(1), mh.group(1))) + return errs + + +def check_lexicon(lyrics, lexicon): + """身份词典校验。命中禁用词直接报错,防止再写出长板/护具。""" + errs = [] + body = "\n".join(l for l in lyrics.split("\n") + if l.strip() and not l.strip().startswith("[")) + low = body.lower() + for key, words in (lexicon.get("banned") or {}).items(): + if key.startswith("_"): + continue + for w in words: + if w.lower() in low: + errs.append("命中禁用词[%s]: %s(身份是街头双翘滑手)" % (key, w)) + return errs + + +def validate(lyrics, tags, lexicon=None): + """全部铁律 + 词典校验,不过就抛。""" + errs = check_rules(lyrics, tags) + if lexicon: + errs += check_lexicon(lyrics, lexicon) + if errs: + raise RuleViolation("违反生歌铁律:\n - " + "\n - ".join(errs)) + return True + + +def enforce_lexicon(lyrics, lexicon): + errs = check_lexicon(lyrics, lexicon) + if errs: + raise LexiconError("歌词身份校验失败:\n - " + "\n - ".join(errs)) + return True diff --git a/songforge.py b/songforge.py new file mode 100644 index 0000000..8ca4861 --- /dev/null +++ b/songforge.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- +"""songforge CLI 入口(被 ComfyUI 升级清空后重建)。 + + python songforge\songforge.py doctor + python songforge\songforge.py gen --style pop --title 夜班 --lyrics-file a.txt +""" +import os +import sys + +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +from songforge.cli.main import main # noqa: E402 + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tools/backup.py b/tools/backup.py new file mode 100644 index 0000000..52dacca --- /dev/null +++ b/tools/backup.py @@ -0,0 +1,208 @@ +# -*- coding: utf-8 -*- +"""带时间戳的备份工具。任何文档/配置/脚本更新前必须先跑它。 + + python songforge/tools/backup.py 生歌规则.md LLM_Wiki.md + python songforge/tools/backup.py --note "加 songforge CLI 章节" AGENTS.md + python songforge/tools/backup.py --from-root D:\\CodeFiles\\Thinking\\ComfyUI-MiniMaxH3 + python songforge/tools/backup.py --restore bak\\20261004-120000 + python songforge/tools/backup.py --list + +产物:workflows/bak//<原相对路径> +恢复:python songforge/tools/backup.py --restore <备份目录> + +坑位记录: +- 早期版本把路径压平成 "目录__文件.bak",文件一多就分不清谁是谁, + 且 AGENTS.md 在仓库根、不在 workflows 下,会退化成 ..__..__..__AGENTS.md.bak。 + 现在保留完整目录树:workflows 内的文件按相对路径存放, + 仓库根的文件统一放在 / 下,一眼能看出来源。 +- 路径分隔符统一转成 "/",Windows 与 Linux 读 manifest 结果一致。 +""" +import datetime +import json +import os +import shutil +import sys + +WF_ROOT = os.path.dirname(os.path.dirname(os.path.dirname( + os.path.abspath(__file__)))) # .../user/default/workflows +BAK = os.path.join(WF_ROOT, "bak") + + +def stamp(): + """毫秒精度:同一秒内连续备份两次不会互相覆盖。""" + return datetime.datetime.now().strftime("%Y%m%d-%H%M%S-%f")[:-3] + + +def comfy_root(): + """workflows 的上三级 = ComfyUI 根。""" + return os.path.dirname( + os.path.dirname( + os.path.dirname(WF_ROOT) + ) + ) + + +def rel_under(path): + """返回可还原、正斜杠的相对路径。 + + - workflows 内:songforge/cli/main.py + - ComfyUI 根: ROOT__/AGENTS.md + - 根之外: ABS__/D/somewhere/file.md + """ + path = os.path.abspath(path) + + for base, prefix in ((WF_ROOT, ""), (comfy_root(), "ROOT__/")): + rel = os.path.relpath(path, base) + if not rel.startswith(".."): + return prefix + rel.replace(os.sep, "/") + + rel = path.replace(":", "").replace(os.sep, "/").lstrip("/") + return "ABS__/" + rel + + +def backup(paths, note=""): + """复制文件到 bak/<时间戳>/,保留原目录树,返回备份目录。""" + # 坑位:原来用 exist_ok=True,同一毫秒内连续两次备份会落进同一目录, + # 上一次的 _manifest.json 直接被覆盖,恢复时就少了一份。改成原子创建。 + base = stamp() + for n in range(0, 100): + cand = os.path.join(BAK, base if n == 0 else "%s-%d" % (base, n + 1)) + try: + os.makedirs(cand, exist_ok=False) + dst_dir = os.path.abspath(cand) + break + except FileExistsError: + continue + else: + raise RuntimeError("无法在 %s 下分配备份目录" % BAK) + + items = [] + for p in paths: + p = os.path.abspath(p) + if not os.path.exists(p): + print(" [skip] 不存在: %s" % p) + continue + + rel = rel_under(p) + dst = os.path.join(dst_dir, *rel.split("/")) + os.makedirs(os.path.dirname(dst), exist_ok=True) + shutil.copy2(p, dst) + + items.append(dict(src=p, bak=dst, rel=rel, + size=os.path.getsize(dst))) + print(" [ok] %s" % rel) + + with open(os.path.join(dst_dir, "_manifest.json"), "w", + encoding="utf-8") as f: + json.dump(dict(stamp=os.path.basename(dst_dir), note=note, + items=items), f, ensure_ascii=False, indent=1) + + print("备份完成 -> %s (%d 个文件)" % (dst_dir, len(items))) + return dst_dir + + +def restore(bak_dir): + """按 manifest 把文件拷回原位。""" + bak_dir = os.path.abspath(bak_dir) + mpath = os.path.join(bak_dir, "_manifest.json") + if not os.path.exists(mpath): + print("找不到 manifest: %s" % mpath) + return 1 + + with open(mpath, encoding="utf-8") as f: + m = json.load(f) + + for it in m["items"]: + if not os.path.exists(it["bak"]): + print(" [miss] 备份件丢失: %s" % it["bak"]) + continue + os.makedirs(os.path.dirname(it["src"]), exist_ok=True) + shutil.copy2(it["bak"], it["src"]) + print(" [restore] %s" % it["rel"]) + + print("恢复完成 <- %s" % bak_dir) + return 0 + + +def list_bak(): + if not os.path.isdir(BAK): + print("还没有任何备份: %s" % BAK) + return 1 + + rows = [] + for name in sorted(os.listdir(BAK), reverse=True): + mp = os.path.join(BAK, name, "_manifest.json") + if not os.path.isfile(mp): + continue + with open(mp, encoding="utf-8") as f: + m = json.load(f) + rows.append((name, len(m.get("items", [])), m.get("note", ""))) + + if not rows: + print("备份目录为空: %s" % BAK) + return 1 + + for name, n, note in rows: + print(" %s %2d 个 %s" % (name, n, note)) + return 0 + + +def _expand(root, patterns): + out = [] + for pat in patterns: + full = os.path.join(root, pat.replace("/", os.sep)) + if os.path.isdir(full): + for dirpath, _, files in os.walk(full): + for fn in files: + if fn.endswith(".pyc"): + continue + out.append(os.path.join(dirpath, fn)) + else: + out.append(full) + return out + + +def main(argv): + if not argv: + print(__doc__) + return 1 + + if argv[0] in ("-h", "--help"): + print(__doc__) + return 0 + + if argv[0] == "--restore": + if len(argv) < 2: + print(__doc__) + return 1 + return restore(argv[1]) + + if argv[0] == "--list": + return list_bak() + + note = "" + root = None + + while argv and argv[0].startswith("--"): + opt = argv[0] + if opt == "--note" and len(argv) >= 2: + note = argv[1] + argv = argv[2:] + elif opt == "--from-root" and len(argv) >= 2: + root = os.path.abspath(argv[1]) + argv = argv[2:] + else: + print("未知参数: %s" % opt) + print(__doc__) + return 1 + + if not argv: + print(__doc__) + return 1 + + backup(_expand(root, argv) if root else argv, note) + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/tools/make_baseline.py b/tools/make_baseline.py new file mode 100644 index 0000000..023cbd7 --- /dev/null +++ b/tools/make_baseline.py @@ -0,0 +1,88 @@ +# -*- coding: utf-8 -*- +"""从历史脚本 remake_v5.py 抽出 8 首已认可老歌的基线资料。 + +用途:QC 需要"歌词正文"才能算召回率。没有歌词,coverage() 恒等于 0, +老歌基线就退化成"召回 >= 0%",等于没有基线——这种假放行比不放行更危险。 + +同时抽出 bpm / duration / key,让 QC 能按真实 8 小节算理论前奏秒数。 +不重新生成音频,只读源码里的 BODY / META。 + +输出:config/baseline/qc_baseline_old.json +源脚本路径可用环境变量 SONGFORGE_REMAKE_V5 覆盖。 +""" +import importlib.util +import json +import os +import re +import sys + +HERE = os.path.dirname(os.path.abspath(__file__)) +_PKG = os.path.dirname(HERE) +OUT = os.path.join(_PKG, "config", "baseline", "qc_baseline_old.json") + +_DEFAULT_SRC = os.path.join( + os.environ.get("USERPROFILE", os.environ.get("HOME", "")), + r".trae-cn\work\6ab523de391489c168050edc\remake_v5.py") +SRC = os.environ.get("SONGFORGE_REMAKE_V5") or _DEFAULT_SRC + +BRACKET = re.compile(r"^\[[^\]]*\]") +CJK = re.compile(r"[\u4e00-\u9fff]") + + +def _load_remake(): + if not os.path.isfile(SRC): + print("[错误] 找不到 remake_v5.py: %s" % SRC) + print(" 设置 SONGFORGE_REMAKE_V5=完整路径 后重试") + return None + spec = importlib.util.spec_from_file_location("remake_v5", SRC) + m = importlib.util.module_from_spec(spec) + spec.loader.exec_module(m) # __main__ 有守卫,不会提交任何任务 + return m + + +def body_text(m, title): + """把 BODY[title] 折成纯歌词正文。""" + seen, keep = set(), [] + for _tag, text in m.BODY[title]: + if "Instrumental" in text: + continue + for line in text.split("\n"): + line = BRACKET.sub("", line).strip() + if not line or not CJK.search(line): + continue + if line in seen: + continue + seen.add(line) + keep.append(line) + return "\n".join(keep) + + +def main(): + m = _load_remake() + if m is None: + return 2 + items = [] + for title, seed, bpm, key, dur, style in m.META: + items.append(dict( + title=title, + file="v5_%s_swl_00001.flac" % title, + bpm=bpm, key=key, duration=dur, + group="old", + lyrics=body_text(m, title), + )) + os.makedirs(os.path.dirname(OUT), exist_ok=True) + with open(OUT, "w", encoding="utf-8") as f: + json.dump(dict(note="8 首用户已认可老歌的基线资料," + "由 tools/make_baseline.py 从 remake_v5.py 抽取", + items=items), f, + ensure_ascii=False, indent=1) + for it in items: + n = len(CJK.findall(it["lyrics"])) + print("%-8s bpm=%-4s dur=%-4s 汉字=%d" % (it["title"], it["bpm"], + it["duration"], n)) + print("\nwrote %s" % OUT) + return 0 + + +if __name__ == "__main__": + sys.exit(main() or 0) diff --git a/tools/make_style_ref.py b/tools/make_style_ref.py new file mode 100644 index 0000000..1da87ec --- /dev/null +++ b/tools/make_style_ref.py @@ -0,0 +1,183 @@ +# -*- coding: utf-8 -*- +"""用历史已认可作品建风格质心语料 -> config/baseline/style_ref.json + +跑法(必须在 ComfyUI venv 的 Python 下): + C:\\comfyui-venv\\Scripts\\python.exe tools\\make_style_ref.py + +音乐目录 / 输出路径读 config/default.json + local.json(勿写死盘符)。 +""" +import json +import os +import re +import sys + +HERE = os.path.dirname(os.path.abspath(__file__)) + +# 坑位:songforge.py(CLI 入口)和 songforge/(包)同名, +# tools/ 内部互相引用一律走文件路径加载。 +sys.path.insert(0, HERE) +import qc_style # noqa: E402 + +_PKG = os.path.dirname(HERE) + + +def _paths_from_config(): + paths = {} + for name in ("default.json", "local.json"): + p = os.path.join(_PKG, "config", name) + if not os.path.isfile(p): + continue + with open(p, encoding="utf-8") as f: + blob = json.load(f) + paths.update(blob.get("paths") or {}) + music = paths.get("music_dir") or os.environ.get("SONGFORGE_MUSIC_DIR") + if not music: + cro = paths.get("comfy_root") or os.environ.get("SONGFORGE_COMFY_ROOT") + if cro: + music = os.path.join( + cro, "output", paths.get("output_prefix") or "MyMusic") + if not music: + music = os.path.join( + os.path.dirname(os.path.dirname(_PKG)), "output", "MyMusic") + out = os.path.join(_PKG, "config", "baseline", "style_ref.json") + cache = os.path.join(_PKG, "cache", "style") + return os.path.abspath(music), out, cache + + +MUSIC, OUT, CACHE = _paths_from_config() + +# 前缀白名单:确认过的直接给 family,避免子串规则误伤 +EXACT = { + "smile13": "pop", + "hit10": "pop", + "dy10": "pop", + "col2": "pop", + "punk4": "punk", + "niaopunk": "punk", + "urbanhiphop": "hiphop", + "chillhiphop": "hiphop", + "chillhiphopen": "hiphop", + "chillhiphopja": "hiphop", + "upbeathiphop": "hiphop", + "funkyhiphop": "hiphop", + "discofunk": "hiphop", + "partytrap": "hiphop", + "smoothhiphop": "hiphop", + "喜剧hiphop": "hiphop", + "欧式hiphop": "hiphop", +} + +# 出现在前缀里就归某家族(前面已排除精确表未命中的) +SUBSTR = ( + ("punk", "punk"), + ("hiphop", "hiphop"), + ("trap", "hiphop"), +) + +SEED = re.compile(r"_\d{5}\.flac$", re.I) +# 只收每个 seed 序列的第一首。同源的 _00002/_00003 全收进来,语料就被 +# 少数几首歌按 seed 数投票,质心被拉偏(踩过:172 个文件其实只有 30 来首歌)。 +FIRST_SEED = re.compile(r"_00001\.flac$", re.I) + + +def family_of(name): + """从文件名解析风格家族,解析不出来返回 None(直接排除)。""" + stem = SEED.sub("", name) + head = stem.split("_")[0] + if head in EXACT: + return EXACT[head] + low = head.lower() + for key, fam in SUBSTR: + if key in low: + return fam + return None + + +def main(): + if not os.path.isdir(MUSIC): + print("[错误] 找不到音频目录 %s" % MUSIC) + return 2 + + files = sorted(f for f in os.listdir(MUSIC) if f.lower().endswith(".flac")) + picked = [] + for f in files: + fam = family_of(f) + if not fam: + continue + if not FIRST_SEED.search(f): # 只收每首歌的第一个 seed + continue + p = os.path.join(MUSIC, f) + if not os.path.isfile(p): + continue + picked.append((f, fam, p)) + + if not picked: + print("[错误] 一首都没匹配上,检查 EXACT / SUBSTR 是否还匹配文件名") + return 2 + + print("语料候选 %d 首,开始算特征(每首几秒)..." % len(picked)) + os.makedirs(CACHE, exist_ok=True) + items = [] + for i, (f, fam, p) in enumerate(picked, 1): + cp = os.path.join(CACHE, f + ".json") + # 缓存 key 带上音频 mtime + 特征列表,音频换了或特征改了都会自动失效 + stamp = "%s|%s" % (os.path.getmtime(p), ",".join(qc_style.FEATURES)) + vec = None + if os.path.isfile(cp): + try: + with open(cp, encoding="utf-8") as fh: + blob = json.load(fh) + if blob.get("key") == stamp: + vec = blob["vec"] + except Exception: + vec = None + if vec is None: + try: + # expect_bpm=None:历史文件没有可靠的 BPM 记录,靠自身中位数定折叠锚点, + # 保证语料里的 tempo 尺度自洽。新歌的 BPM 对齐由 qc_worker 单独判。 + vec, _raw = qc_style.features(p, expect_bpm=None) + except Exception as exc: + print(" [%d/%d] 跳过 %s -> %s: %s" + % (i, len(picked), f, type(exc).__name__, exc)) + continue + with open(cp, "w", encoding="utf-8") as fh: + json.dump(dict(key=stamp, vec=vec), fh) + items.append(dict(file=f, family=fam, vec=vec)) + print(" [%d/%d] %-44s %-6s tempo=%5.1f flat=%.4f" + % (i, len(picked), f[:44], fam, vec["tempo"], vec["flatness"])) + + counts = {} + for it in items: + counts[it["family"]] = counts.get(it["family"], 0) + 1 + print("按家族统计:%s" % counts) + for fam in qc_style.FAMILIES: + if counts.get(fam, 0) < 3: + print("[错误] %s 只有 %d 首,建不出可信质心(至少 3)" + % (fam, counts.get(fam, 0))) + return 2 + + ref = qc_style.build(items) + with open(OUT, "w", encoding="utf-8") as f: + json.dump(ref, f, ensure_ascii=False, indent=1) + + loo = ref["loo"] + print("") + print("已写出 %s" % OUT) + print("样本 %d(%d 首不同的歌),分组留一准确率 %.1f%%,trusted=%s" + % (len(items), loo["n_songs"], loo["acc"] * 100, ref["trusted"])) + print("分家族:%s" % json.dumps(loo["per_family"], ensure_ascii=False)) + print("混淆矩阵(行=真实,列=判成):") + for fam, row in sorted(loo["confusion"].items()): + print(" %-7s %s" % (fam, json.dumps(row, ensure_ascii=False))) + if not ref["trusted"]: + print("") + print("[结论] 风格判定不可信,只写进报告当参考,不当放行闸门。") + print(" 风格的第一物理保证是实测 BPM 与设定 BPM 对齐(QC 的 bpm_rel_tol)。") + print(" 注意:语料里 pop 的 chill/disco/funk 变体和 hiphop 制作手法本就连续,") + print(" 分组 LOO 总体约 60~67%、单家族最低约 43%," + "这是语料重叠,不是分类器没调好。") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) \ No newline at end of file diff --git a/tools/qc_style.py b/tools/qc_style.py new file mode 100644 index 0000000..e57bc63 --- /dev/null +++ b/tools/qc_style.py @@ -0,0 +1,416 @@ +# -*- coding: utf-8 -*- +"""风格特征与风格分类 —— 只吃 librosa,纯数值,不碰 demucs/whisper。 + +为什么单独一个文件: + songforge 主链路是零重依赖的,风格判定属于"生成后验收"的一部分, + 必须在 venv 侧跑。把它独立出来,`qc_worker.py` 和 + `tools/make_style_ref.py` 共用同一套取特征逻辑——否则"建语料"和 + "验收"各写一份,特征对不上,分类结果就是自欺欺人。 + +风格怎么判(重要,不要改成拍脑袋的阈值): + 这里不做"频谱质心 > X 就是朋克"这种单指标硬判。做法是拿历史里 + 已经按风格命名、并且用户听过没否掉的歌当语料,逐首算同一套特征, + 按稳健标准化后建三个风格质心(pop / punk / hiphop),用最近质心 + 分类,并做留一验证(LOO)把准确率一起存下来。 + 准确率不够高时,报告里如实写"风格判定不可信",只当参考不当闸门。 + +坑位记录: + 1. 速度必须做八度折叠。实测 `晚风不加班`(设定 86 BPM)会被判成 + 170.5 BPM,正好是 2 倍——librosa 的 onset 包络在半速拍子上会 + 锁到二倍拍。直接比设定值会把这个风格判死。 + 2. 频谱类特征跨度极大(质心 1400~6000 Hz),必须取 log 再标准化, + 否则质心一项就主导了整段欧氏距离。 + 3. perc_ratio 用 HPSS 的打击成分占比。朋克的军鼓+失真吉他、 + hiphop 的 kick+hi-hat 都会偏高,流行的人声+弦乐偏低。 + 单看这个不如 BPM 有区分度,但作为辅助项能压掉误判。 + 4. 特征里绝对不能有"人声轨 / 混音轨 能量比"。带标签的历史语料有 30~50 首, + 每一首都跑 demucs 要花几十分钟,而且 demucs 对不同混音的分离程度 + 本身就不一致,这个比值混进去是纯噪声。风格只看混音轨。 +""" +import os +import re + +import numpy as np + +FAMILY = { + "pop": "pop", + "punk": "punk", + "emopunk": "punk", + "urbanhiphop": "hiphop", + "chillhiphop": "hiphop", + "partyrap": "hiphop", + "funkhiphop": "hiphop", + "discofunk": "hiphop", + "jazzhiphop": "hiphop", + "guofeng": "hiphop", +} + +FAMILIES = ("pop", "punk", "hiphop") + +# 特征顺序即向量顺序,改这里必须重建 style_ref.json,否则老语料错位。 +FEATURES = ( + "tempo", # 八度折叠后的实测速度 + "tempo_drift", # 速度稳定度(越小越稳) + "centroid", # log 频谱质心 -> 亮/暗 + "rolloff", # log 85% rolloff -> 高频能量 + "hf_ratio", # >6kHz 能量占比 -> 亮泽/嘶叫 + "lf_ratio", # <150Hz 能量占比 -> 低频厚度/sub + "dynamic", # 动态范围 dB + "onset_density", # 每秒击打数 + "perc_ratio", # HPSS 打击成分占比 + "flatness", # 谱平坦度 -> 噪声/失真成分(朋克嘶叫会抬高) + "crest", # 峰值因子,越小越被压得平 +) + +_LOG_IDX = (FEATURES.index("centroid"), FEATURES.index("rolloff")) + + +# ------------------------------------------------------------------ 工具 + +def fold_bpm(value, ref): + """把 value 折叠到离 ref 最近的八度。 + + 坑位:librosa.beat 在慢速律动上会锁到 2 倍拍,不折叠会把 86 BPM 的歌 + 判成 170 BPM,然后"实测速度与设定偏差 98%",一个完全正常的歌被判死。 + """ + if value is None or ref is None: + return value + try: + v = float(value) + r = float(ref) + except (TypeError, ValueError): + return value + # 坑位:非有限值和非正数会一路穿到 log2 里,log2(inf) 再喂给 round() + # 直接抛 OverflowError,把整首 QC 打断。下游只想要"折不动就原样返回"。 + if not (np.isfinite(v) and np.isfinite(r)) or v <= 0 or r <= 0: + return v + ratio = r / v + if not np.isfinite(ratio) or ratio <= 0: + return v + return v * (2.0 ** round(np.log2(ratio))) + + +def fold_track(track, ref): + return np.array([fold_bpm(x, ref) for x in track], dtype=np.float32) + + +def _band_ratio(y, sr, lo, hi): + """指定频带能量占比。用 rfft 直接算,比 mel 滤波器组省事也够稳。""" + if y.size < 256: + return 0.0 + n = 1 << int(np.ceil(np.log2(y.size))) + sp = np.abs(np.fft.rfft(y, n)) ** 2 + freqs = np.fft.rfftfreq(n, 1.0 / sr) + band = sp[(freqs >= lo) & (freqs < hi)].sum() + tot = sp.sum() + return float(band / tot) if tot > 0 else 0.0 + + +# ------------------------------------------------------------------ 取特征 + +def tempo_of(y, sr, expect_bpm=None): + """返回 (折叠后中位速度, 折叠后速度漂移)。expect_bpm 用来定折叠方向。""" + import librosa + y16 = y if sr == 16000 else librosa.resample( + np.asarray(y, dtype=np.float32), orig_sr=sr, target_sr=16000) + sr16 = 16000 if sr != 16000 else sr + oe = librosa.onset.onset_strength(y=y16, sr=sr16) + # librosa 0.11 起 beat.tempo 全是 keyword-only,位置传参直接 TypeError + tr = librosa.beat.tempo(onset_envelope=oe, sr=sr16, hop_length=512, + aggregate=None) + tr = np.asarray(tr, dtype=np.float32) + tr = tr[np.isfinite(tr) & (tr > 0)] + if not tr.size: + return 0.0, 0.0 + raw_med = float(np.median(tr)) + anchor = fold_bpm(raw_med, expect_bpm) if expect_bpm else raw_med + ft = fold_track(tr, anchor) + ft = ft[(ft > 0) & (ft < 400)] + if not ft.size: + return 0.0, 0.0 + med = float(np.median(ft)) + # 坑位:原来用 (p90 - p10) / median 算漂移,但 librosa 逐帧 tempo 的首尾 + # 各有一拍宽的边界伪影,180s 的歌能攒出十几秒伪影区,被 p90/p10 一夹 + # 就放大成 25%+ 的"漂移",而同一首歌按 10s 分块的中位全是设定值 + # (实测 104.2/104.2,IQR=0)。改用中位绝对偏差,只对整体性的加速/ + # 减速敏感,对边界伪影和偶发跳拍免疫。 + drift = _mad_rel(ft, med) + return med, drift + + +def _mad_rel(track, med): + """相对中位绝对偏差。返回 0.0 表示测不出整体漂移。""" + if track.size == 0 or med <= 0: + return 0.0 + mad = float(np.median(np.abs(track - med))) + return (mad / med) if med > 0 else 0.0 + + +def features(path, expect_bpm=None, cache_dir=None): + """算一首歌的完整风格特征向量。 + + 坑位:这个函数只允许吃混音轨(path)。不要往里塞 demucs 人声轨—— + 建历史语料时要对几十首老歌跑,塞进去就等于把风格判定绑死在分离质量上, + 而 demucs 的分离程度本身随混音变化。声线由 qc_worker 单独负责。 + """ + import librosa + import soundfile as sf + + x, sr = sf.read(path, always_2d=True) + x = np.asarray(x, dtype=np.float32) + y = x.mean(axis=1) + + dur = y.size / float(sr) + tempo, tempo_drift = tempo_of(y, sr, expect_bpm) + + y16 = librosa.resample(y, orig_sr=sr, target_sr=16000) + sr16 = 16000 + S = np.abs(librosa.stft(y16, n_fft=2048, hop_length=512)) + 1e-12 + centroid = float(np.mean(librosa.feature.spectral_centroid(S=S, sr=sr16))) + rolloff = float(np.mean(librosa.feature.spectral_rolloff( + S=S, sr=sr16, roll_percent=0.85))) + # 谱平坦度:几何均值/算术均值。噪声化(失真、嘶叫、lo-fi)会把它抬高, + # 干净的钢琴/弦乐 pad 会压得很低。比 "高频能量占比" 更能区分失真音色。 + gm = np.exp(np.mean(np.log(S), axis=0)) + am = np.mean(S, axis=0) + flatness = float(np.mean(gm / am)) + + bs = 3 * sr + n = max(1, y.size // bs) + rmss = np.array([np.sqrt(np.mean(np.square(y[i * bs:(i + 1) * bs])) + 1e-20) + for i in range(n)], dtype=np.float32) + dyn = float(20 * np.log10(max(rmss.max(), 1e-9) / max(rmss.min(), 1e-9))) + + oe = librosa.onset.onset_strength(y=y16, sr=sr16) + hop = 512 + onset_density = float(len(librosa.onset.onset_detect( + onset_envelope=oe, sr=sr16, hop_length=hop)) / max(dur, 1e-6)) + + h, p = librosa.effects.hpss(y16) + perc_ratio = float(np.mean(np.abs(p)) / + max(np.mean(np.abs(h)) + np.mean(np.abs(p)), 1e-9)) + + peak = float(np.max(np.abs(y))) if y.size else 0.0 + rms_all = float(np.sqrt(np.mean(np.square(y)) + 1e-20)) + crest = float(20 * np.log10(max(peak, 1e-9) / max(rms_all, 1e-9))) + + vec = { + "tempo": tempo, + "tempo_drift": tempo_drift, + "centroid": float(np.log10(max(centroid, 1.0))), + "rolloff": float(np.log10(max(rolloff, 1.0))), + "hf_ratio": _band_ratio(y16, sr16, 6000, sr16 / 2), + "lf_ratio": _band_ratio(y16, sr16, 20, 150), + "dynamic": dyn, + "onset_density": onset_density, + "perc_ratio": perc_ratio, + "flatness": flatness, + "crest": crest, + } + return vec, dict(centroid_hz=centroid, rolloff_hz=rolloff, + flatness=flatness, + onset_density=onset_density, perc_ratio=perc_ratio, + crest=crest, peak=peak) + + +def vector(vec): + return np.array([float(vec[k]) for k in FEATURES], dtype=np.float64) + + +# ------------------------------------------------------------------ 分类器 + +def _robust_scale(X): + """中位数 + IQR 标准化。IQR 比 std 抗离群值。""" + med = np.median(X, axis=0) + q1 = np.percentile(X, 25, axis=0) + q3 = np.percentile(X, 75, axis=0) + iqr = np.maximum(q3 - q1, 1e-6) + return med, iqr + + +class StyleRef(object): + """风格质心分类器。语料不足或 LOO 太低时 self.trusted=False。""" + + def __init__(self, data): + self.note = data.get("note", "") + self.loo = data.get("loo", {}) + self.items = data.get("items", []) + self.med = np.array(data["med"], dtype=np.float64) + self.iqr = np.array(data["iqr"], dtype=np.float64) + self.centroids = {k: np.array(v, dtype=np.float64) + for k, v in data["centroids"].items()} + self.counts = data.get("counts", {}) + self.trusted = bool(data.get("trusted", False)) + + def _z(self, vec): + return (vector(vec) - self.med) / self.iqr + + def classify(self, vec): + """返回 (top_family, conf, dists)。conf 是 softmax 后的置信度。""" + z = self._z(vec) + dists = {} + for fam, c in self.centroids.items(): + dists[fam] = float(np.sqrt(np.mean((z - c) ** 2))) + order = sorted(dists, key=lambda k: dists[k]) + if not order: + return "", 0.0, dists + top = order[0] + e = {k: np.exp(-dists[k]) for k in order} + s = sum(e.values()) + return top, float(e[top] / s if s > 0 else 0.0), dists + + def nn(self, vec): + """最近邻:返回 (最像的文件名, 与它的标准化距离, 该文件所属风格)。 + + 这是声线之外第二个'像不像已认可作品'的信号,且自带人耳可解释性: + 报告里能直接说出"最像 身体先听懂(urbanhiphop)"。 + """ + z = self._z(vec) + best = ("", 1e9, "") + for it in self.items: + d = float(np.sqrt(np.mean((z - np.array(it["z"], dtype=np.float64)) ** 2))) + if d < best[1]: + best = (it["file"], d, it.get("family", "")) + return best + + +def load(path): + import json + if not path or not os.path.isfile(path): + return None + with open(path, encoding="utf-8") as f: + return StyleRef(json.load(f)) + + +def _loo_groups(items): + """按"歌"分组,而不是按文件。 + + 坑位(踩过):最初按文件留一,得到 100% 准确率,一度以为分类器完美。 + 实际上同一首歌的 _00001/_00002/_00003 是同一首曲子的不同 seed, + 音频高度同源。留一时只拿掉一个 seed,剩下 5 个"同款"还留在质心里, + 等于让样本给自己作证,准确率必然虚高。 + 真正的留一必须整首拿掉——验证的是"没听过这首歌时能不能认出风格"。 + """ + g = {} + for i, it in enumerate(items): + key = it.get("group") or _group_key(it.get("file", "")) + g.setdefault(key, []).append(i) + return g + + +def _group_key(file): + """从文件名还原"这是哪一首歌"。dy10_城市晚安_swl_00001 -> dy10_城市晚安。 + + 只用于分组,不是标签。 + + 坑位:这里曾经把第一个下划线之前的前缀当"批次号"砍掉,只留后面的歌名。 + 结果 chillhiphop_慢慢来 和 hit10_慢慢来 被并成同一组,58 个文件只出 + 57 个组。留一验证时同一首歌的另一版本还留在质心里,样本等于给自己作证。 + 所以只剥 seed 号和模型后缀,风格前缀必须留着——它本来就是区分同名歌的 + 唯一信息。命名不规范时退回整条文件名也不会错。 + """ + stem = file.rsplit(".flac", 1)[0] + stem = re.sub(r"_\d{5}$", "", stem) # 去掉 seed 号 + stem = re.sub(r"_swl$", "", stem, flags=re.I) # 去掉模型后缀 + return stem + + +def build(items, loo_min=0.65, fam_min=0.50): + """由带标签的语料建质心,并做按歌分组的留一验证。 + + items: [{"file","family","bpm","vec"}] + 返回可直接 json.dump 的字典。 + + 关于阈值为什么这么严(实测数据,别拍脑袋改): + 语料是 58 首歌(pop 35 / hiphop 16 / punk 7)。按歌分组留一后, + 最好的组合也只到 ~65%:pop 有 4 成被判成 hiphop,hiphop 也有近 + 1 成被判成 pop。逐个特征看下来没有救——tempo 单项 49%、centroid + 单项 39%,加特征、加权、改标准化都只在 60~67% 之间晃。 + 根因是语料本身重叠:pop 语料里的 chill/disco/funk 变体和 hiphop + 语料在制作手法上就是连续的,不是分类器没调好。 + + 所以这里刻意把门槛设高(loo_min=0.65 且每个家族都要 >=0.50), + 让 trusted 大概率为 False —— 风格判定只写进报告当参考,不当放行闸门。 + 宁可报告里写"风格不可信",也不要一个 60% 准确率的分类器 + 拿去做"风格不符就判 FAIL"的闸门,那是在用假精度杀人。 + 风格的第一物理保证是实测 BPM 与设定 BPM 对齐(见 HARD.bpm_rel_tol)。 + """ + import json + X = np.array([vector(it["vec"]) for it in items], dtype=np.float64) + med, iqr = _robust_scale(X) + Z = (X - med) / iqr + + cent = {} + counts = {} + for fam in FAMILIES: + idx = [i for i, it in enumerate(items) if it["family"] == fam] + if not idx: + continue + counts[fam] = len(idx) + cent[fam] = np.mean(Z[idx], axis=0) + + # ---- 按歌分组留一:整首拿掉,重算标准化和质心,再看它能不能被认对 + groups = _loo_groups(items) + correct = 0 + total = 0 + per = {} + confusion = {} + for key, idxs in groups.items(): + fam = items[idxs[0]]["family"] + keep = [i for i in range(len(items)) if i not in set(idxs)] + Xk = np.array([vector(items[i]["vec"]) for i in keep], + dtype=np.float64) + mk, ik = _robust_scale(Xk) + Zk = (Xk - mk) / ik + ck = {} + for f in FAMILIES: + j = [n for n, i in enumerate(keep) if items[i]["family"] == f] + if j: + ck[f] = np.mean(Zk[j], axis=0) + if len(ck) < 2: + continue + z = np.mean([(vector(items[i]["vec"]) - mk) / ik for i in idxs], axis=0) + d = {f: float(np.sqrt(np.mean((z - c) ** 2))) for f, c in ck.items()} + top = min(d, key=lambda k: d[k]) + total += 1 + good = top == fam + correct += 1 if good else 0 + a = per.setdefault(fam, [0, 0]) + a[1] += 1 + a[0] += 1 if good else 0 + cell = confusion.setdefault(fam, {}) + cell[top] = cell.get(top, 0) + 1 + + acc = float(correct / total) if total else 0.0 + fam_acc = {k: (v[0] / float(v[1]) if v[1] else 0.0) for k, v in per.items()} + # 总体准确率高也可能被某一个大类撑起来,所以每个家族都要单独过线。 + # 三个家族里只要有一个基本认不出来(比如 punk 全被判成 pop), + # 这个分类器就不能拿去做风格闸门。 + fam_ok = bool(fam_acc) and min(fam_acc.values()) >= fam_min + ref = dict( + features=list(FEATURES), + med=[float(x) for x in med], + iqr=[float(x) for x in iqr], + centroids={k: [float(x) for x in v] for k, v in cent.items()}, + counts=counts, + items=[dict(file=it["file"], family=it["family"], + group=it.get("group") or _group_key(it["file"]), + bpm=it.get("bpm"), vec=it["vec"], + z=[float(x) for x in Z[i]]) + for i, it in enumerate(items)], + loo=dict(acc=round(acc, 4), n=total, n_songs=len(groups), + mode="group-by-song", + per_family={k: dict(acc=round(v[0] / max(v[1], 1), 3), n=v[1]) + for k, v in per.items()}, + confusion=confusion, + fam_min=round(min(fam_acc.values()) if fam_acc else 0.0, 3)), + trusted=bool(total >= 9 and acc >= loo_min and fam_ok), + note=("由 tools/make_style_ref.py 在历史已认可歌上生成,不要手改。" + "trusted 只有在按歌分组 LOO >= %.0f%% 且每个家族都 >= %.0f%% 时为真;" + "实测语料 pop/hiphop 制作手法重叠,多轮尝试后总体约 60~67%%、" + "单家族最低约 43%%," + "所以本文件通常 trusted=false —— 风格判定只当参考," + "风格的第一物理保证是实测 BPM 与设定 BPM 对齐。" + % (loo_min * 100, fam_min * 100)), + ) + return ref \ No newline at end of file diff --git a/tools/qc_worker.py b/tools/qc_worker.py new file mode 100644 index 0000000..aa410d7 --- /dev/null +++ b/tools/qc_worker.py @@ -0,0 +1,1216 @@ +# -*- coding: utf-8 -*- +"""音频验收 worker —— 重活全在这个文件里,由 qc.py 用 ComfyUI venv 的 +Python 拉起。本文件不参与 songforge 主链路,主 CLI 零重依赖。 + +为什么单独拆一个进程: + songforge 主入口跑在系统 Python(3.10,无 librosa/torch)。 + demucs / whisper / CAMPPlus / librosa 只在 ComfyUI venv 里。 + 硬要在主进程 import 会把整个 CLI 的可用性绑到音频栈上, + 等于把地基埋雷。所以:CLI 只负责组装 job、调度、渲染报告, + 真正的测量全部在 venv 侧的 worker 里完成。 + +判定思路(重要): + 阈值不拍脑袋。凡是"多少算好"的指标,都先在 8 首用户已认可的老歌上 + 跑一遍取基线,再按基线派生放行线。否则就会出现"新歌 0.47 算低吗" + 这种没法回答的问题——0.47 到底是差还是好,只有跟已认可作品比才知道。 + +指标踩过的坑(都记在这,别再走回头路): + 1. demucs 的 vocals 轨有器乐泄漏。早期用"前 2s / 前 8s 人声能量占比" + 判开头,结果 8 首老歌这个值也是 0.82~1.36 —— 连用户已认可的歌都 + "纯人声开头"。指标本身失效,不是歌的问题。 + 改用 Whisper 段级时间戳:ASR 在什么时间第一次唱出真汉字, + 那就是人声进入点。这直接对应"有没有真的在唱",不受泄漏干扰。 + 2. F0 漂移不能用"30s 块中位数的 max-min / mean"。老歌里 75% 的漂移 + 全部来自无词段的垃圾音高(yin 在器乐残留上乱跳)。 + 改:先按能量门限挑出有声帧再算 F0,漂移用半音表示,阈值从老歌派生。 + 3. tempo 用人声轨 onset 包络也不稳(人声轨稀疏)。改从混音轨算, + 生成链路没有 time-stretch,混音 tempo 应当全歌恒定。 + 4. tempo 必须做八度折叠。实测设定 86 BPM 的《晚风不加班》被报成 + 170.5 BPM —— librosa 的 onset 包络在慢速律动上会锁到二倍拍。 + 不折叠就直接和设定值比,这个风格会被判死。见 qc_style.fold_bpm。 + 5. 声线不能只和"某一个参考音频"比余弦。参考音频是谁、录哪一版、 + 干声还是混响,都会让这个数飘。改成与 8 首已认可老歌做最近邻: + 报告里直接说"最像 身体先听懂",既有数值又人耳可解释。 + 6. 光按"实义词数 >= 4"判真唱是不行的。Whisper 在纯器乐前奏上会幻觉出 + 成片实义词("L-E-D-O-N-O"、"Oh..."),已认可老歌也被压到 onset=0.0s。 + 改用歌词正文重合判真唱:ASR 段的实义词有多少落在歌词正文里。 + 7. 但"与歌词正文不重合"也不能直接判成生成指令泄漏——纯器乐幻觉同样 + 与歌词零重合。必须再算一次与**真实生成链路** [Instrumental: ...] + 指令的重合率:指令命中高才是泄漏,两个都低才是幻觉。 + 指令文本必须走 force_intro_bars -> filter_intro_bracket 复现, + 拿歌词文件里的手写括号当对照是错的(ironlaw 会自动追加配器短语)。 + 8. 8 小节前奏是硬要求,闸门就不能只放到理论长度的 55%。 + 早期 0.55 让人声在第 4~5 小节就进 sang 也算过, + 等于把铁律降级成建议。现在按完整 8 小节判,只留 ASR 时间戳 + 自身的分辨率容差(onset_grace)。 + +依赖(全部在 C:\\comfyui-venv): + soundfile / librosa / torch / torchaudio / demucs / whisper + CAMPPlus 取自 ComfyUI 的 custom_nodes/ComfyUI_Seed-VC +""" +import json +import os +import sys +import traceback + +import numpy as np + +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +import qc_style # noqa: E402 + +# 8 首用户已认可的老歌,作为声线/ASR/音频指标基线。 +# 歌词正文不在这里硬编码,由 tools/make_baseline.py 从 remake_v5.py 抽到 +# qc_baseline_old.json —— 没有歌词,coverage() 恒为 0,基线就是假的。 +BASELINE_OLD = [ + "亮灯就跳", "干饭魂", "快乐废物指南", "拜师记", + "晒太阳", "晚风不加班", "跳起来", "身体先听懂", +] + +# 物理上不成立的硬边界,与听感无关,可以写死。 +HARD = dict( + dur_tol=0.05, # 时长与设定 ±5% + sr_min=44100, # 输出采样率下限 + peak_max=1.0, # 峰值不得超过 0 dBFS + lufs=(-25.0, -9.0), # 近似响度区间 + silence_max=0.30, # 静音占比上限 + f0=(60.0, 620.0), # 音高范围(男女声都要覆盖到) + bpm_rel_tol=0.18, # 实测速度与设定 BPM 偏差上限 + # 8 小节前奏是铁律,闸门必须按完整 8 小节判。 + # 原来这里是 0.55,等于"第 4~5 小节进 sang 也算过",把铁律降级成建议。 + # 只留 onset_grace 这一条容差,用来吸收 ASR 时间戳自身的分辨率误差 + # (Whisper 段落起点最多会早报 0.5s 左右),不是用来放水进水点的。 + intro_onset_rel=1.0, + onset_grace=1.2, # 秒,ASR 时间戳容差 + # ---- 歌词感知分类阈值(见文件头坑位 6/7)---- + # lyric_hit >= 0.34 -> 真唱;instr_hit >= 0.40 -> 生成指令泄漏。 + # 这两个数是在 8 首已认可老歌 + 本批 8 首上交叉验证出来的: + # 真唱段落在 0.30~1.00,幻觉段与泄漏段在 0.00~0.10,中间没有重叠。 + lyric_hit=0.34, + instr_hit=0.40, +) + +_S = {} # 懒加载单例,避免每首歌重复加载模型 +_EMB_CACHE = {} # 声线 embedding 进程内缓存,跨首复用 + + +# ------------------------------------------------------------------ 基础 + +def _load_audio(path, sr=None): + """读音频 -> (float32 mono ndarray, sr, channels)。sf 读不了才退回 librosa。""" + import soundfile as sf + try: + x, s = sf.read(path, always_2d=True) + except Exception: + import librosa + x, s = librosa.load(path, sr=None, mono=False) + x = np.asarray(x, dtype=np.float32) + if x.ndim == 1: + x = x[:, None] + y = x.mean(axis=1) + return y, int(s), x.shape[1] + + +def _rms(y): + if y.size == 0: + return 0.0 + return float(np.sqrt(np.mean(np.square(y)) + 1e-20)) + + +def _blocks(y, sr, sec): + """切成定长块,最后不足半块丢弃。""" + n = int(sr * sec) + if n <= 0 or y.size < n: + return [] + k = y.size // n + return [y[i * n:(i + 1) * n] for i in range(k)] + + +def _down(y, sr, target=16000): + import librosa + if sr == target: + return y, sr + return librosa.resample(y, orig_sr=sr, target_sr=target), target + + +# ------------------------------------------------------------------ 人声分离 + +def _demucs(): + if "demucs" not in _S: + import torch + from demucs.apply import apply_model + from demucs.pretrained import get_model + bag = get_model("htdemucs") + # get_model 返回 BagOfModels,必须取子模型才有 samplerate/sources/segment + sub = bag.models[0] if hasattr(bag, "models") else bag + sub.to("cuda").eval() + _S["demucs"] = (torch, apply_model, sub) + return _S["demucs"] + + +def vocals_of(path, cache_dir=None): + """分离人声轨。返回 (vocals_mono_44k, sr)。大文件结果缓存到 npy。 + + 注意:这条轨只用于"声线 embedding"和"有声帧挑选", + 绝不用于判断开头有没有人声——它有器乐泄漏,会把器乐前奏误判成演唱。 + """ + if cache_dir: + os.makedirs(cache_dir, exist_ok=True) + cp = os.path.join(cache_dir, os.path.basename(path) + ".voc44.npy") + if os.path.isfile(cp): + return np.load(cp), 44100 + + torch, apply_model, sub = _demucs() + import torchaudio + x, sr, _ch = _load_audio(path) + wav = torch.from_numpy(x.copy()).float()[None] # (1, T) + if sub.samplerate != sr: + # 必须用 torchaudio.functional.resample。torch.nn.functional 没有 resample, + # 用 interpolate 变调不留情,会把 F0 和时长一起改掉,后面所有指标都失真。 + wav = torchaudio.functional.resample( + wav, float(sr), float(sub.samplerate)) + # demucs 要 (batch, channels, time),单声道补成双声道再取回单声道 + st = wav.repeat(1, 2, 1) + with torch.no_grad(): + out = apply_model(sub, st, device="cuda", split=True, overlap=0.25, + segment=7.8, shifts=0, progress=False)[0] + vi = list(sub.sources).index("vocals") + v = out[vi].mean(dim=0).cpu().numpy().astype(np.float32) + v = np.nan_to_num(v) + if cache_dir: + np.save(cp, v) + return v, 44100 + + +# ------------------------------------------------------------------ 声线 + +def _campplus(): + """CAMPPlus 声线 embedding 器。取自 ComfyUI 自带的 Seed-VC。""" + if "campplus" in _S: + return _S["campplus"] + import torch + root = os.environ.get("SONGFORGE_COMFY_ROOT") or os.environ.get( + "COMFY_ROOT") or r"D:\CodeFiles\Thinking\ComfyUI-MiniMaxH3" + for p in (root, os.path.dirname(os.path.abspath(__file__))): + sys.path.insert(0, p) + sys.path.insert(0, os.path.join(p, "custom_nodes", + "ComfyUI_Seed-VC")) + break + import folder_paths + from seed_vc.modules.campplus.DTDNN import CAMPPlus + bin_path = os.path.join(folder_paths.models_dir, "TTS", "Seed-VC", + "campplus_cn_common.bin") + cm = CAMPPlus(feat_dim=80, embedding_size=192) + cm.load_state_dict(torch.load(bin_path, map_location="cpu"), strict=False) + cm.eval().to("cuda") + _S["campplus"] = (torch, cm) + return _S["campplus"] + + +def embed(sig, rate): + """任意采样率的单声道信号 -> 归一化 192 维声线 embedding。""" + import torch + import torchaudio + torch, cm = _campplus() + t = torch.tensor(np.asarray(sig, dtype=np.float32)).reshape(-1) + if rate != 16000: + t = torchaudio.functional.resample(t[None], float(rate), 16000)[0] + if t.numel() < 400: + return torch.zeros(192) + f = torchaudio.compliance.kaldi.fbank(t[None], num_mel_bins=80, + dither=0, sample_frequency=16000) + f = f - f.mean(0, keepdim=True) + with torch.no_grad(): + e = cm(f[None].to("cuda")) + return torch.nn.functional.normalize(e, dim=-1)[0].detach().cpu() + + +def cos(a, b): + import torch + na = float(torch.linalg.norm(a)) + nb = float(torch.linalg.norm(b)) + if na < 1e-8 or nb < 1e-8: + return 0.0 + return float(torch.dot(a.reshape(-1), b.reshape(-1)) / (na * nb)) + + +# ------------------------------------------------------------------ ASR + +_CJK = None + +# Whisper 会自动检测语种并把英文转写出来,但下面几个指标历史上全是按 +# 汉字设计的,英文歌会整批归零。实测症状: +# language 固定 "zh" -> 英文歌被强行按中文识别,召回恒为 0 +# cjk() 只留 U+4E00~U+9FFF -> 英文实义词一个都留不下 +# sung_segs() 固定 4 个汉字 -> "Bounce with me" 永远凑不够,进声点 None +# 所以抽词要按语种走:中文取汉字,英文取单词。 +# +# 注意两个口径必须分开,不能共用一张停用词表: +# real_words() 歌词召回用,数字词 one/four 必须留下 +# (《HitItOnOne》整首 hook 就是 "Hit it on one") +# content_words() 判人声进入点用,再滤掉 mm/yeah/okay 这类哼唱衬词 + + +# 简繁归一表。 +# +# 不引 opencc / zhconv:ComfyUI venv 里没有,装一个几十 MB 的包只为了 +# 折两三百个常用字不划算。而且两侧都过同一张表,所以不需要完整映射—— +# 只有"歌词里可能写、Whisper 也可能吐"的那批繁体字需要登记。 +# 没登记的繁体字原样保留,两侧仍然各自一致,不影响比对。 +# +# 为什么需要:Whisper 的 zh 输出不保证全简体。同一句可能一侧写"节奏"、 +# 另一侧写"節奏",不归一就永远对不上,歌词召回被凭空压低几个点。 +_ZH_T2S = { + "們": "们", "個": "个", "來": "来", "時": "时", "對": "对", "過": "过", + "說": "说", "這": "这", "裡": "里", "後": "后", "會": "会", "學": "学", + "動": "动", "樂": "乐", "響": "响", "點": "点", "頭": "头", "問": "问", + "沒": "没", "還": "还", "讓": "让", "從": "从", "樣": "样", "開": "开", + "關": "关", "當": "当", "兩": "两", "臺": "台", "誰": "谁", "麼": "么", + "為": "为", "無": "无", "愛": "爱", "親": "亲", "覺": "觉", "聽": "听", + "見": "见", "邊": "边", "遠": "远", "風": "风", "飛": "飞", "飯": "饭", + "燈": "灯", "舊": "旧", "膠": "胶", "體": "体", "長": "长", "節": "节", + "聲": "声", "調": "调", "輕": "轻", "彈": "弹", "雙": "双", "腳": "脚", + "擺": "摆", "靜": "静", "間": "间", "線": "线", "順": "顺", "鬆": "松", + "緊": "紧", "穩": "稳", "陽": "阳", "夢": "梦", "記": "记", "憶": "忆", + "橋": "桥", "樓": "楼", "鏡": "镜", "髮": "发", "臉": "脸", "睛": "睛", + "進": "进", "詞": "词", "話": "话", "給": "给", "帶": "带", "牽": "牵", + "掛": "挂", "齊": "齐", "備": "备", "認": "认", "結": "结", "剛": "刚", + "際": "际", "衝": "冲", "擊": "击", "隨": "随", "盪": "荡", "搖": "摇", + "數": "数", "兒": "儿", "勁": "劲", "帥": "帅", "斃": "毙", "錯": "错", + "係": "系", "領": "领", "寶": "宝", "貝": "贝", "歲": "岁", "嗎": "吗", + "喲": "哟", "唷": "哟", "噠": "哒", "嘩": "哗", "噓": "嘘", "噢": "哦", + "嘍": "喽", "唄": "呗", "嘸": "呒", "誰": "谁", "壞": "坏", "傷": "伤", + "熱": "热", "涼": "凉", "涼": "凉", "願": "愿", "驚": "惊", "險": "险", + "離": "离", "電": "电", "腦": "脑", "機": "机", "車": "车", "轉": "转", + "運": "运", "遊": "游", "過": "过", "還": "还", "邊": "边", "層": "层", + "屬": "属", "歲": "岁", "組": "组", "網": "网", "總": "总", "係": "系", + "積": "积", "極": "极", "業": "业", "參": "参", "雙": "双", "發": "发", + "變": "变", "葉": "叶", "號": "号", "嘆": "叹", "嚴": "严", "華": "华", + "處": "处", "備": "备", "復": "复", "夠": "够", "夠": "够", "捨": "舍", + "捨": "舍", "鬧": "闹", "齊": "齐", "讀": "读", "變": "变", "讓": "让", + "讚": "赞", "贊": "赞", "贈": "赠", "贏": "赢", "賽": "赛", "踐": "践", +} + + +def _norm_zh(text): + """繁体 -> 简体。只走常用字表,覆盖歌词和 Whisper 实际会出现的范围。""" + return "".join(_ZH_T2S.get(c, c) for c in text) + + +def cjk(text): + """只保留汉字。Whisper 会在汉字之间塞标点/空格,比对前必须洗掉。 + + 顺带做简繁归一,见 _ZH_T2S 的说明。 + """ + return _norm_zh("".join(c for c in text if "\u4e00" <= c <= "\u9fff")) + + +def _norm_en(text): + """英文归一化:数字统一成单词。 + + Whisper 常把 "one two three" 写成 "1 2 3",歌词原文是英文单词。 + 不归一化的话这两边永远对不上,召回会被凭空压低。 + + 坑位:词表里虽然写了 ten,但下面用 enumerate(digits) 生成替换表, + 只覆盖下标 0~9,ten 根本没有被处理。"10" 在英文歌里不多见, + 但漏掉就是漏掉——这里显式多处理一位。 + """ + import re + digits = ("zero one two three four five six seven eight nine ten").split() + low = text.lower() + for i, w in enumerate(digits): + low = re.sub(r"\b%d\b" % i, " " + w + " ", low) + low = re.sub(r"\b10\b", " ten ", low) + return low + + + +def _en_words(text): + """英文取小写单词,丢掉标点,数字先归一化成单词。""" + import re + return re.findall(r"[a-z']+", _norm_en(text)) + + +def tokens(text, lang="zh"): + """按语种抽出可比对的实义单元。 + + zh -> 汉字(历史口径,保持不变) + en -> 小写单词 + + 分语种的原因不是"照顾英文",而是中文那套汉字口径对英文恒为 0: + cjk() 过滤后英文串必然为空,coverage 分母变 0、分子变 0。 + """ + if _is_en(lang): + return _en_words(text) + return list(cjk(text)) + + +def _is_en(lang): + return str(lang or "zh").lower().startswith("en") + + +# 英文里 Whisper 常见的功能词,不承载歌词内容。 +# +# 关键:数字词 one~ten 刻意不放进来。它们在这批英文歌里是动作词本身—— +# 《HitItOnOne》整首 hook 就是 "Hit it on one / drop on four", +# 把 one/four 当停用词等于把主打句从召回分母里删掉。 +EN_STOP = frozenset(""" +a an the and or but if then than that this these those is are was were be been +being am do does did doing done have has had having i you he she it we they me +him her us them my your his its our their of in on at to for with from by as +into over under again very so no yes +""".split()) + +# 只有"判定人声进入点"时才额外剔掉的哼唱/附和词。 +# 口径必须和歌词召回分开:mm / yeah / okay 这类在歌里是 legit 的 +# 律动衬词,但绝不能算成"真的唱出内容了"。 +EN_FILLER = frozenset(""" +oh ah uh um mm hmm mhm okay ok yeah yep +""".split()) + + +def _whisper(): + if "whisper" not in _S: + import whisper + _S["whisper"] = whisper.load_model("small", device="cuda") + return _S["whisper"] + + +def asr(wav, sr, lang="zh"): + """对整首歌做 ASR,返回 whisper 的完整 result(含分段时间戳)。 + + 坑位记录:不要把音频写成临时 wav 再让 whisper 读。whisper 内部走 ffmpeg + 子进程,遇到它不认识的封装/采样格式就直接 + Error opening input: Invalid data found when processing input + 然后返回空结果——不报错,静默变成"歌词召回 0%",比没有 QC 更可怕。 + 直接传 numpy 数组,whisper 会走 soundfile/内部解码,绕开 ffmpeg。 + + lang 不能硬编码 "zh":那样 Whisper 会把英文歌强行按中文解码, + 输出整片音译汉字或空串。英文歌必须传 "en"。 + """ + wm = _whisper() + w = np.asarray(wav, dtype=np.float32) + if w.ndim > 1: + w = w.mean(axis=1) + if sr != 16000: + w, _ = _down(w, sr, 16000) + if w.size < 16000: + return dict(text="", segments=[]) + r = wm.transcribe(w, language=lang, fp16=True, beam_size=1, + temperature=0.0, condition_on_previous_text=False) + segs = [] + for s in r.get("segments", []) or []: + t = (s.get("text") or "").strip() + n = len(tokens(t, lang)) + if n: + segs.append(dict(t0=float(s.get("start", 0.0)), + t1=float(s.get("end", 0.0)), + text=t, chars=n, + # 置信度字段保留在结果里供排查,但**不再作为 + # 分类主判据**:实测泄漏段与真唱段的 + # no_speech_prob 相同或更高,分不开。 + # 现在的判据是 classify_segs() 里的歌词正文 + # 重合 + 实证泄漏短语。 + nsp=float(s.get("no_speech_prob", -1.0)), + logp=float(s.get("avg_logprob", 0.0)))) + return dict(text=(r.get("text") or "").strip(), segments=segs) + + +# 语气词/无意义哼唱。Whisper 对纯伴奏段落会吐出成百上千个"嗯""啊", +# 实测 pop 前奏被吐 112 个"嗯"、hiphop 前奏 223 个"啊",去重后只有 1 个 +# 字符。这些既不能算歌词,也不能算人声进入点。 +FILLER = set("嗯啊哦噢喔呃哎唉嘿咦哟唔唷哼嘻嘿") + + +def real_cjk(text): + """去掉语气词后的实义汉字。""" + return "".join(c for c in cjk(text) if c not in FILLER) + + +def _strip_tags(text): + """去掉结构标签方括号里的内容([chorus] / [Instrumental: ...] / [intro])。 + + 中文口径不需要这一步:cjk() 只保留 U+4E00~U+9FFF,ASCII 标签 + 天然被扔掉。英文口径是"取单词",标签里的英文会整个活下来—— + 实测 [chorus] 把 chorus、intro 这些根本不会唱出来的词塞进分母, + 召回率被凭空压低一截。 + 与 lyrics/meter.py 的 only_lyrics() 同一口径:结构标签不计入正文。 + """ + import re + return re.sub(r"\[[^\]]*\]", " ", text) + + +def real_words(text, lang="zh"): + """按语种抽实义单元:中文去语气词,英文去功能词和结构标签。 + + 这是"歌词召回"口径:one/four 这类动作词要留下,因为它们是歌词内容。 + 判人声进入点请用 content_words(),那份会再滤掉哼唱衬词。 + """ + if _is_en(lang): + return [w for w in _en_words(_strip_tags(text)) + if w not in EN_STOP] + return list(real_cjk(text)) + + +def content_words(text, lang="zh"): + """判定"真的唱出内容了"的严格口径:在 real_words 基础上再滤哼唱词。""" + if _is_en(lang): + return [w for w in real_words(text, lang) if w not in EN_FILLER] + return list(real_cjk(text)) + + +# 一段要算"真的唱出字",至少要几个实义单元。 +# +# 中文 4 个汉字 ≈ 4 音节。英文一个词约 1.8~2 音节(见 lyrics/meter.py +# 里 en * 2 的算法),所以英文门槛必须比 4 低,否则 +# "Bounce, bounce, bounce with me" 去功能词后只剩 3 个 +# 会被判成"没在唱",人声进入点直接变 None。 +# 用 3 个实词 ≈ 5~6 音节,和中文 4 字口径相当。 +MIN_UNITS = {"zh": 4, "en": 3} + + +def min_units(lang): + return MIN_UNITS.get("en" if _is_en(lang) else "zh", 4) + + +# ------------------------------------------------------------------ 歌词感知分类 +# +# 这一段是本轮最关键的修正。原来的 sung_segs() 只看"实义单元数 >= 阈值", +# 在纯器乐前奏上 Whisper 会幻觉出成片实义词,于是已认可老歌也被判成 +# onset=0.0s,看起来像全部 8 首都违规,实际上是指标失效。 +# +# 正确的判据要把一段 ASR 同时放到两个集合里比: +# +# lyric_hit = 该段实义词落在**歌词正文**里的比例 -> 是不是真的在唱 +# instr_hit = 该段实义词落在**真实生成链路 [Instrumental: ...] 指令** +# 里的比例 -> 是不是把指令唱出来了 +# +# 两种错误必须分开: +# lyric_hit 低 + instr_hit 低 -> 纯器乐幻觉,无害,不参与任何判定 +# lyric_hit 低 + instr_hit 高 -> 生成指令泄漏,必须重生成 +# lyric_hit 高 -> 真唱,拿它算 onset / F0 / head count +# +# 指令集合必须走 force_intro_bars -> filter_intro_bracket 复现真实链路, +# 拿歌词文件里的手写括号当对照是错的:ironlaw 会自动补 kick/bass 兜底和 +# every-beat 层,这些词根本不在作者手写文件里。Bounce 的泄漏正是如此。 + + +def _lyric_pool(lyrics, lang): + """歌词正文集合(去掉结构标签)。""" + return set(content_words(_strip_tags(lyrics or ""), lang)) + + +def _ironlaw(): + """拿到 songforge.rules.ironlaw 模块。worker 是被当脚本直接跑的, + 没有包上下文,所以只能按文件位置把 workflows 目录塞进 sys.path。 + + 找不到就返回 None —— 判定逻辑会退回到"只看歌词重合", + 不会因为拿不到 ironlaw 就整首歌崩掉。 + """ + global _IRONLAW + if _IRONLAW is None: + try: + wf = os.path.dirname(os.path.dirname( + os.path.dirname(os.path.abspath(__file__)))) + if wf not in sys.path: + sys.path.insert(0, wf) + from songforge.rules import ironlaw as _il + _IRONLAW = _il + except Exception: + _IRONLAW = False + return _IRONLAW or None + + +_IRONLAW = None + + +def _instr_pool(lyrics, bars=8): + """真实生成链路最终送进模型的前奏指令词集合。 + + 走真实链路而不是读原始括号,原因见上面的说明。 + 指令是英文自然语言,所以固定用英文词表比对,中文歌也一样。 + """ + il = _ironlaw() + if il is None or not lyrics: + return set() + try: + fixed = il.force_intro_bars(lyrics, bars) + out = il.filter_intro_bracket(fixed) + except Exception: + return set() + import re as _re + m = _re.search(r"^\[Instrumental:(.*)\]$", out, _re.I | _re.M) + if not m: + return set() + return set(_en_words(m.group(1))) + + +def _hit_ratio(text, pool, lang): + """该段实义词落在 pool 里的比例。段太短或 pool 空时返回 0。 + + 下限 3 个实义单元:Whisper 常切出 1~2 词的碎片段, + 这种段的命中比例噪声极大,"the" 落在歌词里就能算出 100%, + 拿它判真唱或泄漏都是自欺。 + """ + a = content_words(text, lang) + if len(a) < 3 or not pool: + return 0.0 + return sum(1 for w in a if w in pool) / float(len(a)) + + +def _phrase_leak(text): + """ASR 段是否直接复述了实证泄漏过的生成指令措辞。 + + 为什么集合重合不够:ironlaw 修好之后,"no fading in, no swelling in" + 这类句子已经不会再进歌词括号,于是 ipool 里不再有 fading / swelling / + quiet 这些词。拿修好后的链路当对照,去查修复前生成的旧候选就会 + 全部漏判——ipool 越干净,历史泄漏越查不出来。 + + 所以这里复用 ironlaw 自己那份实证短语正则:只要 ASR 里出现 + "front and center"、"at full level"、"quiet or sparse" 这类 + 从成品里逐词抄回来的措辞,就直接判泄漏,不依赖 ipool。 + """ + il = _ironlaw() + rx = getattr(il, "_INTRO_LEAK_PHRASE_RE", None) if il else None + if rx is None or not text: + return "" + try: + hits = rx.findall(text) + except Exception: + return "" + if not hits: + return "" + return ", ".join(sorted(set(str(h).lower() for h in hits))) + + +def classify_segs(segs, lyrics="", lang="zh", bars=8): + """给每个 ASR 段打上类别,并返回可直接用的三组。 + + 返回 dict: + segs 原段,每段多出 role / lyric_hit / instr_hit + sung 真唱段(lyric_hit >= HARD["lyric_hit"]) + leaks 指令泄漏段(instr_hit >= HARD["instr_hit"] 且不是真唱) + halluc 纯幻觉段(两个都不高) + """ + lpool = _lyric_pool(lyrics, lang) if lyrics else set() + ipool = _instr_pool(lyrics, bars) if lyrics else set() + sung, leaks, halluc = [], [], [] + for s in segs or []: + t = s.get("text", "") + # 中文歌的前奏指令是英文,但 Whisper 可能把英文唱成音译汉字; + # 中文段仍按中文比对歌词,同时按英文比对指令,两个口径互不干扰。 + rl = _hit_ratio(t, lpool, lang) + ri = _hit_ratio(t, ipool, "en") + phrase = _phrase_leak(t) + s["lyric_hit"] = round(rl, 3) + s["instr_hit"] = round(ri, 3) + s["leak_phrase"] = phrase + if phrase: + # 实证措辞优先:这段直接把生成指令唱出来了, + # 哪怕它碰巧也命中了几个歌词词,也不能算真唱。 + role = "leak" + elif not lpool and not ipool: + # 没歌词可比(--file 模式常见):退回到旧口径, + # 只按实义单元数判。宁可宽松也不要凭空虚报幻觉。 + role = "sung" if len(content_words(t, lang)) >= min_units(lang) \ + else "halluc" + elif rl >= HARD["lyric_hit"]: + role = "sung" + elif ri >= HARD["instr_hit"]: + role = "leak" + else: + role = "halluc" + s["role"] = role + {"sung": sung, "leak": leaks, "halluc": halluc}.get( + role, halluc).append(s) + return dict(segs=list(segs or []), sung=sung, leaks=leaks, + halluc=halluc, lpool=lpool, ipool=ipool) + + +def sung_segs(segs, min_chars=None, lang="zh", lyrics=None, bars=8): + """只保留"真的唱出字"的段。 + + 传了 lyrics 就走歌词感知口径(推荐);没传则退回旧的实义单元数口径, + 保持对既有调用方的兼容。 + """ + if lyrics is not None: + return classify_segs(segs, lyrics, lang, bars)["sung"] + if min_chars is None: + min_chars = min_units(lang) + out = [] + for s in segs or []: + if len(content_words(s.get("text", ""), lang)) >= min_chars: + out.append(s) + return out + + +def vocal_onset(segs, min_chars=None, lang="zh", lyrics=None, bars=8): + """人声进入点:第一个"真的唱出字"的段起点。 + + 为什么不用能量:Whisper 会在纯伴奏段落输出空或单字噪声, + 要求该段实义单元数 >= 阈值才算真唱,配合时间戳就能定位演唱起点。 + + 坑位(已修):光按实义单元数会中 Whisper 幻觉的招——器乐前奏上 + 它能吐出 "L-E-D-O-N-O" 这种成片实义词,把 onset 压到 0.0s。 + 已认可老歌也被压到 0.0s,说明是指标失效而不是歌的问题。 + 所以 lyrics 存在时必须走 classify_segs() 的歌词正文重合判据。 + + 返回 None 表示整首歌没识别出真唱(歌词完整度会一并判 0)。 + """ + for s in sung_segs(segs, min_chars, lang, lyrics=lyrics, bars=bars): + return round(s["t0"], 2) + return None + + +def _collapse_runs(s, max_run=3): + """把同一字符的连续重复压到最多 max_run 次。 + + 坑位:Whisper 在吟唱/哨音段会吐出上百个"呜"。SequenceMatcher 是 + 按顺序找最长公共子串,一段上百字的噪声会把歌词切成两半对齐, + 召回率被凭空压低十几个点。这里先把复读折叠掉。 + """ + out = [] + run = 1 + for i, ch in enumerate(s): + if i and ch == s[i - 1]: + run += 1 + if run > max_run: + continue + else: + run = 1 + out.append(ch) + return "".join(out) + + +def coverage(asr_text, lyrics, lang="zh"): + """ASR 召回率:ASR 里命中的歌词单元 / 歌词正文单元。 + + 为什么用召回而不是相似度:Whisper 对中文演唱常输出"嗯嗯嗯"这类无意义 + 重复,也会把长句压短。相似度会被这些噪声压低,而召回只看"歌词到底唱出来 + 没有",对噪声不敏感。配合老歌基线一起看就稳。 + + 坑位:ASR 文本要先去掉语气词和复读再对齐,否则器乐前奏和吟唱段的 + 幻觉字会切断歌词匹配,实测让召回率虚低。 + + 英文必须按词计(lang="en"):中文那套 cjk()+SequenceMatcher 对英文 + 恒为 0——cjk() 过滤后两边都是空串,分子分母全灭。词级召回同样直接, + 只是分母从"字数"变成"词数",阈值含义不变。 + """ + import difflib + if _is_en(lang): + a = real_words(asr_text, lang) + e = real_words(lyrics, lang) + if not e or not a: + return 0.0 + sm = difflib.SequenceMatcher(None, e, a, autojunk=False) + hit = sum(b.size for b in sm.get_matching_blocks()) + return hit / float(len(e)) + + a = _collapse_runs(real_cjk(asr_text)) + e = cjk(lyrics) + if not e or not a: + return 0.0 + sm = difflib.SequenceMatcher(None, e, a, autojunk=False) + hit = sum(b.size for b in sm.get_matching_blocks()) + return hit / float(len(e)) + + +# ------------------------------------------------------------------ 单曲测量 + +def _f0_voiced(v16, vsr16, floor_db=-34.0): + """只在有声帧上算 F0。 + + 坑位一:直接对整轨 yin,无词段/器乐残留会给出乱七八糟的音高, + 30s 块中位数漂移能到 75%,全是噪声。这里先用短时能量门限把 + 真正在发声的地方挑出来,再交给 yin。 + + 坑位二:门限不能写成 max(floor_db, percentile(db, 60))。percentile + 是按秩取值的,keep 恒等于 1-0.60 = 40% 的帧——它不是能量门,而 + 是一个固定百分位选择器。实测三首新歌的有声帧占比精确等于 40.0%, + 41% 的无人声尾奏照样被标成"发声",yin 在上面测出 368/414 Hz 的 + 假音高,一首歌的 F0 漂移被撑到 9.6 半音。必须用绝对 dB 门限。 + """ + import librosa + if v16.size <= 2048: + return np.array([0.0]), np.array([0.0]) + hop = 512 + rms = librosa.feature.rms(y=v16, frame_length=2048, hop_length=hop)[0] + if rms.size == 0: + return np.array([0.0]), np.array([0.0]) + db = 20 * np.log10(np.maximum(rms, 1e-9)) + # 绝对门限,固定 -34dBFS;再放宽到峰值下方 floor_db,两者取高。 + abs_gate = db > floor_db + peak_gate = db > float(db.max()) + floor_db + keep = abs_gate | peak_gate + if keep.mean() > 0.92: + # 整首几乎都在响(老歌混音偏响),门限失去区分力,退回软门限但 + # 仍按能量排序取绝对上限,绝不回到固定 40%。 + keep = db > np.percentile(db, 8) + f0 = librosa.yin(v16, fmin=HARD["f0"][0], fmax=HARD["f0"][1], + sr=vsr16, frame_length=2048, hop_length=hop) + n = min(f0.size, keep.size) + return f0[:n], keep[:n] + + +def measure(path, expect=None, cache_dir=None, with_asr=True): + """测一首。expect 里可带 duration/bpm/intro_bars/lyrics/title/group。 + + lang 也从 expect 里取("zh" / "en"),决定 ASR 语种和歌词召回口径。 + """ + import librosa + expect = expect or {} + lang = expect.get("lang") or expect.get("language") or "zh" + y, sr, ch = _load_audio(path) + dur = len(y) / float(sr) + + peak = float(np.max(np.abs(y))) if y.size else 0.0 + bs = 3 * sr + n = max(1, y.size // bs) + rmss = [_rms(y[i * bs:(i + 1) * bs]) for i in range(n)] + lufs = 20 * np.log10(max(float(np.mean(rmss)), 1e-9)) - 0.691 + dyn = 20 * np.log10(max(max(rmss), 1e-9) / max(min(rmss), 1e-9)) + silence = float(np.mean(np.abs(y) < 0.01)) + + y16, sr16 = _down(y, sr) + centroid = float(np.mean(librosa.feature.spectral_centroid(y=y16, sr=sr16))) + rolloff = float(np.mean(librosa.feature.spectral_rolloff( + y=y16, sr=sr16, roll_percent=0.85))) + + # ---- 速度:从混音轨算,且做八度折叠 + # 生成链路没有 time-stretch,混音 tempo 应当全歌恒定。 + # 折叠是必须的:librosa 在慢速律动上会锁到二倍拍, + # 不折叠的话设定 86 BPM 会被报成 170 BPM,直接判死。 + tempo_med, tempo_drift = qc_style.tempo_of( + y, sr, expect_bpm=expect.get("bpm")) + tempo_raw = 0.0 + if not expect.get("bpm"): + tempo_raw = tempo_med + + # ---- 人声轨:只用来做声线 embedding 和挑有声帧 + v, vsr = vocals_of(path, cache_dir) + v16, vsr16 = _down(v, vsr) + + # ---- 人声进入点:靠 ASR 时间戳,不靠能量(见文件头坑位 1) + lyr = expect.get("lyrics", "") + bars = int(expect.get("intro_bars") or 8) + txt, segs = ("", []) + if with_asr: + a = asr(v, vsr, lang) + txt, segs = a["text"], a["segments"] + # 歌词感知分类:真唱 / 指令泄漏 / 纯幻觉,三者必须分开。 + # 后面所有依赖"人在哪、唱了什么"的指标都只用真唱段。 + cls = classify_segs(segs, lyr, lang, bars) if with_asr else \ + dict(segs=[], sung=[], leaks=[], halluc=[]) + trusted = cls["sung"] + onset = vocal_onset(segs, lang=lang, lyrics=lyr if with_asr else None, + bars=bars) + # 前 2s / 8s 内是否有真歌词(铁律:禁止纯人声/spoken 开头) + # 坑位:只数真唱段里的实义单元。Whisper 在器乐前奏上会幻觉出上百个 + # "嗯",甚至 "L-E-D-O-N-O" 这种成片实义词;不筛就会把干净前奏判成 + # "开头 112 字",逼你重生成一首没问题的歌。 + head_chars_2 = sum(len(content_words(s["text"], lang)) + for s in trusted if s["t0"] < 2.0) + head_chars_8 = sum(len(content_words(s["text"], lang)) + for s in trusted if s["t0"] < 8.0) + # 泄漏段单列。它不是幻觉,也不是歌词,必须独立报出来: + # 这是"铁律被破坏"的证据,只能靠重生成修,QC 口径改不动。 + leak_segs = [dict(t0=round(s["t0"], 2), t1=round(s["t1"], 2), + instr_hit=s.get("instr_hit"), + text=s["text"][:80].replace("\n", " ")) + for s in cls["leaks"]] + + # ---- 音高稳定性:只在真唱段上测,且只用段内音高稳定的帧 + # 坑位一:不能按 30s 整块取中位。块里必然混进器乐间奏和吟唱段, + # 实测 pop 的 115~145s 是 50 个"呜"的吟唱,yin 在上面给出 437Hz, + # 单这一块就把全歌 F0 漂移从 1.4 半音撑到 9.6 半音。 + # 坑位二:漂移用 MAD 而不是 P90-P10。吟唱/换声是少数段,极差法 + # 对它们最敏感;MAD 只反映"整体音高是否偏移",正好对上 + # "声线、速度、调性中途不变化"这条铁律。 + f0, keep = _f0_voiced(v16, vsr16) + f0v = f0[keep] if f0.size == keep.size else f0 + f0v = f0v[np.isfinite(f0v) & (f0v > 0)] + hop = 512 + seg_med = [] + seg_f0 = [] + for s in trusted: + i0 = max(0, int(s["t0"] * vsr16 / hop)) + i1 = min(len(f0), int(s["t1"] * vsr16 / hop)) + if i1 - i0 < 8: + continue + sv = f0[i0:i1] + sv = sv[np.isfinite(sv) & (sv > 0)] + if sv.size < 8: + continue + # 段内音高跨度超过 6 个半音,说明这段是吟唱/滑音/残留,不是 + # 一个稳定的 singing 音高,跳过。 + inner = float(np.percentile(sv, 90) - np.percentile(sv, 10)) + if inner >= 60.0: + continue + seg_med.append(float(np.median(sv))) + seg_f0.append(sv) + if len(seg_med) >= 3: + arr = np.array(seg_med) + base = max(float(np.median(arr)), 1e-9) + st = 12.0 * np.log2(arr / base) + f0_drift_st = float(np.median(np.abs(st))) + else: + # 拿不到 3 个稳定段就退回 0 并标记不可信,由judge 决定要不要卡。 + f0_drift_st = 0.0 + + # 音高范围必须和漂移用同一批帧(真唱稳定段),不能用全曲有声帧。 + # 坑位:全曲口径下 yin 会在 demucs 残留上撞自己的 fmax 上限。实测 + # 朋克有 4.49% 的帧报 640Hz(fmax 就是 620,报 640 是饱和不是音高), + # 而老歌"跳起来"同样出现 640,只是 p2 恰好落在界内才没被卡—— + # 说明这是口径缺陷不是朋克的毛病。只用真唱段后朋克 p98 从 640 降到 343。 + if seg_f0: + f0v = np.concatenate(seg_f0) + + # ---- 声线自相似:前半 vs 后半,直接抓"中途换声线" + half = v.size // 2 + e_all = embed(v, vsr) + e_self = cos(embed(v[:half], vsr), embed(v[half:], vsr)) \ + if half > vsr else 0.0 + + cov = coverage(txt, lyr, lang) if with_asr else 0.0 + + # ---- 风格分类:只看混音轨,不掺 demucs(见 qc_style 文件头坑位 4) + svec, sraw = qc_style.features(path, expect_bpm=expect.get("bpm")) + + res = dict( + file=os.path.basename(path), path=os.path.abspath(path), + title=expect.get("title") or os.path.basename(path), + group=expect.get("group", "new"), + style=expect.get("style", ""), + lang=lang, + duration=round(dur, 2), sample_rate=sr, channels=ch, + peak=round(peak, 4), lufs=round(lufs, 2), dynamic_db=round(dyn, 2), + silence_ratio=round(silence, 4), + centroid_hz=round(centroid, 1), rolloff_hz=round(rolloff, 1), + tempo_med=round(tempo_med, 1), tempo_drift=round(tempo_drift, 4), + vocal_onset=onset, + head_chars_0_2=head_chars_2, head_chars_0_8=head_chars_8, + sung_segs=len(trusted), leak_segs=len(leak_segs), + halluc_segs=len(cls["halluc"]), + leaks=leak_segs[:6], + leak_first=leak_segs[0] if leak_segs else None, + f0_p2=round(float(np.percentile(f0v, 2)), 1) if f0v.size else 0.0, + f0_med=round(float(np.median(f0v)), 1) if f0v.size else 0.0, + f0_p98=round(float(np.percentile(f0v, 98)), 1) if f0v.size else 0.0, + f0_drift_st=round(f0_drift_st, 2), + asr_chars=len(tokens(txt, lang)), asr_segs=len(segs), + coverage=round(cov, 4), asr_head=txt[:120], + voice_self=round(float(e_self), 4), + flatness=round(sraw.get("flatness", 0.0), 4), + expect=dict(expect), + _emb=e_all, _svec=svec, + ) + return res + + +# ------------------------------------------------------------------ 判定 + +def _pct(seq, q): + return float(np.percentile(seq, q)) if len(seq) else 0.0 + + +def _med(seq): + return float(np.median(seq)) if len(seq) else 0.0 + + +def derive_baseline(old): + """从老歌派生软阈值。老歌是"用户已认可"的,所以它们必须整体通过。""" + return dict( + n_old=len(old), + coverage=round(_med([r["coverage"] for r in old]), 4), + voice_self=round(_med([r.get("voice_self", 0.0) for r in old]), 4), + # 声线地板:老歌留一最近邻的 P10。新歌至少要等于这个数。 + voice_floor=round(_pct([r.get("voice_nn_sim", 0.0) for r in old], 10), 4) + if any(r.get("voice_nn_sim") for r in old) else 0.0, + dynamic_db=round(_med([r["dynamic_db"] for r in old]), 2), + centroid=round(_med([r["centroid_hz"] for r in old]), 1), + f0_drift_st=round(_pct([r["f0_drift_st"] for r in old], 90), 2), + tempo_drift=round(_pct([r["tempo_drift"] for r in old], 90), 4), + ) + + +def derive_soft(old, base): + """放行线怎么来的(不要拍脑袋,也不要直接套老歌最小值): + + 歌词召回 >= 老歌中位 * 0.85 老歌本身是"唱出来了"的 + 声线最近邻 >= 老歌 P10 - 0.02 不比已认可的最差那首更远 + 声线自相似 >= 老歌 P10 - 0.02 前后半段不能换人 + 动态/质心 >= 老歌 P10 - 余量 偏离即风格跑偏 + F0 漂移 <= 老歌 P90 + 1 半音 中途变调的上限由已认可作品决定 + """ + nn = [r.get("voice_nn_sim", 0.0) for r in old] + has_nn = any(nn) + return dict( + coverage=round(base["coverage"] * 0.85, 4), + voice_nn=round(_pct(nn, 10) - 0.02, 4) if has_nn else 0.0, + voice_self=round(_pct([r.get("voice_self", 0.0) for r in old], 10) - 0.02, + 4), + dyn_lo=round(_pct([r["dynamic_db"] for r in old], 10) - 2.0, 2) + if old else 6.0, + centroid_lo=round(_pct([r["centroid_hz"] for r in old], 10) * 0.85, 1) + if old else 1800.0, + f0_drift_st_max=round(base["f0_drift_st"] + 1.0, 2), + tempo_drift_max=round(base["tempo_drift"] + 0.05, 4), + ) + + +def judge(rows, baseline): + old = [r for r in rows if r.get("group") == "old" and "error" not in r] + new = [r for r in rows if r.get("group") != "old" and "error" not in r] + + base = derive_baseline(old) + soft = derive_soft(old, base) + if baseline: + base.update(baseline.get("base", {})) + soft.update(baseline.get("soft", {})) + + verdicts = [] + for r in new: + if "error" in r: + verdicts.append(dict(file=r["file"], verdict="ERROR", + why=[r["error"]])) + continue + why = [] + e = r.get("expect", {}) + if e.get("duration"): + d = abs(r["duration"] - e["duration"]) / float(e["duration"]) + if d > HARD["dur_tol"]: + why.append("时长 %.1fs 与设定 %ds 偏差 %.1f%%(>5%%)" + % (r["duration"], e["duration"], d * 100)) + if r["sample_rate"] < HARD["sr_min"]: + why.append("采样率 %d < %d" % (r["sample_rate"], HARD["sr_min"])) + if r["peak"] > HARD["peak_max"] + 1e-4: + why.append("峰值 %.4f 削波" % r["peak"]) + if not (HARD["lufs"][0] <= r["lufs"] <= HARD["lufs"][1]): + why.append("响度 %.1f dB 超出 %.0f~%.0f" + % (r["lufs"], HARD["lufs"][0], HARD["lufs"][1])) + if r["silence_ratio"] > HARD["silence_max"]: + why.append("静音占比 %.1f%% 过高(>30%%)" % (r["silence_ratio"] * 100)) + if not (HARD["f0"][0] <= r["f0_p2"] and r["f0_p98"] <= HARD["f0"][1]): + why.append("音高范围 %.0f~%.0f Hz 越界" + % (r["f0_p2"], r["f0_p98"])) + + # ---- 铁律一:禁止纯人声 / spoken 开头(用 ASR 段,不可用能量) + if r["head_chars_0_2"] > 0: + unit = "词" if _is_en(r.get("lang", e.get("lang"))) else "汉字" + why.append("前 2s 就已唱出 %d 个%s,疑似纯人声/spoken 开头" + % (r["head_chars_0_2"], unit)) + # ---- 铁律二:8 小节器乐前奏(完整 8 小节,不是 55%) + # 原来判的是 vocal_onset < want * 0.55,等于"第 4~5 小节进 sang + # 也算过",把铁律降级成建议。现在按完整 8 小节判,只留 + # onset_grace 吸收 ASR 时间戳本身的分辨率误差。 + if e.get("bpm") and e.get("intro_bars"): + want = e["intro_bars"] * 60.0 / float(e["bpm"]) * 4 + gate = want * HARD["intro_onset_rel"] - HARD["onset_grace"] + if r["vocal_onset"] is None: + why.append("整首未识别出真唱(人声进入点 None)") + elif r["vocal_onset"] < gate: + why.append("人声在 %.1fs 就进,%d 小节前奏需 %.1fs" + % (r["vocal_onset"], e["intro_bars"], want)) + # ---- 铁律三:生成指令不能被唱出来 + # 这是 --lean-tags 之外的独立一条:instruction 是模型的歌词条件通道, + # 一旦自然语言要求句被当歌词唱出来,歌里就有几句没人写的词, + # QC 口径改不动,只能重生成。 + if r.get("leak_segs"): + lf = r.get("leak_first") or {} + # 泄漏有两类证据:实证短语命中(不依赖指令集合)和指令词集合重合。 + # 修复 ironlaw 之后集合里已经没有 fading / swelling 这类词了, + # 所以短语命中才是主判据,报告里必须把命中的原话打出来。 + hit = lf.get("leak_phrase") or "" + if not hit: + hit = "指令词命中 %.0f%%" % ( + (lf.get("instr_hit") or 0) * 100) + why.append("前奏唱出生成指令 %d 处(最早 %.1fs,%s):%s" + % (r["leak_segs"], lf.get("t0", 0), hit, + (lf.get("text") or "")[:40])) + # ---- 禁止中途变速 / 变调 / 换声线 + if r["tempo_drift"] > soft["tempo_drift_max"]: + why.append("速度中途漂移 %.1f%%(>%.1f%%),疑似变速" + % (r["tempo_drift"] * 100, + soft["tempo_drift_max"] * 100)) + if r["f0_drift_st"] > soft["f0_drift_st_max"]: + why.append("音高中途漂移 %.1f 半音(>%.1f),疑似变调" + % (r["f0_drift_st"], soft["f0_drift_st_max"])) + if r.get("voice_self", 0.0) < soft["voice_self"]: + why.append("前后半段声线自相似 %.3f 低于 %.3f,疑似中途换声线" + % (r.get("voice_self", 0.0), soft["voice_self"])) + # ---- 速度与设定 BPM 对齐(风格可听性的第一道物理保证) + if e.get("bpm") and r["tempo_med"] > 0: + rel = abs(r["tempo_med"] - e["bpm"]) / float(e["bpm"]) + if rel > HARD["bpm_rel_tol"]: + why.append("实测速度 %.1f BPM 与设定 %d 偏差 %.0f%%,风格不成立" + % (r["tempo_med"], e["bpm"], rel * 100)) + # ---- 软阈值(老歌基线派生) + if r.get("coverage", 0) < soft["coverage"]: + why.append("歌词召回 %.0f%% 低于老歌基线派生线 %.0f%%" + % (r["coverage"] * 100, soft["coverage"] * 100)) + if soft["voice_nn"] > 0 and r.get("voice_nn_sim", 0.0) < soft["voice_nn"]: + why.append("声线最近邻相似度 %.3f(最像《%s》)低于老歌派生线 %.3f" + % (r.get("voice_nn_sim", 0.0), r.get("voice_nn", "?"), + soft["voice_nn"])) + if r["dynamic_db"] < soft["dyn_lo"]: + why.append("动态范围 %.1f dB 低于老歌 P10 派生线 %.1f dB(偏平)" + % (r["dynamic_db"], soft["dyn_lo"])) + if r["centroid_hz"] < soft["centroid_lo"]: + why.append("频谱质心 %.0f Hz 低于老歌 P10 派生线 %.0f Hz(偏闷)" + % (r["centroid_hz"], soft["centroid_lo"])) + + # ---- 风格判定:与历史带标签语料比。 + # 只在语料本身可信(LOO 验证通过)时才当闸门,否则只写进报告当参考。 + want_fam = e.get("family") or qc_style.FAMILY.get(e.get("style", "")) + got_fam = r.get("style_fam", "") + if r.get("style_trusted") and want_fam and got_fam and got_fam != want_fam: + why.append("风格判为 %s(最近像《%s》,置信 %.0f%%),与设定 %s 不符" + % (got_fam, r.get("style_like", "?"), + r.get("style_conf", 0) * 100, want_fam)) + + verdicts.append(dict( + file=r["file"], title=r["title"], style=r.get("style", ""), + style_fam=got_fam, style_conf=r.get("style_conf"), + style_trusted=r.get("style_trusted"), + style_like=r.get("style_like"), + voice_nn=r.get("voice_nn"), voice_nn_sim=r.get("voice_nn_sim"), + vocal_onset=r.get("vocal_onset"), + leak_segs=r.get("leak_segs", 0), leaks=r.get("leaks", [])[:3], + verdict="PASS" if not why else "FAIL", why=why)) + + return dict(baseline=base, soft=soft, verdicts=verdicts) + + +# ------------------------------------------------------------------ main + +def run_job(job): + root = job["comfy_root"] + music = job["music_dir"] + ref = job.get("ref") + cache = job.get("cache_dir") or os.path.join(root, "songforge", "cache", "qc") + os.makedirs(cache, exist_ok=True) + + style_ref = qc_style.load(job.get("style_ref")) + e_ref = None + if ref and os.path.isfile(ref): + ry, rsr, _ = _load_audio(ref) + e_ref = embed(ry, rsr) + + rows = [] + for item in job["files"]: + p = item["path"] if os.path.isabs(item["path"]) \ + else os.path.join(music, item["path"]) + try: + r = measure(p, item, cache_dir=cache, + with_asr=job.get("asr", True)) + if e_ref is not None: + r["voice_ref"] = round(cos(r["_emb"], e_ref), 4) + + # ---- 风格分类:与历史带标签语料的质心比(只看混音轨) + if style_ref is not None and "_svec" in r: + fam, conf, dists = style_ref.classify(r["_svec"]) + near, ndist, nfam = style_ref.nn(r["_svec"]) + r["style_fam"] = fam + r["style_conf"] = round(conf, 4) + r["style_trusted"] = style_ref.trusted + r["style_like"] = near + r["style_like_family"] = nfam + r["style_like_dist"] = round(ndist, 4) + r["style_dists"] = {k: round(v, 4) for k, v in dists.items()} + + r.pop("_emb", None) + r.pop("_svec", None) + rows.append(r) + except Exception as exc: + rows.append(dict(file=os.path.basename(p), group=item.get( + "group", "new"), error="%s: %s" % (type(exc).__name__, exc), + traceback=traceback.format_exc()[-600:])) + print(" .. %s" % os.path.basename(p), flush=True) + + # ---- 声线最近邻必须在全部量完之后做: + # 比的不是某一个参考音频,而是 8 首已认可老歌整体。 + # 老歌自己也要算,但必须留一(跟自己比永远是 1.0,没有参考价值)。 + # 坑位(踩过):最初只给新歌算 NN,老歌行没有 voice_nn_sim, + # 而 derive_soft 又是从老歌行派生闸门 -> 阈值算成 0, + # `if soft["voice_nn"] > 0` 永远不成立,声线这一项等于形同虚设。 + old_nn = [] + for r in rows: + if "error" in r: + continue + nn = _voice_nn(r, job["files"], music, cache) + if nn: + r.update(nn) + if r.get("group") == "old" and r.get("voice_nn_sim"): + old_nn.append(r["voice_nn_sim"]) + floor = float(np.percentile(old_nn, 10)) if old_nn else None + if floor is not None: + for r in rows: + r["voice_nn_floor"] = round(floor, 4) + return rows + + +def _voice_emb_of(path, cache): + """带磁盘缓存的声线 embedding。整轮 QC 有 11 首,重复算纯浪费。""" + key = os.path.abspath(path) + if key in _EMB_CACHE: + return _EMB_CACHE[key] + cp = os.path.join(cache, os.path.basename(path) + ".emb.npy") + if os.path.isfile(cp): + e = np.load(cp).astype(np.float64) + else: + v, vsr = vocals_of(path, cache) + t = embed(v, vsr) + e = np.asarray(t.detach().cpu().numpy() + if hasattr(t, "detach") else t, dtype=np.float64) + np.save(cp, e) + _EMB_CACHE[key] = e + return e + + +def _voice_nn(row, files, music, cache): + """这首歌 vs 8 首已认可老歌的声线最近邻。 + + 坑位:老歌自己比老歌余弦接近 1,没有参考价值,所以要做留一—— + 计算某首老歌时把它自己从候选里剔掉。这样 8 首老歌互相之间 + 的相似度才是"已认可作品之间的自然差异",新歌的闸门才有意义。 + """ + self_path = os.path.abspath(row["path"]) + e_new = _voice_emb_of(row["path"], cache) + best = ("", -1.0) + for it in files: + if it.get("group") != "old": + continue + p = it["path"] if os.path.isabs(it["path"]) \ + else os.path.join(music, it["path"]) + if os.path.abspath(p) == self_path: + continue # 留一:自己不能给自己打分 + try: + eo = _voice_emb_of(p, cache) + except Exception: + continue + na, nb = float(np.linalg.norm(e_new)), float(np.linalg.norm(eo)) + if na < 1e-8 or nb < 1e-8: + continue + s = float(np.dot(e_new, eo) / (na * nb)) + if s > best[1]: + best = (it.get("title") or os.path.basename(p), s) + if not best[0]: + return None + return dict(voice_nn=best[0], voice_nn_sim=round(best[1], 4)) + + +def main(argv): + spec = json.loads(argv[1]) + rows = run_job(spec) + base_cache = spec.get("baseline_file") + baseline = None + if base_cache and os.path.isfile(base_cache): + with open(base_cache, encoding="utf-8") as f: + baseline = json.load(f) + rep = judge(rows, baseline) + rep["rows"] = rows + print("###JSON###" + json.dumps(rep, ensure_ascii=False)) + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv)) diff --git a/workflows/template.json b/workflows/template.json new file mode 100644 index 0000000..c23f344 --- /dev/null +++ b/workflows/template.json @@ -0,0 +1,220 @@ +{ + "id": "songforge-template-2026-10-09", + "revision": 6, + "last_node_id": 11, + "last_link_id": 30, + "nodes": [ + { + "id": 0, + "type": "CheckpointLoaderSimple", + "pos": [50, -200], + "size": [300, 200], + "flags": {}, + "order": 0, + "mode": 0, + "inputs": [ + {"name": "ckpt_name", "type": "STRING", "widget": {"name": "ckpt_name"}, "link": null} + ], + "outputs": [ + {"name": "MODEL", "type": "MODEL", "links": [0]}, + {"name": "CLIP", "type": "CLIP", "links": [1, 2]}, + {"name": "VAE", "type": "VAE", "links": [3]} + ], + "widgets_values": ["ace_step_1.5_turbo_swl_fused.safetensors"] + }, + { + "id": 1, + "type": "LoraLoader", + "pos": [300, -200], + "size": [300, 200], + "flags": {}, + "order": 1, + "mode": 0, + "inputs": [ + {"name": "lora_name", "type": "STRING", "widget": {"name": "lora_name"}, "link": null}, + {"name": "strength_model", "type": "FLOAT", "widget": {"name": "strength_model"}, "link": null}, + {"name": "strength_clip", "type": "FLOAT", "widget": {"name": "strength_clip"}, "link": null} + ], + "outputs": [ + {"name": "MODEL", "type": "MODEL", "links": [4]}, + {"name": "CLIP", "type": "CLIP", "links": [5, 6]} + ], + "widgets_values": ["swl_voice_best_ep257.safetensors", 0.5, 0.5] + }, + { + "id": 2, + "type": "ModelSamplingAuraFlow", + "pos": [450, -200], + "size": [300, 200], + "flags": {}, + "order": 2, + "mode": 0, + "inputs": [ + {"name": "model", "type": "MODEL", "link": 0}, + {"name": "shift", "type": "FLOAT", "widget": {"name": "shift"}, "link": null}, + {"name": "sampling", "type": "COMBO", "widget": {"name": "sampling"}, "link": null} + ], + "outputs": [ + {"name": "MODEL", "type": "MODEL", "links": [7]} + ], + "widgets_values": [1.73, "flow"] + }, + { + "id": 3, + "type": "EmptyAceStep1.5LatentAudio", + "pos": [50, 100], + "size": [300, 200], + "flags": {}, + "order": 3, + "mode": 0, + "inputs": [ + {"name": "seconds", "type": "FLOAT", "widget": {"name": "seconds"}, "link": null}, + {"name": "batch_size", "type": "INT", "widget": {"name": "batch_size"}, "link": null} + ], + "outputs": [ + {"name": "LATENT", "type": "LATENT", "links": [8]} + ], + "widgets_values": [150, 1] + }, + { + "id": 4, + "type": "TextEncodeAceStepAudio1.5", + "pos": [450, 50], + "size": [500, 250], + "flags": {}, + "order": 3, + "mode": 0, + "inputs": [ + {"name": "clip", "type": "CLIP", "link": 1}, + {"name": "tags", "type": "STRING", "widget": {"name": "tags"}, "link": null}, + {"name": "lyrics", "type": "STRING", "widget": {"name": "lyrics"}, "link": null}, + {"name": "seed", "type": "INT", "widget": {"name": "seed"}, "link": null}, + {"name": "bpm", "type": "INT", "widget": {"name": "bpm"}, "link": null}, + {"name": "duration", "type": "FLOAT", "widget": {"name": "duration"}, "link": null}, + {"name": "timesignature", "type": "COMBO", "widget": {"name": "timesignature"}, "link": null}, + {"name": "language", "type": "COMBO", "widget": {"name": "language"}, "link": null}, + {"name": "keyscale", "type": "COMBO", "widget": {"name": "keyscale"}, "link": null}, + {"name": "generate_audio_codes", "type": "BOOLEAN", "widget": {"name": "generate_audio_codes"}, "link": null}, + {"name": "cfg_scale", "type": "FLOAT", "widget": {"name": "cfg_scale"}, "link": null}, + {"name": "temperature", "type": "FLOAT", "widget": {"name": "temperature"}, "link": null}, + {"name": "top_p", "type": "FLOAT", "widget": {"name": "top_p"}, "link": null}, + {"name": "top_k", "type": "INT", "widget": {"name": "top_k"}, "link": null}, + {"name": "min_p", "type": "FLOAT", "widget": {"name": "min_p"}, "link": null} + ], + "outputs": [ + {"name": "CONDITIONING", "type": "CONDITIONING", "links": [9]} + ], + "widgets_values": ["", "", 42, 120, 150, "4", "zh", "C major", true, 3.0, 0.68, 0.88, 50, 0.02] + }, + { + "id": 5, + "type": "TextEncodeAceStepAudio1.5", + "pos": [450, 400], + "size": [500, 250], + "flags": {}, + "order": 4, + "mode": 0, + "inputs": [ + {"name": "clip", "type": "CLIP", "link": 2}, + {"name": "tags", "type": "STRING", "widget": {"name": "tags"}, "link": null}, + {"name": "lyrics", "type": "STRING", "widget": {"name": "lyrics"}, "link": null}, + {"name": "seed", "type": "INT", "widget": {"name": "seed"}, "link": null}, + {"name": "bpm", "type": "INT", "widget": {"name": "bpm"}, "link": null}, + {"name": "duration", "type": "FLOAT", "widget": {"name": "duration"}, "link": null}, + {"name": "timesignature", "type": "COMBO", "widget": {"name": "timesignature"}, "link": null}, + {"name": "language", "type": "COMBO", "widget": {"name": "language"}, "link": null}, + {"name": "keyscale", "type": "COMBO", "widget": {"name": "keyscale"}, "link": null}, + {"name": "generate_audio_codes", "type": "BOOLEAN", "widget": {"name": "generate_audio_codes"}, "link": null}, + {"name": "cfg_scale", "type": "FLOAT", "widget": {"name": "cfg_scale"}, "link": null}, + {"name": "temperature", "type": "FLOAT", "widget": {"name": "temperature"}, "link": null}, + {"name": "top_p", "type": "FLOAT", "widget": {"name": "top_p"}, "link": null}, + {"name": "top_k", "type": "INT", "widget": {"name": "top_k"}, "link": null}, + {"name": "min_p", "type": "FLOAT", "widget": {"name": "min_p"}, "link": null} + ], + "outputs": [ + {"name": "CONDITIONING", "type": "CONDITIONING", "links": [10]} + ], + "widgets_values": ["", "", 42, 120, 150, "4", "zh", "C major", true, 3.0, 0.68, 0.88, 50, 0.0], + "title": "negative" + }, + { + "id": 6, + "type": "KSampler", + "pos": [1050, 100], + "size": [300, 250], + "flags": {}, + "order": 5, + "mode": 0, + "inputs": [ + {"name": "model", "type": "MODEL", "link": 3}, + {"name": "seed", "type": "INT", "widget": {"name": "seed"}, "link": null}, + {"name": "steps", "type": "INT", "widget": {"name": "steps"}, "link": null}, + {"name": "cfg", "type": "FLOAT", "widget": {"name": "cfg"}, "link": null}, + {"name": "sampler_name", "type": "COMBO", "widget": {"name": "sampler_name"}, "link": null}, + {"name": "scheduler", "type": "COMBO", "widget": {"name": "scheduler"}, "link": null}, + {"name": "positive", "type": "CONDITIONING", "link": 9}, + {"name": "negative", "type": "CONDITIONING", "link": 10}, + {"name": "latent_image", "type": "LATENT", "link": 8}, + {"name": "denoise", "type": "FLOAT", "widget": {"name": "denoise"}, "link": null} + ], + "outputs": [ + {"name": "LATENT", "type": "LATENT", "links": [11]} + ], + "widgets_values": [42, "fixed", 28, 2.0, "euler", "simple", 1.0] + }, + { + "id": 7, + "type": "VAEDecodeAudio", + "pos": [1450, 100], + "size": [300, 200], + "flags": {}, + "order": 6, + "mode": 0, + "inputs": [ + {"name": "samples", "type": "LATENT", "link": 11}, + {"name": "vae", "type": "VAE", "link": 3} + ], + "outputs": [ + {"name": "AUDIO", "type": "AUDIO", "links": [12]} + ], + "widgets_values": [] + }, + { + "id": 8, + "type": "SaveAudioAdvanced", + "pos": [1850, 100], + "size": [300, 200], + "flags": {}, + "order": 7, + "mode": 0, + "inputs": [ + {"name": "audio", "type": "AUDIO", "link": 12}, + {"name": "filename_prefix", "type": "STRING", "widget": {"name": "filename_prefix"}, "link": null}, + {"name": "format", "type": "COMBO", "widget": {"name": "format"}, "link": null} + ], + "outputs": [ + {"name": "AUDIO", "type": "AUDIO", "links": []} + ], + "widgets_values": ["songforge/output", "wav"] + } + ], + "links": [ + {"id": 0, "origin_id": 0, "origin_slot": 0, "target_id": 1, "target_slot": 0, "type": "MODEL"}, + {"id": 1, "origin_id": 0, "origin_slot": 1, "target_id": 4, "target_slot": 0, "type": "CLIP"}, + {"id": 2, "origin_id": 0, "origin_slot": 1, "target_id": 5, "target_slot": 0, "type": "CLIP"}, + {"id": 3, "origin_id": 1, "origin_slot": 0, "target_id": 2, "target_slot": 0, "type": "MODEL"}, + {"id": 4, "origin_id": 2, "origin_slot": 0, "target_id": 6, "target_slot": 0, "type": "MODEL"}, + {"id": 5, "origin_id": 2, "origin_slot": 1, "target_id": 5, "target_slot": 0, "type": "CLIP"}, + {"id": 6, "origin_id": 2, "origin_slot": 2, "target_id": 5, "target_slot": 1, "type": "CLIP"}, + {"id": 7, "origin_id": 3, "origin_slot": 0, "target_id": 7, "target_slot": 0, "type": "LATENT"}, + {"id": 8, "origin_id": 4, "origin_slot": 0, "target_id": 8, "target_slot": 0, "type": "LATENT"}, + {"id": 9, "origin_id": 8, "origin_slot": 0, "target_id": 9, "target_slot": 0, "type": "CONDITIONING"}, + {"id": 10, "origin_id": 9, "origin_slot": 0, "target_id": 6, "target_slot": 0, "type": "CONDITIONING"}, + {"id": 11, "origin_id": 5, "origin_slot": 0, "target_id": 6, "target_slot": 1, "type": "CONDITIONING"}, + {"id": 12, "origin_id": 6, "origin_slot": 0, "target_id": 7, "target_slot": 0, "type": "CONDITIONING"}, + {"id": 13, "origin_id": 7, "origin_slot": 0, "target_id": 8, "target_slot": 0, "type": "AUDIO"}, + {"id": 14, "origin_id": 8, "origin_slot": 2, "target_id": 8, "target_slot": 1, "type": "AUDIO"} + ], + "groups": [], + "definitions": {} +} \ No newline at end of file