FMOD SoundManager         Based on FMOD Ex Version 4.02
Print

Introduction

The class presented here may be used as a Sound Manager based on the FMODlibrary http://www.fmod.org/(external link).
FMOD Ex Version 4.02 was used to create this manager.
Sounds may be loaded from seperate files, or may be grouped together in one or more zip files.
Discussion thread in forum: http://www.ogre3d.org/phpBB2/viewtopic.php?t=17181

Source code

Here are the source code files for this manager.

Sample code

A sample application is provided here to demonstrate simple usage of the SoundManager.
The zip file includes all source, DLLs, LIBs, and data to run the sample.


 SoundMgrSample.zip

And here is just the source to the sample:

Requires API sources files such as fmod.hpp. Download install from http://www.fmod.org.(external link)

Additional Info on non-Microsoft compilers

The version linked below is a modyfied version of the FMOD based SoundManager I got from here.
Important note: This version works with non-Microsoft compilers, too! The normal version - and the version of FMOD of course - has to be compiled with a Visual Studio compiler. We modified the Soundmanager code so that it uses the standard C header file fmod.h, instead of the c++ headerfile fmod.hpp. This way it can be compiled without any problems for example with MingW.

non-Microsoft SoundManager Archive(external link)

 SoundManager_nonMS.zip
For more information in this topic take a look at this:
non-MS Compiling FMOD Forum Entry(external link)

Greets, MicroForge-Team :-)

Usage

Refer to the FMOD documentation for detailed explainations of terms.
As a quick explaination, a 'sound' is created once per wav (mp3,ogg...).
A 'channel' is created once per playback instance of a sound.
So, there may be multiple channels all based on the same sound.

The SoundManager returns ints as 'handles' to the sounds and channels created.
All the variations of SoundManager::CreateSound() will not create duplicate sounds based on the same fileName.
If a repeated fileName is passed in, the handle to the already created sound will be returned.

The first thing that must be done after creating the SoundManager is to Initialize it:

SoundManager *soundMgr;
   soundMgr = new SoundManager;
   soundMgr->Initialize();

To create a single-shot 3D (positional) sound:

int soundFireGun;
   soundFireGun = soundMgr->CreateSound(String("FireGun.wav"));

To play this sound:

int channelFireGun;
   soundMgr->PlaySound(soundFireGun, sceneNode, &channelFireGun);

PlaySound will return a channel number in channelFireGun, but since this is a short single-shot sound, we don't need to save it, and it may be a stack variable.
The variable 'sceneNode' passed in to PlaySound is the Ogre::SceneNode that the sound should be attached to.

To change a channels volume:

FMOD::Channel *channel= soundMgr->GetSoundChannel(&channelFireGun);
   channel->setVolume(0.5);  // Set volume to half of maximum

Looping channels are a little different. After you create one, FMOD may decide it's no longer needed because the camera is far away from it, and discard it.
Therefore, loooping channels should be created when the camera is nearby.
Create the sound, and initialize the channel to INVALID_SOUND_CHANNEL (done once):

int soundWalk;   // Entity's member variable
   int channelWalk; // Entity's member variable
 
   soundWalk = soundMgr->CreateLoopedSound(String("BattleDroidWalk.wav"));
   channelWalk = INVALID_SOUND_CHANNEL;

In the Entity's tick() function (or your equivalent):

// The sound manager may kill our walk sound if it needs the channel.
   // So, here we'll make sure it's playing by calling PlaySound if the camera is close by.
   // If our sound is already playing, PlaySound will just return.
   if (isWalking)
      {
      if (distToCameraSquared < 2500)
         soundMgr->PlaySound(soundWalk, sceneNode, &channelWalk);
      else if (channelWalk != INVALID_SOUND_CHANNEL)
         soundMgr->StopSound(&channelWalk);
      }

And most importantly, the function SoundManager::FrameStarted() must be called every frame.
This function updates all the positional information for the sounds, and also allows the FMOD library to update its data.
The variable 'listenerNode' should be the Ogre::SceneNode that the camera is attached to. This is where the system's "ears" will be.
The variable 'timeElapsed' may be retrieved from a listener's FrameStarted event (evt.timeSinceLastFrame).


Alias: FMOD_SoundManager


Contributors to this page: JustBoo1 points  , OgreWikiBot and jacmoe60863 points  .
Page last modified on Wednesday 30 of June, 2010 21:47:14 GMT by JustBoo1 points .


The content on this page is licensed under the terms of the Creative Commons Attribution-ShareAlike License.
As an exception, any source code contributed within the content is released into the Public Domain.