Files
closed_loop_audio_test_suite/test_audio_playback.py

71 lines
2.5 KiB
Python

#!/usr/bin/env python3
import numpy as np
import sounddevice as sd
import yaml
from pathlib import Path
print("Audio Device Diagnostic Test")
print("=" * 60)
devices = sd.query_devices()
print("\nAvailable devices:")
for i, dev in enumerate(devices):
print(f"{i}: {dev['name']}")
print(f" Inputs: {dev['max_input_channels']}, Outputs: {dev['max_output_channels']}")
print(f" Sample Rate: {dev['default_samplerate']}")
print()
config_path = Path('config.yaml')
with open(config_path, 'r') as f:
config = yaml.safe_load(f)
sample_rate = config['audio']['sample_rate']
duration = 1.0
frequency = 1000
print(f"\nGenerating {frequency}Hz tone for {duration}s at {sample_rate}Hz...")
t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False)
tone = 0.3 * np.sin(2 * np.pi * frequency * t)
print(f"Tone amplitude: min={tone.min():.3f}, max={tone.max():.3f}, mean={tone.mean():.3f}")
print(f"Tone shape: {tone.shape}")
default_device = 6
print(f"\nTest Configuration:")
print(f"Using device: {default_device} ({devices[default_device]['name']})")
print(f"This routes through PulseAudio to the Scarlett 2i2")
stereo_tone = np.column_stack([tone, tone])
print(f"Stereo signal shape: {stereo_tone.shape}")
print("\nPlaying and recording...")
print("(You should hear a 1kHz tone through the Scarlett outputs)")
print("(Connect Scarlett outputs to inputs for loopback test)")
print()
try:
recording = sd.playrec(stereo_tone, samplerate=sample_rate,
channels=2,
device=default_device,
blocking=True)
print("Recording completed!")
print(f"Recording shape: {recording.shape}")
print(f"Channel 1 - min={recording[:, 0].min():.6f}, max={recording[:, 0].max():.6f}, RMS={np.sqrt(np.mean(recording[:, 0]**2)):.6f}")
print(f"Channel 2 - min={recording[:, 1].min():.6f}, max={recording[:, 1].max():.6f}, RMS={np.sqrt(np.mean(recording[:, 1]**2)):.6f}")
ch1_rms = np.sqrt(np.mean(recording[:, 0]**2))
ch2_rms = np.sqrt(np.mean(recording[:, 1]**2))
if ch1_rms < 0.001 and ch2_rms < 0.001:
print("\n⚠️ WARNING: Very low signal detected - likely just noise!")
print("The audio output may not be reaching the input.")
elif ch1_rms > 0.01 or ch2_rms > 0.01:
print("\n✓ Good signal detected!")
else:
print("\n⚠️ Low signal - check connections")
except Exception as e:
print(f"\n✗ Error: {e}")