forked from wlkns-dev/stimmwerk
43 lines
1.6 KiB
Python
43 lines
1.6 KiB
Python
"""Optional audio backend smoke test; run only in the full analysis environment."""
|
|
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
import numpy as np
|
|
import soundfile as sf
|
|
import torch
|
|
|
|
from song_analysis.backends import detect_pitch
|
|
|
|
|
|
class AudioSmokeTests(unittest.TestCase):
|
|
def test_one_second_tone_has_a_bounded_pitch_batch(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
audio = Path(d) / "tone.wav"
|
|
t = np.arange(16000, dtype=np.float32) / 16000
|
|
sf.write(audio, 0.2 * np.sin(2 * np.pi * 440 * t), 16000)
|
|
def predict(signal, sample_rate, **kwargs):
|
|
self.assertEqual(sample_rate, 16000)
|
|
self.assertLessEqual(kwargs["batch_size"], 512)
|
|
frames = 1 + signal.shape[1] // kwargs["hop_length"]
|
|
return torch.full((1, frames), 440.0), torch.ones((1, frames))
|
|
with patch("torchcrepe.predict", side_effect=predict):
|
|
result = detect_pitch(audio)
|
|
self.assertEqual(len(result["hz"]), 101)
|
|
self.assertEqual(result["hz"][50], 440.0)
|
|
|
|
def test_one_second_tone_runs_crepe(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
audio = Path(d) / "tone.wav"
|
|
t = np.arange(16000, dtype=np.float32) / 16000
|
|
sf.write(audio, 0.2 * np.sin(2 * np.pi * 440 * t), 16000)
|
|
result = detect_pitch(audio)
|
|
self.assertEqual(len(result["hz"]), 101)
|
|
self.assertTrue(any(400 < hz < 480 for hz in result["hz"]))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|