37 lines
1.1 KiB
Python
37 lines
1.1 KiB
Python
"""Shared pytest fixtures for audio-fingerprint tests."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
import numpy as np
|
|
import pytest
|
|
import soundfile as sf
|
|
|
|
# Ensure the package source is importable as `src.*` when pytest is invoked
|
|
# from the package directory without an installed wheel.
|
|
_PKG_DIR = Path(__file__).resolve().parents[1]
|
|
if str(_PKG_DIR) not in sys.path:
|
|
sys.path.insert(0, str(_PKG_DIR))
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_audio() -> tuple[np.ndarray, int]:
|
|
"""Generate a 2-second 440 Hz sine for tests."""
|
|
sample_rate = 22050
|
|
duration = 2.0
|
|
t = np.linspace(0, duration, int(sample_rate * duration))
|
|
audio = 0.5 * np.sin(2 * np.pi * 440.0 * t)
|
|
return audio.astype(np.float32), sample_rate
|
|
|
|
|
|
@pytest.fixture
|
|
def audio_file(sample_audio: tuple[np.ndarray, int]) -> Path:
|
|
"""Persist the sample audio to a temp WAV path."""
|
|
audio, sample_rate = sample_audio
|
|
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
|
|
sf.write(tmp.name, audio, sample_rate)
|
|
return Path(tmp.name)
|