Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

OpenAL setting sound position

Started by vNeeki Feb 2, 2012 at 5:50 AM 4 replies 9.7k views
Original Post
vNeeki
vNeeki
Hello.

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

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!
Thanks for your attention.
Ashaman73
Ashaman73

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;
vNeeki
vNeeki

[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 ?
vNeeki
vNeeki
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.
BeerNutts
BeerNutts
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!
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
vNeeki
vNeeki

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

sad.png

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.