FMOD SoundManager         Based on FMOD Ex Version 4.02

Introduction

The class presented here may be used as a Sound Manager based on the FMODlibrary http://www.fmod.org/.
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.

Info For discussion use this forum thread.
Mogre users look here for a .NET wrapper.

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.

 Plugin disabled

Plugin attach cannot be executed.


And here is just the source to the sample:


Updated SoundManager.h, SoundManager.cpp to FMod 1.05, Ogre 1.8.1 / Ogre 2.1

 Plugin disabled

Plugin attach cannot be executed.



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

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

 Plugin disabled

Plugin attach cannot be executed.

For more information in this topic take a look at this:
non-MS Compiling FMOD Forum Entry

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