TGB audio positioning / panning
by Stefan *Shaderman* Greven · 02/12/2009 (7:05 am) · 5 comments
I wondered if it was possible to have audio positioning in TGB 1.7.4 and started searching the forums. I found two threads where people asked the same question and finally Simon Love's promising resource OpenAL and Torque 2D. It didn't work though :/
After further searching and reading, I found out that the key thing is the audio format. I read that audio files need to be mono to get positioning working with OpenAL - great, but still didn't work!
I want to keep it short and thus won't tell you how I started learning about OpenAL, hacked and debugged the engine and so on... :)
The solution is simple: The audio files need to be mono, 8 bit and 44KHz.
Here are some steps to position the firing sound of the player ship in the BehaviorShooter demo:
I've used the file zap1.wav and saved it as mono, 8 bitand 44KHz. You can get the modified version here. Place that file into the game/data/audio folder.
Then edit the file game/behaviors/game/shootsAdv.cs and add this code at the end of it:
Now add these two lines on top of ShootsBehaviorAdv::onBehaviorAdd()
and modify the bottom of ShootsBehaviorAdv::fire():
To keep it simple, I made all changes in the file shootsAdv.cs, this is not an optimal solution!
That's it. I hope this will be helpful for somebody else...
I don't have a MAC and would be glad if someone could tell me if it works on a MAC or not.
Stefan
After further searching and reading, I found out that the key thing is the audio format. I read that audio files need to be mono to get positioning working with OpenAL - great, but still didn't work!
I want to keep it short and thus won't tell you how I started learning about OpenAL, hacked and debugged the engine and so on... :)
The solution is simple: The audio files need to be mono, 8 bit and 44KHz.
Here are some steps to position the firing sound of the player ship in the BehaviorShooter demo:
I've used the file zap1.wav and saved it as mono, 8 bitand 44KHz. You can get the modified version here. Place that file into the game/data/audio folder.
Then edit the file game/behaviors/game/shootsAdv.cs and add this code at the end of it:
$audioPanningFactor = 0.25; // 1.0 = max, 0.0 = panning off
new AudioDescription(Audio3DNonLooping)
{
volume = 1.0;
isLooping= false;
[b]is3D = true;[/b] // <-- important!
};
new AudioProfile(Shot)
{
filename = "~/data/audio/zap1.wav";
description = "Audio3DNonLooping";
preload = true;
};Now add these two lines on top of ShootsBehaviorAdv::onBehaviorAdd()
%width = sceneWindow2D.getCurrentCameraSize(); $cameraWidth = getWord(%width, 0) / 2;
and modify the bottom of ShootsBehaviorAdv::fire():
%projectile.setPosition(%this.owner.position);
%projectile.setRotation(%this.owner.rotation + %this.rotationOffset);
%projectile.setLinearVelocityPolar(%this.owner.rotation + %this.fireAngleOffset, %this.projectileSpeed);
[b]
%audioPosX = (1 / $cameraWidth * %shipX);
alxPlay(Shot, %audioPosX * $audioPanningFactor, 0, 1.0);
[/b]
if (!isEventPending(%this.fireSchedule))
%this.fireSchedule = %this.schedule(%this.fireRate * 1000, "fire", 1);To keep it simple, I made all changes in the file shootsAdv.cs, this is not an optimal solution!
That's it. I hope this will be helpful for somebody else...
I don't have a MAC and would be glad if someone could tell me if it works on a MAC or not.
Stefan
#2
All you need is to set up an AudioProfile and a AudioDescription with is3D set true (see the examples above). You can then play a sound with alxPlay(NameOfTheAudioProfile, posX, posY, posZ) where
NameOfTheAudioProfile is the name of the specified AudioProfile
posX is the horizontal position. Values between -1.0 (left) and 1.0 (right) should be used.
posY should be set to 0
posZ should be set something between 0.5 and 1 to get a smooth transition.
02/12/2009 (11:47 am)
Quote:does this require modifying the TGB source code?No.
All you need is to set up an AudioProfile and a AudioDescription with is3D set true (see the examples above). You can then play a sound with alxPlay(NameOfTheAudioProfile, posX, posY, posZ) where
NameOfTheAudioProfile is the name of the specified AudioProfile
posX is the horizontal position. Values between -1.0 (left) and 1.0 (right) should be used.
posY should be set to 0
posZ should be set something between 0.5 and 1 to get a smooth transition.
#3
02/12/2009 (6:37 pm)
You know, I was *just* thinking about this sort of thing. Very interesting. I might have to test this out with OGG format.
#4
02/12/2009 (7:53 pm)
I've just converted the file from above to OGG (mono) and it worked.
#5
05/18/2009 (9:01 pm)
i think i actually asked about this in the TGB Proivate forums, thank you for fiding out the answer to that... really appreciate it.
Torque Owner Samuel Cartwright
Interesting post. Just one question - does this require modifying the TGB source code?