CowOne
Posts: 5
Joined: Wed Jul 08, 2015 8:31 pm

Pyaudio outputs slow/crackling/garbled audio

Thu Jul 09, 2015 11:17 pm

Hello,

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[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 a similar poor quality on linux, just make the CHUNK = 1 (the output is pretty similar on my laptop)


Additional information:
______________________________________________________________________________________________________
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:
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 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!
Last edited by CowOne on Mon Jul 13, 2015 4:04 pm, edited 4 times in total.

User avatar
DougieLawson
Posts: 39183
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Pyaudio outputs slow/crackling/garbled audio

Fri Jul 10, 2015 8:28 am

Are you using HDMI audio or the TRRS analog audio?
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

CowOne
Posts: 5
Joined: Wed Jul 08, 2015 8:31 pm

Re: Pyaudio outputs slow/crackling/garbled audio

Fri Jul 10, 2015 11:41 am

Hi DougieLawson!

Thank you for the reply!
I'm using TRRS analog phones, but the audio is perfect when I play through aplay together with the same headphones... so I think the problem might be related to pyaudio...

User avatar
DougieLawson
Posts: 39183
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Pyaudio outputs slow/crackling/garbled audio

Fri Jul 10, 2015 4:44 pm

Hold the [SHIFT] key during boot, use the NOOBS recovery config editor to add

Code: Select all

disable_audio_dither=1
to config.txt
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

CowOne
Posts: 5
Joined: Wed Jul 08, 2015 8:31 pm

Re: Pyaudio outputs slow/crackling/garbled audio

Fri Jul 10, 2015 8:20 pm

Just did it: http://imgur.com/4FrjU3Y
But the output is still crackling :|

Do you get the same cracked audio?

User avatar
DougieLawson
Posts: 39183
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Pyaudio outputs slow/crackling/garbled audio

Fri Jul 10, 2015 10:08 pm

I don't use my RPis for audio.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

CowOne
Posts: 5
Joined: Wed Jul 08, 2015 8:31 pm

Re: Pyaudio outputs slow/crackling/garbled audio

Fri Jul 10, 2015 10:43 pm

I am new in the forum and I was in doubt if I should post this issue here or in the Troubleshooting section.
Do you think should I post over there as well, or the Python group is the best place for it?

Thanks,

User avatar
Douglas6
Posts: 4866
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Pyaudio outputs slow/crackling/garbled audio

Fri Jul 10, 2015 11:04 pm

I'm not familiar with PyAudio, but that never stops me from offering advice.
1. Did you install PyAudio from github? That may be better than the repo version.
2. Did you try the callback method of playing from a stream? That's the second example on this page: http://people.csail.mit.edu/hubert/pyaudio/docs/#id4

CowOne
Posts: 5
Joined: Wed Jul 08, 2015 8:31 pm

Re: Pyaudio outputs slow/crackling/garbled audio

Mon Jul 13, 2015 3:57 pm

Thanks for your reply Douglas!

1. Yeah, I just tried that but it still outputs the same result.
It seems that the program plays 200 milliseconds, silences 200, plays 200 again, silences 200, and so on...

2. Yes, that's the example that I mentioned as /pyaudio/test/play_wave_callback.py on my post at Additional information > What I tried > 2-

It plays pretty good with the CallBack method, but my program uses a PyAudio wrapper which plays audio in the same way that it's played on play_wave.py.
I could go ahead and change the wrapper or even create one for my purposes, but that would take a lot of time, and my program is already running perfectly on Ubuntu, so I decided to check if someone already solved this problem since it seems to be an old issue...

I also found this other post: http://stackoverflow.com/questions/2190 ... ing-a-file
But the "solution" doesn't really work. The guy who wrote the answer was probably talking about the raspberry's natural bad audio quality.
I tried to comment over there, but I am not allowed because I don't have a 50 reputation account...

LinuxCircle
Posts: 26
Joined: Sat May 04, 2013 2:03 am
Contact: Website

Re: Pyaudio outputs slow/crackling/garbled audio

Tue Sep 01, 2015 4:40 am

I had the same issue with streaming a stereo live voice through mic in. It will run for 1 minutes before it started to stutter for a few seconds then died completely.

Code: Select all

stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                channels=wf.getnchannels(),
                rate=wf.getframerate(),
                output=True)
The work around was to reduce the channels to 1. I get mono sound. It runs smoothly with minimal distruption, but it's just not optimal when you have a stereo sound source and yet you are only running mono.

There should be a tweak or trick to increase Pi's capacity in processing more than 1 channel
www.linuxcircle.com

Return to “Python”