I am getting audio binary data from a web request, and I would like to pass that data and play it over the speakers immediately. How can I do this? Currently I the errors I get are relating to the properties of the .wav, such as number of channels not specified, samplewidth etc. I dont want to write it to a .wav file, and then load the file and play it. I would like to go from binary audio data -> to playing on speakers, maybe I am going in the wrong direction to solve this but any help would be appreciated. I am assuming there has to be some way to pipe this data into an audio stream and I just dont know how to do that.
I try opening the binary data, writing a .wav object to a variable and then opening an audio stream from that obj. I have also tried explicitly setting the fields in the audio stream to avail.
wav = wave.open(response.audio_content, 'wb')
pa = pyaudio.PyAudio()
audio_stream = pa.open(format=pa.get_format_from_width(wav.getsampwidth()),
channels=wav.getnchannels(),
rate=wav.getframerate(),
output=True)
data = wav.readframes(CHUNK)
while data != '':
audio_stream.write(data)
data = wav.readframes(CHUNK)
audio_stream.stop_stream()
audio_stream.close()
pa.terminate()
Here is an example of the channel error:
wave.Error: # channels not specified