录制内部声音(从软件合成器输出)而不是来自麦克风

Record inside sound(output from software synthesizer) not from microphone

pyaudio这样录音

p = pyaudio.PyAudio()

hostAPICount = p.get_host_api_count()
print("Host API Count = " + str(hostAPICount))

for i in range(p.get_device_count()):
    print(p.get_device_info_by_index(i))
      # check the device. 
      # 0 -> microphone 1-> headphone
    
DEVICE_INDEX = 0 #or 1
CHUNK = 1024
FORMAT = pyaudio.paInt16 # 16bit
CHANNELS = 1            
RATE = 48000             # sampling frequency [Hz]

time_ = 5 # record time [s]       
output_path = "./sample.wav"


stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                input_device_index = DEVICE_INDEX,
                frames_per_buffer=CHUNK)

print("recording ...")
frames = []

for i in range(0, int(RATE / CHUNK * time_)):
    data = stream.read(CHUNK)
    frames.append(data)

print("done.")

stream.stop_stream()
stream.close()
p.terminate()

wf = wave.open(output_path, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()

它适用于 Microphone

但是我想录制我电脑中播放的声音。(合成器应用程序输出它在 MacOX 上使用 CoreAudio)

所以我改了设备号

DEVICE_INDEX = 0 -> DEVICE_INDEX = 1

但是出现这个错误信息

OSError: [Errno -9998] Invalid number of channels

也改了channel但是没用,出现同样的信息

CHANNELS = 1 -> CHANNELS = 2

如何录制从本地应用程序播放的音频?

可能吗?

我不久前遇到了这个问题,老实说无法通过编码来解决它,而是通过使用 https://vb-audio.com/Cable/ 将扬声器声音重定向到输入来解决它。你只需要通过我之前做的这个小检查找到正确的 DEVICE_INDEX 。 (我觉得输入的名字还是一样"CABLE Output (VB-Audio Virtual "

import pyaudio 


p = pyaudio.PyAudio()

for i in range(p.get_device_count()):
    if p.get_device_info_by_index(i)["name"] == "CABLE Output (VB-Audio Virtual ":
        print(p.get_device_info_by_index(i)["index"])
    
p.terminate()
input("Click to finish")