Book Image

JMonkeyEngine 3.0 Cookbook

By : Rickard Eden
Book Image

JMonkeyEngine 3.0 Cookbook

By: Rickard Eden

Overview of this book

Table of Contents (17 chapters)
jMonkeyEngine 3.0 Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Adding some ambient audio


Audio is an extremely important moodsetter in games, and any other cross-media product, which is often overlooked. Bad audio can just as easily break immersion as good audio can improve it.

We're going to add some ambient audio to our scene to help set the mood. Since the sky box we use is a rather gloomy and watery scene, we're going to add the sound of ocean waves crashing against the shore.

Ambient sounds can either be sounds you hear throughout a whole scene, such as the buzzing of traffic in a city, or local to a specific place, the sound of a waterfall, and so on. In this case, we can picture our scene as a small island, and thus the waves should be heard wherever you go.

As it happens, there is a suitable .ogg file in the Environments folder inside Sound. If we have added the jme3-test-data library to our project, we can access it easily.

The SDK can handle both .ogg or uncompressed .wav files. The .ogg format is open and free, meaning you won't need any license to use it. This is not necessarily the case with other compression types.

How to do it…

If we've made the previous recipes, we might already have seen the audio node. The following steps will help show us how to add one to the scene:

  1. We can find the audio node by right-clicking on a spatial, in this case the main scene node, and selecting Add Spatial and then Audio Node.

  2. Next, select it and look at the Properties window.

  3. The first important thing to look at is the Audio Data parameter. In the drop-down menu, the SDK will automatically show the files in the Sounds folder under Project Assets, so we should see Ocean Waves.ogg here. Unchecking the Positional checkbox means there will be no falloff in volume as you move around.

  4. Also check the Looping box to make sure the sound doesn't end when it's finished playing one time.

  5. It's currently not possible to hear the sound in the SDK itself, so we need to start an application to do so. Fortunately, only one line of code is needed to start the sound in our simpleInitApp method. The only catch here is that we need to cast the scene object in an AudioNode instance first. After having loaded the scene, add the following lines of code:

    Node scene = (Node) assetManager.loadModel
    ("Scenes/TestScene.j3o");
    rootNode.attachChild(scene);
    ((AudioNode)scene.getChild("AudioNode")).play();
  6. The sound we added is a very powerful sound and may be a bit overwhelming for our scene. Playing with the Volume property of the AudioNode element can be used to tone down the effect a bit.

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

How it works...

The AudioNode element has a position in the 3D world since it extends Spatial and can hence be made to be heard only from certain places. It can also easily be made to follow objects around. In addition to volume and falloff, audio can also be modified during runtime by area effects such as reverb.

To learn more about how effects can be used to modify audio, check out Chapter 9, Taking Our Game to the Next Level.