Add no-filler tags, media CLI, and ask-before-edit iron law.
selftest / selftest (push) Canceled after 0s
selftest / selftest (push) Canceled after 0s
Default solo-lead tags and report-only filler_ratio; slim Agent docs with media CLI; require consent, git backup or plugin path, and explicit commit/push before code changes. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""音频窄接口:info / convert / clip。由 songforge media 经 qc_python 调用。
|
||||
|
||||
用法(内部):
|
||||
python media_worker.py '{"op":"info","file":"..."}'
|
||||
stdout 最后一行前有 ###JSON###
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
def _load(path):
|
||||
import soundfile as sf
|
||||
import numpy as np
|
||||
y, sr = sf.read(path, always_2d=True)
|
||||
y = np.asarray(y, dtype=np.float32)
|
||||
return y, int(sr)
|
||||
|
||||
|
||||
def op_info(job):
|
||||
import librosa
|
||||
path = job["file"]
|
||||
y, sr = _load(path)
|
||||
ch = y.shape[1]
|
||||
mono = y.mean(axis=1) if ch > 1 else y[:, 0]
|
||||
dur = float(mono.shape[0]) / float(sr)
|
||||
bpm = None
|
||||
try:
|
||||
tempo, _ = librosa.beat.beat_track(y=mono, sr=sr)
|
||||
import numpy as np
|
||||
t = np.asarray(tempo).reshape(-1)
|
||||
bpm = float(t[0]) if t.size else None
|
||||
except Exception:
|
||||
bpm = None
|
||||
return dict(
|
||||
file=os.path.abspath(path),
|
||||
duration_sec=round(dur, 3),
|
||||
sample_rate=sr,
|
||||
channels=ch,
|
||||
frames=int(mono.shape[0]),
|
||||
tempo_est=round(bpm, 1) if bpm else None,
|
||||
size_bytes=os.path.getsize(path),
|
||||
)
|
||||
|
||||
|
||||
def op_convert(job):
|
||||
import soundfile as sf
|
||||
path = job["file"]
|
||||
out = job["out"]
|
||||
fmt = (job.get("to") or "wav").lower().lstrip(".")
|
||||
y, sr = _load(path)
|
||||
os.makedirs(os.path.dirname(os.path.abspath(out)) or ".", exist_ok=True)
|
||||
# soundfile: wav/flac 原生;mp3 需外部,失败则明确报错
|
||||
if fmt == "mp3":
|
||||
try:
|
||||
import numpy as np
|
||||
from pydub import AudioSegment
|
||||
# write temp wav then export
|
||||
import tempfile
|
||||
fd, tmp = tempfile.mkstemp(suffix=".wav")
|
||||
os.close(fd)
|
||||
try:
|
||||
sf.write(tmp, y, sr)
|
||||
AudioSegment.from_wav(tmp).export(out, format="mp3")
|
||||
finally:
|
||||
try:
|
||||
os.remove(tmp)
|
||||
except OSError:
|
||||
pass
|
||||
except Exception as e:
|
||||
raise RuntimeError(
|
||||
"mp3 需要 pydub+ffmpeg;或改用 --to wav/flac。详情: %s" % e)
|
||||
else:
|
||||
subtype = "PCM_16" if fmt == "wav" else None
|
||||
kw = dict(format=fmt.upper())
|
||||
if subtype:
|
||||
kw["subtype"] = subtype
|
||||
sf.write(out, y, sr, **kw)
|
||||
return dict(file=os.path.abspath(path), out=os.path.abspath(out),
|
||||
format=fmt, ok=True)
|
||||
|
||||
|
||||
def op_clip(job):
|
||||
import soundfile as sf
|
||||
path = job["file"]
|
||||
out = job["out"]
|
||||
start = float(job.get("start") or 0.0)
|
||||
dur = job.get("dur")
|
||||
end = job.get("end")
|
||||
y, sr = _load(path)
|
||||
i0 = max(0, int(start * sr))
|
||||
if dur is not None:
|
||||
i1 = min(y.shape[0], i0 + int(float(dur) * sr))
|
||||
elif end is not None:
|
||||
i1 = min(y.shape[0], int(float(end) * sr))
|
||||
else:
|
||||
i1 = y.shape[0]
|
||||
if i1 <= i0:
|
||||
raise ValueError("截取区间无效 start=%s dur=%s end=%s" % (start, dur, end))
|
||||
chunk = y[i0:i1]
|
||||
os.makedirs(os.path.dirname(os.path.abspath(out)) or ".", exist_ok=True)
|
||||
ext = os.path.splitext(out)[1].lstrip(".").lower() or "wav"
|
||||
if ext == "mp3":
|
||||
raise RuntimeError("clip 输出请用 .wav 或 .flac(mp3 请再 convert)")
|
||||
subtype = "PCM_16" if ext == "wav" else None
|
||||
kw = {}
|
||||
if subtype:
|
||||
kw["subtype"] = subtype
|
||||
sf.write(out, chunk, sr, **kw)
|
||||
return dict(
|
||||
file=os.path.abspath(path), out=os.path.abspath(out),
|
||||
start_sec=start, end_sec=i1 / float(sr),
|
||||
duration_sec=round((i1 - i0) / float(sr), 3), ok=True)
|
||||
|
||||
|
||||
OPS = {"info": op_info, "convert": op_convert, "clip": op_clip}
|
||||
|
||||
|
||||
def main():
|
||||
raw = sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()
|
||||
job = json.loads(raw)
|
||||
op = job.get("op")
|
||||
if op not in OPS:
|
||||
raise SystemExit("unknown op: %s" % op)
|
||||
if not job.get("file") or not os.path.isfile(job["file"]):
|
||||
raise SystemExit("file missing: %s" % job.get("file"))
|
||||
out = OPS[op](job)
|
||||
print("###JSON###")
|
||||
print(json.dumps(out, ensure_ascii=False))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
main()
|
||||
except Exception as e:
|
||||
print("###JSON###")
|
||||
print(json.dumps(dict(error=str(e), type=type(e).__name__),
|
||||
ensure_ascii=False))
|
||||
sys.exit(1)
|
||||
+19
-1
@@ -685,6 +685,21 @@ def _collapse_runs(s, max_run=3):
|
||||
return "".join(out)
|
||||
|
||||
|
||||
def filler_ratio(asr_text, lang="zh"):
|
||||
"""ASR 文本中语气/哼唱衬词占比(弱参考,不单独否决)。"""
|
||||
if _is_en(lang):
|
||||
words = _en_words(_strip_tags(asr_text or ""))
|
||||
if not words:
|
||||
return 0.0
|
||||
n = sum(1 for w in words if w in EN_FILLER)
|
||||
return n / float(len(words))
|
||||
chars = cjk(asr_text or "")
|
||||
if not chars:
|
||||
return 0.0
|
||||
n = sum(1 for c in chars if c in FILLER)
|
||||
return n / float(len(chars))
|
||||
|
||||
|
||||
def coverage(asr_text, lyrics, lang="zh"):
|
||||
"""ASR 召回率:ASR 里命中的歌词单元 / 歌词正文单元。
|
||||
|
||||
@@ -875,6 +890,8 @@ def measure(path, expect=None, cache_dir=None, with_asr=True):
|
||||
if half > vsr else 0.0
|
||||
|
||||
cov = coverage(txt, lyr, lang) if with_asr else 0.0
|
||||
# 衬词占比:弱参考,不单独导致 FAIL(与 style_fam 同级)
|
||||
fill_r = filler_ratio(txt, lang) if with_asr else 0.0
|
||||
|
||||
# ---- 风格分类:只看混音轨,不掺 demucs(见 qc_style 文件头坑位 4)
|
||||
svec, sraw = qc_style.features(path, expect_bpm=expect.get("bpm"))
|
||||
@@ -901,7 +918,8 @@ def measure(path, expect=None, cache_dir=None, with_asr=True):
|
||||
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],
|
||||
coverage=round(cov, 4), filler_ratio=round(fill_r, 4),
|
||||
asr_head=txt[:120],
|
||||
voice_self=round(float(e_self), 4),
|
||||
flatness=round(sraw.get("flatness", 0.0), 4),
|
||||
expect=dict(expect),
|
||||
|
||||
Reference in New Issue
Block a user