# 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 loads the whole file into memory:

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

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

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

## Setting AudioNode Properties

AudioNode method | Usage
--- | ---
getStatus() | Returns either Status.Playing, Status.Stopped, or Status.Paused.
setVolume(5) | Set the volume between 0 (mute) and 10 (loud).
setPitch(1) | Make the sound play higher or lower.
setLooping(false) | Play it once and stop.
setLooping(true) | Play it over and over again from the beginning, until stopSource() or pauseSource() is called. E.g. for background noises.
setPositional(true) setLocalTranslation() | Activates 3D audio, the sound appears to come from a certain position. Good for Players or NPCs.
setReverbEnabled(true) | A 3D echo effect that only makes sense to use with moving positional AudioNodes.
setDirectional(true) setDirection() | Activates 3D audio, sound can only be heard from a certain direction. Good for noises that should not be heard through a wall.
setInnerAngle() setOuterAngle() | ?
setPositional(false) setDirectional(false) | The sound comes from a everywhere. Good for environmental background sounds and music.
setLocalTranslation() | Position the AudioNode in the 3D scene if you have set setPositional() to true.
setTimeOffset(0.5f) | Start playing after a pause?
setMaxDistance(100f) | Maximum distance the sound can be heard, in world units.

## Playing Audio Nodes

For playing you use the `com.jme3.audio.AudioRenderer`. A default `audioRenderer` object is provided by the com.jme3.app.Application class.

    audioRenderer.playSource( myAudioNode );

Note: Whether the AudioNode plays continuously or only once, depends on the Loop properties you have set.

You pause and stop music with the following methods:

    audioRenderer.pauseSource( myAudioNode );

    audioRenderer.stopSource( myAudioNode );

## Setting Environment Properties

You can choose from the following environmental presets from `com.jme3.audio.Environment`.

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:

    audioRenderer.setEnvironment(new Environment.Dungeon));

Alternatively you can create a `com.​jme3.​audio.Environment` object to 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 ) );

view online version