Wintermute Engine Forum

Wintermute Engine => Scripts, plugins, utilities, goodies => Topic started by: metamorphium on January 11, 2005, 10:33:07 PM

Title: Dynamic background sounds in WME
Post by: metamorphium on January 11, 2005, 10:33:07 PM
Ever wanted to have random background sound effects in your game? Here's something to get you started.

First some concept:

Make in your sounds directory some subdirectory (mine is called animals) and fill it with group of filenames like this for example:
snd1.ogg
snd2.ogg
snd3.ogg
etc.

then create file Sound_daemon.script (in scripts) which will read following:

Code: WME Script
  1. method Initialize(group,range,minfreq,maxfreq,volume)
  2. {
  3.  
  4.                 //sound group is the path+filename without number
  5.                 this.SD_sound_group = group;
  6.        
  7.                 //How many sounds are in the group?          
  8.                 this.SD_range = range;
  9.                
  10.                 // Minimum time which should pass from the last occurence
  11.                 this.SD_minfreq = minfreq;
  12.                
  13.                 // Maximum time which should pass from the last occurence
  14.                 this.SD_maxfreq = maxfreq;
  15.                 // Group volume
  16.                 this.SD_volume = volume;
  17.  
  18.                 this.infinite = true;
  19. }
  20.  
  21. method Done()
  22. {
  23.         this.infinite = false;
  24. }
  25.  
  26. method RunDaemon()
  27. {
  28.   this.AttachScript("scripts/sound_daemon_body.script");
  29. }
  30.  

and also create file scripts/sound_daemon_body.script

Code: WME Script
  1.         this.entity = Game.CreateEntity();
  2.         while(this.infinite)    {
  3.  
  4.                 // First wait for the random ammount of time from range
  5.         var wait = Random(this.SD_minfreq,this.SD_maxfreq);
  6.         Sleep(wait);
  7.  
  8.                 var ent = this.entity;
  9.                
  10.                 if (!ent.IsSoundPlaying()) // better sound not playing now.
  11.                 {       
  12.                         ent.SetSoundVolume(this.SD_volume);
  13.  
  14.                         var t = Random(1,this.SD_range); //grab random sound
  15.                         var fname= "sounds/" + this.SD_sound_group + t+ ".wav";
  16.                         ent.PlaySound(fname, false);
  17.                 }
  18.        
  19.                 Sleep(20);
  20.         }
  21.  
  22.   Game.DestroyEntity(this.entity); // destroy our entity so we won't mess our memory.
  23.  
  24.  

then make some global variables and assign objects to them. I put it into game.script this:

Code: WME Script
  1. SD_layer1 = new Object("scripts\sound_daemon.script");
  2. SD_layer2 = new Object("scripts\sound_daemon.script");
  3. SD_layer3 = new Object("scripts\sound_daemon.script");
  4.  

note that you can have as much background sound layers as you want.

Last step is to put into scene_init.script

Code: WME Script
  1. SD_layer1.Initialize("animals/snd",7,6000,12000,40);
  2. SD_layer1.RunDaemon();
  3.  

what does it mean?
 
"animals/snd" is the group filename. (snd1, snd2 ... snd7 in my case)
7 is number of group members (7 files)
6000 is minimum time needed for Random (it's in miliseconds)
12000 is maximum time needed for Random (it's in miliseconds)
(those two parameters are specifying borders in which sound can happen)
40 is volume


When you want to stop sound_daemon, just send
Code: WME Script
  1. SD_layer1.Done();
  2.  

In my test run I had three layers created this way. I have background crickets with pretty often occurence.
second layer are 7 different chirping birds more rarely.
and last layer is faint whispering hearing very rarely.

I think you will find your own application for this simple object.

Huge thanks goes to Mnemonic and Jerrot!


EDITED: for my stupid and silly mistake I made...
EDITED: put away filetype. See below.

Title: Re: Dynamic background sounds in WME
Post by: McCoy on January 12, 2005, 12:43:25 AM
Is filetype really need? I think I remember Mnemonic mentioning that the engine searchs first for OGG and then for WAV if a file has the same name, or something like that... coul that mean that you can just use ogg and if there's no such filename with ogg extension but wav, the engine will use the wav, and vice-versa? or that you don't need to put the extension, and the engine will search first for name.ogg and if no luck, name.wav automatically? Or nothing like that? Mnemonic, please explain :)
Title: Re: Dynamic background sounds in WME
Post by: Mnemonic on January 12, 2005, 09:10:49 AM
Mnemonic, please explain :)
If your game references for example "filename.wav", the engine first looks if "filename.ogg" exists, and if it does, it's used instead. So, you have to use an extension, but if you always use .wav, you can replace the files later with Ogg compressed ones and you don't need to modify your code.
In other words, you are right the file type parameter is not necessary.
Title: Re: Dynamic background sounds in WME
Post by: metamorphium on January 12, 2005, 09:46:50 AM
Mnemonic, please explain :)
If your game references for example "filename.wav", the engine first looks if "filename.ogg" exists, and if it does, it's used instead. So, you have to use an extension, but if you always use .wav, you can replace the files later with Ogg compressed ones and you don't need to modify your code.
In other words, you are right the file type parameter is not necessary.

Oh. I haven't known that. Thanks for info. I think everyone should be able to put away the Filetype parameter from code though. :)

Title: Re: Dynamic background sounds in WME
Post by: Jerrot on January 12, 2005, 11:32:09 AM
Ever wanted to have random background sound effects in your game? Here's something to get you started.

YESSS! And I'm happy I won't have to write it on my own now. :))
Thanks a lot for sharing, your staying power rockZ!  ;D