1

I am trying to play a wav file in my raspberry-pi using pygame. I am able to play the music in the speaker.But the sound is very low and many noises are coming along with the sound. Is there some specific setting i have to do with raspberry-pi to make sounds work. My code is as given below.

import pygame
pygame.mixer.init()
pygame.mixer.music.load("abc.wav")
pygame.music.mixer.play()
while pygame.mixer.music.get_busy == True"
    continue

Also tried

aplay abc.wav

But the output is still same.The sound quality is still poor..

  • can you confirm that neither the speakers nor the soundfile are cause for the bad quality? (e.g. by testing them on a different computer) – LuWi Jul 14 '14 at 9:49
  • I tested it on a different computer and it works fine. also i tested with 2 different speakers and both are having the problem .. – slaveCoder Jul 14 '14 at 11:14
  • Are you using the on board audio jack or HDMI audio? – Craig Jul 14 '14 at 20:09
3

I know another user on here that uses pygame on a pi uses these params in his init method:

pygame.mixer.init(48000, -16, 1, 1024)

Documentation on the pygame.mixer.init can be found here:

pygame.mixer.init()

initialize the mixer module

init(frequency=22050, size=-16, channels=2, buffer=4096) -> None

Initialize the mixer module for Sound loading and playback. The default arguments can be overridden to provide specific audio mixing. Keyword arguments are accepted. For backward compatibility where an argument is set zero the default value is used (possible changed by a pre_init call).

The size argument represents how many bits are used for each audio sample. If the value is negative then signed sample values will be used. Positive values mean unsigned audio samples will be used. An invalid value raises an exception.

The channels argument is used to specify whether to use mono or stereo. 1 for mono and 2 for stereo. No other values are supported (negative values are treated as 1, values greater than 2 as 2).

The buffer argument controls the number of internal samples used in the sound mixer. The default value should work for most cases. It can be lowered to reduce latency, but sound dropout may occur. It can be raised to larger values to ensure playback never skips, but it will impose latency on sound playback. The buffer size must be a power of two (if not it is rounded up to the next nearest power of 2).

Some platforms require the pygame.mixerpygame module for loading and playing sounds module to be initialized after the display modules have initialized. The top level pygame.init() takes care of this automatically, but cannot pass any arguments to the mixer init. To solve this, mixer has a function pygame.mixer.pre_init() to set the proper defaults before the toplevel init is used.

It is safe to call this more than once, but after the mixer is initialized you cannot change the playback arguments without first calling pygame.mixer.quit().

1

The volume for raspberry-pi is set low by default.Used this command and it works fine.

amixer set PCM -- 1000

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.