Sign in to follow this  
BloodLust666

OpenAL distance fade

Recommended Posts

So I've been using OpenAL for a while now, but for very simple stuff, like JUST playing sounds, never really implemented if the source is farther away from the listener than it should be quieter. So I have my camera's position and velocity being the listener's position and velocity and each source follows an object which too has position and velocity. This I THOUGHT would be the answer but no luck... I read on OpenAL.org that it doesn't implement the distance factors and it has to be set. So, my question, being that I didn't really understand because there was no example, just definitions of each functions, could someone show me quick, small function that sets the gain appropriate to the distance and velocity from the camera/listener? (I never really ever got into the physics of sounds, just objects hah maybe I need to study up on it)

Share this post


Link to post
Share on other sites
You also need to set the source's distances.
// The distance that the source will be the loudest (if the listener is
// closer, it won't be any louder than if they were at this distance)
alSourcei(source, AL_REFERENCE_DISTANCE, 1.0);
// The distance that the source will be the quietest (if the listener is
// farther, it won't be any quieter than if they were at this distance)
alSourcei(source, AL_MAX_DISTANCE, FLT_MAX);

The given values in that example are the defaults. As the listener moves between the reference and max distances from the source, the perceived volume will increase and decrease accordingly. And being that the max distance defaults to FLT_MAX, that's really far so it won't seem to quiet much as it moves away. Setting more reasonable max distances for the sources will make the distance fading more apparent (exactly what values are reasonable depends on your unit scale and how far away you'd prefer the sounds to be heard).

Share this post


Link to post
Share on other sites
The distances, not the volumes (the default volumes are generally fine). Unless you set the rolloff factor to 0 (default is 1), set the distance model to none (default is inverse distance clamped), set the reference distance to > max distance, or something else that would cause the attenuation math to fail (which you have to be pretty explicit about to do, usually). Just don't set the reference distance to 0.

Share this post


Link to post
Share on other sites
ok, what's the reference distance? is that the max volume when the listener is close to the source? which makes sense if setting the reference distance is 0 because that means 0 is the loudest.

Share this post


Link to post
Share on other sites
The reference distance is typically the distance where the source will be loudest. So if you, say, set the reference distance to 2 and the max gain to 0.8 for the source, if the listener is 2 units or closer to the source, the playback volume for the source will be at 0.8.

The problem with setting the reference distance to 0 is because of the inverse distance math. The math for getting the attenuation is:
flAttenuation = MinDist / (MinDist + (Rolloff * (Distance - MinDist)));

(where Rolloff is the rolloff factor, which can be used to strengthen or weaken the distance attentuation, and MinDist is the reference distance)

If the reference distance was 0, that would cause the math to be 0/x, which would always be 0 (or worse, if the distance was also 0, you'd get 0/0). A reference distance of 0 makes mathmatically no sense because, as in real-life, you can't be listening directly at the point where a sound eminates from. Setting the reference distance to something >0 and clamping solves this issue.

Share this post


Link to post
Share on other sites
I don't think it's working... I'm setting the reference to 1.0f, the max distance to 1000 but the sound is still the same gain where ever the camera moves.

Share this post


Link to post
Share on other sites
Are you perhaps setting AL_SOURCE_RELATIVE? What kind of scale does your gameworld have (ie. are you actually moving things out to near 1000 units, or keeping them within 10 or 100)? Are you changing the alDistanceModel? Also, stereo sounds do not attenuate. Only mono sounds.

If you're still having problems it might be helpful to see some code.

Share this post


Link to post
Share on other sites
objects range from 100 to 10,000, i have a pretty big environment for this particular simulation.

here's my code, this code spreads out through the entire program but i have sequentially here



// al init
m_SoundDevice = alcOpenDevice( NULL );
ALboolean b = alutInitWithoutContext(NULL, NULL);
Listener->m_SoundContext = alcCreateContext(m_SoundDevice, NULL);
alcMakeContextCurrent(Listener->m_SoundContext);

// buffer
alGenBuffers(1, &m_Buffer);
alutLoadWAVFile((ALbyte *)Filename.c_str(), &format, &data, &size, &freq, &loop);
alBufferData(m_Buffer, format, data, size, freq);
alutUnloadWAV(format, data, size, freq);

// source
alGenSources(1, &m_Source);
alSourcei(m_Source, AL_BUFFER, pWAV->m_Buffer);
alSourcei(m_Source, AL_REFERENCE_DISTANCE, m_maxGain); // 1.0f
alSourcei(m_Source, AL_MAX_DISTANCE, m_maxDistance); // 1000.0f

// play it
alListener3f(AL_POSITION, LPos.X, LPos.Y, LPos.Z);
alListener3f(AL_VELOCITY,
LPos.X - pListener->m_LastPos.X,
LPos.Y - pListener->m_LastPos.Y,
LPos.Z - pListener->m_LastPos.Z);

alSource3f(m_Source, AL_POSITION,
SPos.X, SPos.Y, SPos.Z);
alSource3f(m_Source, AL_VELOCITY,
SVel.X, SVel.Y, SVel.Z);
alSourcePlay(m_Source);



all the positions and everything are correct, and i never get any errors. those the only AL functions I'm calling.

Share this post


Link to post
Share on other sites
Double-check the format of the sounds. They should be AL_FORMAT_MONO8 (0x1100) or AL_FORMAT_MONO16 (0x1101), otherwise they won't attenuate. If that's properly set, then what happens if you change the AL_MAX_GAIN source property to 0.5?

Also,
alSourcei(m_Source, AL_REFERENCE_DISTANCE, m_maxGain); // 1.0f

infering what I can from this line, it isn't completely correct. The reference/minimum distance isn't dependant on the source's max gain. Changing the reference distance between 0 and 1 won't do much at this scale (except for 0, where it disables attenuation).

Share this post


Link to post
Share on other sites
what's it when the freq is 0x1103 ?

I thought, from what i understood in the previous posts, that the reference distance is the max gain when the listener and source are very very close.

Share this post


Link to post
Share on other sites
0x1103 is AL_FORMAT_STEREO16, which explains why it's not fading with distance (though I don't know why the freq is set to that; make sure you're using the right order). You'll have to use your favorite wave editor to make the sounds you want 3d-ized to be mono instead of stereo. Background music and local-only sounds (those which never 'move' away from the listener) can stay stereo, though.

The reference distance is the distance at which a source's volume will typically be at the loudest. If the source and listener are 'reference distance' units apart, or closer, the source will play at its set gain as determined by the source's AL_GAIN setting.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this