-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathtest_adc_recording.py
More file actions
88 lines (67 loc) · 3.07 KB
/
test_adc_recording.py
File metadata and controls
88 lines (67 loc) · 3.07 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
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
# Test ADC Recording Integration
# Tests the new ADCRecordStream with adaptive frequency control
# Run with: ./MicroPythonOS/tests/unittest.sh MicroPythonOS/tests/test_adc_recording.py
import unittest
import time
import os
import sys
# Add lib path for imports
# In MicroPython, os.path doesn't exist, so we construct the path manually
# This assumes the test is run from the project root or via unittest.sh
sys.path.append('MicroPythonOS/internal_filesystem/lib')
from mpos import AudioManager
class TestADCRecording(unittest.TestCase):
"""Test ADC recording functionality."""
def setUp(self):
"""Set up test fixtures."""
self.test_file = "test_recording.wav"
# Ensure AudioManager is initialized (mocking pins if needed)
# On desktop, it will use simulation mode
if not AudioManager._instance:
# Initialize with dummy values if needed, but adc_mic_pin is supported
AudioManager(adc_mic_pin=1)
def tearDown(self):
"""Clean up test files."""
try:
os.remove(self.test_file)
except:
pass
def test_record_wav_adc(self):
"""Test recording a short WAV file using ADC."""
# Record for 200ms
duration_ms = 200
sample_rate = 16000
print(f"Starting recording for {duration_ms}ms...")
# Start recording
# Note: On desktop this will use the simulation mode in ADCRecordStream
success = AudioManager.record_wav_adc(
self.test_file,
duration_ms=duration_ms,
sample_rate=sample_rate
)
self.assertTrue(success, "AudioManager.record_wav_adc returned False")
# Wait for recording to finish (plus a buffer for thread startup/shutdown)
# Simulation mode might be slower or faster depending on system load
time.sleep(duration_ms / 1000.0 + 1.0)
# Verify file exists
try:
st = os.stat(self.test_file)
file_size = st[6]
file_exists = True
except OSError:
file_exists = False
file_size = 0
self.assertTrue(file_exists, f"Recording file {self.test_file} was not created")
# Verify file size is reasonable
# Header is 44 bytes
# 200ms at 16000Hz, 16-bit mono = 0.2 * 16000 * 2 = 6400 bytes
# Total should be around 6444 bytes
expected_data_size = int(duration_ms / 1000.0 * sample_rate * 2)
expected_total_size = 44 + expected_data_size
print(f"Created WAV file size: {file_size} bytes (Expected approx: {expected_total_size})")
self.assertTrue(file_size > 44, "File contains only header or is empty")
# Allow some margin of error for timing differences in test environment
# But it should have recorded *something* significant
self.assertTrue(file_size > 1000, f"File size {file_size} seems too small (expected ~{expected_total_size})")
if __name__ == '__main__':
unittest.main()