Jump to content
  • Advertisement
Sign in to follow this  
BloodLust666

OpenAL distance fade

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

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!