【发布时间】:2021-05-14 00:48:11
【问题描述】:
我从 PyAudio 文档中获取了一个修改版的线示例:
def mic_to_vac_pipe(self):
microphone_input_stream = self.pyaudio.open(
format=self.pyaudio.get_format_from_width(2),
channels=1,
rate=self.RATE,
input=True,
output=True,
input_device_index=1,
frames_per_buffer=self.CHUNK_SIZE
)
cable_output_stream = self.pyaudio.open(
format=self.pyaudio.get_format_from_width(2),
channels=1,
rate=self.RATE,
output=True,
output_device_index=5, # change to 8
frames_per_buffer=self.CHUNK_SIZE
)
# This will last for 217483647 seconds, or 68.09 years
for i in range(0, int(self.RATE / self.CHUNK_SIZE * 2147483647)):
data = microphone_input_stream.read(self.CHUNK_SIZE)
cable_output_stream.write(data, self.CHUNK_SIZE)
microphone_input_stream.stop_stream()
microphone_input_stream.close()
cable_output_stream.stop_stream()
cable_output_stream.close()
这曾经可以工作,直到我将它移到一个班级,现在我收到了这个错误:
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Users\pwill\AppData\Local\Programs\Python\Python39\lib\threading.py", line 954, in _bootstrap_inner
self.run()
File "C:\Users\pwill\AppData\Local\Programs\Python\Python39\lib\threading.py", line 892, in run
self._target(*self._args, **self._kwargs)
File "d:\documents\coding\python\kyan\kyan\kyan.py", line 49, in mic_to_vac_pipe
microphone_input_stream = self.pyaudio.open(
File "C:\Users\pwill\AppData\Local\Programs\Python\Python39\lib\site-packages\pyaudio.py", line 750, in open
stream = Stream(self, *args, **kwargs)
File "C:\Users\pwill\AppData\Local\Programs\Python\Python39\lib\site-packages\pyaudio.py", line 441, in __init__
self._stream = pa.open(**arguments)
OSError: [Errno -9986] Could not get stream information
我在使用self.pyaudio.get_host_api_info_by_index(0) (OSError: [Errno -9978] Invalid host api info) 时也遇到了类似的错误。这些错误是否可能相关?
我怀疑它可能与内部的 PortAudio 有关,因为绕过 get_host_api_info_by_index(0) 调用(它用于获取音频设备的数量,所以我可以通过设置设备计数的硬编码数字来绕过)告诉我 PortAudio 是未初始化,即使 PyAudio 已初始化,它也会同时初始化 PortAudio。
我该如何解决这个问题?
【问题讨论】:
-
您的麦克风同时拥有
input=True和output=True。很确定它是其中之一。 -
@TimRoberts 我认为两者都有可能,因为您可以使用内置麦克风的耳机。但是无论如何,删除它后,它仍然会给出相同的错误。
-
错误 -9986 是 paInternalError。不太有用。
-
我什至尝试过使用调试器,但它并没有指出任何有用的地方