# Python/PyGame mixer.music playback of OGG file has intermittent clicking sound - how to proceed?

I have a class within my python + pygame game that handles sound - it worked well until I tried use pygame.mixer.music to switch between streaming .ogg music files. The class switches between them on the pause event.

The issue I'm having is that the music will play, and even switch between tracks fine, but about half the time I'm getting a quiet, regular clicking noise over the sound track. I've checked, and the pygame mixer is set to the same bitrate as my .ogg files. I've tried playing with number of channels and tracks, all to no avail. I'm not sure what the root cause of the clicking is - whether I've chosen a poor file encoding / bitrate, or just not initialized/called pygame mixer correctly.

Here is the problimatic code:

 class SoundMgr():

 
def __init__(self):
pygame.mixer.init(22050, -8, 4, 4096)
self.musicFilename = ""
self.musicPos = 0
self.musicVolume = .35
self.musicMuteVolume = .1

def playMusicTrack(self, which):
pygame.mixer.music.load("data/music/" + which + ".ogg")
pygame.mixer.music.set_volume(self.musicVolume)
pygame.mixer.music.play(-1)
self.musicFilename = which

def dampenMusic(self):
mark = pygame.mixer.music.get_pos() * 0.001
self.musicPos += mark
pygame.mixer.music.load("data/music/" + self.musicFilename + "b.ogg")
pygame.mixer.music.play(-1, self.musicPos)
pygame.mixer.music.set_volume(self.musicMuteVolume)

def undampenMusic(self):
mark = pygame.mixer.music.get_pos() * 0.001
self.musicPos += mark
pygame.mixer.music.load("data/music/" + self.musicFilename + ".ogg")
pygame.mixer.music.play(-1, self.musicPos)
pygame.mixer.music.set_volume(self.musicVolume)        </code>


When I call dampenMusic / undampenMusic the track plays correctly, but about half the time it has a clicking sound over it. The other half of the time it plays perfectly. I've tested, and I can't discover any pattern to when it will / won't have the static.

The clicking noise is about 2 times a second, and is about 50% the volume of the track.

Anyone who is more experienced with pygame / sound encoding able to give me any tips?

• Hmm. Some additional info I just found: the clicking seems to correspond to the value of self.musicPos -- if it starts at 0, 2, 4 seconds, the music will play fine. If it starts at 1, 3, 5 seconds, it will always click. 1.2 seconds works, 5.2 seconds clicks. Trying to figure out a pattern... Mar 9, 2012 at 18:40

## 1 Answer

Clicking generally suggests some kind of buffering issue - you're getting a stray sample in there with an incorrect value.

Here's a few things to try:

• Try a much bigger buffer than 4096 samples. Say 65536 or more.
• Go for 2 channels - most music is stereo, although the documentation says the value is clamped anyway.
• Try 16-bit samples at 44100 Hz.
• Stop using the dampen and undampen functions. Sudden volume changes can also cause a click.
• I tried several values (currently at pygame.mixer.init(44100, 16, 2, 262144)) with a 44100 sampled version of the file, and the clicking is behaving exactly the same regardless of how I set up the mixer. After a lot of testing, I'm pretty certain the clicking is dependant on the self.musicPos value passed to pygame.mixer.music.play(-1, self.musicPos) -- about every 0.4 seconds works - i.e. starting at 0.8 always works, 0.11 always clicks. The problem is the amount isn't an exact pattern - 2.4 seconds always clicks. Is there any way to figure out what values are valid, and which arent? Mar 9, 2012 at 20:04
• I'm not sure why, but using this solution in combination with quitting first (as suggested on another thread) solved the problem. My final call was: pygame.mixer.quit() pygame.mixer.init(44100, -16, 2, 4096) Mar 9, 2012 at 21:24