© Seth Kenlon 2019
Seth KenlonDeveloping Games on the Raspberry Pihttps://doi.org/10.1007/978-1-4842-4170-7_11

11. Sound

Seth Kenlon1 
(1)
Wellington, New Zealand
 

Real video games have sound effects. Everyone knows that. So if you deliver a game to someone without sound, they immediately notice that it’s missing the audio. This chapter introduces the most important love.audio functions and integrates sound into Battlejack.

Finding Audio

As with any game asset, the first problem with adding sound effects to your game is that you need sound effects. Of course, open culture on the Internet has a few potential solutions, and so does your Raspberry Pi, if you’re up for some manual labor.

Before continuing, create a folder in your project directory for your sounds. To save on typing, name the directory snd .
    $ cd ~/battlejack
    $ mkdir snd

The website freesound.​org is a treasure trove of sound effects. Search for terms like level up, power up, ambient music, game, and so on, to rummage through thousands of professional sound effects that are free to use. The licensing of each sound can differ, so look at the requirements set by the creator; some require attribution, some forbid using their work in commercial products, and others require nothing.

There are other websites loaded with sounds, but you don't have to limit yourself to reusing other people’s work. You can create your own high-quality sounds on your Raspberry Pi with Linux Multimedia Studio, better known as LMMS.

LMMS

LMMS is a music production suite available for free on your Raspberry Pi. Whether or not you call yourself a musician, LMMS makes it easy to lay down a beat, synthesize new sounds, sample, loop, distort, enhance, and mix. LMMS comes with ready-to-use instruments, presets, and samples, making it one of the easiest music applications on any platform to get started on.

Install LMMS as usual.
    $ sudo dnf install lmms

It takes a while for the full package to install, since there are so many synths and effects bundled with it, so be patient. Once installed, launch LMMS from the application menu.

Whatever you make in LMMS ends up in the Song Editor window. This is where you sequence, or schedule, sounds. If you are using LMMS to create sound effects, then everything you sequence should happen immediately in the left-most block. If you are using LMMS to make background music for your game, then the sounds happen gradually, over several blocks from left to right.

By default, the Song editor (see Figure 11-1) has four channels (triple oscillator, sample track, beat/bassline, automation) already populated, but these are only example tracks. As you create sounds, you add your own.
../images/467765_1_En_11_Chapter/467765_1_En_11_Fig1_HTML.jpg
Figure 11-1

The Song Editor window

On the far left of the LMMS window are vertical tabs. When clicked, each tab opens a panel. Click the star icon tab for a Presets panel (see Figure 11-2) containing sounds and synthesizers that you can use to create your sound effects and music bed. More can be found in the musical note tab.

Click and hold some of the sounds to get a feel for what’s available.
../images/467765_1_En_11_Chapter/467765_1_En_11_Fig2_HTML.jpg
Figure 11-2

LMMS preset panel

Building a Sound Effect

Once you find a sound that you like, drag the sound into the left panel of the Song editor. This creates a new track in the Song editor, which is the one that you work in to create your sound effect.

This example uses Presets ➤ ZynAddSubFX ➤ Fantasy ➤ 0037-ImpossibleDream5.xlz.

In the Song editor, double-click the first black square in the ImpossibleDream5 track. This opens the Piano Roll editor (see Figure 11-3). As the name implies, this is the digital equivalent to the rolls used in old-fashioned player pianos. Notes are entered into the matrix of the roll so that they are triggered automatically as your song plays.

Since you’re only designing a sound effect, keep your “song” (such as it is) under 2 or 3 seconds. This example creates a sound effect for messages announcing that new cards have been added to the player’s deck, so the mood is happy and empowering.
../images/467765_1_En_11_Chapter/467765_1_En_11_Fig3_HTML.jpg
Figure 11-3

Piano Roll (or matrix) editor

The important buttons in the Piano Roll editor are the pencil and the eraser. The pencil enters new notes into the matrix, while the eraser removes them. You can hear (but not enter) notes by pressing the keys of the keyboard along the left side of the window.

If you’re unfamiliar with making music, don’t be afraid to experiment. Discover the wonders of a well-constructed arpeggio, or the raw thrill of a power chord. There’s no wrong way to design sound effects. If something sounds good to you, then use it.

