I'm trying to use pyaudio to play some wave files but I'm always having slow/crackling/garbled outputs.
When I play this wave file as described bellow, the audio plays just fine:
$ wget http://www.freespecialeffects.co.uk/sou ... lice_s.wav
$ aplay police_s.wav
However, when I open the 'play_wave.py' example from /pyaudio/test, the audio is so slow and garbled that is useless for any application.
Code: Select all
"""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) # sys.exit(-1) wf = wave.open('police_s.wav', '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 data != '': stream.write(data) data = wf.readframes(CHUNK) # stop stream (4) stream.stop_stream() stream.close() # close PyAudio (5) p.terminate()
What I tried:
1- Another Raspberry Pi B+.
2- Change the audio samples per buffer:
As I was supposing the problem was the audio samples per buffer (the CHUNK variable in this example), I made a loop to increment the CHUNK by 1 and played the audio for each increment. I could notice a slight difference for some CHUNK values, but nothing even close to the quality that I get when I play it by aplay.
However, I could notice a big difference between this two files:
1- police_s.wav = 8 bits , 22000Hz, Mono , 176 kb/s -> Way better than the beat.wav played by the same CHUNK (2048)
2- beat.wav = 16bits , 44100Hz, Stereo, 1411 kb/s
When I play the same audio through the example /pyaudio/test/play_wave_callback.py, the output is almost perfect, excepting some interruptions at the end of the audio. So I saw that it doesn't set the CHUNK. It uses the frame_count parameter in the callback function, so I printed it and saw that it was 1024 ¬¬, the same default value that came with the example /pyaudio/test/play_wave.py and that results in a garbled audio.
3- pyaudio 0.2.4:
Since hayderOICO mentioned he was using pyaudio 0.2.4 and said "I'm using PyAudio fine." (viewtopic.php?f=32&t=103190&p=715696&hi ... f8#p715696), I decided to give a try on that older version but I got the same result...
I'm using: Raspberry Pi B+____Raspbian____python 2.7.3____pyaudio v0.2.8____portaudio19-dev
How I installed everything:
$ sudo apt-get update
$ sudo apt-get -y install python-dev python-numpy cython python-smbus portaudio19-dev
$ git clone http://people.csail.mit.edu/hubert/git/pyaudio.git
$ cd pyaudio
$ sudo python setup.py install
$ sudo apt-get install python-pyaudio
From GitHub: https://github.com/jleb/pyaudio
It's very frustrating having the library's example not working properly on Pi. I don't think it's a hardware limitation since the same audio plays well with aplay and other libraries like pygame and SDL2.
I found this three posts on the raspberry's forum with no solution:
1- viewtopic.php?f=28&t=113027&p=773638&hi ... f8#p773638
2- viewtopic.php?f=32&t=103190&p=715696&hi ... f8#p715696
3- viewtopic.php?f=32&t=25173&p=774430&hil ... f8#p774430
And these on stackoverflow:
1- http://stackoverflow.com/questions/2864 ... w-on-linux
2- http://stackoverflow.com/questions/2190 ... ing-a-file
I am new to Raspberry Pi and audio programming, so I hope to be doing something stupid...
As I am already using a specific wrapper for pyaudio, I really would like to keep using it instead of moving to another library...
I appreciate any help, suggestions and advice.
Thanks in advance!