Initial commit: 基于旋律的音乐哼唱检索系统 (Query by Humming)
This commit is contained in:
+139
@@ -0,0 +1,139 @@
|
||||
"""旋律合成:把音符序列渲染成可听音频。
|
||||
|
||||
两个用途:
|
||||
|
||||
* **曲库回放**——给每首曲目生成一段示范音频,前端可试听;
|
||||
* **评测查询**——按「人哼唱」的特点(起音滑音、颤音、弱谐波、呼吸噪声)
|
||||
合成查询音频,再交给完整流水线识别,从而端到端地度量系统性能。
|
||||
|
||||
注意:用同一套合成器生成曲库与查询会让评测偏乐观,因此正式评测
|
||||
以磁盘上的**真实 MIDI** 曲库为准,合成曲库只用于规模化测试。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import numpy as np
|
||||
|
||||
from .config import DEFAULT, Config
|
||||
from .melody import Melody
|
||||
|
||||
__all__ = ["render_notes", "render_melody", "SONG_TIMBRE", "HUM_TIMBRE"]
|
||||
|
||||
#: 曲库示范音:谐波较丰富、颤音浅、噪声低
|
||||
SONG_TIMBRE: dict = dict(
|
||||
harmonics=(1.0, 0.45, 0.22, 0.12, 0.06),
|
||||
vibrato_hz=5.0,
|
||||
vibrato_depth=0.12,
|
||||
glide_s=0.012,
|
||||
noise_sigma=0.0015,
|
||||
note_gap=0.06,
|
||||
)
|
||||
|
||||
#: 哼唱查询音:谐波弱、颤音深、起音滑音明显、呼吸噪声大
|
||||
HUM_TIMBRE: dict = dict(
|
||||
harmonics=(1.0, 0.30, 0.10, 0.04),
|
||||
vibrato_hz=5.4,
|
||||
vibrato_depth=0.30,
|
||||
glide_s=0.045,
|
||||
noise_sigma=0.004,
|
||||
note_gap=0.04,
|
||||
)
|
||||
|
||||
|
||||
def render_notes(
|
||||
midis: np.ndarray,
|
||||
starts: np.ndarray,
|
||||
durs: np.ndarray,
|
||||
sr: int = DEFAULT.sample_rate,
|
||||
*,
|
||||
harmonics: tuple[float, ...] = (1.0, 0.45, 0.22, 0.12, 0.06),
|
||||
vibrato_hz: float = 5.0,
|
||||
vibrato_depth: float = 0.12,
|
||||
glide_s: float = 0.012,
|
||||
noise_sigma: float = 0.0015,
|
||||
note_gap: float = 0.06,
|
||||
attack_s: float = 0.03,
|
||||
release_s: float = 0.08,
|
||||
sustain: float = 0.78,
|
||||
peak: float = 0.85,
|
||||
seed: int = 0,
|
||||
) -> np.ndarray:
|
||||
"""加法合成一段旋律,返回 float32 单声道波形。"""
|
||||
midis = np.asarray(midis, dtype=np.float64).reshape(-1)
|
||||
starts = np.asarray(starts, dtype=np.float64).reshape(-1)
|
||||
durs = np.asarray(durs, dtype=np.float64).reshape(-1)
|
||||
if midis.size == 0:
|
||||
return np.zeros(int(0.1 * sr), dtype=np.float32)
|
||||
|
||||
rng = np.random.default_rng(seed)
|
||||
total = float(starts[-1] + durs[-1] + release_s + 0.05)
|
||||
n_total = int(total * sr) + 1
|
||||
out = np.zeros(n_total, dtype=np.float64)
|
||||
|
||||
for i in range(midis.size):
|
||||
sounding = max(float(durs[i]) * (1.0 - note_gap), 0.02)
|
||||
n_samp = int(sounding * sr)
|
||||
if n_samp < 16:
|
||||
continue
|
||||
t = np.arange(n_samp, dtype=np.float64) / sr
|
||||
|
||||
base = float(midis[i])
|
||||
if glide_s > 0.0 and i > 0:
|
||||
g = np.clip(t / glide_s, 0.0, 1.0)
|
||||
g = g * g * (3.0 - 2.0 * g) # smoothstep
|
||||
pitch = float(midis[i - 1]) + (base - float(midis[i - 1])) * g
|
||||
else:
|
||||
pitch = np.full(n_samp, base)
|
||||
|
||||
# 颤音在起音后渐入,避免开头就抖
|
||||
if vibrato_depth > 0.0:
|
||||
ramp = np.minimum(t / 0.15, 1.0)
|
||||
pitch = pitch + vibrato_depth * ramp * np.sin(2.0 * np.pi * vibrato_hz * t)
|
||||
|
||||
freq = 440.0 * np.power(2.0, (pitch - 69.0) / 12.0)
|
||||
phase = 2.0 * np.pi * np.cumsum(freq) / sr
|
||||
|
||||
sig = np.zeros(n_samp, dtype=np.float64)
|
||||
for k, amp in enumerate(harmonics, start=1):
|
||||
if amp <= 0.0:
|
||||
continue
|
||||
sig += amp * np.sin(k * phase + rng.uniform(0.0, 2.0 * np.pi))
|
||||
|
||||
env = np.full(n_samp, sustain, dtype=np.float64)
|
||||
a_n = min(int(attack_s * sr), n_samp)
|
||||
if a_n > 1:
|
||||
env[:a_n] = np.linspace(0.0, 1.0, a_n)
|
||||
r_n = min(int(release_s * sr), n_samp)
|
||||
if r_n > 1:
|
||||
env[-r_n:] *= np.linspace(1.0, 0.0, r_n)
|
||||
sig *= env
|
||||
|
||||
i0 = int(float(starts[i]) * sr)
|
||||
i1 = min(i0 + n_samp, n_total)
|
||||
if i1 <= i0:
|
||||
continue
|
||||
out[i0:i1] += sig[: i1 - i0]
|
||||
|
||||
if noise_sigma > 0.0:
|
||||
out += rng.normal(0.0, noise_sigma, n_total)
|
||||
|
||||
peak_val = float(np.abs(out).max())
|
||||
if peak_val > 0.0:
|
||||
out *= peak / peak_val
|
||||
return out.astype(np.float32)
|
||||
|
||||
|
||||
def render_melody(
|
||||
mel: Melody,
|
||||
sr: int = DEFAULT.sample_rate,
|
||||
*,
|
||||
timbre: str = "song",
|
||||
seed: int = 0,
|
||||
**overrides,
|
||||
) -> np.ndarray:
|
||||
"""按预设音色渲染旋律。``timbre`` 取 ``"song"`` 或 ``"hum"``。"""
|
||||
params = dict(SONG_TIMBRE if timbre == "song" else HUM_TIMBRE)
|
||||
params.update(overrides)
|
||||
return render_notes(
|
||||
mel.midis, mel.starts, mel.durs, sr, seed=seed, **params
|
||||
)
|
||||
Reference in New Issue
Block a user