File size: 3,468 Bytes
65da30d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import os
import time
import tempfile
import pytest
import soundfile as sf
from reachy_mini.media.media_manager import MediaManager, MediaBackend

@pytest.mark.audio
def test_play_sound_default_backend() -> None:
    """Test playing a sound with the default backend."""
    media = MediaManager(backend=MediaBackend.DEFAULT_NO_VIDEO)
    # Use a short sound file present in your assets directory
    sound_file = "wake_up.wav"  # Change to a valid file if needed
    media.play_sound(sound_file)
    print("Playing sound with default backend...")
    # Wait a bit to let the sound play (non-blocking backend)
    time.sleep(2)
    # No assertion: test passes if no exception is raised.
    # Sound should be audible if the audio device is correctly set up.

@pytest.mark.audio
def test_record_audio_and_file_exists() -> None:
    """Test recording audio and check that the file exists and is not empty."""
    media = MediaManager(backend=MediaBackend.DEFAULT_NO_VIDEO)
    duration = 2  # seconds
    tmpfile = tempfile.NamedTemporaryFile(suffix='.wav', delete=False)
    tmpfile.close()
    media.start_recording()
    time.sleep(duration)
    media.stop_recording()
    audio = media.get_audio_sample()
    samplerate = media.get_input_audio_samplerate()
    if audio is not None:
        sf.write(tmpfile.name, audio, samplerate)
    assert os.path.exists(tmpfile.name)
    assert os.path.getsize(tmpfile.name) > 0
    # comment the following line if you want to keep the file for inspection
    os.remove(tmpfile.name)
    # print(f"Recorded audio saved to {tmpfile.name}")

@pytest.mark.audio
def test_DoA() -> None:
    """Test Direction of Arrival (DoA) estimation."""
    media = MediaManager(backend=MediaBackend.DEFAULT_NO_VIDEO)
    doa = media.audio.get_DoA()
    assert doa is not None
    assert isinstance(doa, tuple)
    assert len(doa) == 2
    assert isinstance(doa[0], float)
    assert isinstance(doa[1], bool)


'''
@pytest.mark.audio_gstreamer
def test_play_sound_gstreamer_backend() -> None:
    """Test playing a sound with the GStreamer backend."""
    media = MediaManager(backend=MediaBackend.GSTREAMER)
    time.sleep(2)  # Give some time for the audio system to initialize
    # Use a short sound file present in your assets directory
    sound_file = "wake_up.wav"  # Change to a valid file if needed
    media.play_sound(sound_file)
    print("Playing sound with GStreamer backend...")
    # Wait a bit to let the sound play (non-blocking backend)
    time.sleep(2)
    # No assertion: test passes if no exception is raised.
    # Sound should be audible if the audio device is correctly set up.
'''

@pytest.mark.audio_gstreamer
def test_record_audio_and_file_exists_gstreamer() -> None:
    """Test recording audio and check that the file exists and is not empty."""
    media = MediaManager(backend=MediaBackend.GSTREAMER)
    duration = 2  # seconds
    tmpfile = tempfile.NamedTemporaryFile(suffix='.wav', delete=False)
    tmpfile.close()
    media.start_recording()
    time.sleep(duration)
    audio = media.get_audio_sample()
    assert audio is not None    
    media.stop_recording()


def test_no_media() -> None:
    """Test that methods handle uninitialized media gracefully."""
    media = MediaManager(backend=MediaBackend.NO_MEDIA)

    assert media.get_frame() is None
    assert media.get_audio_sample() is None
    assert media.get_input_audio_samplerate() == -1
    assert media.get_output_audio_samplerate() == -1