python - PyAudio outputs slow/crackling/garbled audio -


i'm trying use pyaudio play wave files i'm having slow/crackling/garbled outputs.

when play wave file described bellow, audio plays fine:

$ wget http://www.freespecialeffects.co.uk/soundfx/sirens/police_s.wav $ aplay police_s.wav 

however, when open 'play_wave.py' example /pyaudio/test, audio slow , garbled useless application.

"""pyaudio example: play wave file."""  import pyaudio import wave import sys  chunk = 1024  #if len(sys.argv) < 2: #    print("plays wave file.\n\nusage: %s filename.wav" % sys.argv[0]) #    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() 

to reproduce similar poor quality on laptop/pc, make chunk = 1 (the output pretty similar on ubuntu)

 

 

additional information:

what tried:

1- raspberry pi b+.

2- change audio samples per buffer:

as supposing problem audio samples per buffer (the chunk variable in example), made loop increment chunk 1 , played audio each increment. notice slight difference chunk values, nothing close quality when play aplay. however, notice big difference between 2 files:

1- police_s.wav = 8 bits , 22000hz, mono , 176 kb/s -> way better beat.wav played same chunk (2048)

2- beat.wav = 16bits , 44100hz, stereo, 1411 kb/s

when play same audio through example /pyaudio/test/play_wave_callback.py, output perfect, excepting interruptions @ end of audio. saw doesn't set chunk. uses frame_count parameter in callback function, printed , saw 1024 ¬¬, same default value came example /pyaudio/test/play_wave.py , results in garbled audio.

3- pyaudio 0.2.4: since hayderoico mentioned using pyaudio 0.2.4 , said "i'm using pyaudio fine.", decided give try on older version got same result...

4- added disable_audio_dither=1 config.txt

i'm using:     raspberry pi b+     raspbian     python 2.7.3     pyaudio v0.2.8     portaudio19-dev     trrs analog audio

how installed everything:

1st try:

$ 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 

2nd try:

$ sudo apt-get install python-pyaudio 

3rd try:

from github: https://github.com/jleb/pyaudio

 

 

it's frustrating having library's example not working on pi. don't think it's hardware limitation since same audio plays aplay , other libraries pygame , sdl2.

i new raspberry pi , audio programming, hope doing stupid... using specific wrapper pyaudio, keep using instead of moving library... appreciate help, suggestions , advice.

thanks in advance!

i had similar problem on raspberry pi (and on mac). in experience pyaudio library pain work (after 2 weeks of battling it, ended using pygame). worked me check default sample rate of audio out , check same , play sounds numpy arrays.

so on rpi, i'd check (extrapolating ubuntu here...) files

/etc/pulse/daemon.conf ,  /etc/asound.conf or  ~/.asoundrc 

does direct playback of numpy arrays work? if in roundabout way... here code test if like

import pyaudio  import numpy np   def gensin(frequency, duration, samprate):      """ frequency in hz, duration in seconds , samplerate         in hz"""      cycles = np.linspace(0,duration*2*np.pi,num=duration*samprate)     wave = np.sin(cycles*frequency,dtype='float32')     t = np.divide(cycles,2*np.pi)      return t, wave  frequency=8000 #in hz duration=1 #in seconds samprate=44100 #in hz  t, sinwav = gensin(frequency,duration,samprate)  p = pyaudio.pyaudio()  stream = p.open(format = pyaudio.paint32,                  channels = 1,                  rate = samprate,                  output = true)  stream.start_stream()  stream.write(sinwav) 

this worked on rpi , mac, said before ended using pygame because on , off ramps couldn't rid of crackling @ beginning , end , sample rate wasn't changed. recommend against pyaudio, if set on wish best of luck! :)


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -