本文介绍了如何在pyaudio中启用WASAPI独占模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这些 pyaudio 的预编译二进制文件和 WASAPI 支持.我想通过 WASAPI 播放 wav 文件.我找到了这个 api 的默认输出设备的索引:

I'm using these precompiled binaries of pyaudio with WASAPI support. I want to play a wav file via WASAPI. I found index of default output device for this 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}

然后我通过这个设备播放一个 wav 文件:

Then I play a wav file via this 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()

播放文件时,我仍然能够听到系统中的其他声音,但在独占 WASAPI 模式下,必须阻止所有其他声音.那么如何在pyaudio中开启WASAPI独占模式呢?

When file is playing I'm still able to hear another sounds in the system, but in exclusive WASAPI mode all other sounds must be blocked. So how to enable WASAPI exclusive mode in pyaudio?

推荐答案

pyaudio 的来源需要更改.我们需要修改_portaudiomodule.c.

There is need to change sources of pyaudio. We need to modify _portaudiomodule.c.

包括 pa_win_wasapi.h:

Include pa_win_wasapi.h:

#include pa_win_wasapi.h

更改这一行:

outputParameters->hostApiSpecificStreamInfo = NULL;

关于这个:

struct PaWasapiStreamInfo wasapiInfo;
wasapiInfo.size = sizeof(PaWasapiStreamInfo);
wasapiInfo.hostApiType = paWASAPI;
wasapiInfo.version = 1;
wasapiInfo.flags = (paWinWasapiExclusive|paWinWasapiThreadPriority);
wasapiInfo.threadPriority = eThreadPriorityProAudio;

outputParameters->hostApiSpecificStreamInfo = (&wasapiInfo);

现在我们需要编译pyaudio.

Now we need to compile pyaudio.

  1. 将 portaudio 目录放在 pyaudio 中,名称为 portaudio-v19,名称很重要
  2. 安装 MinGW/MSYS:我们需要的 gcc、make 和 MSYS 控制台
  3. 在 MSYS 控制台 cd 到 portaudio-v19
  4. ./configure --with-winapi=wasapi --enable-shared=no
  5. 制作
  6. cd ..
  7. 更改这些行:

  1. Place portaudio dir in pyaudio with name portaudio-v19, name is important
  2. Install MinGW/MSYS: gcc, make and MSYS console we need
  3. In MSYS console cd to portaudio-v19
  4. ./configure --with-winapi=wasapi --enable-shared=no
  5. make
  6. cd ..
  7. change these lines:

external_libraries += ['winmm']

extra_link_args += ['-lwinmm']

在 setup.py 中:

in setup.py on these:

external_libraries += ["winmm","ole32","uuid"]

extra_link_args += ["-lwinmm","-lole32","-luuid"]

仅此而已.现在 pyadio 可以在 WASAPI 独占模式下播放声音了.

That's all. Now pyadio is able to play sound in WASAPI exclusive mode.

这篇关于如何在pyaudio中启用WASAPI独占模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 16:38