Tutorial 03 : FMOD Ex & OpenGL

Tutorial Download Section

This tutorial is an introduction to FMOD Ex and 3D sound.

Some musics are placed in a 3D environment. All OpenGl related things are based on OpenGl Tutorial 26 and will not be explained here.
The music that reach your ears depends on your position in the '3D World'.
 

The first thing to do is to initialize FMOD Ex.

The camera used for moving in the scene is right handed (ie +X as left, +Y as up and +Z as forwards). By default, FMOD Ex is left handed (ie +X = right, +Y = up, +Z = forwards).
To use a right handed system, we need to specify this to FMOD Ex. For this, in the initialization of our FMOD Ex system, we use FMOD_INIT_3D_RIGHTHANDED flag.

The distance unit is the meter, this is specified with DISTANCE_FACTOR (1 for meter, 3.28 for feet ...).
 

Initialization

    /*
     * Camera is right handed, so specify it to FMOD Ex here.
     * FMOD will treat +X as left, +Y as up and +Z as forwards.
     */

    result = system.init(12, FMOD_INIT_3D_RIGHTHANDED, null);
    ERRCHECK(result);
   
    /*
     * Set the distance units. (meters/feet etc).
     */

    result = system.set3DSettings(1.0f, 1.0f, 1.0f);
    ERRCHECK(result);

To create a 3D sound, use the FMOD_3D flag when creation using createSound/createStream.

Remark : If you want to use musics from a jar file, you have to load them into the memory.
This is done here using a ByteBuffer in which the file is copied in, an FMOD_CREATESOUNDEXINFO object specify the length (in bytes) of the file and finally the FMOD_OPENMEMORY flag.
 

Creating 3D sound

    //Load music into the memory
    ByteBuffer datas = loadFileIntoMemory("/Data/Music/fire.wav");
    FMOD_CREATESOUNDEXINFO exinfo = FMOD_CREATESOUNDEXINFO.create();
    exinfo.setLength(datas.capacity());
   
    //Create a 3D Sound
    result = system.createSound(datas, FMOD_HARDWARE | FMOD_3D | FMOD_OPENMEMORY, exinfo, sound1);
    ERRCHECK(result);

Now, sounds are created. You can play and position them everywhere you want.
Sound are start paused to modify the sound position without it being audible.
 

Positionning sounds

    FMOD_VECTOR position = FMOD_VECTOR.create(20.0f * DISTANCE_FACTOR, 0.0f, 0.0f);
    FMOD_VECTOR velocity = FMOD_VECTOR.create(0.0f, 0.0f, 0.0f);

    result = system.playSound(FMOD_CHANNEL_FREE, sound1, true, channel1);
    ERRCHECK(result);
    result = channel1.set3DAttributes(position, velocity);
    ERRCHECK(result);
    result = channel1.setPaused(false);
    ERRCHECK(result);

 

When the player move, you should have to update the listener attributes (position, orientation and velocity).
This should be done at each frame.

Here we update the listener position, orientation and velocity (deplacement speed).
The velocity is calculated in meter per second : velocity = (currentPosition-lastPosition)/time
 

Update Listener(s) attributes

private FMOD_VECTOR forward, up, vel, listenerpos, lastpos;

private void updateFmodEx()
{
    // ==========================================================================================
    // UPDATE THE LISTENER
    // ==========================================================================================


    listenerpos = camera.getPosition();

    // ********* NOTE ******* READ NEXT COMMENT!!!!!
    // velocity = how far we moved last FRAME (m/f), then time compensate it to SECONDS (m/s).

    velocity.setX( (listenerPos.getX()-lastPos.getX()) / INTERFACE_UPDATETIME );
    velocity.setY( (listenerPos.getY()-lastPos.getY()) / INTERFACE_UPDATETIME );
    velocity.setZ( (listenerPos.getZ()-lastPos.getZ()) / INTERFACE_UPDATETIME );

    FMOD_VECTOR forward = camera.getForward();
    FMOD_VECTOR up      = camera.getUp();

    //Update listener attributes
    result = system.set3DListenerAttributes(0, listenerPos, velocity, forward, up);
    ERRCHECK(result);

    //Update FMOD Ex system
    system.update();

    //Store pos for next time
    lastPos.release();
    lastPos = listenerPos;
}

 

 

Previous Project

Back

Last modified on 01/07/2010
Copyright © 2004-2010 Jérôme JOUVIE - All rights reserved. http://jerome.jouvie.free.fr/