Implementing FMOD

From Valve Developer Community
Revision as of 09:21, 22 November 2009 by £cho (talk | contribs) (Created page with 'Implementing [http://www.fmod.org/ FMOD] into the Source engine is incredibly easy and the benefits speak for themselves, however you will need to know how FMOD works in order to…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Implementing FMOD into the Source engine is incredibly easy and the benefits speak for themselves, however you will need to know how FMOD works in order to get anything out of adding it to your modification.

What is FMOD?

"The FMOD Ex sound system is a revolutionary audio engine for game developers, multimedia developers, sound designers, musicians and audio engineers. The development of FMOD Ex is based on the years of experienced of Firelight Technologies' previous product FMOD 3. FMOD Ex is intended to push the creative boundaries of audio implementation for games and the like, whilst using minimal resources and being fully scalable." - [1]

Essentially, FMOD is a sound system that can run in a different instance while your modification is running. This means you can have music playing while a player is connecting to a server or loading a new map, not to mention play files in formats that Miles, the default sound system of the Source engine, can't. The possibilities are endless, and because it is relatively easy to implement, there's really no reason not to add it to your modification.

FMOD operates under a free for non-profit license. As long as you don't plan to ever sell your modification, you may continue using FMOD for free, otherwise you'll need to pay for a license.

The Installation

You will need to download the latest version of FMOD Ex. You can do this by visiting FMOD's download page on their website. Once downloaded, make sure the files are installed to a directory in your source code. It's easier if you just made an "fmod" folder in your /src folder and put everything in there.

In addition to the files FMOD provides you, you will need to download the following files and add them to your client project:

These files are free for non-profit use, just like FMOD. Please do not attempt to edit these files unless you know what you're doing.

NOTE: FMOD will not operate under the server project at this time. The primary reason for this is because the server does not store the path to a client's mod folder. It does store the mod folder's name (i.e hl2, hl2dm, etc), however not the directory tree, which means sounds cannot be found relative to a client's modification folder. There is probably a hacky way to go about this, however it is best just to stick with the Miles sound system for the server and use FMOD for the client until a real solution is found.

The Setup

Now that the installation is done, it's time to setup our system. There's not a whole lot to do here, but these are necessary steps you'll have to take in order to begin using FMOD.

To start, right-click on your client project in the Solution Explorer and go to "Properties". Expand "Configuration Properties" on the left of the new window that appears and click on "C++". Edit "Additional Include Directories" to include FMOD's /api/inc folder. For example, if you made a new "fmod" folder in your /src folder of your source code files as suggested, you would add this to your list of include directories: ..\..\fmod\api\inc

Stay in the same window and click on "Linker", which is the option under "C++". Edit "Additional Library Directories" to include FMOD's /api/lib folder. Again, if you made a new "fmod" folder in your /src folder of your source code files, add this: ..\..\fmod\api\lib

All that needs to be done now is to start the FMOD sound system when the modification launches. To do this, open up cdll_client_int.cpp and add the following in the Init function under g_pClientMode->Enable();

// FMOD - Start 'er up!
FMODManager()->InitFMOD();

Similarly, in the same file in the Shutdown function, add the following below g_pClientMode->Shutdown();

// FMOD - Shut us down
FMODManager()->ExitFMOD();

Usage

The installation and setup are both complete! Assuming you followed this little tutorial step-by-step, you now have successfully implemented the FMOD sound system into your modification. Now it's time to learn how to use it, otherwise this would all have been for nothing.

Whenever you want to use the FMOD system, you will need to add the following to your list of includes at the top of the cpp you are editing;

#include "fmod_manager.h"

After this is done, you can proceed to call any one of fmod_manager's public functions using FMODManager()->. Here's an example:

FMODManager()->TransitionAmbientSounds( "ambient/so-ambience1.mp3" );

The above transitions between a sound that is playing via FMOD by slowly fading it now, then playing the new sound. In most cases, you'll want to use this instead of the PlayAmbientSound function to prevent two sounds from playing simultaneously.

To stop all sounds playing via FMOD, you can use the following:

FMODManager()->StopAmbientSound();

Sometimes you'll probably want to see if a sound is already playing before playing it again or playing a new sound. For this, you can use the following: FMODManager()->IsSoundPlaying( "relative path to sound file to check here" );

The IsSoundPlaying function will return true if the sound is already playing and false if it is not or if no sound is currently playing.