20. Sound Effects

../../_images/sounds.svg

Adding sound to your game isn’t too hard. There are two steps:

  1. Load the sound

  2. Play the sound

For these examples, I’m using a sound file called laser.wav that you can download here. Make sure you save the file in the same directory as any Python program that tries to use it.

20.1. Loading Sounds

../../_images/loading_sound.svg

The code below creates a new variable called laser_sound. It calls Arcade`s arcade.load_sound() function. It passes the filename of our sound.

laser_sound = arcade.load_sound("laser.wav")

For this to work, you need to have a sound downloaded and named laser.wav in the same directory as your Python file. The computer will not find the sound if it is in a different directory.

This loads the sound, but does not play it. We only want to load the sound once. We don’t want to load the sound off the disk every time we play it. It can be kept in memory.

20.2. Playing Sounds

../../_images/headphones.svg

The code to play sounds is straight-forward. Just call the play_sound() function, and pass in the variable that we set equal to the sound we loaded:

arcade.play_sound(laser_sound)

Putting the two together, you might think we could do this to play sounds:

import arcade

laser_sound = arcade.load_sound("laser.wav")
arcade.play_sound(laser_sound)

But that doesn’t work. The program ends before the sound has a chance to play. The play_sound button doesn’t pause while the sound plays, it returns right away and keeps going. This was a similar issue that we had when we opened a window, and we can solve it the same way:

1
2
3
4
5
6
import arcade

arcade.open_window(300, 300, "Sound Demo")
laser_sound = arcade.load_sound("laser.wav")
arcade.play_sound(laser_sound)
arcade.run()

For this reason, I recommend loading the sound in the __init__ method of the class that will play the sound.

20.3. Triggering Sounds

../../_images/switch.svg

We want to play the sound when something happens. So this example loads the sound once during the __init__. When the user hits the space bar, that is when we trigger the sound to play.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import arcade


class MyApplication(arcade.Window):

    def __init__(self, width, height):
        super().__init__(width, height, "Trigger Sound With Key")

        # Load the sound when the application starts
        self.laser_sound = arcade.load_sound("laser.wav")

    def on_key_press(self, key, modifiers):

        # If the user hits  the space bar, play the sound that we loaded
        if key == arcade.key.SPACE:
            arcade.play_sound(self.laser_sound)


def main():
    window = MyApplication(300, 300)
    arcade.run()

main()

20.4. Seeing If a Sound is Playing

Sometimes we want to know if a sound is playing. For example, if the user is bumping the edge of the screen, we may want to play the bump sound once, and not 60 times per second. Because that sounds weird.

This complicates our code just a little bit. Not only do we need an attribute for the sound file, we need an attribute for the sound player. We’ll use this to check if the sound is playing or not.

The pattern for this code looks like the following. In the __init__ where we load the sound, also create a sound player attribute:

self.explosion_sound = arcade.load_sound(":resources:sounds/explosion2.wav")
self.explosion_sound_player = None

Then, when we play the sound, check to see if the sound is playing. If there is no player, or the playing boolean is false, we’ll play the sound. We’ll also capture the sound player that is returned by the function.

if not self.explosion_sound_player or not self.explosion_sound_player.playing:
    self.explosion_sound_player = arcade.play_sound(self.explosion_sound)

20.5. Finding Sounds

There are a few sounds built into Arcade. You can load :resources:sounds/explosion2.wav or one of the other sounds found near the bottom of the resources page:

https://api.arcade.academy/en/latest/resources.html

Great places to find free sounds to use in your program:

20.6. Sound File Formats

../../_images/waves.svg

There are several types of sound file formats that you can find sounds in:

  • .wav - This is a raw “wave” format. The sound is not compressed at all. You do not need a special library to decompress the sound, but the sound file itself can be rather large.

  • .mp3 - MPEG Layer III Audio (mp3) is one of the most popular compressed sound file formats. This file format is what enabled on-line music to become popular. However some of the compression algorithms have patents on them, making it not as suitable for free software.

  • .m4a - Apple’s file format for compressed, but unprotected audio.

  • .ogg - A open-source sound file that uses Ogg-Vorbis for compression. A less popular but patent-free method of sound storage.

Arcade should be able to play files in either the mp3 or ogg format. If you have issues getting it to work, try converting the sound to a raw wav format.

If you need to convert file formats, or trim a sound file, I suggest using the program Audacity.