Pausing and resuming positional audio

Guys,



I'm using the new sound system in my demo game and I'd apreciate some help with two issues:



1 - I can't get positional audio fx to stop.

I added a AudioTrack with a RangedAudioTracker, just like the TestJmexAudio example. If I try to disable and stop (or pause) tha AudioTrack

it doesn't have any effect. Is there a way I can stop playing a positional sample like this?



I have two samples for my car, one for idle engine and one for full throttle, and I want to play the idle one when not pressing the accel key, and the other one when pressing.



2 - Changing the pitch doesn't have any effect when the sample is already playing

I wanted to change the pitch a bit to simulate the effect of the acceleration, but I got in two problems: I can't change at all the pitch of positional audio, OK.

I tried to change with not positional audio and didn't heard anything different. The changes only work if done before playing the sample.



Any help would be much apreciated.



Erick Passos (Perick)

Regarding number 2:

It's working for me, with both ENVIRONMENT and HEADSPACE audio.



I have modified TestJmexAudio to change pitch, and it is working here, please try it out and let me know how it is working for you (the pitch of the female voice should ping-pong from 1.0 to 1.9):



public class TestJmexAudio extends SimpleGame {

    private AudioSystem audio;
    private ArrayList<RangedAudioTracker> trackers = new ArrayList<RangedAudioTracker>();

    /**
     * Entry point for the test,
     *
     * @param args
     */
    public static void main(String[] args) {
        TestJmexAudio app = new TestJmexAudio();
        app.setDialogBehaviour(ALWAYS_SHOW_PROPS_DIALOG);
        app.start();
    }

    @Override
    protected void simpleInitGame() {

        // setup a very simple scene
        Sphere emit1 = new Sphere("emit1", new Vector3f(0,0,0), 24, 16, 3);
        rootNode.attachChild(emit1);
        Box emit2 = new Box("emit2", new Vector3f(0,0,0), 1, 1, 1);
        emit2.setLocalTranslation(new Vector3f(10, 0, 0));
        rootNode.attachChild(emit2);
        SpatialTransformer st = new SpatialTransformer(1);
        st.setObject(emit2, 0, -1);
        st.setPosition(0, 0.0f, new Vector3f(8,-8,0));
        st.setPosition(0, 0.5f, new Vector3f(6,-4,-6));
        st.setPosition(0, 1.0f, new Vector3f(0,0,-8));
        st.setPosition(0, 1.5f, new Vector3f(-6,4,-6));
        st.setPosition(0, 2.0f, new Vector3f(-8,8,0));
        st.setPosition(0, 2.5f, new Vector3f(-6,4,6));
        st.setPosition(0, 3.0f, new Vector3f(0,0,8));
        st.setPosition(0, 3.5f, new Vector3f(6,-4,6));
        st.setPosition(0, 4.0f, new Vector3f(8,-8,0));
        st.interpolateMissing();
        st.setRepeatType(Controller.RT_WRAP);
        emit2.addController(st);
       
        // SOUND STUFF BELOW
       
        // grab a handle to our audio system.
        audio = AudioSystem.getSystem();
       
        // setup our ear tracker to track the camera's position and orientation.
        audio.getEar().trackOrientation(cam);
        audio.getEar().trackPosition(cam);
       
        // setup a music score for our demo
        AudioTrack music1 = getMusic(TestJmexAudio.class.getResource("/jmetest/data/sound/test.ogg"));
        audio.getMusicQueue().setRepeatType(RepeatType.ALL);
        audio.getMusicQueue().setCrossfadeinTime(2.5f);
        audio.getMusicQueue().setCrossfadeoutTime(2.5f);
        audio.getMusicQueue().addTrack(music1);
       // audio.getMusicQueue().play();

        // setup positional sounds in our scene
        sfx1 = getSFX(TestJmexAudio.class.getResource("/jmetest/data/sound/CHAR_CRE_1.ogg"));
        RangedAudioTracker track1 = new RangedAudioTracker(sfx1, 25, 30);
        track1.setToTrack(emit1);
        track1.setTrackIn3D(true);
        track1.setMaxVolume(0.35f);  // set volume on the tracker as it will control fade in, etc.
        trackers.add(track1);
       
        AudioTrack sfx2 = getSFX(TestJmexAudio.class.getResource("/jmetest/data/sound/Footsteps.wav"));
        RangedAudioTracker track2 = new RangedAudioTracker(sfx2, 25, 30);
        track2.setToTrack(emit2);
        track2.setTrackIn3D(true);
        track2.setMaxVolume(1.0f);
        //trackers.add(track2);
    }
   
    AudioTrack sfx1 = null;
   
    private AudioTrack getMusic(URL resource) {
        // Create a non-streaming, non-looping, relative sound clip.
        AudioTrack sound = AudioSystem.getSystem().createAudioTrack(resource, false);
        sound.setType(TrackType.MUSIC);
        sound.setRelative(true);
        sound.setTargetVolume(0.7f);
        sound.setLooping(false);
        return sound;
    }

    private AudioTrack getSFX(URL resource) {
        // Create a non-streaming, looping, positional sound clip.
        AudioTrack sound = AudioSystem.getSystem().createAudioTrack(resource, false);
        sound.setType(TrackType.ENVIRONMENT);
        sound.setRelative(false);
        sound.setLooping(true);
        return sound;
    }
   
    float pitch = 1.0f;
    float pitchAdder = 1f;
   
    @Override
    protected void simpleUpdate() {
        // update our audio system here:
        audio.update();
       
        pitch = pitch + pitchAdder * tpf;
        if (pitch > 1.9f || pitch < 1.0f) {
           pitchAdder =- pitchAdder;
        }
        sfx1.setPitch(pitch);
       
        for (int x = trackers.size(); --x >= 0; ) {
            RangedAudioTracker t = trackers .get(x);
            t.checkTrackAudible(cam.getLocation());
        }
    }

    @Override
    protected void cleanup() {
        audio.cleanup();
    }
}

Thanks for the help Tobias.



It really works. The problem was a bit stupid of my part, but I'll mention just for the records.



I was updating the pitch with an expression like this:



setPitch(1+carSpeed/maxSpeed);



So I would obtain 2 as pitch when reaching the maxSpeed. The problem was that carSpeed was an int primitive type, and the compiler was considering the division an integer one, so it was always returning 0;

To fix, I did this:



setPitch( 1 + ((float) carSpeed)/maxSpeed );



And it worked. Thanks anyway, I tested your code and it worked as well.



I gave up using the positional audio, because I need pitch changing more. So I'll stick with my current implementation with discrete samples for each gear, based on the car speed, and altering the pitch inside each gear speed range.



Erick Passos

Glad it helped, but it turns out I am the stupid one.



Check here and be happy again: http://www.jmonkeyengine.com/jmeforum/index.php?topic=4593.msg39938#msg39938