"""Voice worker: binary frames, input conversion to 16 kHz mono, clip normalisation.""" from __future__ import annotations import numpy as np import pytest from voice_worker.audio import InputConverter, peak_normalise from voice_worker.protocol import (KIND_AUDIO, KIND_TTS, ProtocolError, decode_audio_frame, encode_audio_frame, encode_tts_frame, decode_tts_frame) def _pcm16(x: np.ndarray) -> bytes: return (np.clip(x, -1, 1) * 32767).astype(" float: spec = np.abs(np.fft.rfft(x * np.hanning(len(x)))) return float(np.fft.rfftfreq(len(x), 1 / rate)[int(np.argmax(spec))]) @pytest.mark.unit def test_audio_frame_round_trip_keeps_speaker_and_pcm(): pcm = _pcm16(np.linspace(-0.5, 0.5, 960)) frame = encode_audio_frame("123456789012345678", pcm) assert frame[0] == KIND_AUDIO speaker, payload = decode_audio_frame(frame) assert speaker == "123456789012345678" assert payload == pcm @pytest.mark.unit def test_tts_frame_round_trip_keeps_id_and_sequence(): pcm = b"\x01\x00\x02\x00" frame = encode_tts_frame("reply-7", 3, pcm) assert frame[0] == KIND_TTS assert decode_tts_frame(frame) == ("reply-7", 3, pcm) @pytest.mark.unit @pytest.mark.parametrize("frame", [b"", b"\x01", b"\x09\x01a\x00\x00", b"\x01\x05ab", b"\x01\x01a\x00", b"\x01\x02\xff\xfe\x00\x00"]) def test_malformed_audio_frames_are_rejected(frame): with pytest.raises(ProtocolError): decode_audio_frame(frame) @pytest.mark.unit def test_48k_stereo_becomes_16k_mono_and_keeps_the_tone(): rate = 48000 t = np.arange(rate) / rate tone = 0.5 * np.sin(2 * np.pi * 1000 * t) stereo = np.stack([tone, tone], axis=1).reshape(-1) out = InputConverter(rate, 2).push(_pcm16(stereo)) assert out.dtype == np.float32 assert abs(len(out) - rate // 3) <= 1 assert abs(_dominant_hz(out, 16000) - 1000) < 5 assert 0.4 < np.max(np.abs(out[500:])) < 0.6 @pytest.mark.unit def test_tones_above_the_new_nyquist_are_filtered_out(): rate = 48000 t = np.arange(rate) / rate high = 0.5 * np.sin(2 * np.pi * 11000 * t) out = InputConverter(rate, 1).push(_pcm16(high)) # At least 30 dB down instead of folding back to 5 kHz. assert np.sqrt(np.mean(out[500:] ** 2)) < 0.5 / np.sqrt(2) * 10 ** (-30 / 20) @pytest.mark.unit def test_streaming_in_20ms_frames_matches_one_shot_conversion(): rng = np.random.default_rng(1) sig = rng.uniform(-0.5, 0.5, 48000 * 2 // 2 * 2) one = InputConverter(48000, 2).push(_pcm16(sig)) conv = InputConverter(48000, 2) raw = _pcm16(sig) step = 960 * 2 * 2 # 20 ms of 48 kHz stereo PCM16 parts = [conv.push(raw[i:i + step]) for i in range(0, len(raw), step)] assert np.allclose(np.concatenate(parts), one, atol=1e-6) @pytest.mark.unit def test_16k_mono_passes_through(): sig = np.linspace(-0.25, 0.25, 1600) out = InputConverter(16000, 1).push(_pcm16(sig)) assert np.allclose(out, sig, atol=1e-4) @pytest.mark.unit def test_unsupported_input_formats_are_refused(): with pytest.raises(ValueError): InputConverter(44100, 2) with pytest.raises(ValueError): InputConverter(48000, 6) @pytest.mark.unit def test_odd_byte_counts_are_carried_to_the_next_frame(): sig = _pcm16(np.full(10, 0.5)) conv = InputConverter(16000, 1) a = conv.push(sig[:5]) b = conv.push(sig[5:]) assert len(a) + len(b) == 10 @pytest.mark.unit def test_quiet_clip_is_raised_to_the_target_peak(): x = np.full(100, 0.2, dtype=np.float32) assert np.isclose(np.max(np.abs(peak_normalise(x))), 0.9) @pytest.mark.unit def test_near_silence_is_not_boosted_past_the_gain_cap(): x = np.full(100, 0.001, dtype=np.float32) assert np.isclose(np.max(np.abs(peak_normalise(x))), 0.01) @pytest.mark.unit def test_loud_clip_is_brought_down_to_the_target_peak(): x = np.array([0.0, 1.0, -0.5], dtype=np.float32) assert np.isclose(np.max(np.abs(peak_normalise(x))), 0.9)