Please login or register.

Login with username, password and session length
Advanced search  

News:

IRC channel - server: waelisch.de  channel: #wme (read more)

Author Topic: Dynamic background sounds in WME  (Read 5022 times)

0 Members and 1 Guest are viewing this topic.

metamorphium

  • Global Moderator
  • Addicted to WME forum
  • *
  • Karma: 12
  • Offline Offline
  • Gender: Male
  • Posts: 1511
  • Vampires!
    • View Profile
    • CBE  software s.r.o.
Dynamic background sounds in WME
« 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.

« Last Edit: January 04, 2009, 06:52:17 AM by Jyujinkai »
Logged
J.U.L.I.A. Enhanced Edition, Vampires!, J.U.L.I.A., J.U.L.I.A. Untold, Ghost in the Sheet

McCoy

  • The cocido eater
  • Frequent poster
  • ****
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 365
  • Spurrrrrrring
    • View Profile
    • Spur Games
Re: Dynamic background sounds in WME
« Reply #1 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 :)
Logged

Click here to sign my sig!

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5675
    • View Profile
    • Dead:Code Site
Re: Dynamic background sounds in WME
« Reply #2 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.
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

metamorphium

  • Global Moderator
  • Addicted to WME forum
  • *
  • Karma: 12
  • Offline Offline
  • Gender: Male
  • Posts: 1511
  • Vampires!
    • View Profile
    • CBE  software s.r.o.
Re: Dynamic background sounds in WME
« Reply #3 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. :)

Logged
J.U.L.I.A. Enhanced Edition, Vampires!, J.U.L.I.A., J.U.L.I.A. Untold, Ghost in the Sheet

Jerrot

  • Global Moderator
  • Addicted to WME forum
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 688
    • View Profile
Re: Dynamic background sounds in WME
« Reply #4 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
Logged
Mooh!
 

Page created in 0.297 seconds with 22 queries.