gpt4 book ai didi

python - 支持 ASIO 的多声道 PyAudio

转载 作者:太空狗 更新时间:2023-10-30 03:01:11 26 4
gpt4 key购买 nike

我正在尝试在 Win7 上使用 PyAudio 连接到 PreSonus AudioBox 1818VSL,但我在一次录制超过 2 个 channel (立体声)时遇到了一些问题。 PreSonus 驱动程序创建了许多立体声输入音频设备(例如立体声 channel 1 和 2、3 和 4 等)和一个 18 输入 channel 的 ASIO 设备。我可以毫无问题地从任何立体声设备进行录音。为了最大限度地减少延迟并从 > 2 个 channel 进行记录,我正在尝试使用 ASIO 设备。

我一直在使用来自 http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio 的 PyAudio 版本, 已编译支持 ASIO, DS, WMME, WASAPI, WDMKS。

调用 pyaudio_handle.is_format_supported() 显示 ASIO 设备支持 44.1、48 和 96 kHz 的 8 到 32 位数据。

下面是pa.get_device_info_by_index(32)返回的字典

{'defaultHighInputLatency': 0.046439909297052155,
'defaultHighOutputLatency': 0.046439909297052155,
'defaultLowInputLatency': 0.046439909297052155,
'defaultLowOutputLatency': 0.046439909297052155,
'defaultSampleRate': 44100.0,
'hostApi': 2L,
'index': 32,
'maxInputChannels': 18L,
'maxOutputChannels': 18L,
'name': u'AudioBox ASIO Driver',
'structVersion': 2L}

下面是我用来创建 PyAudio 输入流的代码。回调函数只是将数据插入列表并返回 pyaudio.paContinue 直到我得到我想要的样本数量,然后它返回 pyaudio.paComplete

pyaudio_handle = pyaudio.PyAudio()
stream = pyaudio_handle.open(
format=pyaudio.get_format_from_width(2,unsigned=False),
channels=4,
rate=48000,
input=True,
frames_per_buffer=256,
input_device_index=32,
stream_callback=pyaudio_stream_callback,
)

尝试以高于 44.1 kHz 的速率初始化 ASIO 驱动程序会导致 PyAudio 挂起而不返回。以 44.1 kHz 初始化会产生以下错误:IOError: [Errno Unanticipated host error] -9999

您可以提供解决此错误的任何帮助都会有所帮助。我什至愿意证明 ASIO 在 Win7 上运行时可以在 PyAudio 中使用 > 2 个 channel 。谢谢。

最佳答案

我能够使用 96 kHZ 的 ASIO 驱动程序录制 8 声道音频(M-audio M-Track Eight)。

来自

p = pyaudio.PyAudio()
p.get_device_info_by_index(4)

我发现 'index': 4 是 ASIO 驱动程序:

{'defaultLowInputLatency': 0.005804988662131519, 
'defaultHighOutputLatency': 0.09287981859410431,
'defaultLowOutputLatency': 0.005804988662131519,
'defaultSampleRate': 44100.0,
'maxInputChannels': 8,
'maxOutputChannels': 8,
'structVersion': 2,
'name': 'M-Audio M-Track Eight ASIO',
'index': 4,
'hostApi': 2,
'defaultHighInputLatency': 0.09287981859410431}

所以我从 PyAudio 上的示例代码开始但从 wave 切换到 scipy.io.wavfile 以编写多声道 .wav 文件,因为 wave 仅支持立体声。

import pyaudio
import wave
import numpy as np
from scipy.io import wavefile

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 8
RATE = 96000
RECORD_SECONDS = 10
WAVE_OUTPUT_FILENAME = "output.wav"

p = pyaudio.PyAudio()

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

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


#Not really sure what b'' means in BYTE STRING but numpy needs it
#just like wave did...
framesAll = b''.join(frames)

#Use numpy to format data and reshape.
#PyAudio output from stream.read() is interlaced.
result = np.fromstring(framesAll, dtype=np.int16)
chunk_length = len(result) / CHANNELS
result = np.reshape(result, (chunk_length, CHANNELS))

#Write multi-channel .wav file with SciPy
wavfile.write(WAVE_OUTPUT_FILENAME,RATE,result)

中提琴! 96 kHz、16 位、8 channel .wav 文件!

哦,细节

  • Win7 64 位
  • M-Audio 8 64 位 Windows 驱动程序 1.0.11
  • Python 3.4.2 32 位
  • 用于 Win7 的 PyAudio 0.2.8 来自 here
  • numpy-1.9.2
  • scipy-0.15.1-win32-superpack-python3.4

关于python - 支持 ASIO 的多声道 PyAudio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26391055/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com