74 lines
2.4 KiB
Python
74 lines
2.4 KiB
Python
"""pytest 公共夹具。
|
|
|
|
测试尽量不依赖磁盘上的真实曲库:多数用例用合成的小旋律,只有 API 端到端
|
|
测试才会构建一个临时曲库(并跳过音频渲染以加速)。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import numpy as np
|
|
import pytest
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
sys.path.insert(0, str(ROOT))
|
|
|
|
from qbh.config import DEFAULT, Config # noqa: E402
|
|
from qbh.melody import Melody # noqa: E402
|
|
from qbh.synth import render_melody # noqa: E402
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def cfg() -> Config:
|
|
return DEFAULT
|
|
|
|
|
|
@pytest.fixture
|
|
def c_major_melody() -> Melody:
|
|
"""C 大调音阶上的一段旋律(8 个音)。"""
|
|
midis = [60, 62, 64, 67, 69, 67, 64, 62]
|
|
starts = np.arange(8) * 0.5
|
|
durs = np.full(8, 0.45)
|
|
return Melody(np.array(midis, float), starts, durs, "scale", "C大调音阶")
|
|
|
|
|
|
@pytest.fixture
|
|
def long_melody() -> Melody:
|
|
"""20 个音的旋律,用于片段/子序列相关测试。"""
|
|
midis = [60, 62, 64, 65, 67, 69, 71, 72, 71, 69, 67, 65, 64, 62, 60, 62, 64, 67, 69, 72]
|
|
starts = np.arange(20) * 0.5
|
|
durs = np.full(20, 0.45)
|
|
return Melody(np.array(midis, float), starts, durs, "long", "长旋律")
|
|
|
|
|
|
@pytest.fixture
|
|
def tiny_library(cfg: Config):
|
|
"""三条合成旋律构成的极简曲库(内存 SQLite)。"""
|
|
from qbh.library import Library
|
|
from qbh.store import Store
|
|
|
|
store = Store(":memory:")
|
|
library = Library(store, cfg, midi_dir=Path("__nonexistent__"), audio_dir=Path("__nonexistent__"))
|
|
melodies = [
|
|
Melody(np.array([60, 62, 64, 67, 69, 67, 64, 62], float),
|
|
np.arange(8) * 0.5, np.full(8, 0.45), "m_a", "旋律A"),
|
|
Melody(np.array([72, 71, 69, 67, 65, 64, 62, 60], float),
|
|
np.arange(8) * 0.5, np.full(8, 0.45), "m_b", "旋律B"),
|
|
Melody(np.array([60, 60, 67, 67, 69, 69, 67], float),
|
|
np.arange(7) * 0.5, np.full(7, 0.45), "m_c", "旋律C"),
|
|
]
|
|
for melody in melodies:
|
|
library.add_melody(melody, song_id=melody.song_id, title=melody.title,
|
|
source="test", render_audio=False)
|
|
yield library
|
|
store.close()
|
|
|
|
|
|
@pytest.fixture
|
|
def hum_audio(c_major_melody, cfg):
|
|
"""把 C 大调旋律渲染成「哼唱」音频。"""
|
|
return render_melody(c_major_melody, cfg.sample_rate, timbre="hum", seed=7)
|