3D Sound in JME3 (6 posts)

Topic tags: 3D, sound
  • Profile picture of gradpole gradpole said 10 months ago:

    I’m having a problem with the Hello Audio and getting the 3D sound aspect of it to work. Perhaps I have just misunderstood the tutorial but here is what I am trying to do: I’d like to “shoot” a point onto a structure, find the collision point, and place a 3D sound node at that point that gets louder the closer the camera gets to it and softer the farther the camera gets from it. I attached the code of what I have so far and basically it is cut-and-paste from the two tutorials but for some reason the sound does not wane/grow louder the way I want it to. Not sure if I’m missing something or what but I’d truly appreciate any help on this matter. Wasn’t sure if it might require some special hardware / sound card to get the audio to do this. Thanks for any help people can provide on this! Even code snippets to try out would be a great plus! Thanks!

            CollisionResults results = new CollisionResults();
            Ray ray = new Ray(cam.getLocation(), cam.getDirection());
            wall_structure.collideWith(ray, results);
            for (int i = 0; i < results.size(); i++) {
              // For each hit, we know distance, impact point, name of geometry.
              float dist = results.getCollision(i).getDistance();
              Vector3f pt = results.getCollision(i).getContactPoint();
            }
            if (results.size() > 0){
              // The closest collision point is what was truly hit:
              CollisionResult closest = results.getClosestCollision();
              mark.setLocalTranslation(closest.getContactPoint());
              
            
            audio_mark = new AudioNode(assetManager, "Sounds/hello.ogg",true);
            audio_mark.setPositional(true);
            audio_mark.setMaxDistance(4);
            audio_mark.setLocalTranslation(closest.getContactPoint());
            audio_mark.setVolume(2);
            placed_audio = true;
            audioRenderer.playSource(audio_mark);
    
    .......
    
    @Override
      public void simpleUpdate(float tpf) {
        listener.setLocation(cam.getLocation());
        listener.setRotation(cam.getRotation());
        if(placed_audio){
        float d0 = (audio_nature.getLocalTranslation().subtract(cam.getLocation())).length();
        System.out.println(d0);
        }
      }
  • Profile picture of pspeed pspeed821p said 10 months ago:

    Two things:
    1) you must be running the nightly builds as position audio was very much broken in alpha 4
    2) setMaxDistance() does not do what one would expect just by the reading of its name.

    The lastest javadocs have a much better description of this:http://jmonkeyengine.org/javadoc/com/jme3/audio/AudioNode.html#setMaxDistance(float)

    setRefDistance is what you use to control how fast the audio falls off.

    I updated the javadocs when I fixed positional audio some months back.

  • Profile picture of gradpole gradpole said 10 months ago:

    Ahh I didn’t even see the setRefDistance in the JavaDocs! Sorry about that and I’m going to give the JME_2011-07-22 a try. I replaced setMaxDistance with setRefDistance but no change in the effect. Could it perhaps be a soundcard issue if the build turns up to not be the problem? Are there any specific requirements for getting the 3D audio effect? If it helps, I’m running this on a Macbook Pro from around 2008 with Mac OS X 10.5.8. Thanks!

  • Profile picture of pspeed pspeed821p said 10 months ago:

    The only other things I can think of… positional audio must be mono sound sources.

    The bug I fixed (among others) was the position was always relative to the listener. So no matter how much you walked around the sound would always be coming from the same place relative to the camera. If you were running straight alpha 4 then this was definitely a problem for you. Whether it’s the _only_ problem remains to be seen. :)

  • Profile picture of gradpole gradpole said 10 months ago:

    Worked like a charm … I believe the ogg file I had wasn’t mono as I put it back into the nightly build and it didn’t work so straight alpha 4 might have been working before … but at least I have auto-updating on now :) Thanks again for your help on this! I put another post in earlier on a separate forum about a good Java library that interfaces well with jME3 for the Wii controller for Mac OS X 10.5.8+ … wouldn’t happen to have any personal insights on the matter would you?

  • Profile picture of pspeed pspeed821p said 10 months ago:

    Positional audio was definitely broken before… I racked my brain trying to get it working in Mythruna for a while before I fixed it. :)

    re: Wii controllers… I don’t know anything about that, sorry.