Spaces:
Running
Running
| import sounddevice as sd | |
| import numpy as np | |
| import time | |
| import queue | |
| def test_microphone(duration=3): | |
| devices = sd.query_devices() | |
| print("\nScanning all input devices for signal...\n") | |
| for i, dev in enumerate(devices): | |
| if dev['max_input_channels'] > 0: | |
| print(f"Testing Device {i}: {dev['name']}") | |
| try: | |
| q = queue.Queue() | |
| def callback(indata, frames, time, status): | |
| q.put(indata.copy()) | |
| # Use device specific sample rate to avoid mismatches | |
| sr = int(dev['default_samplerate']) | |
| with sd.InputStream(device=i, samplerate=sr, channels=1, dtype=np.int16, callback=callback): | |
| start = time.time() | |
| max_amp = 0 | |
| while time.time() - start < 1.0: # Test for 1 second | |
| try: | |
| data = q.get(timeout=0.1) | |
| current_max = np.max(np.abs(data)) | |
| max_amp = max(max_amp, current_max) | |
| except queue.Empty: | |
| pass | |
| print(f" -> Max Amplitude: {max_amp}") | |
| if max_amp > 100: | |
| print(" ✅ SIGNAL DETECTED!") | |
| else: | |
| print(" ❌ No Signal (Silence)") | |
| except Exception as e: | |
| print(f" -> Error: {e}") | |
| print("-" * 30) | |
| if __name__ == "__main__": | |
| test_microphone() | |