SOUND VOLUME DEPENDING ON DISTANCE

Tutorial on how to implement increasing sound volume on sound source approach

  • Bogo
  • 05/23/2015 11:22 AM
  • 5699 views
Hi!
Welcome to the 'Sound volume depending on distance' tutorial for RPG Maker XP.

In order to implement a solution I am about to explain, you need to get a script I have submitted not long ago. You can find it here:
http://rpgmaker.net/scripts/568/

Got it? Great, let's move on.
1. Let's assume you have a lovely map with a waterall in some place, something like the map below (it's brilliant, I know):


Player reaches this location at point 1 and we want him to hear some quiet water falling sound at point 2. When he will be moving towards the waterfall, sound volume will increase with peak reached at point 3.

2. The first thing you need to do (well, after creating a map) is too put an event somewhere on the map. This event should be placed where you think is the best place to simulate sound source (waterfall in this case). Take a look at image below:



I put a 'sound source' event at the top of the waterfall (red circle). If you think a better place to put a sound source would be at the bottom of the waterfall (or a one of those trees around there) you're free to do so.

Next thing I want you to notice is a green circle around event ID. Remember your event ID number, we will use it later.

3. Now we have to create an event, which will run in the background and perform some calculations for us. This event needs to be a pararell process. You can use any of your pararel events, that you already have on your map (for example a flying bird) and use it as this pararell event (remember, that optimizations are always desired!).



4. Ok, we are almost there. Last thing we need to do is to implement the script I mentioned at the beginning of this tutorial. You add a script just like any other event ('Show text', "Wait...', etc). An option to add a script is on the bottom of the third tab of events picker ('Script...'). When you click that option, a window awaiting for code pops up. Here goes our script:



PLEASE NOTE that new line (starting with 'Audio...') is not from pressing Return/Enter, it's been automaticaly added. Here's the code for script call:

$game_map.playDistanceAudio(1,'Audio/BGS/011-Waterfall01')

The best way to make sure the script call will work, is to type in the above text letter by letter, then - after '(1,' - start adding whitespaces until text marker goes to the next line, then start typing 'Audio/BGS...'. IT IS VERY IMPORTANT, that '(1,' text will be placed in the same line as '$game_map.playDistanceAudio' !!!

One more thing: make sure you a have the '011-Waterfall01' file in your BGS folder!
You can put any sound you want there, of course, and name it as you want; just remember that sound file you are pointing to at script call has to exist.

Now press OK and... it's done! Your sound volume based on distance system should run smoothly and make you very happy!

But wait, there's more. Let's make it like a pro (almost). So here goes point...

5. The pararell process wich calls our script runs in the background for the whole time. It's not good from the perspective of time resources (it takes processor time needlessly), so I decided to use another form of optimization. We don't need to change sound volume when player is not moving, so let's detect his movement and call the script then:



There is one more thing I need to warn you about. You can't have two sounds of this type playing simultaneously. The script uses RPG Maker background audio system to play the sound, and you can't have two background sounds playing at the same moment. If situation occurs, when you call our script for two different events / sounds, effects will be messy (veeeery big performane drop). You need to take care of your events and use conditional flags and other stuff to determine which sound is 'free to go' at the time.

That's it, folks. If you have some problems, write a comment and I will try to responds (I'm often busy, so don't get angry, if it takes long;)). Take care!