Please login or register.

Login with username, password and session length
Advanced search  

News:

Forum rules - please read before posting, it can save you a lot of time.

Author Topic: Sound emitter  (Read 6398 times)

0 Members and 1 Guest are viewing this topic.

GreyDay

  • Lurker
  • *
  • Karma: 1
  • Offline Offline
  • Posts: 23
    • View Profile
Sound emitter
« 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.
« Last Edit: July 21, 2011, 10:59:53 AM by GreyDay »
Logged

GreyDay

  • Lurker
  • *
  • Karma: 1
  • Offline Offline
  • Posts: 23
    • View Profile
Re: Sound emitter
« Reply #1 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;
}
Logged

keybone

  • Regular poster
  • ***
  • Karma: 0
  • Offline Offline
  • Posts: 112
    • View Profile
    • Lucine
Re: Sound emitter
« Reply #2 on: March 02, 2012, 10:26:41 AM »

nice script!  ::rock
Logged
Lucine Company http://www.lucine.it/
Tales of Lucine: The Realm of Hobdark http://www.lucine.it/TalesOfLucine
 

Page created in 0.033 seconds with 18 queries.