When you’re happy with your first sound effect, go to the File menu and select Export.

In the Export window (see Figure 11-4), navigate to the snd folder in your project directory. At the bottom of the Export window, set Files of type to Compressed OGG-File (*.ogg). This format is common in the game industry because it is an open source format that renders a very small file size, meaning your sound files don’t result in an impossibly large game package once you’re ready to distribute. Name the file powerup.ogg and click Save.
../images/467765_1_En_11_Chapter/467765_1_En_11_Fig4_HTML.jpg
Figure 11-4

Export window

In the Export project window that appears, keep the default settings and click the Start button.

Repeat this process for each sound effect that you want to create. So that you only have one LMMS file per project, you can use the same Song editor for each sound effect. Just mute the tracks that you’re finished with as you go. The Mute button for each track is the green light on the left end of the Song Editor track label, as in shown in Figure 11-5.
../images/467765_1_En_11_Chapter/467765_1_En_11_Fig5_HTML.jpg
Figure 11-5

Muting a track

As you work, be sure to save your LMMS project. By default, you are prompted to save into a lmms folder in your home directory. This is acceptable, since you won’t be distributing the LMMS project along with your game. The sound effect exports, however, must always be saved into the snd folder in your Battlejack project directory.

Listening to Your Effects

To hear your sound effects as they will play in your game, navigate to your snd directory on your desktop and click the .ogg file that you want to hear. The default music player, called Sayonara (see Figure 11-6), opens with that file as the sole item in a playlist in the left panel.
../images/467765_1_En_11_Chapter/467765_1_En_11_Fig6_HTML.jpg
Figure 11-6

Sayonara player

Adjusting Export Length

Depending on your aesthetic, some sounds that you use may have a long “tail,” meaning that they echo for a few seconds after the notes themselves have ended. The synth preset ImpossibleDream5 is an example of such an effect, and if you listen to the file in Sayonara, you can hear that the file ends before the sounds fade.

To fix this, return to LMMS. Mute any of the tracks that you don’t need, and unmute the one that you do. Play the track and watch the timer in the top left of the LMMS window while listening for the sound to fade. It doesn’t need to fade out completely, especially if you intend to play background music during the game. When the sound is mostly faded, take a mental note of how many seconds have elapsed.

Stop playback and move the playhead in the Song editor to the position, in seconds, when the sound faded. With a sound clip using ImpossibleDream5 notes that last about 2.5 seconds, you might position the playhead at 10 seconds.

Finally, click the black grid boxes between the sound clip and the playhead (see Figure 11-7). This marks those squares as occupied with sound, even though there are no notes being played.
../images/467765_1_En_11_Chapter/467765_1_En_11_Fig7_HTML.jpg
Figure 11-7

Creating a buffer at the end of a track

Export the track again and play it in Sayonara for quality assurance.

Creating Music

Making music for your game is basically the same process as making sound effects, only longer. There’s plenty of Creative Commons and royalty-free music online, so you don’t have to compose your own game music. However, creating your own music for your game can be fun and rewarding.

Since Battlejack is just a battle card game, the music doesn’t need to be much more than a background soundscape, without too much activity or complexity to distract the player from the actual game. In fact, regardless of what kind of game you are composing for, the music you use must be capable of looping, since you can never predict just how long it will be needed.

Unlike most simple sound effects, music composition will probably require more than one track in your Song editor. The sample code included with this book includes two LMMS project files so that you can see how tracks and sound clips in a song fit together.

When you’re ready to export your music, use the same settings as you did for your sound effects, with one exception. In the Export project window (see Figure 11-8), select “Export as loop (remove end silence)” so that your music can be looped in LÖVE.
../images/467765_1_En_11_Chapter/467765_1_En_11_Fig8_HTML.jpg
Figure 11-8

Exporting

LMMS can’t guarantee that your music will loop seamlessly; designing seamless loop points is an art all its own, but you can experiment with ways to disguise loops that don’t work, or you can just incorporate a fade-out and fade-in so that the loop, while noticeable, is at least innocuous.

Sound Code

To add sounds to your game, you must create a variable for each sound file that you want to play, classifying it as either a static file for sound effects or a stream for background music. You use this variable to trigger the sound as needed.

