# How should I attenuate sound in a 2D game where you can zoom in and out?

In my 2D game, I have some ambient sounds like the noise of a waterfall or the humming of a generator. However, it is not practical to play these sound all the time at full volume as in a large level the overlap of many of such noises would be quite unpleasant to the ear.

I am currently thinking about a system that modulates the volume of certain sounds depending on their distance to the player. However, this is partly not a great solution as (for example) in a zoomed state the player might be quite far away from the sound source (like a water fall) but still be able to see it on screen but not hear it (due to the distance) which is quite awkward.

Is there a solution for such a problem?

• "Best practices" is too broad, but it looks like the crux of your question is dealing with the zooming, which is more answerable and interesting. So I removed the "best practices" bit. – Josh Nov 20 '13 at 17:09

I think you are on the right track. Your proposed system of distance-based attenuation should work if you

• always use the in-game distance to the object from the player, which remains fixed regardless of the zoom level (don't use the "on-screen" distance, which changes as you zoom) and

• set up your attenuation system to begin playing an object's sounds as soon as its distance to the player in the world is such that the object would almost be visible to the player at the maximum zoom level.

This way players can hear things before they see them, which imparts a sense of realism and discovery. It also prevents most scenarios of playing too many ambient sounds.

Of course, you still have the potential to overload the ambient sound channel if you happen to have a lot of sound-emitting objects in the same spot in the world. This can be solved by

• designing your levels not to do that, or, if that's not viable,

• giving objects a sound priority, and taking only the top n highest-priority objects, or

• modulating down the volume of all ambient sounds by (more or less) "dividing by the number of sounds in the scene," making sure that you still smoothly fade out sounds as their objects become further away in order to avoid "popping" as you discretely jump from five objects to four, for example.

• precompute, for a given level or the whole game, the maximum number of objects that can be in-range and playing sounds at once (do this as part of the level or game data build process). Based on that you can determine how loud to play any one object such that the worst-case never exceeds a certain volume.

The latter two options address the problem purely from a volume perspective, however -- they don't account for the fact that even if you play 500 background sounds quietly enough not to overwhelm a player's eardrums, you are still playing 500 sounds, which can be noisy not in the volume sense but in the confusing sense.

To handle that problem, I think you are best off trying to reduce the total number of ambient emitters or taking only the top n prioritized sounds (for reference, taking the top n is more-or-less what we settled on for Guild Wars 2).