Files
HumRetrieval/qbh/config.py
T

87 lines
2.4 KiB
Python

"""集中管理全部算法阈值。
所有可调参数都收在这里,便于复现实验、做消融与参数扫描。
`Config` 是冻结 dataclass,用 `with_()` 派生变体,避免全局可变状态。
"""
from __future__ import annotations
from dataclasses import dataclass, fields, replace
@dataclass(frozen=True)
class Config:
# ---------- 采样与预处理 ----------
sample_rate: int = 16_000
band_low_hz: float = 60.0
band_high_hz: float = 2_000.0
band_order: int = 4
denoise_window: int = 3
target_rms: float = 0.06
trim_threshold_db: float = -45.0
trim_padding_ms: float = 50.0
# ---------- 分帧 ----------
frame_ms: float = 48.0
hop_ms: float = 10.0
# ---------- YIN 基频提取 ----------
fmin_hz: float = 70.0
fmax_hz: float = 1_000.0
yin_threshold: float = 0.12
yin_max_cmndf: float = 0.55
voicing_conf: float = 0.5
voicing_db_below_peak: float = -45.0
median_filter_frames: int = 7
octave_fold_low: float = 9.0
octave_fold_high: float = 15.0
# ---------- 音符切分 ----------
split_semitones: float = 0.8
min_note_ms: float = 70.0
bridge_gap_ms: float = 60.0
merge_semitones: float = 0.5
repeat_merge_semitones: float = 0.35
max_notes: int = 600
# ---------- 音程 / 轮廓编码 ----------
interval_clip: float = 12.0
contour_jump: float = 0.75
# ---------- 粗召回索引 ----------
candidates_min: int = 40
candidates_per_sqrt: float = 7.0
candidate_score_floor: float = 0.10
full_scan_below_songs: int = 60
contour_variants_max: int = 40
# ---------- 精排 DTW ----------
dtw_pitch_free_band: float = 0.3
dtw_pitch_cap: float = 10.0
dtw_dur_cap: float = 2.5
dtw_pitch_weight: float = 1.0
dtw_dur_weight: float = 0.7
transpose_range: int = 24
transpose_span: int = 4
transpose_candidates: int = 8
dtw_score_lambda: float = 0.6
dtw_max_cells: int = 4_000_000
# ---------- 分数融合 ----------
w_dtw: float = 0.80
w_index: float = 0.20
# ---------- 检索 ----------
top_k: int = 10
def to_dict(self) -> dict:
"""转为普通字典,便于写入评测报告 / 前端展示。"""
return {f.name: getattr(self, f.name) for f in fields(self)}
def with_(self, **kwargs) -> "Config":
"""派生一个只改动指定字段的新配置。"""
return replace(self, **kwargs)
DEFAULT = Config()