我正在尝试使用 PyAudio 录制输入声音和输出声音。查看 PyAudio 文档 (here),我确定我将不得不使用 input, output, input_device_index
和 output_device_index
变量来编辑我的声音流的结果。
鉴于文档,我相信我应该有一种方法可以打开/关闭输入和输出,并配置什么设备对应什么流。我的设备是:
<块引用>#{'index': 0, 'name': 'Microsoft Sound Mapper - Input'}
<块引用>#{'index': 1, 'name': '麦克风阵列(英特尔® 智能 '}
<块引用>#{'index': 2, 'name': 'Microsoft Sound Mapper - 输出'}
<块引用>#{'index': 3, 'name': 'Speakers (Realtek(R) Audio)'}
到目前为止,我的输入工作正常,因为我能够记录麦克风检测到的任何声音。我遇到的问题是:
当我尝试执行 input=False
时,我收到错误 raise IOError("Not input stream"
。我的输出流似乎根本不起作用。
到目前为止我的代码:
WAVE_OUTPUT_FILENAME = (r"C:\Users\Shyryyuu\Downloads\output.wav")
p = pyaudio.PyAudio()
#{'index': 0, 'name': 'Microsoft Sound Mapper - Input'}
#{'index': 1, 'name': 'Microphone Array (Intel® Smart '}
#{'index': 2, 'name': 'Microsoft Sound Mapper - Output'}
#{'index': 3, 'name': 'Speakers (Realtek(R) Audio)'}
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
output=True,
frames_per_buffer=CHUNK,
input_device_index=1,
output_device_index=2
)
print("* recording")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
答案 0 :(得分:0)
Windows 实际上提供了一种本地方式来录制您的系统音频,而无需安装以使用其他解决方案。我主要使用这种方法是因为我不知道如何安装提到的 here 的 PyAudio Fork。
如果您进入系统的声音设置,假设它是 Windows,您会在左上角找到一个录音选项卡。单击此按钮后,您还会找到一个 通常禁用的“立体声混音”图标。
如果您启用此功能,此通道将捕获所有设备输出音频,而无需进行任何其他设置。因此,您可以将 input_device_index
设置为您计算机上的 Stereo Mix 索引。