pygame audio playback speed

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.


Open your audio file in a free audio tool like Audacity. It will tell you the sampling rate of your media. It will also allow you to convert to a different sampling rate so all your sounds can be the same.


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


I figured it out... There is a wave module http://docs.python.org/library/wave.html and it can read the sample rate for wav files.

Tags:

Python

Pygame