Maybe this little shell (warning: created on their knees) of their example will help you understand the meaning of the code they wrote.
import pyaudio import wave import sys class AudioFile: chunk = 1024 def __init__(self, file): """ Init audio stream """ self.wf = wave.open(file, 'rb') self.p = pyaudio.PyAudio() self.stream = self.p.open( format = self.p.get_format_from_width(self.wf.getsampwidth()), channels = self.wf.getnchannels(), rate = self.wf.getframerate(), output = True ) def play(self): """ Play entire file """ data = self.wf.readframes(self.chunk) while data != '': self.stream.write(data) data = self.wf.readframes(self.chunk) def close(self): """ Graceful shutdown """ self.stream.close() self.p.terminate()
source share