backlinks to this page:
Github:
This is an old revision of the document!
Pygame has several commands to play sound. Pygame offers a set of Sound commands to load and play short sound files - usually .wav
or .ogg
files. For longer files, pygame offers pygame.music commands to stream longer music / sound / voice files directly from the harddisk.
In the source code example below, loading sounds and music is done from a subfolder called data
, like the graphic files in the previous source code example.
To get rid of a nasty delay between giving the play command for a sound and hearing it you need to initialize the pygame mixer. This is done before writing the pygame.init() command. Under Linux, I got best results with this line:
pygame.mixer.pre_init(44100, -16, 2, 2048) # setup mixer to avoid sound lag pygame.init() #initialize pygame
Note that loading the sound and music files from harddisk must be done after setting up the mixer. In the code-example below, an-turr.ogg is a longer music file while jump.wav and fail.wav are short sound effects:
try: pygame.mixer.music.load(os.path.join('data', 'an-turr.ogg'))#load music jump = pygame.mixer.Sound(os.path.join('data','jump.wav')) #load sound fail = pygame.mixer.Sound(os.path.join('data','fail.wav')) #load sound except: raise UserWarning, "could not load or play soundfiles in 'data' folder :-("
After loading the sound effects or the music and thus creating pygame objects, you can simply call the .play()
method of those objects. Inside the brackets you write -1 for endless play or the number of times or the time length to play the sound.
#for sound effects: #Sound.play(loops=0, maxtime=0, fade_ms=0): return Channel jump.play() # play the jump sound effect onceThe music is usually played endlessly:
#music is already the name of the music object #pygame.mixer.music.play(loops=0, start=0.0): return None music.play(-1) # play endless
<note tip>you hear just a short crack noise instead of a sound? Some sounds simply do not work with pygame. Try to open (and edit) the sound with a sound editor like Audacity and save it again, preferable in the free .ogg (vorbis) format.</note>
Note that you do not need pygame's graphic at all to play music files. One of the code examples below use graphical output, the other example use only text output. In text output, you still need to initialize pygame and set up the mixer.
Unlike music, sound effects are easy enough to generate and not worth the hassle of copyright fights. You may find good sound effects here:
To generate sound effects, you can either:
To run this example you need:
file | in folder | download |
---|---|---|
010_sound_and_music.py | pygame | Download the whole Archive with all files from Github: https://github.com/horstjens/ThePythonGameBook/archives/master |
010_sound_only_no_graphic.py | pygame |
|
an-turr.ogg from modarchive.org | pygame/data |
|
fail.wav | pygame/data |
|
jum.wav | pygame/data |
View/Edit/Download the file directly in Github: https://github.com/horstjens/ThePythonGameBook/blob/master/pygame/010_sound_only_no_graphic.py
click reload in your browser if you see no code here:
View/Edit/Download the file directly in Github: https://github.com/horstjens/ThePythonGameBook/blob/master/pygame/010_sound_and_music.py
click reload in your browser if you see no code here:
~~DISQUS~~