Wednesday, April 22, 2009

With all this talk about climate change and such, I got curious about how much total energy is stored in the heat in the atmosphere vs. the oceans. It seems that we talk about climate change, but because we are humans, we are highly biased toward the air being the climate and somewhat ignore the oceans. If we were all dolphins, we would see the oceans and ignore the air.

SO lets get some numbers, all gleaned from Google searches, mostly with hits on Wikipedia...

Weight of the atmosphere: 5.1x10^18 kg.Weight of the oceans: 1.5x10^21 kg.

So the water out weighs the atmosphere by 294 times.

But, water holds more heat per kg than air...

The specific heat of water: 2.015 kJ/kg-K.

This means that it takes 2015 Joules of energy to raise one killogram of water by one degree Kelvin or Centigrade.

the specific heat of air: 1.0 kJ/kg-K

So water holds about twice the heat per pound than air, so

The total heat needed to change the temperature of the ocean vs. the air is 588 times greater.

My point?

the global temperature that we all measure and quote as 'global warming' is irrelevant.

The oceans are the real climate and fluctuations in the atmosphere are trivial.

Saturday, April 11, 2009

Our project had a lot of difficulty in getting sound to work in XNA and in communication between the programmers and the sound artists.This tutorial is intended as instructions for the audio folks at Sandswept Studios, but feel free to use it too.

Key Concepts

XACT lets the audio engineers tweak all sorts of sound parameters without the programmers having to worry about it.The programmer just plays the cue.The audio engineer can tweak volume, sound fall off distance, etc.

First and most important, you must have the correct version of XACT to work with XNA!This means that you must use the XACT that comes with XNA Studio.On my machine XACT is at C:\Program Files\Microsoft XNA\XNA Game Studio\v3.0\Tools\Xact.exe

When you are editing sounds in XACT it is nice to be able to hear them, so you have to run AudConsole.exethat is in the same directory that Xact.exe is in. You would think that XACT could just play sounds, but no, you have to run AudConsole.exe

Create An XACT Project

So lets get started. Run XACT and create a new project.File ==> New projectI put the new project at C:\Users\rkeene\Documents\AudioTest\ and called it AudioTest

XACT Uses wave banks which are the 'raw sound' usualy from a .wav or .aif file.We will use the regular old windows chimes sound. So create a wave bankWave Banks ==> New Wave BankYou should get an empty wave bank window.Now go find the .wav file we want. On my Vista machine it is inC:\WINDOWS\Media\chimes.wavSo drag-n-drop that file onto the WaveBank area.

Now we want a sound bank, which ties a raw wave bank entry to all sorts of settings like volume and pitch.Sound Banks ==> New Sound BankThen drag and drop the chimes.wav wave bank entry to the upper part of the Sound Banks window.Sounds let you add event like volume changes, pitch changes, etc. creating nifty effects.

We also need an actual cue. This is the name the programmer will use to find the cue and play it in the C# code in XNA.So, drag-n-drop the sound to the lower part of the cues area.

Now we have a wave bank, a sound bank, and a project. So click on the save icon to save the project.

Sound Fade With Distance

In most games it is nifty to have sounds fade as your view point gets furthur away from the sound source.Like in DETOUR the factory makes machine noises and we want them to fade out as the player's viewpoitn gets furthur from the factory.But there are 'big' sound like nukes going off, and 'small' sounds like a dog barking. Big sounds fade out less with distance, and small sounds fade quickly.In XACT there are RPC Presets that let you setup this fade out.

You need to determine what units distance is in in your game. Is it meters? Inches? Pixels?On the DETOUR board tiles are 254x254 game units. These are also called World Coordinates.So while the view point may be on tile 12,5 the world coordinates will be 12*254, 200, 5*254which is X,Y,Z coordinates. The board is in the XZ plane and Y is 'up'.Thus for our factory sound we want to be at full volume if you are looking at the factory and fade outto silent in about 2000 units of distance.

First lets look at Variables. On the left side there is Variables ==> Cue Instance ==> DistanceThis is the variable that we will relate to volume. Since our largest board is about 40x40 and thus10,000 world units max, we want to limit distance ranges to 0 to 10000.So click on Distance and down below set the range to 0 and 10000.

Next drag and drop your cue on to the RPC Presets item on the left of the screen. This attaches the cue to the Presets.Click on RPC Presets and a window comes up with a black window and lines on it. The lines are (or will be) the sound level related to distance.

So in the RPC Presets window you first select the Variable whic is Distance.Then select the Object whic is Sound, and then the Parameter which is Volume.Thus "The distance effects sounds by changing colume."

Now you can drage the line around and get whatever sound vs. distance function you like.You can double click on the line to make a new drag point. And Ctrl-Z undoes mistakes.

Testing 1 2 3 Testing

