news 2026/7/1 18:17:10

python 操作麦克风

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
python 操作麦克风

目录

查询麦克风:

测试麦克风:


查询麦克风:

import sounddevice as sd # 1. 查看所有主机API print("=== All Host APIs ===") for i, h in enumerate(sd.query_hostapis()): print(f"HostAPI {i}: {h['name']}") print("\n=== All Devices ===") # 2. 查看所有设备 for i, d in enumerate(sd.query_devices()): hostapi = sd.query_hostapis(d['hostapi']) device_type = [] if d['max_input_channels'] > 0: device_type.append("Input") if d['max_output_channels'] > 0: device_type.append("Output") print(f"Device {i}: {d['name']}") print(f" HostAPI: {hostapi['name']}") print(f" Type: {', '.join(device_type)}") print(f" Input channels: {d['max_input_channels']}") print(f" Output channels: {d['max_output_channels']}") print() # 3. 尝试使用默认设备 print("\n=== Testing Default Devices ===") print(f"Default input device: {sd.default.device[0]}") print(f"Default output device: {sd.default.device[1]}") # 4. 直接使用默认输入设备 try: def audio_callback(indata, frames, time, status): if status: print(f"Status: {status}") print(f"Audio shape: {indata.shape}") with sd.InputStream(samplerate=16000, channels=1, dtype='int16', blocksize=320, callback=audio_callback): input("🎤 Recording with default device... Press Enter to stop\n") except Exception as e: print(f"Error: {e}") # 5. 或者,列出所有输入设备 print("\n=== All Input Devices ===") input_devices = [] for i, d in enumerate(sd.query_devices()): if d['max_input_channels'] > 0: hostapi = sd.query_hostapis(d['hostapi']) input_devices.append((i, d, hostapi['name'])) print(f"Device {i}: {d['name']}") print(f" HostAPI: {hostapi['name']}") print(f" Channels: {d['max_input_channels']}") # 6. 选择一个可用的输入设备 if input_devices: print("\n=== Try using first available input device ===") mic_index = input_devices[0][0] device_info = input_devices[0][1] channels = min(1, device_info['max_input_channels']) # 使用单声道更通用 try: with sd.InputStream(device=mic_index, samplerate=16000, channels=channels, dtype='int16', blocksize=320, callback=lambda indata, frames, time, status: print(f"Audio shape: {indata.shape}")): input(f"🎤 Recording with {device_info['name']}... Press Enter to stop\n") except Exception as e: print(f"Error with device {mic_index}: {e}") else: print("No input devices found at all!")

测试麦克风:

import sounddevice as sd import numpy as np # 直接使用设备10 mic_index = 10 print("Testing microphone...") def callback(indata, frames, time, status): volume = np.linalg.norm(indata) * 10 print(f"Microphone level: {volume:.2f}", end='\r') # 尝试不同的参数组合 settings_to_try = [ {'samplerate': 16000, 'channels': 1, 'dtype': 'int16'}, {'samplerate': 44100, 'channels': 1, 'dtype': 'float32'}, {'samplerate': 48000, 'channels': 1, 'dtype': 'int16'}, ] for i, settings in enumerate(settings_to_try): print(f"\nTry {i+1}: {settings}") try: with sd.InputStream( device=mic_index, callback=callback, **settings ): input(f"Settings {i+1} working! Press Enter to stop...\n") break except Exception as e: print(f"Failed: {e}")

读取麦克风:

import sounddevice as sd import numpy as np from scipy import signal mic_index = 10 target_samplerate = 16000 # 目标采样率 original_samplerate = 44100 # 设备支持的采样率 print(f"Recording at {original_samplerate}Hz, resampling to {target_samplerate}Hz") def callback(indata, frames, time, status): """接收44100Hz音频,重采样到16000Hz""" if status: print(status) # 如果是立体声,转换为单声道 if indata.shape[1] > 1: audio = np.mean(indata, axis=1) else: audio = indata.flatten() # 重采样到16000Hz num_samples = int(len(audio) * target_samplerate / original_samplerate) resampled = signal.resample(audio, num_samples) print(f"Original: {len(audio)} samples, Resampled: {len(resampled)} samples", end='\r') with sd.InputStream(device=mic_index, samplerate=original_samplerate, channels=1, dtype='float32', callback=callback): input("Recording and resampling... Press Enter to stop\n")
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/25 19:27:16

代付业务解析

代付业务是指支付机构商户依托支付机构的服务能力,向指定对公或对私银行账户完成资金划转的业务模式。该业务广泛应用于各类资金结算场景,核心场景包括金融平台放款、资金存管划付、理财资金提现、企业代发工资、供货商货款结算、渠道佣金返还等。在业务…

作者头像 李华
网站建设 2026/6/26 4:13:33

UltraISO注册码最新版可用吗?配合YOLOFuse系统镜像刻录

UltraISO注册码最新版可用吗?配合YOLOFuse系统镜像刻录 在人工智能落地越来越依赖“开箱即用”解决方案的今天,一个常见的现实问题是:我们能否绕过繁琐的环境配置,直接将训练好的多模态模型部署到边缘设备上?尤其是在…

作者头像 李华
网站建设 2026/7/2 1:53:24

YOLOFuse MediaPipe 集成教程:跨平台手势识别联动

YOLOFuse MediaPipe 跨平台手势识别联动方案深度解析 在智能交互设备日益普及的今天,用户对自然、稳定、全天候可用的人机交互方式提出了更高要求。尤其是在车载系统、工业控制和安防监控等关键场景中,传统依赖可见光摄像头的手势识别方案常常因光照变…

作者头像 李华
网站建设 2026/6/13 8:41:51

YOLOFuse知识蒸馏方案设想:用大模型带小模型

YOLOFuse知识蒸馏方案设想:用大模型带小模型 在智能安防、自动驾驶和夜间巡检等实际场景中,单一可见光摄像头在低光照、雾霾或遮挡环境下常常“力不从心”。热源目标看不见、轮廓模糊、误检漏检频发——这些问题正推动着多模态感知技术的快速发展。红外&…

作者头像 李华
网站建设 2026/6/30 23:07:54

YOLOFuse推理脚本详解:infer_dual.py使用技巧与输出路径说明

YOLOFuse推理脚本详解:infer_dual.py 使用技巧与输出路径说明 在智能监控、自动驾驶和安防巡检等实际场景中,单一模态的目标检测系统常常“力不从心”。比如夜晚的街道上,可见光摄像头几乎看不见行人,而红外相机却能清晰捕捉到人体…

作者头像 李华
网站建设 2026/6/18 17:24:59

YOLOFuse仅上传RGB图片会怎样?系统行为与应对策略

YOLOFuse仅上传RGB图片会怎样?系统行为与应对策略 在智能监控、夜间巡检和自动驾驶等实际场景中,光照条件千变万化。一个常见的挑战是:白天表现良好的目标检测模型,到了夜晚或烟雾环境中却频频“失明”。为解决这一问题&#xff0…

作者头像 李华