Tutorial – how to use FMOD with C++

Note: This was written a while ago, therefore the quality is not up to standards, although the tutorial is still valid.

Hi guys. In this tutorial I am going to teach you how to implement the sound system called, FMOD : http://www.fmod.org/

Downloading:

Go to the url: http://www.fmod.org/fmod-downloads.html and select your OS, this tutorial will be based on the windows platform. You will be given an installation file (exe), install it normally. If you choose to change the installation path you should note it down, as you’ll need it later on. For FMOD to work properly, you must do the following.

Installation

Include Directories:

  • %FMOD ROOT%\FMOD Programmers API Windows\api\inc

Library Directories:

  • %FMOD ROOT%\FMOD Programmers API Windows\api\lib

Copying: Then you should also copy these dll’s to your project output folder:

  • fmodex.dll
  • fmodex64.dll
  • fmodexL.dll
  • fmodexL64.dll

Linking: You should link this library:

  • fmodex_vc.lib

Initializing FMOD

To gain access to FMOD, you must include:


#include "fmod.hpp"
#include "fmod_errors.h" // Only if you want error checking

Lets create a class called SoundSystemClass shall we!

class SoundSystemClass
{
 // Pointer to the FMOD instance
 FMOD::System *m_pSystem;
 SoundSystemClass () {}
}

And for the simplicity, I typedef the default fmod sound class to my own name, as following:

typedef FMOD::Sound* SoundClass;

To initialize FMod, we create a contructor for the class, error checking is still minimal as that will come later.

SoundSystemClass ()
 {
    if (FMOD::System_Create(&m_pSystem) != FMOD_OK)
    {
       // Report Error
       return;
    }

    int driverCount = 0;
    m_pSystem->getNumDrivers(&driverCount);

    if (driverCount == 0)
    {
       // Report Error
       return;
    }

    // Initialize our Instance with 36 Channels
    m_pSystem->init(36, FMOD_INIT_NORMAL, NULL);
 }

When create FMod, you get a pointer to the instance as a result, which you will be able to use later, and as a security check, we checked that the current pc actually has a sound driver. To initialize this pointer, you call the method, ->init(..), which requires the number of channels, some flags, and some extra driver information if needed. We call the ->init(…), with 36 channels, which can be increased or decreased to your choice, each channel can hold ONE sound, so it all depends on your game, and we pass the normal initialization flag. To create a sound, we create a new method called createSound(…), which takes a pointer to our typedef SoundClass, and the sound file path:


void createSound(SoundClass *pSound, const char* pFile)
 {
    m_pSystem->createSound(pFile, FMOD_HARDWARE, 0, pSound);
 }

When we create a new sound with FMod, we specify the file path, could be any, and the creation flags, we specified HARDWARE, should be fine for now, and some info for the sound itself, and of course, a pointer in return. To play the sound, we need to specify the channel, and we can send some flags as well, therefore, we create the method called playSound(…):


void playSound(SoundClass pSound, bool bLoop = false)
 {
    if (!bLoop)
       pSound->setMode(FMOD_LOOP_OFF);
    else
    {
       pSound->setMode(FMOD_LOOP_NORMAL);
       pSound->setLoopCount(-1);
    }

    m_pSystem->playSound(FMOD_CHANNEL_FREE, pSound, false, 0);
 }

In this method, we have a boolean called bLoop, which is an option the user can take, weather the sound should loop or not. If not, we simply set the Mod FMOD_LOOP_OFF from the sound pointer (Simple, right?), but if the user chooses to loop the sound, we set the mode FMOD_LOOP_NORMAL and set the loop count to -1, which means unlimited, you can play with it if you want. And finally, we create a method for releasing the sound, called releaseSound(…), which takes a pointer to the sound.


void releaseSound(SoundClass pSound)
 {
    pSound->release();
 }

Example of usage of class:

// Initialize our sound system
SoundSystemClass sound;
sound.Initialize();

// Create a sample sound
SoundClass soundSample;
sound.createSound(&soundSample, "C:\\mysound.wav");

// Play the sound, with loop mode
sound.playSound(soundSample, true);

// Do something meanwhile...

// Release the sound
sound.releaseSound(soundSample);

Sound System Class code:

typedef FMOD::Sound* SoundClass;

class SoundSystemClass
{
    // Pointer to the FMOD instance
    FMOD::System *m_pSystem;

    SoundSystemClass ()
    {
       if (FMOD::System_Create(&m_pSystem) != FMOD_OK)
       {
          // Report Error
          return;
       }

       int driverCount = 0;
       m_pSystem->getNumDrivers(&driverCount);

       if (driverCount == 0)
       {
          // Report Error
          return;
       }

       // Initialize our Instance with 36 Channels
       m_pSystem->init(36, FMOD_INIT_NORMAL, NULL);
    }

    void createSound(SoundClass *pSound, const char* pFile)
    {
       m_pSystem->createSound(pFile, FMOD_HARDWARE, 0, pSound);
    }

    void playSound(SoundClass pSound, bool bLoop = false)
    {
       if (!bLoop)
          pSound->setMode(FMOD_LOOP_OFF);
       else
       {
          pSound->setMode(FMOD_LOOP_NORMAL);
          pSound->setLoopCount(-1);
       }

       m_pSystem->playSound(FMOD_CHANNEL_FREE, pSound, false, 0);
    }

    void releaseSound(SoundClass pSound)
    {
       pSound->release();
    }
};

Hope you enjoyed this tutorial. Feel free to post some advice on how to improve, though this is my first tutorial I have ever made. 🙂

Advertisements