Chapter 9. Cranking Up the Bass – Music and Sound Effects
Since the beginning of video games, sound has been a central media in games. Many games can be recognized only by listening to them. In the last few years, game industries have chosen to neglect audio in favor of better graphics; yet audio takes a very important part in a wide range of games. Independent studios in particular often put a huge effort in making games unique in their art style, which includes audio, graphics, and story. If used appropriately, music themes and sounds can have a tremendous impact on the atmosphere conveyed by a game.
In this chapter, we are going to cover the technical background of embedding audio into a game, taking the opportunity to have a closer look at SFML's Audio module. We are going to do the following:
Play different music themes in the background
Play sound effects that correspond to game events such as explosions
Position sound effects in the 2D world to convey a feeling of spatial sound
First, we want to play background music depending on the state we are currently in. We have prepared two themes: One for the menu, and one for the game itself. We'll define a corresponding enum
:
We'll then create a class that has an interface dedicated to music playing:
The method names should be self-explanatory. We have a single sf::Music
instance that represents the currently-played music. The
mFilenames
variable maps music...
We have many gameplay events that can be represented by sounds: Fired machine guns, launched missiles, explosions, collection of pickups, and so on. Unlike music, sound effects are mostly very short. As a consequence, they can be loaded completely into memory, and we can also use the raw WAV format for these files without wasting too much memory. We are going to use the sf::SoundBuffer
resource class to store the audio samples for our sound effects.
The following enumeration of sound effects is used in our game. We'll also create a typedef
for the resource holder of sf::SoundBuffer
.
We implement a class for the sound effects, one similar to the MusicPlayer
class:
The most interesting part about sound effects is yet to come. An immersive atmosphere only builds up if sounds are properly located within the game world. Like graphical objects, sounds can have a position.
The coordinate system for sounds is three-dimensional. SFML's sound API works with the sf::Vector3f
type, a 3D vector with the members x
, y
, and z
. SFML internally uses Open Audio Library (OpenAL), an interface for low-level audio functionality, which is also the origin of the sound spatialization concepts we are going to discuss here.
Spatializing sounds means nothing more than to locate them in the 3D space, that is, to give them a spatial representation.
The audition of spatial sound effects depends on the listener. A useful analogy is to compare the listener with your head. The listener's location and orientation can be described with the following three 3D vectors:
In this chapter, we have covered the basic topics for dealing with audio in 2D games. We saw how the concepts are implemented using SFML classes and functions. In particular, we played different music themes depending on the application state. We also created sound effects for various in-game events, and positioned them in the world in order to enable a spatial audition.
With this knowledge, you are now able to incorporate audio into your game; a crucial component for creating an immersive gaming experience. As you have seen during this chapter, SFML makes the implementation very simple. The actual challenge is the fine-tuning and the combination of suitable sound effects and music themes in order to create a unique atmosphere.
The audio functionality completes the attempt to create a playable game that uses different sources of media. However, so far, only one player can play it. In the next chapter, we are going to improve the situation by adding multiplayer support.