File size: 2,342 Bytes
1778490
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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