Continuous music play and fade in/out
Posted:
Sun Sep 17, 2006 3:18 pm
by relaxis
Is there any way to get the music to fade out when a button is pressed?
Also, is there any way to get the music to KEEP on playing from the first dat file when the LoadGame(); function is used to load another dat file?
(ie - music play event on file 1, load file 2 - keep playing music from file1)
Posted:
Mon Sep 18, 2006 12:06 pm
by makslane
To fade the music, just change the music volume with the variable musicvol (0.0 - 1.0) in a Draw Actor or Timer event.
After load the second game file, the music will stops. So, you need to play again.
Posted:
Sun Apr 08, 2007 7:04 pm
by d-soldier
If I already have a sound file playing (ie. buring sound near animated fire) and I want to set up an active bounding box (canvas/filled region) where the sound will play, and as the player(actor)walks out of that region, the sound fades out.. how would i do that? And please be specific, I'm dumb when it comes to coding. :shock:
Posted:
Sun Apr 08, 2007 7:23 pm
by makslane
d-soldier wrote:If I already have a sound file playing (ie. buring sound near animated fire) and I want to set up an active bounding box (canvas/filled region) where the sound will play, and as the player(actor)walks out of that region, the sound fades out.. how would i do that? And please be specific, I'm dumb when it comes to coding.
You need to store the channel used to play the sound.
The channel can be a integer variable (actor).
To track when start the fade, use other variable (like canFade) and set to true when the player walk out of the region.
Is better create a variable volume (real, actor) and initialize with the value 1.0 to make the fade.
In the Draw Actor event, put something like:
- Code: Select all
if(canFade && volume > 0.0)
{
//Fade
setVolume(channel, volume);
volume *= .99;
}
Look more here:
http://game-editor.com/forum/viewtopic. ... ht=channel
http://game-editor.com/docs/script_refe ... #setvolume
About variable creation:
http://game-editor.com/forum/viewtopic.php?t=3246
How to control the speed of the volume fade?
Posted:
Wed Apr 18, 2007 7:08 am
by DST
The fade works, but it takes a long time to reach zero. Is there a way to speed it up? Whenever I lower either volume(variable) or the fade (.99) it only makes the sound drop to that level before fading out, and then fades at the slow speed. Any Ideas?
Posted:
Wed Apr 18, 2007 3:38 pm
by makslane
Try some low value like .5
- Code: Select all
volume *= .5;
Music Fade is uncontrollable
Posted:
Tue May 08, 2007 6:12 am
by DST
As i said in my previous post, when you lower that value, the music immediately switches to that value upon fade, and then fades out at the same speed. going from 100 to 50 in a split second, then taking 15 seconds to reach zero...well, that's a horrible fade. The fade length seems to be uncontrollable. I want to go from 99 to 0 in a matter of 3 seconds. Please tell me how to do this.
Posted:
Tue May 08, 2007 7:37 pm
by Fuzzy
Make an array.
in global code do this:
float Fade[16] = {0.9375, 0.875, 0.8125, 0.75, 0.6875, 0.625, 0.5625, 0.5, 0.4375, 0.375, 0.3125, 0.25, 0.1875, 0.25, 0.125, 0.0625};
Initialize an integer to zero and use that to refer to the Fade[16]. with each draw, add 1 to the int.
if (count < 16)
{
setVolume(channel, Fade[count]);
count ++;
}
You can do some nifty tricks with this. Take the distance of the player from the sound actor. change the distance from a float to an int with a type cast...
SoundDist = (int)distance(player.x, player.y, enemy.x, enemy.y);
and then force it to a positive value....
SoundDist = abs(SoundDist);
so now its 0 to whatever. we need to change it to the same range as the number of elements in the array. Its called normalizing. We do it with an AND operator.
SoundDist = SoundDist & 16;
So now GE considers out distance to be from 0 to 16. We use this to look up Fade...
setVolume(channel, Fade[SoundDist]);
A third way.... No array required.... use an int called SoundDist, same as before, and a float var called Vol.
switch(SoundDist)
{
case 0 :
Vol = 1.0;
break;
case 1 :
Vol = 0.9;
break;
case 2 :
Vol = 0.8;
break;
}
and so on...
A fourth way. This is the smoothest result. Determine the max distance you want to hear the sound at. Store it in a float var. Lets use MaxDist for a name. use a float called Vol to store our final result.
Get the distance from the player to the enemy.
Dist = distance(player.x, player.y, enemy.x, enemy.y);
which gives a nice float value from 0.0 to whatever. Add a tiny amount to it because we are going to divide, and want to avoid divide by zero errors.
Dist = Dist + 0.001;
Now we divide the two variables.
Vol = MaxDist / Dist;
But wait! at full distance, we get a vol of 1.0! thats no good! we need to reverse it.. easily done.
Vol = 1.0 - Vol;
and we set the volume as before..
setVolume(channel, Vol);
Done! Not checked for errors though!