Pygame bad sound quality

Viewed 323

Im trying to play a backround music in pygame but the qualtiy is kinda bad, altough if i play the the mp3 file outside python, for example in vlc, the sound quality is perfectly fine, so there is no issue with the file itself / my headset. this is the code that i used to play the song:

mixer.music.load("song.mp3")
mixer.music.set_volume(0.6)
mixer.music.play(-1)

any ideas how to fix it?

2 Answers

Try using pygame.mixer.init()

For example:

pygame.mixer.init(44100, -16, 2, 2048) # (frequency, size, channels, buffer)

It's possible that a low buffer size is attributing to a poor audio quality. Play around with the values.

Read up on mixer.init here

I was able the fix this issue, and it turns out there was no issue with my code.

I did some testing with different mp4 and ogg files, and I have found that, for some reason, if an mp4 file has very low kpbs rate, then its going to sound weird—although in music players like Groove and VLC it’s going to sound fine—when played in pygame with:

mixer.music.load("song.mp3")
mixer.music.set_volume(0.6)
mixer.music.play(-1) # the -1 is just so the song starts to play again, if it finished

So what I did is download the song with a higher kbps rate, and the quality instantly got better in pygame / python aswell.

Related