matanc1
matanc1

Reputation: 7013

Pyaudio sound quality when playing a file

So I'm trying to run the following script on my raspberry pi:

"""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(sys.argv[1], '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()

I created a WAV file using: arecord -D plughw:1 --duration=5 -f cd -vv ~/rectest.wav

When I play it like: aplay ~/rectest.wav it plays nicely and the sound is loud and clear.

When I use the script (run python play.py ~/rectest.wav) the sound quality is terrible. I've also noticed that the recording is 5 seconds but when I play it using the script it takes around 8-9 seconds for it to finish playing (it seems the distortion makes it longer).

What's going on here? How do I fix it?

Upvotes: 2

Views: 4231

Answers (1)

Fahadkalis
Fahadkalis

Reputation: 3261

I was also facing the problem, but I get rid from it simply by just Mute the Gain control and decrease the volume of speaker and MIC till white level.

enter image description here

After that my sound quality become crystal clear mean more better.

so just open sound controller by this command

alsamixer

and then use your arrow keys to switch b/w options, volume increase/decrease of Speaker/MIC and use SPACEBAR to mute/unmute the Control Gain.

Upvotes: 1

Related Questions