pygame audio playback speed

Solution 1:

I had some mp3 audio tracks playing back slowed down. I updated the mixer frequency to be based on the mp3 sample rate using mutagen like so:

import pygame, mutagen.mp3

song_file = "your_music.mp3"

mp3 = mutagen.mp3.MP3(song_file)
pygame.mixer.init(frequency=mp3.info.sample_rate)

pygame.mixer.music.load(song_file)
pygame.mixer.music.play()

And it fixed the problem.

Solution 2:

To improve Chris H answer. Here is a example of how to use the wave library.

import wave
import pygame

file_path = '/path/to/sound.wav'
file_wav = wave.open(file_path)
frequency = file_wav.getframerate()
pygame.mixer.init(frequency=frequency)
pygame.mixer.music.load(file_path)
pygame.mixer.music.play()

Remember that if you want to change frequency or any other parameter used in pygame.mixer.init you must call pygame.mixer.quit first. Pygame documentation