Showing posts with label mp3. Show all posts
Showing posts with label mp3. Show all posts

Tuesday, August 14, 2007

Sound with FMOD

There are many libraries that can be easily integrated with an Ogre3D project. We chose to use FMOD. This is a cool library that can play just about any sound, .wav, .mp3, .ogg, .whatever...

In addition to that I decided to use the Sound Manager available in the wiki. The good thing about it? You don't have to learn a thing about the FMOD library. It encapsulates all the major functions.

First we downloaded the FMOD Ex Programmers API and intalled it. Since we use VC++8 we linked with the fmodex_vc.lib library and added the include files.

After integrating the SoundManager in the project it is necessary to use the following code:

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

int jaguarSound = soundMgr->CreateSound(String("jaguar.wav"));
int music = soundMgr->CreateLoopedSound(String(music.mp3"));
int music2 = soundMgr->CreateStream(String(music.mp3")); //stream example

int jaguarSoundChannel = INVALID_SOUND_CHANNEL;
int musicSoundChannel = INVALID_SOUND_CHANNEL;

soundMgr->PlaySound( jaguarSound, mSceneNode, &jaguarSoundChannel );
soundMgr->PlaySound( music, mSceneNode, &musicSoundChannel );
First you create sounds and streams. Sounds are completely loaded to memory while streams are loaded in small chunks of data and are more suitable for large sounds.

When you play a sound the sound manager will create a sound channel for it. A sound can be played in multiple sound channels at the same time. Sound channels are useful if you want to stop the sound.

soundMgr->StopSound( &musicSoundChannel );

Each Sound Channel is associated with a SceneNode. This means that the farther the Node is from the origin the sound will be lower. The same applies to left and right movements. This is called 3D Sound and only is available to Sounds and not to Streams.