Chris H
Chris H

Reputation: 6581

pygame audio playback speed

quick question.

I'm running pygame under linux just to play some audio files. I've got some .wav files and I'm having problems playing them back at the right speed.

import pygame.mixer, sys, time

#plays too fast
pygame.mixer.init(44100)
pygame.mixer.music.load(sys.argv[1])
pygame.mixer.music.play()
time.sleep(5)
pygame.mixer.quit()

#plays too slow
pygame.mixer.init(22100)
pygame.mixer.music.load(sys.argv[1])
pygame.mixer.music.play()
time.sleep(5)
pygame.mixer.quit()

I've ggogle code searched some stuff but everybody seems to be fine calling the init function with its default parameters. Can others try running this script and seeing if they get the same behavior or not? Does anybody know how to speed it up? Or adjust the speed for each file?

Thanks.

Upvotes: 9

Views: 20620

Answers (5)

Lost Odinson
Lost Odinson

Reputation: 408

If you're using Ogg Vorbis (.ogg) encoding, the same problem of stuttering audio happens. You'll have to read the frequency of what you're trying to play before initializing the mixer object.

Here's how to play .ogg audio with appropriate frequency using pygame.

from pyogg import VorbisFile
from pygame import mixer

# path to your audio
path = "./file.ogg"
# an object representing the audio, see https://github.com/Zuzu-Typ/PyOgg
sound = VorbisFile(path)
# pull the frequency out of the Vorbis abstraction
frequency = sound.frequency
# initialize the mixer
mixer.init(frequency=frequency)
# add the audio to the mixer's music channel
mixer.music.load(path)
# mixer.music.set_volume(1.0)
# mixer.music.fadeout(15)
# play
mixer.music.play()

Upvotes: 2

alej0
alej0

Reputation: 136

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

Upvotes: 5

Charles Clayton
Charles Clayton

Reputation: 17946

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.

Upvotes: 5

Chris H
Chris H

Reputation: 6581

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.

Upvotes: 2

Pace
Pace

Reputation: 43787

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.

Upvotes: 3

Related Questions