python - PyAudio stream.is_active(),它是如何知道自己是否活跃的

标签 python pyaudio portaudio

我正在尝试了解 stream.is_active() 的工作原理。说文档中的波形文件阅读器示例:

"""PyAudio Example: Play a wave file (callback version)."""

import pyaudio
import wave
import time
import sys

if len(sys.argv) < 2:
    print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])
    sys.exit(-1)

wf = wave.open(sys.argv[1], 'rb')

# instantiate PyAudio (1)
p = pyaudio.PyAudio()

# define callback (2)
def callback(in_data, frame_count, time_info, status):
    data = wf.readframes(frame_count)
    return (data, pyaudio.paContinue)

# open stream using callback (3)
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                channels=wf.getnchannels(),
                rate=wf.getframerate(),
                output=True,
                stream_callback=callback)

# start the stream (4)
stream.start_stream()

# wait for stream to finish (5)
while stream.is_active():
    time.sleep(0.1)

# stop stream (6)
stream.stop_stream()
stream.close()
wf.close()

# close PyAudio (7)
p.terminate()

所以我不确定使 is_active() 返回 False 的条件是什么。如果 wf.readframes 用完了,它会返回 0 还是错误数据?如果 0 是指示器,如果我实际上将 0 放入我的 data 中会怎么样。在 pyaudio.py 中定义了 is_active() :

def is_active( self ): """ 返回流是否处于事件状态。

:rtype: bool
"""

return pa.is_stream_active(self._stream)

但我无法更深入地了解 portaudio (pa) 的 is_stream_active()。

最佳答案

portaudio 文档说:

A stream is active after a successful call to Pa_StartStream(), until it becomes inactive either as a result of a call to Pa_StopStream() or Pa_AbortStream(), or as a result of a return value other than paContinue from the stream callback. In the latter case, the stream is considered inactive after the last buffer has finished playing.

http://portaudio.com/docs/v19-doxydocs/portaudio_8h.html#a1f8709c4971932643681a6f374c4bb5a

关于python - PyAudio stream.is_active(),它是如何知道自己是否活跃的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53341875/

相关文章:

python - 在 BeautifulSoup 解析中没有得到正确的响应

python - 使用一个 pyaudio 流进行数据读取和写入

python - 使用 PyAudio 流式传输网络广播

windows - 如何最大化实时处理(Portaudio)的性能

python - 我应该使用 numpy.polyfit 还是 numpy.polynomial.polyfit 或 numpy.polynomial.polynomial.Polynomial?

python - 如何使用向后组合迭代拆分字符串?

python - 如何使用从 sounddevice Stream 类返回的 NumPy 数组来执行附加处理?

实时控制音量

python - Pipenv:无条件安装平台特定包的依赖项?

python - PyAudio 工作,但每次都吐出错误消息