Sign in to follow this  
P0jahn

Distance between two entities decide volume of a sound.

Recommended Posts

P0jahn    307

I need help with an algorithm for my 2D Game.

 

Say entity AA is emitting a sound. The strength of the emitting sound is determined by the distance between AA and BB(this could be the main character).

The further away they are, the weaker the sound is. The max distance(pass this distance and the sound volume is 0) is of course customizable.

The volume setting can be from 0.0 to 1.0.

 

Where and when to do these checks is not a problem. I know my engine and I can resolve that. How to check it is the problem.

Share this post


Link to post
Share on other sites
P0jahn    307

If you are talking to someone that is far away from you, you wont hear anything. As you move closer to him/her, the sounds becomes clearer and stronger.

I want to implement a logic like this in my game.

Share this post


Link to post
Share on other sites
BeerNutts    4400

Some audio API's support this by default using spatial clues.  For example, sfml provides a listener class which denotes where the "listener" is (basically, where a player is in the world, and where it's facing so surround sound will work as well).  And the sound class provides a method for setting the position of the sound when it is played.

 

These combined causes things that happen far away to either be not heard, or dimly heard automatically, as well as provides the correct orientation (so if the player is facing North, and a sound comes from the East, only the right side would play the sound).

 

FWIW, sfml's Audio class is built upon OpenAL, so I would suggest either using SFML or using openAL to provide these functions for you.

 

Good luck!

Edited by BeerNutts

Share this post


Link to post
Share on other sites
Yourself    1961

In case you have a library that doesn't have this feature, it is fairly trivial.

here is a snippet (linear fallof) from a sound library that I have writen some time ago.

// mRollofFactor defaults to 1
// mReferenceDistance defaults to 1
// mMaxDistance is relative to world size
float DSND::Listener::GetVolume( const Vec3f& pos )
{
	float distance = (mPosition - pos).Length();
	distance = MIN(mMaxDistance,distance);

	float gain = 1.f - mRollofFactor * (distance - mReferenceDistance) / (mMaxDistance - mReferenceDistance);
	return MAX(0.f, MIN(1.f, gain));
}

 

 

Share this post


Link to post
Share on other sites
alvaro    21246

volume = max_volume - sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));

That's a pretty bad answer...

 

You want to decide on a falloff function. Physics dictates that the sound volume should be proportional to 1/distance^2. If you want to cut it off completely at some point, you should probably get a smooth function that roughly looks like 1/distance^2 but reaches 0 at the desired distance and doesn't have a discontinuity. For instance

 

volume = reference_volume * max(1/distance_squared - 1/max_distance_squared, 0);

Share this post


Link to post
Share on other sites
Ludus    1020

As has been mentioned before, some API's include this functionality. The SDL_mixer library for SDL has a few functions that simulate audio drop off due to distance. It also has a function that not only does that, but includes panning effects calculated from the angle between the audio source and the listener.

 

It would be wise to check your API's documentation to see if it has similar functionality.

Share this post


Link to post
Share on other sites
P0jahn    307

Thanks for the info. I noticed that I cant use any of the code in this thread because once the sound is playing, you cant change the volume(you have to stop it first).

So I am looking for some APIs that have this implemented.

 

Is this feature called binding Listener to Camera?

Share this post


Link to post
Share on other sites
cr88192    1570


volume = max_volume - sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));

That's a pretty bad answer...
 
You want to decide on a falloff function. Physics dictates that the sound volume should be proportional to 1/distance^2. If you want to cut it off completely at some point, you should probably get a smooth function that roughly looks like 1/distance^2 but reaches 0 at the desired distance and doesn't have a discontinuity. For instance
 
 
volume = reference_volume * max(1/distance_squared - 1/max_distance_squared, 0);


I think it depends some on what a person wants from the sound-source.

for example, the falloff can be used to various effects for different kinds of things, like say, for the sound of a general area (such as a crowd of people or a large room), you might want square-root falloff, but for the ambient noises of a chirping console, you might want squared falloff, and for a character making noise (speaking, yelling, ...), a linear falloff might be better.


for example, in my mixer, there are multiple levels of attenuation:
none, same volume everywhere in the world;
low, square-root falloff (sqrt(volume/dist));
default, linear falloff (volume/dist);
high, square falloff (volume/dist^2).

note that the minimum value for distance is 1.


the sound is skipped if the calculated volume falls below 0.001, and there is also some math to handle things like panning and Doppler shifting and similar (basically, tweaking playback speeds based on relative velocities).

generally, some of these calculations also take relative camera orientation into effect as well, such as things may be slightly louder in-front than above or behind, and things generally above/below will be closer to center-pan than ones at roughly the same height.

Share this post


Link to post
Share on other sites
Ludus    1020

I noticed that I cant use any of the code in this thread because once the sound is playing, you cant change the volume(you have to stop it first).

 

 

What you need to do is change the volume/panning of the channel the sound is playing on, rather than just setting it when it initially plays. This will require you to implement a method of keeping track of which channel a sound is playing on.

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