portaudio - How to enable WASAPI exclusive mode in pyaudio -
i'm using these precompiled binaries of pyaudio wasapi support. want play wav file via wasapi. found index of default output device api:
import pyaudio p = pyaudio.pyaudio() print p.get_host_api_info_by_index(3) >>{'index': 3, 'name': u'windows wasapi', 'defaultoutputdevice': 11l, 'type': 13l, 'devicecount': 3l, 'defaultinputdevice': 12l, 'structversion': 1l}
then play wav file via device:
import pyaudio import wave chunk = 1024 wf = wave.open('test.wav', 'rb') # instantiate pyaudio (1) p = pyaudio.pyaudio() # open stream (2) stream = p.open(format=p.get_format_from_width(wf.getsampwidth()), channels=wf.getnchannels(), rate=wf.getframerate(), output_device_index=11, output=true) # read data data = wf.readframes(chunk) # play stream (3) while data != '': stream.write(data) data = wf.readframes(chunk) # stop stream (4) stream.stop_stream() stream.close() # close pyaudio (5) p.terminate()
when file playing i'm still able hear sounds in system, in exclusive wasapi mode other sounds must blocked. how enable wasapi exclusive mode in pyaudio?
there need change sources of pyaudio. need modify _portaudiomodule.c.
include pa_win_wasapi.h:
#include pa_win_wasapi.h
change line:
outputparameters->hostapispecificstreaminfo = null;
on this:
struct pawasapistreaminfo wasapiinfo; wasapiinfo.size = sizeof(pawasapistreaminfo); wasapiinfo.hostapitype = pawasapi; wasapiinfo.version = 1; wasapiinfo.flags = (pawinwasapiexclusive|pawinwasapithreadpriority); wasapiinfo.threadpriority = ethreadpriorityproaudio; outputparameters->hostapispecificstreaminfo = (&wasapiinfo);
now need compile pyaudio.
- place portaudio dir in pyaudio name portaudio-v19, name important
- install mingw/msys: gcc, make , msys console need
- in msys console cd portaudio-v19
./configure --with-winapi=wasapi --enable-shared=no
make
cd ..
change these lines:
external_libraries += ['winmm']
extra_link_args += ['-lwinmm']
in setup.py on these:
external_libraries += ["winmm","ole32","uuid"]
extra_link_args += ["-lwinmm","-lole32","-luuid"]
python setup.py build --static-link -cmingw32
python setup.py install --skip-build
that's all. pyadio able play sound in wasapi exclusive mode.
Comments
Post a Comment