python 让电脑发出声音代码,用python录制输出声音

我想用python程序记录我的笔记本电脑发出的声音。我找到了

PyAudio

并提出了以下完成任务的方案:

import pyaudio, wave, sys

chunk = 1024

FORMAT = pyaudio.paInt16

CHANNELS = 1

RATE = 44100

RECORD_SECONDS = 5

WAVE_OUTPUT_FILENAME = sys.argv[1]

p = pyaudio.PyAudio()

channel_map = (0, 1)

stream_info = pyaudio.PaMacCoreStreamInfo(

flags = pyaudio.PaMacCoreStreamInfo.paMacCorePlayNice,

channel_map = channel_map)

stream = p.open(format = FORMAT,

rate = RATE,

input = True,

input_host_api_specific_stream_info = stream_info,

channels = CHANNELS)

all = []

for i in range(0, RATE / chunk * RECORD_SECONDS):

data = stream.read(chunk)

all.append(data)

stream.close()

p.terminate()

data = ''.join(all)

wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')

wf.setnchannels(CHANNELS)

wf.setsampwidth(p.get_sample_size(FORMAT))

wf.setframerate(RATE)

wf.writeframes(data)

wf.close()

问题是我得把耳机插孔接到麦克风插孔上。我试着替换这些线条:

input = True,

input_host_api_specific_stream_info = stream_info,

用这些:

output = True,

output_host_api_specific_stream_info = stream_info,

但是我得到了这个错误:

回溯(最近一次呼叫的最后一次):

文件“./test.py”,第25行,in

数据=流读取(块)

文件“/library/python/2.5/site packages/pyaudio.py”,第562行,已读

pacannotreadfromanoutputonlystream)

ioerror:[错误号不是输入流]-9975

有没有一种方法来实例化pyaudio流,以便它从计算机的输出输入,而我不必将耳机插孔连接到麦克风?有更好的方法吗?我更喜欢使用python应用程序,避免使用cocoa。

你可能感兴趣的:(python,让电脑发出声音代码)