Multi-Channel PyAudio with ASIO Support

I am trying to interact with the PreSonus AudioBox 1818VSL with PyAudio on Win7, but I have problems recording more than two channels (stereo) at a time. The PreSonus driver creates many stereo audio devices (e.g. stereo channels 1 and 2, 3 and 4, etc.) and an 18-channel ASIO device. I can record from any of the stereo devices without any problems. To minimize delay and recording from> 2 channels, I am trying to use an ASIO device.

I use the PyAudio assembly from http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio , which compiled support for ASIO, DS, WMME, WASAPI, WDMKS,

The call pyaudio_handle.is_format_supported()shows that the ASIO device supports 8 to 32-bit data at 44.1, 48 and 96 kHz.

Below is the dictionary returned 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}

Below is the code I used to create the PyAudio input stream. The callback function simply pushes the data into the list and returns pyaudio.paContinueuntil I get the number of samples I want, then it returns 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,
)

Attempting to initialize the ASIO driver with a speed of more than 44.1 kHz causes PyAudio to hang and not return. When initializing 44.1 kHz the following error: IOError: [Errno Unanticipated host error] -9999.

Any help you can allow to resolve this error will be helpful. I would even agree to the proof that ASIO works s> 2 channels in PyAudio when running on Win7. Thank you

+4
source share
1

8- (M-audio M-Track Eight), ASIO 96 .

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)

Viola! 96 , 16-, 8- .wav !

,

  • Win7 64bit
  • M-Audio Eight 64- Windows 1.0.11
  • Python 3.4.2 32
  • PyAudio 0.2.8 Win7
  • NumPy-1.9.2
  • SciPy-0.15.1-win32--python3.4
+4

All Articles