[SOLVED] Positional Audio not falling off

Here is an example run to show positions of Camera and AudioNode:

Here is copy of console output:

Camera position: (0.0, -0.01, 600.0)
Effect position: (81.000015, -135.00002, 2.75)
Effect position: (81.000015, -135.00002, 2.75)
Camera position: (0.0, -100.01, 700.0)
Camera position: (0.0, -200.01, 800.0)
Camera position: (0.0, -300.00998, 900.0)
Camera position: (0.0, -400.01, 900.0)
Camera position: (0.0, -500.01, 900.0)
Camera position: (0.0, -600.01, 900.0)
Camera position: (0.0, -700.01, 900.0)
Camera position: (0.0, -800.01, 900.0)
Camera position: (0.0, -900.01, 900.0)
Camera position: (0.0, -1000.01, 900.0)
Camera position: (0.0, -1100.01, 900.0)
Camera position: (0.0, -1200.01, 900.0)
Effect position: (81.000015, -135.00002, 2.75)
Effect position: (81.000015, -135.00002, 2.75)
Camera position: (0.0, -1300.01, 900.0)
Camera position: (0.0, -1400.01, 900.0)
Camera position: (0.0, -1500.01, 900.0)
Camera position: (0.0, -1600.0099, 900.0)
Camera position: (0.0, -1700.0099, 900.0)
Camera position: (0.0, -1800.0099, 900.0)
Camera position: (0.0, -1900.0099, 900.0)
Camera position: (0.0, -2000.0099, 900.0)
Camera position: (0.0, -2100.01, 900.0)
Camera position: (0.0, -2200.01, 900.0)
Camera position: (0.0, -2300.01, 900.0)
Camera position: (0.0, -2400.01, 900.0)
Camera position: (0.0, -2500.01, 900.0)
Camera position: (0.0, -2600.01, 900.0)
Camera position: (0.0, -2700.01, 900.0)
Effect position: (81.000015, -135.00002, 2.75)
Effect position: (81.000015, -135.00002, 2.75)

I’m hearing the AudioNode always at the same volume despite using a mono audio file and these settings:

Here is how I create the AudioNodes, it’s attached to a node which represent a tile on the board:

private HashMap<String, AudioNode> soundEffects;

....

// Add sound effects
soundEffects = new HashMap<String, AudioNode>();
soundEffects.put("pickup", createSoundEffect(vm, "cardPlace1.wav"));
soundEffects.put("putdown", createSoundEffect(vm, "cardPlace2.wav"));
soundEffects.put("flip", createSoundEffect(vm, "cardPlace2.wav"));
soundEffects.put("turn", createSoundEffect(vm, "cardPlace2.wav"));
soundEffects.put("shuffle", createSoundEffect(vm, "cardPlace2.wav"));
soundEffects.put("add", createSoundEffect(vm, "cardPlace2.wav"));
soundEffects.put("delete", createSoundEffect(vm, "cardPlace2.wav"));

...

private AudioNode createSoundEffect(ViewModel vm, String filepath) {
    AudioNode sound = new AudioNode(vm.app.getAssetManager(), "Sound/Effects/" + filepath, DataType.Buffer);
    sound.setPositional(true);
    sound.setLooping(false);
    sound.setVolume(10);
    sound.setRefDistance(2f);
    this.attachChild(sound);
    return sound;
}

Here is how I play the AudioNode:

public void playSoundEffect(String effectName) {
    soundEffects.get(effectName).play();
    System.out.println("Effect position: " + soundEffects.get(effectName).getWorldTransform().getTranslation());
}

Here is how I update the Listener position & rotation:

// Set the locations to what was calculated
vm.app.getCamera().setLocation(cameraFake.getWorldTranslation());
vm.app.getCamera().lookAt(lookAtFake.getWorldTranslation(), Vector3f.UNIT_Z);
vm.app.getListener().setLocation(vm.app.getCamera().getLocation());
vm.app.getListener().setRotation(vm.app.getCamera().getRotation());
System.out.println("Camera position: " + vm.app.getListener().getLocation());

I’ve looked at many threads regarding positional audio, this seemed like the closest fit Question concerning positional audionode. Thanks everyone.

http://javadoc.jmonkeyengine.org/com/jme3/audio/AudioNode.html#setMaxDistance-float-

Read the actual javadoc for the method, don’t just call it… it’s probably important.

1 Like

That did the trick. I actually never called setMaxDistance() but after reading it it makes sense why it wasn’t attenuating. The default is 200 and in my world’s units the camera was always more distant than that. Thanks pspeed.

It’s hit everyone once. That’s why I was pretty sure it was the cause.

Glad it worked.

1 Like

All cases are easy to test with audio in my Editor :slight_smile:

3 Likes

Have to say that setMaxDistance is quite a misleading name cosidering what the value does

1 Like

Thus the giant javadoc text.

I think the name reflects what the underlying audio library calls it if I recall correctly.

Edit: it’s the fact that it doesn’t default to a really really huge number that always bugged me. If that were the case then most people would never even have to know about it.

1 Like

My biggest confusion with it was thinking of a use case where you would want to only attenuate the sound to a certain max distance and then hear it at full volume beyond that. I can understand if you are interpolating the volume level between two points you would want some max distance to use in that equation, hence setMaxDistance(). I don’t know why the sound wouldn’t be 0% volume beyond that point instead of 100%.

I used this as the large value as you mention pspeed.

sound.setMaxDistance(Float.MAX_VALUE);

Anyone see a downside in that?

Well kind of, considering you have only 64 audiochannels available it would be nice to cull far audio sources once they drop below lets say 0.5dB

1 Like

Yes, but this doesn’t happen anyway no matter what. You have to manage your own sounds.

I’m not sure what the point of max distance is though I can think of some use-cases where it would matter. My guess is that once you exceed max distance then it stops doing the attenuation calculation.

1 Like