【发布时间】:2020-05-20 01:01:04
【问题描述】:
我正在按照here 列出的代码构建我自己的智能扬声器。我购买了this 蓝牙扬声器/麦克风。当我用它大胆地录制音频时,麦克风工作得很好,当我使用以下使用 PyAudio 但不使用 SpeechRecognition 的代码时工作正常
import pyaudio
import wave
from array import array
FORMAT=pyaudio.paInt16
CHANNELS=2
RATE=44100*2
CHUNK=1024
RECORD_SECONDS=5
FILE_NAME="RECORDING.wav"
audio=pyaudio.PyAudio() #instantiate the pyaudio
#recording prerequisites
stream=audio.open(format=FORMAT,channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
#starting recording
frames=[]
for i in range(0,int(RATE/CHUNK*RECORD_SECONDS)):
data=stream.read(CHUNK)
data_chunk=array('h',data)
vol=max(data_chunk)
if(vol>=500):
print("something said")
frames.append(data)
else:
print("nothing")
print("\n")
#end of recording
stream.stop_stream()
stream.close()
audio.terminate()
#writing to file
wavfile=wave.open(FILE_NAME,'wb')
wavfile.setnchannels(CHANNELS)
wavfile.setsampwidth(audio.get_sample_size(FORMAT))
wavfile.setframerate(RATE)
wavfile.writeframes(b''.join(frames))#append frames recorded to file
wavfile.close()
但是当我尝试使用以下代码时
import speech_recognition as sr
import pyaudio
r = sr.Recognizer()
mic = sr.Microphone(device_index=1)
with mic as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source, timeout=5)
print(r.recognize_google(audio))
有了这个扬声器/麦克风,它可以无限期地挂起。我使用了一个 USB 麦克风,切换了 device_index,它工作正常。当我 list_microphone_names() 我可以在我的选项列表中看到蓝牙麦克风为“耳机麦克风(蓝牙 H”以及我的 USB 麦克风“麦克风(蓝色雪球)”,但是当我 list_working_microphones() 时,蓝牙麦克风不见了。本质上,它识别出设备存在,但在 r.listen() 期间没有通过它听到音频。
有人知道是什么原因造成的吗?
【问题讨论】:
标签: python bluetooth speech-recognition pyaudio