|
import pytest
|
|
import numpy as np
|
|
import librosa
|
|
from preprocess import preprocess_audio
|
|
|
|
def test_preprocess_audio():
|
|
"""
|
|
Test the `preprocess_audio` function to ensure it processes audio correctly.
|
|
"""
|
|
|
|
sample_rate = 16000
|
|
duration = 1.0
|
|
t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False)
|
|
audio = 0.5 * np.sin(2 * np.pi * 440 * t)
|
|
|
|
|
|
processed_audio = preprocess_audio(audio, sample_rate)
|
|
|
|
|
|
|
|
expected_length = len(processed_audio)
|
|
|
|
|
|
assert isinstance(processed_audio, np.ndarray), "Processed audio should be a numpy array"
|
|
assert processed_audio.shape[0] == expected_length, "Processed audio length is incorrect"
|
|
assert np.all(np.isfinite(processed_audio)), "Processed audio contains non-finite values"
|
|
|
|
def test_preprocess_audio_edge_cases():
|
|
"""
|
|
Test edge cases for `preprocess_audio` function.
|
|
"""
|
|
sample_rate = 16000
|
|
empty_audio = np.array([])
|
|
|
|
|
|
processed_audio = preprocess_audio(empty_audio, sample_rate)
|
|
|
|
|
|
assert isinstance(processed_audio, np.ndarray), "Processed audio should be a numpy array"
|
|
assert processed_audio.size == 0, "Processed audio should be empty for empty input"
|
|
|
|
|
|
short_audio = np.array([0.0])
|
|
|
|
|
|
processed_audio = preprocess_audio(short_audio, sample_rate)
|
|
|
|
|
|
assert isinstance(processed_audio, np.ndarray), "Processed audio should be a numpy array"
|
|
assert processed_audio.size > 0, "Processed audio should not be empty for short input"
|
|
|
|
if __name__ == "__main__":
|
|
pytest.main()
|
|
|