2026-02-13 20:33:58 +01:00
|
|
|
# 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
|
2026-02-17 21:32:40 +01:00
|
|
|
# This assumes the test is run from the project root or via unittest.sh
|
|
|
|
|
sys.path.append('MicroPythonOS/internal_filesystem/lib')
|
2026-02-13 20:33:58 +01:00
|
|
|
|
2026-02-17 21:32:40 +01:00
|
|
|
from mpos import AudioManager
|
2026-02-13 20:33:58 +01:00
|
|
|
|
2026-02-17 21:32:40 +01:00
|
|
|
class TestADCRecording(unittest.TestCase):
|
|
|
|
|
"""Test ADC recording functionality."""
|
2026-02-13 20:33:58 +01:00
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
"""Set up test fixtures."""
|
2026-02-17 21:32:40 +01:00
|
|
|
self.test_file = "test_recording.wav"
|
2026-02-13 20:33:58 +01:00
|
|
|
|
2026-02-17 21:32:40 +01:00
|
|
|
# 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)
|
2026-02-13 20:33:58 +01:00
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
|
"""Clean up test files."""
|
|
|
|
|
try:
|
2026-02-17 21:32:40 +01:00
|
|
|
os.remove(self.test_file)
|
2026-02-13 20:33:58 +01:00
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
|
2026-02-17 21:32:40 +01:00
|
|
|
def test_record_wav_adc(self):
|
|
|
|
|
"""Test recording a short WAV file using ADC."""
|
2026-02-13 20:33:58 +01:00
|
|
|
|
2026-02-17 21:32:40 +01:00
|
|
|
# Record for 200ms
|
|
|
|
|
duration_ms = 200
|
2026-02-13 20:33:58 +01:00
|
|
|
sample_rate = 16000
|
2026-02-17 21:32:40 +01:00
|
|
|
|
|
|
|
|
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
|
2026-02-13 20:33:58 +01:00
|
|
|
)
|
|
|
|
|
|
2026-02-17 21:32:40 +01:00
|
|
|
self.assertTrue(success, "AudioManager.record_wav_adc returned False")
|
2026-02-13 20:33:58 +01:00
|
|
|
|
2026-02-17 21:32:40 +01:00
|
|
|
# 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)
|
2026-02-13 20:33:58 +01:00
|
|
|
|
2026-02-17 21:32:40 +01:00
|
|
|
# Verify file exists
|
2026-02-13 20:33:58 +01:00
|
|
|
try:
|
2026-02-17 21:32:40 +01:00
|
|
|
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})")
|
2026-02-13 20:33:58 +01:00
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|