Implementing Positional Sound

I am trying to put 3Dish audio in a game I'm developing, and I'm unsure how to do positional sound. I want to be able to adjust the volume of the audio's channels depending on how far it is and the orientation.

• Are you fine with/are you currently using any middleware for sound? What sound API are you currently using? – Tetrad Apr 25 '11 at 15:59
• I'm using Flash. All I have control over is the volume of the left and right channels. – Ryan S Apr 25 '11 at 16:19

Since you didn't mention any API for sound I'll give you the general answer.

First get the distance between the audio listener and the audio source
d(L,S) = sqrt((x2-x1)2 + (y2-y1)2 + (z2-z1)2)

Then you define a distance where you don't want it to be audible anymore, lets say 150m

If your distance is 40 meters from the source, then the volume is about 73%.

To give you an equation that would be:
1-(d(L,S)/maxDistance)
or (1-(40/150)) = about .73
That will be the percentage of volume from 1 to 100%

That's assuming you want the volume to change linearly with your position change.

• Just realized you want this multichannel. To do that, you can use what I've said to get the main volume, then for the individual volume, you can take the rotation that would be looking straight at the audio source, then subtract that from your actual look rotation. You can then take the Y value of this resulting vector and say -90 degrees is completely in the right channel, 90 degrees is completely in the left channel. I cant answer you in the case of flash so this answer is probably useless if you don't understand vectors well. – brandon Apr 25 '11 at 16:37
• You'd need to split that volume appropriately for the left and right channels depending on the angle between the listener and the source too. – Ray Dey Apr 25 '11 at 16:41
• Lol beat me to it :P – Ray Dey Apr 25 '11 at 16:41
• Other than linear, what options do I have? What is the relationship between volume and distance in real life? Also, I just realized that something will sound the same from in front of you as behind you. What do games do to distinguish this? – Ryan S Apr 25 '11 at 17:48
• It's known as sound attenuation. There are (fairly advanced) equations for it. In a simple case you can use linear falloff (as described in the answer), exponential or exponential squared falloff (1/d or 1/d^2, respectively. – Ray Dey Apr 25 '11 at 21:46