使用 PyAudio 隔离输入设备音频和输出设备音频
Isolating Input Device Audio and Output Device Audio with PyAudio
我正在尝试使用 PyAudio 录制输入声音和输出声音。查看 PyAudio 文档 (here),我确定我将不得不使用 input, output, input_device_index
和 output_device_index
变量来编辑我的声音流的结果。
鉴于文档,我相信我应该有办法将输入和输出 on/off 并配置什么设备对应什么流。我的设备是:
#{'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)'}
到目前为止,我的输入工作正常,因为我能够记录麦克风检测到的任何声音。我遇到的问题是:
- 无法打开输入
- 无法关闭输出
- 无法仅获取输出音频
- 无法将两个音频流放在一起
当我尝试执行 input=False
时,出现错误 raise IOError("Not input stream"
。我的输出流似乎根本不起作用。
到目前为止我的代码:
WAVE_OUTPUT_FILENAME = (r"C:\Users\USER\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()
Windows 实际上提供了一种本机方式来录制您的系统音频,而无需安装即可使用其他解决方案。我主要使用这种方法是因为我不知道如何安装提到的 PyAudio Fork here。
如果您转到系统的声音设置,假设它是 Windows,您会在左上角找到一个录音选项卡。单击此按钮后,您还会发现一个
通常禁用的“立体声混音”图标。
如果启用此功能,此通道将捕获所有设备输出的音频,而无需进行任何其他设置。因此,您可以将 input_device_index
设置为您计算机上的 Stereo Mix 索引。
我正在尝试使用 PyAudio 录制输入声音和输出声音。查看 PyAudio 文档 (here),我确定我将不得不使用 input, output, input_device_index
和 output_device_index
变量来编辑我的声音流的结果。
鉴于文档,我相信我应该有办法将输入和输出 on/off 并配置什么设备对应什么流。我的设备是:
#{'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)'}
到目前为止,我的输入工作正常,因为我能够记录麦克风检测到的任何声音。我遇到的问题是:
- 无法打开输入
- 无法关闭输出
- 无法仅获取输出音频
- 无法将两个音频流放在一起
当我尝试执行 input=False
时,出现错误 raise IOError("Not input stream"
。我的输出流似乎根本不起作用。
到目前为止我的代码:
WAVE_OUTPUT_FILENAME = (r"C:\Users\USER\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()
Windows 实际上提供了一种本机方式来录制您的系统音频,而无需安装即可使用其他解决方案。我主要使用这种方法是因为我不知道如何安装提到的 PyAudio Fork here。
如果您转到系统的声音设置,假设它是 Windows,您会在左上角找到一个录音选项卡。单击此按钮后,您还会发现一个
通常禁用的“立体声混音”图标。
如果启用此功能,此通道将捕获所有设备输出的音频,而无需进行任何其他设置。因此,您可以将 input_device_index
设置为您计算机上的 Stereo Mix 索引。