Chapter 10. Managing the Music and Sound Effects

In this chapter, you will learn how to manage your music and sound effects. LibGDX provides you with four interfaces to handle different types of audio data. The first two interfaces that we will discuss are targeted at playing back prerecorded audio files. The two remaining interfaces give us even more low-level access to the audio device. They can be used to record and play back raw samples of audio data, which is a so-called Pulse Code Modulation (PCM) encoded audio signal. Next, we will take a look at the great world of sound generators. These tools are extremely handy as they allow you to quickly create new sound effects in a short period of time.

Lastly, looping background music and some sound effects for certain in-game events will be added to Canyon Bunny. The game's audio settings can be changed in the options menu of the menu screen through two new checkboxes and sliders for music and sound effects.

Playing back the music and sound effects

LibGDX provides cross-platform audio playback for prerecorded audio files of the following three supported file formats:

  • .wav (RIFF WAVE)
  • .mp3 (MPEG-2 Audio Layer III)
  • .ogg (Ogg Vorbis)

    Note

    However, Ogg support is not available for the robovm version because iOS doesn't support it.

There are two interfaces, namely Music and Sound. They serve two different use cases of playing back audio files. In terms of LibGDX, sounds are audio files that usually play no longer than one second; think of laser or machine gun sounds. Audio files used as Sound objects are loaded and decoded so that they can be directly sent to the audio device. Obviously, the decoded audio data kept in memory can heavily increase the overall memory usage. On the contrary, audio files are used as the Music objects are streamed, which means that only the necessary portion of it is decoded and held in memory. Therefore, the Music objects should be used for playing long audio files, such as background music.

Note

The Music objects may require more CPU cycles because the streamed audio data needs to be decoded before it can be sent to the audio device. This is not the case with the Sound objects because they are already decoded upon loading. So, it is simply a trade-off between performance and memory usage.

The next sections will give you an overview of LibGDX's Sound and Music interfaces.

Exploring the Sound interface

The Sound interface is suitable for playing back short audio files. New sound instances can be requested from LibGDX's Gdx.audio module using the newSound() method as follows:

Sound sound = Gdx.audio.newSound(Gdx.files.internal("sound.wav"));

This line of code will allocate new memory for the decoded audio data of the sound.wav file. The Sound interface also makes use of the Disposable interface, which implicates that the allocated resources need to be disposed manually using the respective dispose() method when a sound instance is no longer needed, as shown in the following code snippet:

sound.dispose(); // free allocated memory

Calling the play() method of a sound instance will start the playback of its audio data and will return an ID that is associated with the sound that is being played. A sound ID can be used to refer to a specific playing sound. This allows you to control sounds at a later time, such as stopping the playback and changing the volume, pitch, and pan.

Note

LibGDX will silently ignore any requests to a referred sound if that sound is no longer playing.

There are several overloaded methods to start playing a sound for a single time or in an endless loop. Optionally, values for the sound's volume, pitch, and pan can also be passed as shown here:

long play();
long play(float volume);
long play(float volume, float pitch, float pan);
long loop();
long loop(float volume);
long loop(float volume, float pitch, float pan);

The value for volume ranges between 0.0 and 1.0, where higher values result in louder audio signals. Sounds can be pitched up and down resulting in lower or higher frequency audio signals, respectively. A pitch value of 1.0 will play the sound at its normal speed. Pitch values above 1.0 will let the sound play faster, while values below 1.0 will result in slower playback. A pan value of 0.0 will play the sound equally loud on the left and right audio channels, which is also referred to as the center. Negative pan values will play the sound only on the left audio channel whereas positive pan values will achieve the opposite.

Consider a scenario where we have two sounds, one for a cat and the other for a dog. Both sounds will be loaded in LibGDX as separate sound instances that can be used to play back several copies of them in parallel. To stop all playing copies of a (cat or dog) sound instance, the stop() method can be called. Alternatively, a sound ID can be passed to the stop() method to stop a specific copy of the (cat or dog) sound instance only, as shown here:

void stop();
void stop(long soundId);

Sounds that are currently playing can be modified using their sound ID and one of the following methods:

void setVolume(long soundId, float volume);
void setPan(long soundId, float pan, float volume);
void setPitch(long soundId, float pitch);
void setLooping(long soundId, boolean looping);

Exploring the Music interface

The Music interface is suitable for playing back long audio files and is designed in a very similar way when compared with the Sound interface. Therefore, new music instances can also be requested from LibGDX's Gdx.audio module; however, the method is called newMusic(), as shown here:

Music music = Gdx.audio.newMusic(Gdx.files.internal("music.mp3"));

The Music interface also makes use of the Disposable interface. The music instances that are no longer needed should always be disposed to free the allocated memory as follows:

music.dispose(); // free allocated memory

Apart from this, the interface provides the expected method to control music playback as follows:

void play();
void pause();
void stop();

Additionally, there are some methods to modify the music being played, as shown in the following listing:

void setPan(float pan, float volume);
void setVolume(float volume);
void setLooping(boolean isLooping);

At times, it might be useful to query the music for its state, such as its current position (in milliseconds) and whether it is still playing or not, as follows:

boolean isPlaying();
float getPosition();
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.22.164