Wintermute Engine Forum

Wintermute Engine => Scripts, plugins, utilities, goodies => Topic started by: GreyDay on July 21, 2011, 10:29:59 AM

Title: Sound emitter
Post by: GreyDay on July 21, 2011, 10:29:59 AM
While working on The Shine of a Star, we made a sound emitter to fade sound in and out from a place in the scene. It works by measuring the distance between the actor and a point in the scene, the closer to the point the louder the sound to a maximum of 100 percent.

We put the script as a function in base.script and calls it from wherever we want to use it.

The arguments are sound (the pathway to the song/sound in a string), x, y, a radius from where the sound should start fade in, if it should loop and which channel it should play the sound through. If no loop is chosen, it loops automatically and if no channel is set, it uses the playSound method.

So here is the script:

Code: [Select]
function soundEmitter(var sound, var positionX, var positionY, var maxRadius, var loop, var channel)
{
if(loop == null)
{
loop = true;
}

if (channel == null)
{
Game.PlaySound(sound,loop);
}
else
{
Game.PlayMusicChannel(channel, sound, loop);
}
var update = true;
var distance = distanceCalc(positionX, positionY);
var p1;
var precent;


while(update)
{

distance = distanceCalc(positionX, positionY);
p1 = 1 - distance/maxRadius;
precent = p1 * 100;

if(precent >100)
{
precent = 100;
}
if (precent < 0)
{
precent = 0;
}
if (channel == null)
{
Game.SetSoundVolume(precent);
}
else
{
Game.SetMusicChannelVolume(channel, precent);
}

Sleep(10);
}

}

And here is a simple example of how to call for the function. In this particular example, the function is called from a sprite event for a bird to play a random bird cry every time it opens it's mouth:

Code: [Select]
on "Speak"
{
r = Random(1,3);
soundEmitter("Musik\Den första scenen\Mourning Bird "+ r +".ogg",this.X,this.Y,1600,false);
}

Hope it is usefull for someone, Cheers!

EDIT: Worth mentioning is that the script measure distance (i.e., a circle), and will work on both the X-axis and the Y-axis.
Title: Re: Sound emitter
Post by: GreyDay on July 21, 2011, 07:44:32 PM
Noticed that I missed the distance calculation method... So here it is! Also put in the base.inc file.
Code: [Select]
function distanceCalc(var positionX, var positionY)
{
 var x = positionX - actor.X;
 var y = positionY - actor.Y;
 var math = Math.Pow(x,2) + Math.Pow(y,2);
 var distance = Math.Sqrt(math);
 
 return distance;
}
Title: Re: Sound emitter
Post by: keybone on March 02, 2012, 10:26:41 AM
nice script!  ::rock