Sunday, March 25, 2012

PyAudio: How to get 24 bit support under windows

PyAudio is a really useful and user-friendly python extension for accessing the soundcard from python.

Pyaudio uses the portaudio bindings between some windows sound architecture and python. Only the WASAPI and the ASIO APIs on windows 7 support real 24 bit resolution. The standard version that comes shipped with pyaudio is compiled for WMME which is old and emulates win xp sound. It converts the 24 bit data to 16 bit and calculates with this precission internally. It returns 24 bit numbers but this is zero-padded16 bit data (can be seen as there is no change in the quantizing noise floor)

So pyaudio and portaudio needs to be rebuilt from source to support WASAPI. (or ASIO but you have to register at steinberg as a developer to get the SDK from there)

This is how I managed to build it:

1.) download and unpack the latest PyAudio sources

2.) download and unpack the latest portaudio sources from svn (snapshot).  Untar it to portaudio-v19/ inside the PyAudio directory. This specific directory name is important.

3.) install cygwin. Make sure all packages related to mingw, gcc and make are installed

4.) switch your default gcc to version 3.x by executing /usr/bin/ from within the cygwin console.

5.) in cygwin build portaudio:
cd portaudio-v19

CFLAGS="-mno-cygwin" LDFLAGS="-mno-cygwin" ./configure --with-host_os=mingw --with-winapi=wasapi --enable-static=yes --enable-debug-output=yes

    You will get the file ./lib/.libs/libportaudio.a  (198 kB)

6.) See if portaudio works. Try to execute the example programs in ./bin/. They must run without error in the normal windows terminal. With pa_devs.exe you can check if the WASAPI api can be opened. Propably the windows default audio output settings have to be matched (samplerate bit depth) to the test program.

7.) To build PyAudio, edit and change the platform specific configuration for win32:
elif sys.platform == 'win32':
external_libraries += ["winmm","ole32","uuid"]
 extra_link_args += ["-lwinmm","-lole32","-luuid"]

9.) run in a normal windows terminal as an administrator (so the native python is invoked):
python build --static-link -cmingw32
python install

Then I could run the examples in ./test (I had to change the windows recording and playback devices shared samplerate and bitrate sometimes when i got a invalid device error)

Further help and details can be found here:

These steps worked for me on Windows 7, 32 bit with PyAudio-0.2.7 and Portaudio-V19 stable SVN rev 1788
(tried it on 12.02.2013)

I couldn't get it working for 64 bit python. I ended up using the precompiled package from here:
Which includes WASAPI support and runs on Win 7 64 bit.

No comments:

Post a Comment