我试图在Win7上使用PyAudio与PreSonus AudioBox 1818VSL连接,但是在一次录制超过2个通道(立体声)时遇到一些麻烦. PreSonus驱动程序创建许多立体声输入音频设备(例如立体声通道1和2,3和4等)和18输入通道ASIO设备.我可以毫无问题地从任何立体声设备录制.为了最小化延迟并从>记录2个频道,我正在尝试使用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个通道.谢谢.
解决方法:
我能够在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.wav文件以写入多声道.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通道.wav文件!
哦,细节
> Win7 64bit
> M-Audio Eight 64位Windows驱动程序1.0.11
> Python 3.4.2 32位
> PyAudio 0.2.8 for Win7 from
here
> numpy-1.9.2
> scipy-0.15.1-win32-superpack-python3.4