Python脚本轻松实现检测麦克风功能

Written by

in

文章目录
  • 在进行音频处理或开发需要使用麦克风的应用程序时,确保麦克风功能正常是非常重要的。本文将介绍一个简单的Python脚本,它能够帮助我们检测本地麦克风的功能,确保我们的设备能够正常录音。
  • 下面是一个名为sound_check.py的Python脚本,它使用sounddevice库来检测和测试麦克风,同时使用soundfile库来保存录音文件。 功能概述 获取麦克风列表:脚本首先会列出所有可用的麦克风设备。 选择麦克风设备:用户可以从列表中选择一个麦克风进行测试。 录音:脚本将使用选定的麦克风进行录音,时长为5秒。 保存录音:录音完成后,脚本会将录音保存为WAV文件。
  • 在开始之前,请确保你的Python环境已经安装了sounddevice和soundfile这两个库。如果没有安装,可以通过以下命令进行安装(清华镜像源下载): pip install sounddevice soundfile -i https://pypi.tuna.tsinghua.edu.cn/simple
  • 以下是sound_check.py脚本的详细代码解析: # 导入所需的库 import sounddevice as sd import soundfile as sf # 定义测试麦克风的函数 def test_microphone(device_index=None, output_filename=”output.wav”): # 设置录音参数 duration = 5 # 录音时长(秒) fs = 44100 # 采样频率 # 获取麦克风列表 devices = sd.query_devices() # 如果提供了设备索引并且有效,则使用指定的麦克风 if device_index is not None and device_index < len(devices): print(f”Using microphone: {devices[device_index][‘name’]}”) else: print(“Using default microphone.”) # 获取并设置麦克风支持的采样率 supported_rates = devices[device_index][‘default_samplerate’] if supported_rates != fs: print(f”Adjusting sample rate to {int(supported_rates)} Hz (supported by the device).”) fs = int(supported_rates) # 确保采样率是整数 print(“Recording…”) # 使用sounddevice录制声音 recording = sd.rec(int(duration * fs), samplerate=fs, channels=1, device=device_index) # 等待录音完成 sd.wait() print(“Recording finished.”) # 保存录音为WAV文件 sf.write(output_filename, recording, fs) print(f”File saved as {output_filename}”) # 主函数入口 if __name__ == “__main__”: # 获取麦克风列表并打印 devices = sd.query_devices() for i, device in enumerate(devices): print(f”{i}: {device[‘name’]}”) # 用户输入选择麦克风设备索引 device_index = int(input(“Enter the index of the microphone to use: “)) test_microphone(device_index)
  • 运行脚本,它会自动列出所有可用的麦克风设备。 根据列表,输入你想要测试的麦克风的索引号。 输入索引号后回车,脚本将开始录音,并在5秒后保存录音文件。 通过这个简单的脚本,可以轻松地检测本地麦克风设备是否工作正常,并且能够保存录音以供进一步分析。无论是在开发过程中还是日常使用中,这个脚本工具都能提供极大的便利。
  • python调用麦克风和扬声器,并调用百度实时语音转文字 1. 导入必要的模块和配置百度的 SDK import time import queue import sounddevice as sd import numpy as np from aip import AipSpeech import sys # 百度云配置信息 APP_ID = ‘你的 App ID’ # 替换为实际的 APP ID API_KEY = ‘你的 Api Key’ # 替换为实际的 API KEY SECRET_KEY = ‘你的 Secret Key’ # 替换为实际的 SECRET KEY client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) # Queue to hold the recorded audio data audio_queue = queue.Queue() speaker_queue = queue.Queue() # Callback function to capture audio data from microphone def audio_callback(indata, frames, time, status): if status: print(status, file=sys.stderr) audio_queue.put(indata.copy()) # Callback function to capture audio data from speaker def speaker_callback(indata, frames, time, status): if status: print(status, file=sys.stderr) speaker_queue.put(indata.copy()) 2. 实现实时语音识别类 class RealTimeSpeechRecognizer: def __init__(self, client, name): self.client = client self.name = name def send_audio(self, audio_data): result = self.client.asr(audio_data, ‘pcm’, 16000, { ‘dev_pid’: 1537, }) if result.get(‘err_no’) == 0: print(f”{self.name} 识别结果: {result[‘result’]}”) else: print(f”{self.name} 错误: {result[‘err_msg’]}”) # 调用百度的语音转文字的接口 def recognize_speech(audio_data, recognizer): audio_data = np.concatenate(audio_data) recognizer.send_audio(audio_data.tobytes()) 3. 开始音频流并处理音频数据 def start_audio_stream(mic_recognizer, speaker_recognizer, speaker_device_index): with sd.InputStream(callback=audio_callback, channels=1, samplerate=16000, dtype=’int16′) as mic_stream, sd.InputStream(callback=speaker_callback, channels=1, samplerate=16000, dtype=’int16′, device=speaker_device_index) as spk_stream: print(“Recording audio… Press Ctrl+C to stop.”) mic_audio_buffer = [] speaker_audio_buffer = [] try: while True: while not audio_queue.empty(): mic_audio_buffer.append(audio_queue.get()) while not speaker_queue.empty(): speaker_audio_buffer.append(speaker_queue.get()) if len(mic_audio_buffer) >= 10: recognize_speech(mic_audio_buffer, mic_recognizer) mic_audio_buffer = [] # Clear buffer after sending if len(speaker_audio_buffer) >= 10: recognize_speech(speaker_audio_buffer, speaker_recognizer) speaker_audio_buffer = [] # Clear buffer after sending time.sleep(0.1) except KeyboardInterrupt: print(“Stopping audio recording.”) 4. 主程序入口 if __name__ == “__main__”: speaker_device_index = 8 # 使用 pulse 设备(索引 8)来捕获扬声器输出 mic_recognizer = RealTimeSpeechRecognizer(client, “麦克风接收:”) speaker_recognizer = RealTimeSpeechRecognizer(client, “扬声器接收:”) start_audio_stream(mic_recognizer, speaker_recognizer, speaker_device_index) 到此这篇关于Python脚本轻松实现检测麦克风功能的文章就介绍到这了,更多相关Python检测麦克风内容请搜索风君子博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持风君子博客! 您可能感兴趣的文章: 使用Python实现从麦克风获取音频并识别 python调用pyaudio使用麦克风录制wav声音文件的教程 python 通过麦克风录音 生成wav文件的方法
  • 目录
    • 轻松检测麦克风功能
    • 脚本介绍
    • 一、Python环境准备
    • 二、代码解析
    • 三、使用方法
    • 四、知识扩展

    在进行音频处理或开发需要使用麦克风的应用程序时,确保麦克风功能正常是非常重要的。本文将介绍一个简单的Python脚本,它能够帮助我们检测本地麦克风的功能,确保我们的设备能够正常录音。

    下面是一个名为sound_check.pyPython脚本,它使用sounddevice库来检测和测试麦克风,同时使用soundfile库来保存录音文件。

    功能概述

    • 获取麦克风列表:脚本首先会列出所有可用的麦克风设备。
    • 选择麦克风设备:用户可以从列表中选择一个麦克风进行测试。
    • 录音:脚本将使用选定的麦克风进行录音,时长为5秒。
    • 保存录音:录音完成后,脚本会将录音保存为WAV文件。

    在开始之前,请确保你的Python环境已经安装了sounddevice和soundfile这两个库。如果没有安装,可以通过以下命令进行安装(清华镜像源下载):

    pip install sounddevice soundfile -i https://pypi.tuna.tsinghua.edu.cn/simple
    

    以下是sound_check.py脚本的详细代码解析:

    # 导入所需的库
    import sounddevice as sd
    import soundfile as sf
    
    # 定义测试麦克风的函数
    def test_microphone(device_index=None, output_filename="output.wav"):
        # 设置录音参数
        duration = 5  # 录音时长(秒)
        fs = 44100  # 采样频率
    
        # 获取麦克风列表
        devices = sd.query_devices()
        # 如果提供了设备索引并且有效,则使用指定的麦克风
        if device_index is not None and device_index < len(devices):
            print(f"Using microphone: {devices[device_index]['name']}")
        else:
            print("Using default microphone.")
    
        # 获取并设置麦克风支持的采样率
        supported_rates = devices[device_index]['default_samplerate']
        if supported_rates != fs:
            print(f"Adjusting sample rate to {int(supported_rates)} Hz (supported by the device).")
            fs = int(supported_rates)  # 确保采样率是整数
    
        print("Recording...")
        # 使用sounddevice录制声音
        recording = sd.rec(int(duration * fs), samplerate=fs, channels=1, device=device_index)
    
        # 等待录音完成
        sd.wait()
    
        print("Recording finished.")
    
        # 保存录音为WAV文件
        sf.write(output_filename, recording, fs)
    
        print(f"File saved as {output_filename}")
    
    # 主函数入口
    if __name__ == "__main__":
        # 获取麦克风列表并打印
        devices = sd.query_devices()
        for i, device in enumerate(devices):
            print(f"{i}: {device['name']}")
    
        # 用户输入选择麦克风设备索引
        device_index = int(input("Enter the index of the microphone to use: "))
        test_microphone(device_index)
    

    • 运行脚本,它会自动列出所有可用的麦克风设备。
    • 根据列表,输入你想要测试的麦克风的索引号。
    • 输入索引号后回车,脚本将开始录音,并在5秒后保存录音文件。

    通过这个简单的脚本,可以轻松地检测本地麦克风设备是否工作正常,并且能够保存录音以供进一步分析。无论是在开发过程中还是日常使用中,这个脚本工具都能提供极大的便利。

    python调用麦克风和扬声器,并调用百度实时语音转文字

    1. 导入必要的模块和配置百度的 SDK

    import time
    import queue
    import sounddevice as sd
    import numpy as np
    from aip import AipSpeech
    import sys
     
    # 百度云配置信息
    APP_ID = '你的 App ID'  # 替换为实际的 APP ID
    API_KEY = '你的 Api Key'  # 替换为实际的 API KEY
    SECRET_KEY = '你的 Secret Key'  # 替换为实际的 SECRET KEY
     
    client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
     
    # Queue to hold the recorded audio data
    audio_queue = queue.Queue()
    speaker_queue = queue.Queue()
     
    # Callback function to capture audio data from microphone
    def audio_callback(indata, frames, time, status):
        if status:
            print(status, file=sys.stderr)
        audio_queue.put(indata.copy())
     
    # Callback function to capture audio data from speaker
    def speaker_callback(indata, frames, time, status):
        if status:
            print(status, file=sys.stderr)
        speaker_queue.put(indata.copy())

    2. 实现实时语音识别类

    class RealTimeSpeechRecognizer:
        def __init__(self, client, name):
            self.client = client
            self.name = name
     
        def send_audio(self, audio_data):
            result = self.client.asr(audio_data, 'pcm', 16000, {
                'dev_pid': 1537,
            })
            if result.get('err_no') == 0:
                print(f"{self.name} 识别结果: {result['result']}")
            else:
                print(f"{self.name} 错误: {result['err_msg']}")
     
    # 调用百度的语音转文字的接口
    def recognize_speech(audio_data, recognizer):
        audio_data = np.concatenate(audio_data)
        recognizer.send_audio(audio_data.tobytes())

    3. 开始音频流并处理音频数据

    def start_audio_stream(mic_recognizer, speaker_recognizer, speaker_device_index):
        with sd.InputStream(callback=audio_callback, channels=1, samplerate=16000, dtype='int16') as mic_stream, 
                sd.InputStream(callback=speaker_callback, channels=1, samplerate=16000, dtype='int16', device=speaker_device_index) as spk_stream:
            print("Recording audio... Press Ctrl+C to stop.")
            mic_audio_buffer = []
            speaker_audio_buffer = []
            try:
                while True:
                    while not audio_queue.empty():
                        mic_audio_buffer.append(audio_queue.get())
                    while not speaker_queue.empty():
                        speaker_audio_buffer.append(speaker_queue.get())
     
                    if len(mic_audio_buffer) >= 10:
                        recognize_speech(mic_audio_buffer, mic_recognizer)
                        mic_audio_buffer = []  # Clear buffer after sending
     
                    if len(speaker_audio_buffer) >= 10:
                        recognize_speech(speaker_audio_buffer, speaker_recognizer)
                        speaker_audio_buffer = []  # Clear buffer after sending
     
                    time.sleep(0.1)
            except KeyboardInterrupt:
                print("Stopping audio recording.")

    4. 主程序入口

    if __name__ == "__main__":
        speaker_device_index = 8  # 使用 pulse 设备(索引 8)来捕获扬声器输出
     
        mic_recognizer = RealTimeSpeechRecognizer(client, "麦克风接收:")
        speaker_recognizer = RealTimeSpeechRecognizer(client, "扬声器接收:")
     
        start_audio_stream(mic_recognizer, speaker_recognizer, speaker_device_index)

    到此这篇关于Python脚本轻松实现检测麦克风功能的文章就介绍到这了,更多相关Python检测麦克风内容请搜索风君子博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持风君子博客!

    您可能感兴趣的文章:

    • 使用Python实现从麦克风获取音频并识别
    • python调用pyaudio使用麦克风录制wav声音文件的教程
    • python 通过麦克风录音 生成wav文件的方法

    站内搜索