# OpenAL setting sound position

This topic is 2813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello.

I've been trying to set the sound position(2d space) of an openal sound source without much luck!

This is how im setting the listener's vector :

 //Every frame const Vector2& v = mgr->GetEntity("player")->GetPosition(); alListener3f(AL_POSITION,v.x,v.y,0.0f); 

And when adding a sample to the quee im playing it like this :

 Vector2 sndPos(listener - sound); //sound : offset of sound in 2d space , listener : player's position in 2d space sndPos.x /= 100.0f; sndPos.y /= 100.0f; alSource3f(source,AL_POSITION,sndPos.x,sndPos.y,0.0f); alSourcePlay(source); 

But i can't hear any sound unless i comment out this line:
 alSource3f(source,AL_POSITION,sndPos.x,sndPos.y,0.0f); 

Which is way too frustrating so i would love to get some help on the matter!

##### Share on other sites

And when adding a sample to the quee im playing it like this :

Vector2 sndPos(listener - sound); //sound : offset of sound in 2d space , listener : player's position in 2d space
sndPos.x /= 100.0f;
sndPos.y /= 100.0f;

alSource3f(source,AL_POSITION,sndPos.x,sndPos.y,0.0f);
alSourcePlay(source);

Hmmm. isn't sound the offset relative to the player ? Then you need to calculate your position like this:
 Vector2 sndPos(listener); //sound : offset of sound in 2d space , listener : player's position in 2d space sndPos.x += sound.x/100.0f; sndPos.y += sound.y/100.0f; 

##### Share on other sites

[quote name='vNeeki' timestamp='1328161831' post='4908626']
And when adding a sample to the quee im playing it like this :

Vector2 sndPos(listener - sound); //sound : offset of sound in 2d space , listener : player's position in 2d space
sndPos.x /= 100.0f;
sndPos.y /= 100.0f;

alSource3f(source,AL_POSITION,sndPos.x,sndPos.y,0.0f);
alSourcePlay(source);

Hmmm. isn't sound the offset relative to the player ? Then you need to calculate your position like this:
 Vector2 sndPos(listener); //sound : offset of sound in 2d space , listener : player's position in 2d space sndPos.x += sound.x/100.0f; sndPos.y += sound.y/100.0f; 
[/quote]

Thanks , now i can hear the sound but its still not sounding correctly.
Any ideas ?

##### Share on other sites
I think that the problem lies in attenuation level.Is there any good way to calculate attenuation level based on distance between the listener and target position clamped to 0.0..1.0 range?

Edit : Wrote quickly a function that does that(works 100%) but still it seems that the positioning is not correct.

##### Share on other sites
FWIW, I did this using Sfml's Sound (which sits on top of OpenAL), so it's probably alot like OpenAl. Here are my functions with respect to spatial sounds on a 2d top-down game:

This sets the player's location for the sound system
 BOOL Quake2dSoundListnerSet(cpVect ListenerLocation, cpFloat Angle) { sf::Listener::SetPosition(ListenerLocation.x, -3.0f, ListenerLocation.y); sf::Listener::SetDirection(cos(Angle)*100.0f, 100.0f, sin(Angle)*100.0f); return TRUE; } 

This is the part that plays the sound
 BOOL Quake2dSoundPlay(etQuake2dSound eSound, cpVect SoundLocation, U32 u32RepeatId) { sf::Sound *pSoundInst; . . . pSoundInst->SetPosition(SoundLocation.x, 0.0f, SoundLocation.y); pSoundInst->SetMinDistance(gMinDistance); pSoundInst->SetAttenuation(5.5f); pSoundInst->Play(); } 

So, I'm basically saying "Here's my guy, and he's at these coordinates (world coords) and facing this direction." And then, when a sound plays, I say, "Something is making a sound, and it's at these coordinates (world coords)." SFML (Open AL) handles the rest.

BTW, This link helped me alot: http://blog.tbam.com.ar/2009/05/sound-spatiaiization-for-2d-games-in.html

GL!

##### Share on other sites

FWIW, I did this using Sfml's Sound (which sits on top of OpenAL), so it's probably alot like OpenAl. Here are my functions with respect to spatial sounds on a 2d top-down game:

This sets the player's location for the sound system
 BOOL Quake2dSoundListnerSet(cpVect ListenerLocation, cpFloat Angle) { sf::Listener::SetPosition(ListenerLocation.x, -3.0f, ListenerLocation.y); sf::Listener::SetDirection(cos(Angle)*100.0f, 100.0f, sin(Angle)*100.0f); return TRUE; } 

This is the part that plays the sound
 BOOL Quake2dSoundPlay(etQuake2dSound eSound, cpVect SoundLocation, U32 u32RepeatId) { sf::Sound *pSoundInst; . . . pSoundInst->SetPosition(SoundLocation.x, 0.0f, SoundLocation.y); pSoundInst->SetMinDistance(gMinDistance); pSoundInst->SetAttenuation(5.5f); pSoundInst->Play(); } 

So, I'm basically saying "Here's my guy, and he's at these coordinates (world coords) and facing this direction." And then, when a sound plays, I say, "Something is making a sound, and it's at these coordinates (world coords)." SFML (Open AL) handles the rest.

BTW, This link helped me alot: http://blog.tbam.com...d-games-in.html

GL!

That was useful even if didn't really gave the results that i'm after.I'm afraid that i will implement this procedurally , ie :
 float dist = soundPos.DistanceFrom(listener); if (dst <= 100) SetGain(..),SetAttn(..) else if (dst <= 200) SetGain(..),SetAttn(..) And so on 

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 11
• 23
• 39
• 75