import numpy as np import pytest scipy_signal = pytest.importorskip("scipy.signal") from client.audio import resample_24k_to_16k def test_length_and_dtype(): frame = np.zeros(1920, dtype=np.int16) out = resample_24k_to_16k(frame) assert out.shape == (1280,) assert out.dtype == np.int16 def test_constant_signal_stays_constant(): frame = np.full(1920, 1000, dtype=np.int16) out = resample_24k_to_16k(frame) # poly resample of a constant is the same constant up to filter ringing assert np.abs(out.astype(int).mean() - 1000) < 5 def test_silence_in_silence_out(): frame = np.zeros(1920, dtype=np.int16) out = resample_24k_to_16k(frame) assert int(np.max(np.abs(out))) == 0 def test_sine_peak_frequency_preserved(): # 1 kHz sine at 24 kHz: 80 ms = 80 cycles. t = np.arange(1920) / 24_000.0 sig = (8000 * np.sin(2 * np.pi * 1000 * t)).astype(np.int16) out = resample_24k_to_16k(sig) # 1 kHz in a 1280-sample 16 kHz window is bin (1000 / 16000) * 1280 = 80. spec = np.abs(np.fft.rfft(out.astype(np.float32))) peak = int(np.argmax(spec)) assert abs(peak - 80) <= 1 def test_zero_length_input_raises(): with pytest.raises(ValueError): resample_24k_to_16k(np.zeros(0, dtype=np.int16)) def test_wrong_dtype_rejected(): with pytest.raises(TypeError): resample_24k_to_16k(np.zeros(1920, dtype=np.float32))