我在Windows10上用python制作了一个简单的语音助手,带有语音识别功能,我也想复制macOs的代码
我下载了PortAudio和PyAudio,代码运行正常,但当我播放音频曲目时,我什么也听不到:((当我尝试使用语音识别时,程序没有检测到)
我猜是有权限之类的东西…有人有主意吗
(我还检查了我是否使用了正确的设备索引,我确实使用了索引0(Mackbook内置麦克风)
下面是一些代码示例:
import pyaudio
import wave
chunk = 1024 # Record in chunks of 1024 samples
sample_format = pyaudio.paInt16 # 16 bits per sample
channels = 1
fs = 44100 # Record at 44100 samples per second
seconds = 3
filename = "output.wav"
p = pyaudio.PyAudio() # Create an interface to PortAudio
print('Recording')
stream = p.open(format=sample_format,
channels=channels,
rate=fs,
frames_per_buffer=chunk,
input=True)
frames = [] # Initialize array to store frames
# Store data in chunks for 3 seconds
for i in range(0, int(fs / chunk * seconds)):
data = stream.read(chunk)
frames.append(data)
# Stop and close the stream
stream.stop_stream()
stream.close()
# Terminate the PortAudio interface
p.terminate()
print('Finished recording')
# Save the recorded data as a WAV file
wf = wave.open(filename, 'wb')
wf.setnchannels(channels)
wf.setsampwidth(p.get_sample_size(sample_format))
wf.setframerate(fs)
wf.writeframes(b''.join(frames))
wf.close()
我找到了答案
实际上,代码一直运行良好😅, 问题是我使用了
Visual Studio Code
,由于某种原因,它弄乱了麦克风权限🤦♂️现在我用
python [filename].py
通过终端运行代码,它工作得很好相关问题 更多 >
编程相关推荐