Make sure AudConsole is running, and up at the center top of the Xact tools there is a play button.Click on the Cue, and then click play. Do you hear it?Also, in the RPC Presets tool you can set the distance, click on the line, and then click the play buttonin the RPC window and hear the sounds volume at a given distance.

Exporting

Next we need to export from XACT for XNA.At the top of the XACT windows is a View menu and the first two items are View XBox36 and View Windows.This is how you switch between compiling and exporting for the XBox and the PC.

You want to be able to tell the XBox vs. Windows files appart so click on the Wave Bank in the left tree,and on the lower left there should be fields forXBox 360 Build Path and Windows Build Path.

We use the convention that the file names end with _w or _x. So change these to be differnet.Do the same for the sound banks.Do the same for the top most level project too.

In my test project the top level is AudioTestso the build path is AudioTest_w.xgs and SudioTest_x.xgsThe sound banks are SB1_w.xsb and SB1_x.xsband wave banks are WB1_w.xwb and WB1_x.xwb

Now you build, so make sure View is on View Windows Properties, and click the Builds The Current Projecticon in the top center of the XACT windows.It will ask about the report you might want. I check Concise and enter rpt_w.txt

The reason for the report is that as your game gets bigger and has zillions of cues, the report helps you keep thing straight.You can also give the report to the programmer so they know what the cue names are.This is where all those 'notes' fields come in handy.It is wise to add a note entry to every cue describing what it is intended for in the game.

Phew! We have exported the windows version.

You can set the View to View XBox Properties and export again to get the XBox version of the files.Windows version files will not work on the XBox. Period.

Note: sound effects (sfx) are usually loaded into memory in advance so there is no delay to play them.Music is HUGE so it is streamed from disk to the audio hardware on-the-fly with buffers.If you click on the Wave Bank, the properties below will include Streaming or InMemory.Sound effect should be InMemory, and Music should be Streaming.

C# and XNA and Sounds

Ok, now you have all the sounds build for XNA.

Now we move to the XNA C# world.In XNA you have to load the project (.xgs) the sound bank (.xsb) and the wave bank (.xwb)Then you find the cue, give it 3D position (optional), and play it. If your sound person did things correctly, then that is all.

First we need to get the files into the XNA project. So copy the three files into the Content area. (Ok, get organized and use a subdirectory, etc.)Now in the Content part of the Solution Explorer, Add ==> Existing ItemYou will need to set the pop up file chooser to All Files (*.*). It initially filters for non-sound files.Select the three files and hit Ok.

They are now in the project, but you need to tell XNA and Visual Studio how to use them.Select all three under the Content area and in the properties area set...

The Update there is to 'fail fast' if there is a problem, and is not required.

Note: If you play a cue and never call Update on the audio engine, you not hear anything.Note: The above is for in-memory sounds like sound effects. Music is streamed and the code mightthen look like...

TheAudioEngine = new AudioEngine("Content\\music_w.xgs"); TheWaveBank = new WaveBank(TheAudioEngine, "Content\\music_w_wb.xwb", 0, 4); TheSoundBank = new SoundBank(TheAudioEngine, "Content\\music_w_sb.xsb"); TheAudioEngine.Update();Note the 0,4 on the end of the WaveBank entry. This is critical.It is the offset and packet sizeand effects how smoothly the music plays back.

Oops - We need to differentiate between XBOX and not XBOX code so we need a#if XBOX // Load the XBox versions of the files.#else // Load the Windows versions of the files.#endifaround our loads where the _w becomes _x

Now lets play our sound.The simplest way is...

TheSoundBank.PlayCue("mycue");

Also in the main Update in your game you need to Update TheSoundEngine

Ok, the FocusTile.LocationWorld gives me the X,Y,Z coordinates of the tile in world units.The FactoryTile is where my factory is.The d is the distance between the two.We have to set the Distance variable on the cue, and the Apply3D does the stereoleft-right speaker shift.(You left and right speakers are in the correct place on your desk top? The one with the volume knob is always the right speaker.)

Wow, that is how sound is done.

XBox Note: When you make the XNA project for the XBox you must remove the wave bank, project, and sound bank files in Contentand re-add the XBox versions.

Tips and TricksRandomize Cue Sound ChoiceYou can make a single cue play several different sound randomly. The programmer does not need to even know which one will play.Cues have a weight or probability. You can give each sound in the cue a different weight and that is how often they will get picked.This is cool if you have a repetitive sound, like gun fire, and you want to randomly choose between several different gun sounds to make it not-so-boring.C# Code StructureYou can have several Audio Engines at the same time. We break out one music engine, and 3 different Sfx engines. Then we have a PlaySfx call that takes an enum flag as to which Sfx set the cue is in. This makes our code control checkins much faster since we don't have one giant Sfx Sound Bank and Wave Bank.

It is also handy to have a currently playing music track, and a NextMusicCue that will get played when the current one finishes.

We also have an SfxItem class that hold info about a currently playing item and each of the Sfx engines. So our overall update method loos like this...