python 实现录音pcm格式功能

一、目的

在实现键盘控制程序开始之后,实现自动录音生成pcm格式function。

要考虑的问题:实时录音,录音结束的条件(阈值)。主要工具: speech_recognition包,代码简单、效果好。

二、电脑录音 (包括使用树莓派时的问题)

主要参考: github

https://github.com/Uberi/speech_recognition

博客:

Python3从零开始搭建一个语音对话机器人

https://blog.csdn.net/NIeson2012/article/details/96476878

三、code

python 实现录音pcm格式功能_第1张图片

speech_recognition

https://pypi.org/project/SpeechRecognition/

第一步、安装SpeechRecognition

python 实现录音pcm格式功能_第2张图片
pip install SpeechRecognition (时间比较久)崩溃

pip3 install -i https://pypi.doubanio.com/simple/ SpeechRecognition

只安装SpeechRecognition是不够的,现在运行会报错,提示找不到PyAudio
python 实现录音pcm格式功能_第3张图片

第二步、安装 pip install pyaudio

Requirements
To
use all of the functionality of the library, you should have:

· Python 2.6, 2.7, or 3.3+ (required)

· PyAudio 0.2.11+ (required only if you need to use microphone input, Microphone)

尝试python -m pip install pyaudio 不行

尝试pip3 install -i https://pypi.doubanio.com/simple/ pyaudio 不行

解决: whl文件在python\Scripts下安装

https://blog.csdn.net/Sau_Hit/article/details/85938063

https://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml

第三步、装之后

  1. 设置录音设备

不知道哪个设备,先打印出设备信息

import speech_recognition as sr

for index, name in enumerate(sr.Microphone.list_microphone_names()):

    print("Microphone with name \"{1}\" found for `Microphone(device_index={0})`".format(index, name))



 

python 实现录音pcm格式功能_第4张图片
with sr.Microphone(sample_rate = rate, device_index = 1 ) as source:
找到之后加上

  1. 第一次不能识别speech,可能原因recognizer_instance.energy_threshold设置太高,尝试调小或者提前recognizer_instance.adjust_for_ambient_noise动态调整。

recognizer_instance.energy_threshold

recognizer_instance.pause_threshold

参考

https://pypi.org/project/SpeechRecognition/1.2.1/

https://pypi.org/project/SpeechRecognition/

https://github.com/Uberi/speech_recognition/blob/c89856088ad81d81d38be314e3db50905481c5fe/speech_recognition/init.py

  1. 使用树莓派 Raspberry Pi board,

The recognizer
hangs on recognizer_instance.listen;
specifically, when it’s calling Microphone.MicrophoneStream.read.

This
usually happens when you’re using a Raspberry Pi board,

This causes the default microphone used by PyAudio to simply block when we try to read
it. need a USB sound card (or USB microphone). Once you do this, change all
instances of Microphone() to Microphone(device_index
= MICROPHONE_INDEX), where MICROPHONE_INDEX is the hardware-specific index of the microphone.

你可能感兴趣的:(python 实现录音pcm格式功能)