import os import wave import pytest import torchaudio import os import sys current_dir = os.path.dirname(os.path.abspath(__file__)) parent_dir = os.path.dirname(current_dir) sys.path.append(parent_dir) from Client import Client @pytest.fixture def mock_client(): client = Client("test_sid", "test_client_id", original_sr=44100) return client def test_client_init(mock_client): assert mock_client.sid == "test_sid" assert mock_client.client_id == "test_client_id" assert mock_client.call_id == None assert mock_client.buffer == bytearray() assert mock_client.output_path == "test_sid_output_audio.wav" assert mock_client.target_language == None assert mock_client.original_sr == 44100 assert mock_client.vad.sample_rate == 16000 assert mock_client.vad.frame_length == 25 assert mock_client.vad.frame_shift == 20 assert mock_client.vad.energy_threshold == 0.05 assert mock_client.vad.pre_emphasis == 0.95 def test_client_add_bytes(mock_client): mock_client.add_bytes(b"test") assert mock_client.buffer == b"test" def test_client_resample_and_clear(mock_client): location = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__))) speaking_bytes = wave.open(location + "/speaking.wav", "rb").readframes(-1) mock_client.add_bytes(speaking_bytes) resampled_waveform = mock_client.resample_and_clear() torchaudio.save(location + "testoutput.wav", resampled_waveform, 16000) with wave.open(location + "testoutput.wav", "rb") as wf: sample_rate = wf.getframerate() assert mock_client.buffer == bytearray() assert sample_rate == 16000 def test_client_vad(mock_client): location = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__))) speaking_bytes = wave.open(location + "/speaking.wav", "rb").readframes(-1) mock_client.add_bytes(speaking_bytes) resampled_waveform = mock_client.resample_and_clear() assert mock_client.buffer == bytearray() assert mock_client.vad_analyse(resampled_waveform) == True silent_bytes = wave.open(location + "/silence.wav", "rb").readframes(-1) mock_client.add_bytes(silent_bytes) resampled_waveform = mock_client.resample_and_clear() assert mock_client.buffer == bytearray() assert mock_client.vad_analyse(resampled_waveform) == False