diff options
| author | HiFiPhile <[email protected]> | 2026-06-22 21:30:58 +0200 |
|---|---|---|
| committer | HiFiPhile <[email protected]> | 2026-06-22 21:30:58 +0200 |
| commit | 693cdce08e14833f26f4e8a1f26e4fd546be4c35 (patch) | |
| tree | 7667d2223dc32d9f21b5b60ab200e64ed46e3e20 /examples/device/audio_4_channel_mic/src/plot_audio_samples.py | |
| parent | 41e9eaa65a935136085d78ec4b99c81ff991b560 (diff) | |
| parent | cd3561bf158afd5a5718904b8139a338d1e3b67c (diff) | |
Merge remote-tracking branch 'tinyusb/master' into pr-osal-spin-deinit
Signed-off-by: HiFiPhile <[email protected]>
Diffstat (limited to 'examples/device/audio_4_channel_mic/src/plot_audio_samples.py')
| -rwxr-xr-x | examples/device/audio_4_channel_mic/src/plot_audio_samples.py | 49 |
1 files changed, 47 insertions, 2 deletions
diff --git a/examples/device/audio_4_channel_mic/src/plot_audio_samples.py b/examples/device/audio_4_channel_mic/src/plot_audio_samples.py index 4d61e7f5e..618745934 100755 --- a/examples/device/audio_4_channel_mic/src/plot_audio_samples.py +++ b/examples/device/audio_4_channel_mic/src/plot_audio_samples.py @@ -4,6 +4,51 @@ import matplotlib.pyplot as plt import numpy as np import platform + +def find_windows_input_device(name_hint, channels, preferred_apis=None): + """Pick a Windows input device index from query_devices() output.""" + preferred_apis = preferred_apis or [] + name_hint = name_hint.lower() + candidates = [] + + for index in range(len(sd.query_devices())): + device_info = sd.query_devices(index) + max_input_channels = int(device_info.get('max_input_channels', 0)) + + if max_input_channels < channels: + continue + + device_name = str(device_info.get('name', '')).lower() + if name_hint not in device_name: + continue + + score = 0 + + # Prefer exact channel matches to avoid selecting a different stream layout. + if max_input_channels == channels: + score += 100 + else: + score += 10 + + hostapi_index = int(device_info.get('hostapi', -1)) + api_name = '' + if hostapi_index >= 0: + api_name = str(sd.query_hostapis(hostapi_index)).lower() + + for priority, api_hint in enumerate(preferred_apis): + if api_hint in api_name: + score += 50 - priority + break + + candidates.append((score, index)) + + if not candidates: + raise ValueError( + 'No input device matching hint="{}" with at least {} channels'.format(name_hint, channels) + ) + + return max(candidates, key=lambda item: item[0])[1] + if __name__ == '__main__': # If you got "ValueError: No input device matching", that is because your PC name example device @@ -14,8 +59,8 @@ if __name__ == '__main__': duration = 1 # Duration of recording if platform.system() == 'Windows': - # WDM-KS is needed since there are more than one MicNode device APIs (at least in Windows) - device = 'Microphone (MicNode_4_Ch), Windows WASAPI' + # Match by substring to support names like "Microphone (2- MicNode_4_Ch)". + device = find_windows_input_device('micnode_4_ch', channels=4, preferred_apis=['wasapi', 'wdm-ks', 'mme']) elif platform.system() == 'Darwin': device = 'MicNode_4_Ch' else: |
