PyAudio stream.is_active(), how does it know whether it is active or not

Viewed 1286

I am trying understand how stream.is_active() works. say the wave file reader example from the Documentation:

"""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()

So I am not sure what is the condition to make is_active() returns False. If wf.readframes ran out will it return 0 or error to data? If 0 is the indicator what if I actually what 0 to be in my data. In the pyaudio.py, is_active() is defined:

def is_active(self): """ Returns whether the stream is active.

:rtype: bool
"""

return pa.is_stream_active(self._stream)

But I am not able to go deeper to portaudio (pa)'s is_stream_active().

1 Answers

The portaudio documentation says:

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

Related