Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

Sounds !

Finally ! The last thing that missed in my game was sound !

Well... I mean, the last thing that missed to be submittable to the jam. Because if we talk about a release to the Play Store, for example, many things are missing, like... hum... where to start ? Quality graphics, polished gameplay, quality sounds, quality UI.... quality, quality, quality...

Let's get back to what interests us in this post : Sound !

In my game, Major Tom wanders in his wrecked spaceship. Without gravity, and more important, without air. Therefore, if I wanted realism, there wouldn't be any environmental sounds. There would be only Major Tom breath sound, and muffled sounds from contact between Major Tom and the environment.

But, I thought that this approach would be very interesting only if it's very well done, with quality recording of an actor playing stress breathing as the oxygen decreases... well... That was definitely not in my range.


Therefore, I added sounds.

I had to create sounds, the best I can, with Audacity... Generating a white noise, cutting the high frequencies and boosting the low frequencies to create a background sound. Recording with my phone (high quality sounds guys !) pressured air going through my lips, and putting the sound in backward... That's the kind of things I did yesterday. And for few sounds, I was desperate, and I took sounds on Freesound.org.


Implementing the sounds

Implementing sounds in libGDX is very easy. There are 2 class : Sound and Music.

A Sound will be loaded in the memory, while a Music will streamed, so you can use large size high-quality music files without needing to use your memory.

First, I'll load my sound files in the AssetManager, in the LoadingScreen.java :

//Loading of the sounds
game.assets.load("Sounds/Piston.ogg", Sound.class);
game.assets.load("Sounds/Jetpack.ogg", Sound.class);
game.assets.load("Sounds/Impact.ogg", Sound.class);
game.assets.load("Sounds/Door.ogg", Sound.class);
game.assets.load("Sounds/Fuel Refill.ogg", Sound.class);
game.assets.load("Sounds/Oxygen Refill.ogg", Sound.class);
game.assets.load("Sounds/Button On.ogg", Sound.class);
game.assets.load("Sounds/Button Off.ogg", Sound.class);
game.assets.load("Sounds/Exit.ogg", Sound.class);
game.assets.load("Sounds/Gaz Leak.ogg", Sound.class);
game.assets.load("Sounds/Background.wav", Music.class);

Then I can use these sounds in my game. You can see that I have 10 Sounds and 1 Music, for the background sound.

The background sound

The background sound is created in the GameScreen.java.

backgroundSound = game.assets.get("Sounds/Background.wav", Music.class);
backgroundSound.setLooping(true);
backgroundSound.play();
backgroundSound.setVolume(0.15f);

Note the setLooping() function, that allows you to loop the Music indefinitely.

The other sounds

I won't detail the use of every other sounds because, 1) it will be redundant and 2) I am REALLY lacking time to finish my entry before the deadline. I'll just show some basic things.

Each object (Hero, Door, Gas Leak, Switch...) will have its sound. Thus, to create the sound, in the creator I'll use :

sound = game.assets.get("Sounds/Gas Leak.ogg", Sound.class);

Then, there are many ways to play a sound. You can simply play the sound once with :

sound.play()

or, you can play sound in loop with :

sound.loop()

You can also set the volume, the pitch and balance the sound between the left and right speakers at the same time you play the sound with :

sound.play(volume, pitch, pan)
or
sound.loop(volume, pitch, pan)

And finally, you can give an ID to the sound at the same time you play it, which will be VERY useful to interact with a precise instance of a sound.

long soundId;

soundId = sound.play(volume, pitch, pan);


Then, when you want to stop every instance of a sound you can do

sound.stop();

But if you want to stop only a precise instance of a sound, you use its ID

sound.stop(soundId);


Ex : The gas leak sound :

In the Leak.java, I create the Sound in the creator() :

sound = game.assets.get("Sounds/Gas Leak.ogg", Sound.class);
soundId = sound.loop(0.1f, MathUtils.random(0.98f, 1.02f), 0);

Note that when I create the sound, I set the pitch with a random float (MathUtils.random(0.98f, 1.02f)). This method is useful to have different object creating the same sound (in this case, the gas leak), without having an echo effect. Every gas leak will create a slightly different sound.

I want the sound to be louder as the hero get closer to the leak. For that, in the render (that is the active() function in the Leak.java) I have this line :

sound.setVolume(soundId, 4/(new Vector2(hero.heroBody.getPosition().sub(posX, posY)).len()));

With this code, I set the sound volume according to the distance between the hero and the leak, at every render step. The "4" is completely arbitrary. I chose it by try/error. This gave me satisfactory results. I have to fight a number that satisfies me for every object that has a distance dependent sound. For example, for the Piston.java, I use the number "10".


Well, that's pretty much it for the sounds. With slight differences for the different case, but you can see the codes of the differents objects, for the variations.