Translations of this page:

This is an old revision of the document!
—-

10. Sound and Music

WME has very extensive support for sound and music. You can make your game sound quite well without having to needlessly restrict yourself. In this chapter we're going to add a few sounds and a music to our game to make it more alive.

Before we do that, we have to clarify a few things. WME currently supports two audio formats ogg/vorbis and wav.
The main difference is let's say a tradeoff between quality and size.

  • ogg/vorbis - is a compressed format, which allows much smaller sizes but there's an issue of quality loss, although if you choose good ratio, it's not recognizable for many people.
  • wav - is an uncompressed format which is not that usable for music(in CD quality 1 minute of stereo sound takes cca. 5.3MB), but it can prove useful for little short sounds as there's no delay in decompressing sound.

We'll start with the resource kit for chapter 10. We have there two music tracks, we're going to use. Metric.ogg is the music for a dream scene and for the circle and voyages.ogg is the music for the rest. Create new folder in the data folder and call it music and copy those two files in there.

Let's speak about a music a bit.

We access music from the Game object and we have basically two options how to play our music. Either we call

Game.PlayMusic(ogg file);

Or we use a specific music channel to do that

Game.PlayMusicChannel(ogg file);
  • Game.PlayMusic(file, Looping, Loop Start) has two additional arguments - Looping (true if the file should repeat all over) and Looping Start in ms, which defines from what position the music should repeat.
  • Game.PlayMusicChannel (channel, file, Looping, Loop Start has also the number of channel used to play music.
  • Game.StopMusic(); stops the current music.
  • Game.StopMusicChannel(channel); stops the music running in specified channel.
  • Game.PauseMusic(); pauses the music (PauseMusicChannel(channel))
  • Game.ResumeMusic(); resumes the paused music (PauseMusicChannel(channel))
  • Game.IsMusicPlaying(); returns true if Music currently plays (IsMusicChannelPlaying(channel))
  • Game.GetMusicVolume(); returns number from 0-100 with current volume (GetMusicChannelVolume(channel)
  • Game.SetMusicVolume(volume); sets new volume (SetMusicChannelVolume(channel,volume)

There are more methods but for now we'll be happy with those. Now what's the difference from using channel version? You can use more music tracks at the same time, you can crossfade them (one goes volume down while the other volume up for seamless transition).

Open the scene_init.script of the Dream scene and modify the beginning.

#include "scripts\base.inc" 
 
// here comes the stuff which initializes the scene 
 
Game.Interactive = false; 
 
Game.PlayMusic("music\metric.ogg"); 
Game.SetMusicVolume(70);

Test the game and you immediately see, that it gets better atmosphere. But we'll now change the mechanism to the music channels so we can make a neat crossfades. Modify the lines to read:

  Game.PlayMusicChannel(0,"music\metric.ogg",true); 
  Game.SetMusicChannelVolume(0,70);

We've first of all used the music channel 0 for the music playing, but we've also set the track to looping track by providing true as a looping value.
For crossfading we use Game.MusicCrossfade(channel1,channel2,time, swap); which is a method decreasing volume of channel1 and increasing volume of channel2 in time. Swap means that what was in the channel1 gets to channel2 and vice versa so you after the crossfade operate with channel1 for the new track. Note that this is automatically true, so if you don't want the swap you have to specify false as the last parameter.

In this light, we'll open the Street scene and modify scene_init.script at the bottom to read:

//////////////////////////////////////////////////////////////////////////////// 
if(!StateStreet.Visited) 
{ 
  StateStreet.Visited = true; 
  Game.PlayMusicChannel(1,"music\voyages.ogg",true); 
  Game.MusicCrossfade(0,1,6000); 
  // this is our first visit in this scene... 
}

Simply said, we start the new music track in channel 1 and then crossfade both channels to achieve the fluent transition from track to track.
Last but not least we'll return back the original track but this time we'll use new set of functions just for the sake of trying them.

Open the scene_init.script of the Circle scene and to the beginning (just below the include) put:

 Game.Interactive = false; 
 for (var volume=Game.GetMusicChannelVolume(0);volume>0;volume=volume-1) 
 { 
    Game.SetMusicChannelVolume(0,volume); 
    Sleep(20); 
 } 
 
 Game.StopMusicChannel(0); 
 Game.PlayMusic("music\metric.ogg");   
 
 Game.Interactive = true;


 
wmebook/ch10.1197311863.txt.gz · Last modified: 2007/12/10 19:37 by metamorphium
Recent changes RSS feed Creative Commons License Driven by DokuWiki