For instance, for the powerup message alert, first create a variable at the top of the game.lua file.
fxp = love.audio.newSource("snd" .. d .. "powerup.ogg", "static")
function game.load() --for context
game.cleanup()    --for context
Trigger the sound when the message screen loads to announce a new powerup. Add this code to the msg.activate function of msg.lua:
if earncard ~= nil then
   love.audio.play(fxp)
else -- only one card to display
   return true
end

The if statement establishes whether the message screen is displayed to announce a powerup or because a black card was drawn. If a black card was drawn, nothing is done for now, but if a powerup has been earned, then the powerup sound effect plays. Of course, if you have a sound effect for a traitor card, then you can trigger that sound in the else clause.

Background music is established in main.lua. It’s up to you whether you create separate music for the menu screen and the game, but in this example, the same music plays behind the music and the game.

Since the music is both instantiated and played immediately, everything happens in the love.load function of main.lua.
   music = love.audio.newSource("snd" .. d .. "darkbattle.ogg", "stream")
   music:setLooping(true)
   love.audio.play(music)

For sound files greater than a few seconds, set the file type to stream so that the entire file isn’t loaded into memory.

Since the background music is meant to loop, the variable is also set to loop with the :setLooping(true) LÖVE function.

Go through the code of your game and add background music and sound effects for important events.

If you try to play your game, you may notice that all of your audio is badly distorted. This is a known issue and is easy to fix. You will fix it in the next section.

Fixing the Raspberry Pi Sound Settings

LÖVE uses a technology called OpenAL to play sound. OpenAL is a powerful audio driver providing head-related transfer function (HRTF), more commonly known as 3D audio. For this reason, it’s very popular for gaming. As you know, the Raspberry Pi is a low-powered computer, and not exactly known as a “gaming rig.” So when you attempt to play high-powered, specialized OpenAL audio through a mini-computer designed for everyday computer tasks, you experience distortion.

Note

This audio issue is specific to OpenAL on a Raspberry Pi or other low-powered computers. If you’re using something more powerful than a Pi, then you do not need to complete this section unless you’re experiencing distorted audio in LÖVE.

OpenAL can be configured to be gentler with its host system. By default, global configuration files on Linux are stored in the directory /etc (no one’s quite sure any more what that stands for, but there’s some evidence that it does indeed mean et cetera, which is an oddly imprecise name for such an important folder). Since this directory exists outside of your home directory, it’s only accessible by the root user, which you invoke with the sudo command. Edit the OpenAL configuration file using the sudo command to open it in Geany.
  $ sudo geany /etc/openal/alsoft.conf
You must change two lines in the file. First, you need to edit the number of audio frames that occur between audio mixing updates. This is something that usually OpenAL determines on its own, but in the Raspberry Pi, OpenAL is overly optimistic about what its host system is capable of achieving. Find this line:
  #period_size = 1024
The hash (#) character means this line is “commented out,” meaning that OpenAL is currently ignoring the line entirely. Delete the hash character (or as programmers usually say, “uncomment”). Change the number of frames in each update period to 2048. The line should now match this:
  period_size = 2048
Next, find this line:
  #periods = 0
This line is also commented out, so uncomment it by deleting the hash character. A low number here (it is explained in the comments of the configuration file) means faster response from the computer when playing sounds. That’s the ideal but obviously the Raspberry Pi isn’t up to the challenge, so set this value to 8. This tells OpenAL to mix audio in advance so that they’re prepared for playback when needed, rather than trying to mix all the audio on demand.
  periods = 8

Save the file and close Geany.

Try playing the game now and you will find that all of your audio problems are solved.

Since this is a known issue that you have now solved, create a README file in your project directory, documenting the problem and its solution in the event that you distribute the game to other Pi users .
  # Audio issues
  If you experience audio distortion when playing
  this game, edit /etc/openal/alsoft.conf and set these
  values.
  period_size = 2048
  periods = 8

This is good practice, since you can’t possibly predict what platform any given user will use to play your game.

Homework

Many games provide some control over sound settings. You could add sound options to your menu screen, such as the following.
  • Allow the background music to be silenced, leaving only sound effects.

  • Allow all sound in the game to be muted.