from __future__ import annotations import time from dataclasses import dataclass from typing import Any from recorder import ( list_input_devices, resolve_input_device, start_recording, stop_recording, ) @dataclass(frozen=True) class MicrophoneTestResult: ok: bool message: str class AudioSettingsService: def list_input_devices(self) -> list[dict[str, Any]]: return list_input_devices() def resolve_input_device(self, input_spec: str | int | None) -> int | None: return resolve_input_device(input_spec) def test_microphone( self, input_spec: str | int | None, *, duration_sec: float = 0.35, ) -> MicrophoneTestResult: try: stream, record = start_recording(input_spec) time.sleep(duration_sec) audio = stop_recording(stream, record) except Exception as exc: return MicrophoneTestResult( ok=False, message=f"Microphone test failed: {exc}", ) if getattr(audio, "size", 0) > 0: return MicrophoneTestResult( ok=True, message="Microphone test successful.", ) return MicrophoneTestResult( ok=False, message="No audio captured. Try another device.", )