-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathtest_mixer.py
More file actions
58 lines (45 loc) · 1.7 KB
/
test_mixer.py
File metadata and controls
58 lines (45 loc) · 1.7 KB
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
# type: ignore
import numpy as np
import pytest
from livekit.rtc import AudioMixer
from livekit.rtc.utils import sine_wave_generator
SAMPLE_RATE = 48000
BLOCKSIZE = SAMPLE_RATE // 10
@pytest.mark.asyncio
async def test_mixer_two_sine_waves():
"""
Test that mixing two sine waves (440Hz and 880Hz) produces an output
containing both frequency components.
"""
duration = 1.0
mixer = AudioMixer(
sample_rate=SAMPLE_RATE,
num_channels=1,
blocksize=BLOCKSIZE,
stream_timeout_ms=100,
capacity=100,
)
stream1 = sine_wave_generator(440, duration, SAMPLE_RATE)
stream2 = sine_wave_generator(880, duration, SAMPLE_RATE)
mixer.add_stream(stream1)
mixer.add_stream(stream2)
mixer.end_input()
mixed_signals = []
async for frame in mixer:
data = np.frombuffer(frame.data.tobytes(), dtype=np.int16)
mixed_signals.append(data)
await mixer.aclose()
if not mixed_signals:
pytest.fail("No frames were produced by the mixer.")
mixed_signal = np.concatenate(mixed_signals)
# Use FFT to analyze frequency components.
fft = np.fft.rfft(mixed_signal)
freqs = np.fft.rfftfreq(len(mixed_signal), 1 / SAMPLE_RATE)
magnitude = np.abs(fft)
# Identify peak frequencies. We'll pick the 5 highest peaks.
peak_indices = np.argsort(magnitude)[-5:]
peak_freqs = freqs[peak_indices]
print("Peak frequencies:", peak_freqs)
# Assert that the peaks include 440Hz and 880Hz (with a tolerance of ±5 Hz)
assert any(np.isclose(peak_freqs, 440, atol=5)), f"Expected 440 Hz in peaks, got: {peak_freqs}"
assert any(np.isclose(peak_freqs, 880, atol=5)), f"Expected 880 Hz in peaks, got: {peak_freqs}"