# Audio in jME3

There are two ways to handle audio data: Short audio files are to be stored entirely in memory, while long audio files (music) is streamed from the hard drive as it is played. Place audio files in the `assets/Sound/` directory of your project. jME3 supports Ogg Vorbis (.ogg) and Wave (.wav) formats.

## Creating Audio Nodes

The main class to look at is `com.jme3.audio.AudioNode`. By default, a new audio node is buffered, i.e. JME loads the whole file into memory before playing:

    AudioNode boom = new AudioNode(audioRenderer, assetManager, "Sound/boom.wav");

If it is a long file, you set the boolean to true to stream the audio.

    AudioNode music = new AudioNode(audioRenderer, assetManager, "Sound/music.wav", true);

## Setting AudioNode Properties

AudioNode Method | Usage
--- | ---
getStatus() | Returns either Status.Playing, Status.Stopped, or Status.Paused.
setVolume(1) | Sets the volume gain. 1 is the default volume, 2 is twice as loud, 0 is mute.
setPitch(1) | Makes the sound play in a higher or lower pitch. Default is 1.
AudioNode Method | Usage
setLooping(false) | Configures the sound that, if it is played, it plays once and stops. This is the default.
setLooping(true) | Configures the sound that, if it is played, it plays repeats from the beginning, until stop() or pause() are called. Good for ambient background noises.
setPositional(false) setDirectional(false) | All 3D effects switched off. This sound is global and comes from everywhere. Good for environmental ambient sounds and background music.
setTimeOffset(0.5f) | Start playing the sound after waiting the given amount of seconds. Default is 0.
setMaxDistance(100f) | Maximum distance the sound can be heard, in world units. Default is 20.
AudioNode Method | Usage
setPositional(true) setLocalTranslation(new Vector 3f(0,0,0)) | Activates 3D audio, the sound appears to come from a certain position, where it is loudest. Position the AudioNode in the 3D scene if you have setPositional() true. Position it with mobile players or NPCs.
setReverbEnabled(true) | A 3D echo effect that only makes sense to use with moving positional AudioNodes. The reverb effect is influenced by the environment that the audio renderer is in. See "Setting Environment Properties" below.
AudioNode Method | Usage
setDirectional(true) setDirection(new Vector3f(0,0,1)) | Activates 3D audio. This sound can only be heard from a certain direction. Specify the direction and angle in the 3D scene if you have setDirectional() true. Good for noises that should not be heard through a wall.
setInnerAngle() setOuterAngle() | Set the angle in degrees for the directional audio. The angle is relative to the direction. By default, both angles are 360° and the sound can be heard from all directions.

## Play, Pause, Stop

You play, pause, and stop a node called myAudioNode by using the respective of the following three methods:

    myAudioNode.play();

    myAudioNode.pause();

    myAudioNode.stop();

Note: Whether an Audio Node plays continuously or only once, depends on the Loop properties you have set above!

## Setting Environment Properties

Optionally, You can choose from the following environmental presets from `com.jme3.audio.Environment`. This presets influence subtle echo effects that evoke associations of different environments in your users. You use it together with setReverbEnbaled(true) mentioned above.

Environment | density | diffusion | gain | gainHf | decayTime | decayHf | reflGain | reflDelay | lateGain | lateDelay
--- | --- | --- | --- | --- | --- | --- | --- | --- | --- | ---
Garage | 1.00f | 1.0f | 1.0f | 1.00f | 0.90f | 0.5f | 0.751f | 0.0039f | 0.661f | 0.0137f
Dungeon | 0.75f | 1.0f | 1.0f | 0.75f | 1.60f | 1.0f | 0.950f | 0.0026f | 0.930f | 0.0103f
Cavern | 0.50f | 1.0f | 1.0f | 0.50f | 2.25f | 1.0f | 0.908f | 0.0103f | 0.930f | 0.0410f
AcousticLab | 0.50f | 1.0f | 1.0f | 1.00f | 0.28f | 1.0f | 0.870f | 0.0020f | 0.810f | 0.0080f
Closet | 1.00f | 1.0f | 1.0f | 1.00f | 0.15f | 1.0f | 0.600f | 0.0025f | 0.500f | 0.0006f

Activate the preset with setEnvironment(). E.g. in a dungeon environment:

    audioRenderer.setEnvironment(new Environment.Dungeon));

A sound engineer can create a custom `com.​jme3.​audio.Environment` object and specify custom environment factors. Activate your custom environment settings in the Environment constructor:

    audioRenderer.setEnvironment(
            new Environment( density, diffusion, gain, gainHf, decayTime, decayHf,
                    reflGain, reflDelay, lateGain, lateDelay ) );

You can find more info about OpenAL and its advanced features here: OpenAL 1.1 Specification

view online version