Below is a code example from the PyAudio documentation, showing how to play a .wav file.
I understand that setting output=False
in the open
method prevents the file from playing, but what is the point of this? Is this reserved for debugging purpose?
"""PyAudio Example: Play a wave file."""
import pyaudio
import wave
import sys
CHUNK = 1024
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()
# open stream (2)
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
# read data
data = wf.readframes(CHUNK)
# play stream (3)
while len(data) > 0:
stream.write(data)
data = wf.readframes(CHUNK)
# stop stream (4)
stream.stop_stream()
stream.close()
# close PyAudio (5)
p.terminate()
You can have input and output streams in PyAudio
, setting output = False
(which I think is the default anyway) just means it's not an output stream.
An output stream would be used (for example) to play an existing file to the sound subsystem (as per your code snippet).
An input stream may be used to pull info from the sound subsystem to record to a file.
I could see why someone may wonder why you would ever have both input and output set to false (and indeed, I think that's an error condition) but having output
being false is fine provided input
is true.