Files
long 3cd3553af2 feat: 新增 training-videos 素材流水线
AI 跟练的素材生产工具,产物写入 TUICallKit-Vue3/training:
- 教练动作帧:AI 设定稿 → 切帧对齐、锁脸、插帧 → 导出小程序帧与 COS 高清帧
- 示范视频:Remotion 渲染四个动作的示范视频
- 背景音乐:程序合成的原创循环音乐(npm run bgm)
2026-10-31 10:33:25 +08:00

349 lines
15 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
训练背景音乐:用程序合成一段舒缓的循环氛围音乐(原创,不含任何采样和版权素材)
用法:python3 scripts/bgm_synth.py
产物:out/music/calm.mp3 上传到 COS,地址填到 TUICallKit-Vue3/training/pose/exercises.ts 的 BGM_URL
work/music/calm.wav、work/music/spectrogram.png 检查用
编曲:D 大调,72 拍/分,32 小节(约 1 分 47 秒),首尾相接循环播放
A 铺底长音 + 电钢琴分解和弦(3+3+2 的节奏,比拍子慢一点,轻轻摇晃)
A' 加入颤音琴长音旋律,高八度的“光晕”铺底进来
B 换一组和弦(低音 G→F#→E→A 下行),旋律继续
A'' 分解和弦变密,最后两小节逐渐变稀
混响、延迟、滤波都按首尾相接计算(循环卷积),结尾的余音绕回开头,循环处没有接缝;
循环点落在属和弦回到主和弦、音乐最轻的地方,播放器循环时即使空出几十毫秒也听不出来。
随机数固定种子,每次生成的结果完全一样。
"""
import subprocess
import wave
from pathlib import Path
import numpy as np
ROOT = Path(__file__).resolve().parents[1]
MP3 = ROOT / "out" / "music" / "calm.mp3"
WORK = ROOT / "work" / "music"
SR = 44100
BPM = 72
BEAT = 60 / BPM
BARS = 32
N = round(BARS * 4 * BEAT * SR)
LOOP_S = N / SR
SEED = 20260929
# 整体响度(RMS);小程序里再按 0.35 左右的音量播放,比教练语音低一截
TARGET_RMS_DB = -19.0
PEAK_LIMIT = 10 ** (-1.0 / 20)
# 首尾各 30ms 淡入淡出:播放器循环时如果有空隙,也不会“咔哒”一声
EDGE_FADE_S = 0.03
MP3_BITRATE = "128k"
rng = np.random.default_rng(SEED)
# 和弦:铺底声部、低音、分解和弦用音(MIDI 音高,60 = 中央 C)
CHORDS = {
"Dmaj9": ([54, 57, 61, 64], 38, [66, 69, 73, 76, 78, 81]),
"Bm11": ([54, 57, 62, 64], 35, [66, 69, 71, 74, 76, 78]),
"Gmaj9": ([54, 57, 59, 62], 31, [67, 71, 74, 78, 81, 83]),
"A9sus4": ([55, 59, 62, 64], 33, [64, 69, 71, 74, 76, 79]),
"Gmaj7": ([55, 59, 62, 66], 31, [67, 71, 74, 78, 79, 83]),
"F#m7": ([54, 57, 61, 64], 30, [66, 69, 73, 76, 78, 81]),
"Em9": ([55, 59, 62, 66], 28, [64, 67, 71, 74, 78, 79]),
"A13sus4": ([55, 59, 62, 66], 33, [64, 69, 71, 74, 76, 78]),
}
# 每个和弦两小节,共 16 个
PROGRESSION = ["Dmaj9", "Bm11", "Gmaj9", "A9sus4"] * 2 + ["Gmaj7", "F#m7", "Em9", "A13sus4"] + ["Dmaj9", "Bm11", "Gmaj9", "A9sus4"]
SECTIONS = ["A"] * 4 + ["A1"] * 4 + ["B"] * 4 + ["A2"] * 4
# 铺底的明亮程度(滤波开合),段落之间有起伏
BRIGHTNESS = {"A": 0.25, "A1": 0.35, "B": 0.5, "A2": 0.4}
# 分解和弦:(两小节内第几个八分音符, 用第几个和弦音, 力度)
SPARSE = [(0, 0, 0.62), (3, 2, 0.46), (6, 4, 0.52), (8, 3, 0.56), (11, 1, 0.42), (14, 2, 0.48)]
FALL = [(0, 5, 0.56), (2, 4, 0.42), (3, 2, 0.46), (6, 3, 0.5), (8, 4, 0.54), (10, 3, 0.4), (11, 1, 0.44), (14, 2, 0.46)]
FLOW = [
(0, 0, 0.6), (1, 2, 0.36), (2, 4, 0.46), (3, 5, 0.4), (4, 4, 0.5), (5, 2, 0.34), (6, 3, 0.44),
(8, 1, 0.56), (9, 3, 0.36), (10, 4, 0.46), (11, 5, 0.4), (12, 3, 0.5), (13, 2, 0.34), (14, 1, 0.42),
]
CODA = [(0, 4, 0.5), (3, 2, 0.42), (6, 1, 0.36), (8, 0, 0.3)]
ARP = {"A": SPARSE, "A1": SPARSE, "B": FALL, "A2": FLOW}
# 旋律(颤音琴):(从头数第几拍, 音高, 拍数, 力度),只在 A'、B 两段
MELODY = [
(32, 81, 3, 0.5), (35, 78, 1, 0.4), (36, 76, 4, 0.45),
(40, 74, 2, 0.45), (42, 78, 2, 0.42), (44, 81, 4, 0.48),
(48, 83, 3, 0.5), (51, 81, 1, 0.4), (52, 78, 4, 0.45),
(56, 76, 3, 0.45), (59, 74, 1, 0.38), (60, 76, 4, 0.42),
(64, 86, 2, 0.48), (66, 83, 2, 0.42), (68, 81, 4, 0.45),
(72, 85, 2, 0.46), (74, 81, 2, 0.4), (76, 78, 4, 0.44),
(80, 83, 2, 0.46), (82, 79, 2, 0.4), (84, 78, 4, 0.44),
(88, 76, 3, 0.42), (91, 78, 1, 0.36), (92, 76, 4, 0.4),
]
TABLE = 4096
GRID = np.arange(TABLE + 1)
def hz(midi: float) -> float:
return 440.0 * 2 ** ((midi - 69) / 12)
def at(beat: float) -> int:
return round(beat * BEAT * SR)
def add(bus: np.ndarray, start: int, sig: np.ndarray, pan: float = 0.0):
"""单声道信号按声像(-1 左 ~ 1 右,等功率)叠加到立体声总线;超出结尾的部分绕回开头"""
gains = (np.cos((pan + 1) * np.pi / 4), np.sin((pan + 1) * np.pi / 4))
pos, i = start % N, 0
while i < len(sig):
take = min(len(sig) - i, N - pos)
for ch in (0, 1):
bus[ch, pos : pos + take] += sig[i : i + take] * gains[ch]
i += take
pos = 0
def spectral(x: np.ndarray, gain) -> np.ndarray:
"""频域滤波(整段循环信号,零相位)"""
f = np.fft.rfftfreq(x.shape[-1], 1 / SR)
return np.fft.irfft(np.fft.rfft(x) * gain(f), x.shape[-1])
def lowpass(x: np.ndarray, fc: float, order: int = 2) -> np.ndarray:
return spectral(x, lambda f: 1 / np.sqrt(1 + (f / fc) ** (2 * order)))
# ───────── 乐器 ─────────
def pad_tables(f: float, cutoffs: tuple = (600.0, 2400.0)):
"""铺底音色:带低通的锯齿波谐波(暗 / 亮两张波表,按亮度混合),谐波相位随机,听起来不刺"""
x = np.arange(TABLE) / TABLE
phases = np.random.default_rng(round(f * 1000)).uniform(0, 2 * np.pi, 64)
tables = []
for fc in cutoffs:
tab = np.zeros(TABLE)
for k in range(1, 64):
w = (1 / k) / np.sqrt(1 + (k * f / fc) ** 4)
if k * f > 9000 or w < 1e-4:
break
tab += w * np.sin(2 * np.pi * k * x + phases[k])
tables.append(np.append(tab, tab[0]))
return tables
def render_pad(bus: np.ndarray, start: int, f: float, hold: float, tau: float, bright: float, attack: float = 1.6, cutoffs: tuple = (600.0, 2400.0)):
"""三个声部(-7 / 0 / +7 音分)左中右铺开,缓起(attack 秒),结束后按 tau 慢慢消失"""
n = int((hold + 5 * tau) * SR)
t = np.arange(n) / SR
rise = np.clip(t / attack, 0, 1)
env = (0.5 - 0.5 * np.cos(np.pi * rise)) * np.where(t < hold, 1.0, np.exp(-(t - hold) / tau))
# 亮度缓慢起伏,周期是循环长度的 1/5(首尾一致)
abs_t = start / SR + t
mix = np.clip(bright + 0.15 * np.sin(2 * np.pi * abs_t / (LOOP_S / 5)), 0, 1)
dark, light = pad_tables(f, cutoffs)
for cents, pan, gain in ((-7, -0.6, 0.8), (0, 0.0, 1.0), (7, 0.6, 0.8)):
fv = f * 2 ** (cents / 1200)
ph = ((fv * t + rng.uniform()) % 1.0) * TABLE
d = np.interp(ph, GRID, dark)
b = np.interp(ph, GRID, light)
add(bus, start, (d + (b - d) * mix) * env * gain, pan)
def rhodes(f: float, vel: float, hold: float) -> np.ndarray:
"""电钢琴(FM):1:1 调制出温暖的音体,14 倍频的短促调制做出“叮”的击弦声;两个载波差 2.6 音分,带一点合唱感"""
n = int((hold + 2.5) * SR)
t = np.arange(n) / SR
tau = float(np.clip(2.6 * (220 / f) ** 0.5, 0.7, 2.6))
env = (1 - np.exp(-t / 0.003)) * np.exp(-t / tau) * np.where(t < hold, 1.0, np.exp(-(t - hold) / 0.3))
index = vel * (1.9 * np.exp(-t / 0.15) + 0.6 * np.exp(-t / 1.4))
w = 2 * np.pi * f * t
w2 = w * 1.0015
body = 0.62 * np.sin(w + index * np.sin(w)) + 0.38 * np.sin(w2 + 0.8 * index * np.sin(w2))
tine = 0.22 * np.exp(-t / 0.05) * np.sin(w + 1.2 * np.exp(-t / 0.04) * np.sin(14 * w))
return (body + tine) * env * vel
def vibes(f: float, vel: float, hold: float) -> np.ndarray:
"""颤音琴:基音长衰减 + 4 倍泛音短衰减,带轻微的马达颤音"""
n = int((hold + 3.0) * SR)
t = np.arange(n) / SR
w = 2 * np.pi * f * t
tone = np.exp(-t / 3.0) * np.sin(w) + 0.17 * np.exp(-t / 0.5) * np.sin(4 * w)
trem = 1 - 0.14 * (0.5 - 0.5 * np.cos(2 * np.pi * 3.2 * t))
env = (1 - np.exp(-t / 0.004)) * np.where(t < hold, 1.0, np.exp(-(t - hold) / 0.5))
return tone * trem * env * vel
def bass(f: float, vel: float, hold: float) -> np.ndarray:
"""柔和的低音:正弦加少量 2~4 次谐波(手机外放也能隐约听到)"""
n = int((hold + 1.2) * SR)
t = np.arange(n) / SR
rise = np.clip(t / 0.05, 0, 1)
env = (0.5 - 0.5 * np.cos(np.pi * rise)) * (0.7 + 0.3 * np.exp(-t / 0.6)) * np.where(t < hold, 1.0, np.exp(-(t - hold) / 0.25))
w = 2 * np.pi * f * t
return (np.sin(w) + 0.3 * np.sin(2 * w) + 0.12 * np.sin(3 * w) + 0.05 * np.sin(4 * w)) * env * vel
# ───────── 编曲 ─────────
def humanize() -> int:
return int(np.clip(rng.normal(0, 0.006), -0.015, 0.015) * SR)
def arrange():
pad, halo, keys, vib, low = (np.zeros((2, N)) for _ in range(5))
last_slot = len(PROGRESSION) - 1
for i, name in enumerate(PROGRESSION):
voicing, root, arp = CHORDS[name]
section = SECTIONS[i]
beat0 = i * 8
last = i == last_slot
# 铺底:最后一个和弦只撑一小节,然后慢慢淡出,把循环点留在最轻的地方
hold, tau = (4 if last else 8) * BEAT, 1.8 if last else 1.3
for m in voicing:
render_pad(pad, at(beat0), hz(m), hold, tau, BRIGHTNESS[section])
if section != "A":
# 高八度的光晕:更慢地浮起来,给中高频一点空气感
render_pad(halo, at(beat0), hz(m + 12), hold, tau, 0.6, attack=2.5, cutoffs=(1200.0, 4000.0))
add(low, at(beat0), bass(hz(root), 0.8, 4 * BEAT - 0.12))
if not last:
add(low, at(beat0 + 4), bass(hz(root), 0.62, 4 * BEAT - 0.12))
for slot, k, vel in CODA if last else ARP[section]:
m = arp[k]
note = rhodes(hz(m), vel + rng.uniform(-0.03, 0.03), 2.5 * BEAT)
add(keys, at(beat0 + slot / 2) + humanize(), note, float(np.clip((m - 72) / 24, -0.35, 0.35)))
for beat, m, beats_, vel in MELODY:
add(vib, at(beat) + humanize(), vibes(hz(m), vel, beats_ * BEAT - 0.05), 0.08)
return pad, halo, keys, vib, low
# ───────── 空间效果 ─────────
def reverb_ir(seconds: float = 4.5) -> np.ndarray:
"""合成厅堂混响:去相关的左右声道噪声,按频段衰减(低频长、高频短),20ms 预延迟"""
n = int(seconds * SR)
t = np.arange(n) / SR
f = np.fft.rfftfreq(n, 1 / SR)
bands = [(0, 250, 3.6), (250, 1000, 3.0), (1000, 3500, 2.3), (3500, 8000, 1.5), (8000, SR, 0.8)]
ir = np.zeros((2, n))
for ch in (0, 1):
spec = np.fft.rfft(rng.standard_normal(n))
for lo, hi, rt60 in bands:
band = np.fft.irfft(np.where((f >= lo) & (f < hi), spec, 0), n)
ir[ch] += band * 10 ** (-3 * t / rt60)
ir *= 1 - np.exp(-t / 0.03)
ir = np.concatenate([np.zeros((2, int(0.02 * SR))), ir], axis=1)
return ir / np.sqrt((ir**2).sum(axis=1, keepdims=True))
def circular_reverb(send: np.ndarray, ir: np.ndarray) -> np.ndarray:
"""循环卷积:结尾的混响尾巴绕回开头,和下一遍循环无缝衔接"""
mid_l = 0.7 * send[0] + 0.3 * send[1]
mid_r = 0.3 * send[0] + 0.7 * send[1]
return np.stack([np.fft.irfft(np.fft.rfft(x) * np.fft.rfft(h, N), N) for x, h in ((mid_l, ir[0]), (mid_r, ir[1]))])
def pingpong(send: np.ndarray, beats: float = 0.75, feedback: float = 0.32, taps: int = 6) -> np.ndarray:
"""附点八分的左右乒乓延迟,回声逐次变暗(循环移位,同样首尾相接)"""
d = at(beats)
mono = lowpass(send.mean(axis=0), 2800)
out = np.zeros((2, N))
for k in range(1, taps + 1):
out[k % 2] += np.roll(mono, k * d) * feedback**k
mono = lowpass(mono, 4000, 1)
return out
def rms(x: np.ndarray) -> float:
return float(np.sqrt(np.mean(x**2)))
def db(v: float) -> float:
return 20 * np.log10(max(v, 1e-12))
def level(x: np.ndarray, target_db: float) -> np.ndarray:
return x * (10 ** (target_db / 20) / rms(x))
def mixdown() -> np.ndarray:
pad, halo, keys, vib, low = arrange()
pad = level(pad, -21)
halo = level(halo, -31)
keys = level(keys, -21)
vib = level(vib, -23)
low = level(low, -28)
dry = pad + halo + keys + vib + low
wet = circular_reverb(0.5 * pad + 0.9 * halo + 0.6 * keys + 0.7 * vib + 0.08 * low, reverb_ir())
echo = pingpong(keys + 0.6 * vib)
mix = dry + level(wet, db(rms(dry)) - 4.5) + level(echo, db(rms(keys)) - 11)
# 母带:切掉 30Hz 以下的闷响
mix = spectral(mix, lambda f: 1 / np.sqrt(1 + (30 / np.maximum(f, 1)) ** 4))
mix = level(mix, TARGET_RMS_DB)
peak = np.abs(mix).max()
if peak > PEAK_LIMIT:
# 极少数峰值软限幅(tanh 在小信号区近似线性)
mix = np.tanh(mix / PEAK_LIMIT) * PEAK_LIMIT
fade = int(EDGE_FADE_S * SR)
ramp = 0.5 - 0.5 * np.cos(np.pi * np.arange(fade) / fade)
mix[:, :fade] *= ramp
mix[:, -fade:] *= ramp[::-1]
return mix
# ───────── 输出与检查 ─────────
def write_wav(path: Path, mix: np.ndarray):
path.parent.mkdir(parents=True, exist_ok=True)
dither = (rng.uniform(-0.5, 0.5, mix.shape) + rng.uniform(-0.5, 0.5, mix.shape)) / 32767
pcm = np.clip(np.round((mix + dither) * 32767), -32768, 32767).astype("<i2")
with wave.open(str(path), "wb") as w:
w.setnchannels(2)
w.setsampwidth(2)
w.setframerate(SR)
w.writeframes(pcm.T.tobytes())
def spectrogram(path: Path, mix: np.ndarray, width: int = 1600, height: int = 420):
"""时间 × 频率(对数刻度 40Hz~16kHz)的能量图,检查段落结构和频谱平衡"""
from PIL import Image
mono = mix.mean(axis=0)
win, hop = 4096, N // width
frames = np.stack([mono[i * hop : i * hop + win] * np.hanning(win) for i in range(width) if i * hop + win <= N], axis=1)
mag = 20 * np.log10(np.abs(np.fft.rfft(frames, axis=0)) + 1e-9)
f = np.fft.rfftfreq(win, 1 / SR)
rows = np.geomspace(40, 16000, height)[::-1]
img = np.stack([np.interp(rows, f, mag[:, c]) for c in range(mag.shape[1])], axis=1)
img = np.clip((img - (img.max() - 80)) / 80, 0, 1)
Image.fromarray((img * 255).astype(np.uint8), "L").save(path)
def report(mix: np.ndarray):
mono = mix.mean(axis=0)
bar = N // BARS
sections = [db(rms(mono[i * 8 * bar : (i + 1) * 8 * bar])) for i in range(4)]
spec = np.abs(np.fft.rfft(mono)) ** 2
f = np.fft.rfftfreq(N, 1 / SR)
centroid = float((spec * f).sum() / spec.sum())
half = int(0.5 * SR)
seam = db(rms(np.concatenate([mono[-half:], mono[:half]])))
print(f"时长 {LOOP_S:.2f}s,整体 RMS {db(rms(mix)):.1f}dBFS,峰值 {db(np.abs(mix).max()):.1f}dBFS,频谱重心 {centroid:.0f}Hz")
print("各段 RMS(A / A' / B / A''):" + " / ".join(f"{v:.1f}" for v in sections) + f" dB;循环点前后 1 秒 {seam:.1f}dB")
def main():
mix = mixdown()
WORK.mkdir(parents=True, exist_ok=True)
wav = WORK / "calm.wav"
write_wav(wav, mix)
spectrogram(WORK / "spectrogram.png", mix)
MP3.parent.mkdir(parents=True, exist_ok=True)
subprocess.run(
["ffmpeg", "-y", "-loglevel", "error", "-i", str(wav), "-codec:a", "libmp3lame", "-b:a", MP3_BITRATE,
"-metadata", "title=小甄跟练 · 舒缓", str(MP3)],
check=True,
)
report(mix)
print(f"已生成 {MP3.relative_to(ROOT)}({MP3.stat().st_size / 1024:.0f}KB),上传到 COS 后把地址填到 exercises.ts 的 BGM_URL")
if __name__ == "__main__":
main()