Jump to content

  • Log In with Google      Sign In   
  • Create Account


Distance between two entities decide volume of a sound.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 P0jahn   Members   -  Reputation: 242

Like
1Likes
Like

Posted 14 March 2013 - 10:51 AM

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.



Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 12337

Like
6Likes
Like

Posted 14 March 2013 - 11:20 AM

What's the question, exactly? How to check if the distance between AA and BB is over some threshold? Or are you wondering what the falloff function should be?


Edited by Álvaro, 14 March 2013 - 11:21 AM.


#3 P0jahn   Members   -  Reputation: 242

Like
1Likes
Like

Posted 14 March 2013 - 11:28 AM

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.



#4 BeerNutts   Crossbones+   -  Reputation: 2661

Like
3Likes
Like

Posted 14 March 2013 - 11:41 AM

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, 14 March 2013 - 11:41 AM.

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)

#5 Yourself   Crossbones+   -  Reputation: 1064

Like
1Likes
Like

Posted 14 March 2013 - 12:04 PM

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));
}

 

 



#6 ProvenDantheman   Members   -  Reputation: 111

Like
-1Likes
Like

Posted 14 March 2013 - 01:10 PM

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



#7 Álvaro   Crossbones+   -  Reputation: 12337

Like
0Likes
Like

Posted 14 March 2013 - 01:54 PM

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


#8 Ludus   Members   -  Reputation: 970

Like
0Likes
Like

Posted 14 March 2013 - 02:19 PM

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.



#9 P0jahn   Members   -  Reputation: 242

Like
0Likes
Like

Posted 14 March 2013 - 03:44 PM

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?



#10 BGB   Crossbones+   -  Reputation: 1549

Like
0Likes
Like

Posted 14 March 2013 - 03:47 PM


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.

#11 Ludus   Members   -  Reputation: 970

Like
0Likes
Like

Posted 14 March 2013 - 05:35 PM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS