在python记录输出声音 [英] record output sound in python

查看:1799
本文介绍了在python记录输出声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要编程录制声音出来我的笔记本电脑的蟒蛇。我发现 PyAudio ,并以下列程序完成任务上来:

i want to programatically record sound coming out of my laptop in python. i found PyAudio and came up with the following program that accomplishes the task:

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()

问题是我必须耳机插孔连接到麦克风插孔。我试图替换这些行:

the problem is i have to connect the headphone jack to the microphone jack. i tried replacing these lines:

input = True,
input_host_api_specific_stream_info = stream_info,

这些:

output = True,
output_host_api_specific_stream_info = stream_info,

但后来我得到这个错误:

but then i get this error:

回溯(最近通话最后一个):结果
   文件./test.py25行,在结​​果
     数据= stream.read(块)结果
   文件/Library/Python/2.5/site-packages/pyaudio.py,线路562,在读取结果
     paCanNotReadFromAnOutputOnlyStream)结果
  IO错误:[错误不输入流] -9975

Traceback (most recent call last):
File "./test.py", line 25, in
data = stream.read(chunk)
File "/Library/Python/2.5/site-packages/pyaudio.py", line 562, in read
paCanNotReadFromAnOutputOnlyStream)
IOError: [Errno Not input stream] -9975

有没有办法来实例化PyAudio流,使得其从计算机的输出输入和我没有到耳机插孔连接到麦克风?有没有更好的方式去这件事吗?我会preFER坚持瓦特/ Python应用程序,并避免可可。

is there a way to instantiate the PyAudio stream so that it inputs from the computer's output and i don't have to connect the headphone jack to the microphone? is there a better way to go about this? i'd prefer to stick w/ a python app and avoid cocoa.

推荐答案

您可以安装 Soundflower ,它允许你创建额外的音频设备,以及它们之间的音频路由。这样,您就可以定义系统的输出到Soundflower设备,并从它使用PyAudio读出声音。

You can install Soundflower, which allows you to create extra audio devices and route audio between them. This way you can define your system's output to the Soundflower device and read the audio from it using PyAudio.

您也可以看看 PyJack ,音频客户端的杰克

You can also take a look at PyJack, an audio client for Jack.

这篇关于在python记录输出声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