Game Development Community

dev|Pro Game Development Curriculum

OpenAL and Torque 2D

by Simon Love · 02/28/2006 (6:58 pm) · 3 comments

Well, after countless hours spent bleeding my eyes out over the forums, trying to find decent instruction about OpenAL sound panning, I have finally found the way!

Take Note that I use the 1.0.2 version of T2D, with the audiofunctions.cc from TGE 1.4. The directory structure is thus different from the Beta1 version of T2d.

Maybe nobody cares about panning anymore, as it is soooooooo 20th century, but I'm retro to the core, so there.

First of all, the engine needs a tiny tiny code change.

In the audiofunctions.cc file, more specifically in the ALlistener3f consolefunction, you should find something that looks like this

if(argc != 3 || argc != 5)
   {
      Con::errorf(ConsoleLogEntry::General, "alListener3f: wrong number of arguments");
      return;
   }

Simply change the above || to && (OR to AND). This fix prevents getting the message "wrong number of args", no matter how many args you conjure up.

Now that the engine is fixed, it will be easier to implement our little panning functions.

Basically, in the common\client\audio.cs file, you (or someone :)) have declared a listener, but only specified its volume by calling AL_GAIN_LINEAR. The following step is not necessary, as I guess it will default to 0,0,0, but let's just make sure, allright?

By calling

alListener3f("AL_POSITION", 0.0, 0.0,0.0);

you are telling the engine that the listener (you) is situated at coordinates 0,0,0 in the world.

Now, you need to simply play a sound, and save the handle of that sound somewhere. There are several ways of creating sounds, but let's stick to alxplay for now....So...

$Handle = alxplay(MightyTruckEngineSound);

This will store the handle of the sound in the global variable $Handle. Note that MightyTruckEngineSound must be a defined audio Profile in this example(Don't just put the filename here, it won't work).

Then, whenever you need to change the panning of your sound, you simply call

Alxsource3f($handle,"AL_POSITION","0 0 0");

If you change the first digit of that triplet, you will move the sound from left to right.
Since a value of 0 will be right in front of our listener, entering a negative number will pan to the left, a positive number will pan to the right.

Second digit is y (up and down), and third digit is z (depth).

On a final note, I'm guessing that putting the listener one Z unit away from the sound (basically pull the listener back towards your head) might help it pan smoothly. If the listener's Z position is 0 and the sound's Z position is also 0, when the sound pans across the center, the sound will jump from speaker to speaker in an abrupt manner. What we want is smooth, so having the sound pan in front of the listener might make the transition smoother.

Comments and suggestions are welcome, you guys think I should put this in TDN?

Thank to Chris Labombard for helpful comments :)

About the author

I am here to help. I've worked at every imaginable position in game development, having entered the field originally as an audio guy.


#1
03/15/2006 (9:12 am)
I was woundering about this very thing, thanks for pointing me in the right direction!!
#2
07/23/2006 (7:51 pm)
OK.. i tried to implement a panning function as you explained here... but the sound's panning does not change, when i call the function..
should it work with T2D 1.1.0 Pro?!
#3
10/31/2007 (6:34 am)
Anyone been able to get this to work with TGB 1.5.x?